This repository has been archived by the owner on Jun 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
customproperties.go
89 lines (73 loc) · 1.92 KB
/
customproperties.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
package gosolar
import "fmt"
// BulkSetCustomProperty sets a custom property on a series of URIs.
func (c *Client) BulkSetCustomProperty(uris []string, name string, value interface{}) error {
// load up the uris that are going to be affected
var cpuris []string
for _, uri := range uris {
cpuris = append(cpuris, uri+"/CustomProperties")
}
bulkRequest := struct {
URIs []string `json:"uris"`
Properties map[string]interface{} `json:"properties"`
}{
URIs: cpuris,
Properties: map[string]interface{}{
name: value,
},
}
_, err := c.post("BulkUpdate", &bulkRequest)
if err != nil {
return fmt.Errorf("failed to post bulk update: %v", err)
}
return nil
}
// SetCustomProperty sets a custom property value on a specific URI.
func (c *Client) SetCustomProperty(uri, name string, value interface{}) error {
property := map[string]interface{}{
name: value,
}
_, err := c.post(uri+"/CustomProperties", &property)
if err != nil {
return fmt.Errorf("failed to update custom property: %v", err)
}
return nil
}
// SetCustomProperties sets multiple properties on an entity.
func (c *Client) SetCustomProperties(uri string, properties map[string]interface{}) error {
_, err := c.post(uri+"/CustomProperties", &properties)
if err != nil {
return fmt.Errorf("failed to update custom property: %v", err)
}
return nil
}
// CreateCustomProperty creates a new custom property of a specified type.
func (c *Client) CreateCustomProperty(cpEntity, cpType, cpName, cpDesc string) error {
var cpLength string
if cpType == "string" {
cpLength = "400"
} else {
cpLength = "0"
}
props := []string{
cpName,
cpDesc,
cpType,
cpLength,
"",
"",
"",
"",
"",
"",
"",
"false",
"",
}
endpoint := fmt.Sprintf("Invoke/%s/CreateCustomProperty", cpEntity)
_, err := c.post(endpoint, &props)
if err != nil {
return fmt.Errorf("failed to create custom property: %v", err)
}
return nil
}