-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_device_credentials.sh
executable file
·51 lines (48 loc) · 1.7 KB
/
create_device_credentials.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#! /bin/bash
if [[ "$1"x == ""x || "$2"x == ""x ]]; then
echo "usage: $0 <uuid> <bootstrap password>"
exit -1
fi
read -r -d '' DEVICEREG << EOM
{
"name": "$1",
"type": "DEVICE",
"c8y_IsDevice":{},
"c8y_Hardware":{
"revision": "v1.0",
"serialNumber": "$1"
}
}
EOM
echo -n "Waiting for device registration "
result=""
# waits until the devices has been accepted and prints the device credentials
while (true); do
result=$(
curl -f --silent -XPOST \
-u "devicebootstrap:$2" \
--data "{\"id\":\"$1\"}" \
-H 'Content-Type: application/vnd.com.nsn.cumulocity.deviceCredentials+json' \
-H 'Accept: application/vnd.com.nsn.cumulocity.deviceCredentials+json' \
https://ubirch.cumulocity.com/devicecontrol/deviceCredentials
)
if [[ "$?" == "0" ]]; then
echo "Device has been registered!"
echo $result
auth=$(echo $result | jq -r '(.username + ":" + .password)')
id=$(curl --silent -XPOST -u "$auth" \
-H 'Content-Type: application/vnd.com.nsn.cumulocity.managedObject+json' \
-H 'Accept: application/vnd.com.nsn.cumulocity.managedObject+json' \
--data "$DEVICEREG" \
https://ubirch.cumulocity.com/inventory/managedObjects | jq -r .id)
echo $id
curl --silent -XPOST -u "$auth" \
-H 'Content-Type: application/vnd.com.nsn.cumulocity.externalId+json' \
-H 'Accept: application/vnd.com.nsn.cumulocity.externalId+json' \
--data "{\"type\":\"c8y_Serial\",\"externalId\":\"$1\"}" \
https://ubirch.cumulocity.com/identity/globalIds/$id/externalIds
exit
fi
echo -n "."
sleep 5
done