-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcreate.go
125 lines (104 loc) · 2.89 KB
/
create.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package thing
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"errors"
iotclient "github.com/arduino/iot-client-go"
"github.com/arduino/iot-cloud-cli/internal/config"
"github.com/arduino/iot-cloud-cli/internal/iot"
)
// CreateParams contains the parameters needed to create a new thing.
type CreateParams struct {
// Mandatory - contains the name of the thing
Name string
// Optional - contains the ID of the device to be bound to the thing
DeviceID string
// Mandatory if device is empty - contains the path of the template file
Template string
// Mandatory if template is empty- name of things to be cloned
CloneID string
}
// Create allows to create a new thing
func Create(params *CreateParams) (string, error) {
if params.Template == "" && params.CloneID == "" {
return "", fmt.Errorf("%s", "provide either a thing(ID) to clone (--clone) or a thing template file (--template)\n")
}
conf, err := config.Retrieve()
if err != nil {
return "", err
}
iotClient, err := iot.NewClient(conf.Client, conf.Secret)
if err != nil {
return "", err
}
var thing *iotclient.Thing
if params.CloneID != "" {
thing, err = clone(iotClient, params.CloneID)
if err != nil {
return "", err
}
} else if params.Template != "" {
thing, err = loadTemplate(params.Template)
if err != nil {
return "", err
}
} else {
return "", errors.New("provide either a thing(ID) to clone (--clone) or a thing template file (--template)")
}
thing.Name = params.Name
force := true
if params.DeviceID != "" {
thing.DeviceId = params.DeviceID
}
thingID, err := iotClient.AddThing(thing, force)
if err != nil {
return "", err
}
return thingID, nil
}
func clone(client iot.Client, thingID string) (*iotclient.Thing, error) {
clone, err := client.GetThing(thingID)
if err != nil {
return nil, fmt.Errorf("%s: %w", "retrieving the thing to be cloned", err)
}
thing := &iotclient.Thing{}
// Copy device id
if clone.DeviceId != "" {
thing.DeviceId = clone.DeviceId
}
// Copy properties
for _, p := range clone.Properties {
thing.Properties = append(thing.Properties, iotclient.Property{
Name: p.Name,
MinValue: p.MinValue,
MaxValue: p.MaxValue,
Permission: p.Permission,
UpdateParameter: p.UpdateParameter,
UpdateStrategy: p.UpdateStrategy,
Type: p.Type,
VariableName: p.VariableName,
Persist: p.Persist,
Tag: p.Tag,
})
}
return thing, nil
}
func loadTemplate(file string) (*iotclient.Thing, error) {
templateFile, err := os.Open(file)
if err != nil {
return nil, err
}
defer templateFile.Close()
templateBytes, err := ioutil.ReadAll(templateFile)
if err != nil {
return nil, err
}
thing := &iotclient.Thing{}
err = json.Unmarshal([]byte(templateBytes), thing)
if err != nil {
return nil, fmt.Errorf("%s: %w", "reading template file: template not valid: ", err)
}
return thing, nil
}