/
updateDeviceGroupCmd.auto.go
128 lines (104 loc) · 3.05 KB
/
updateDeviceGroupCmd.auto.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
126
127
128
// Code generated from specification version 1.0.0: DO NOT EDIT
package cmd
import (
"fmt"
"io"
"net/http"
"net/url"
"github.com/reubenmiller/go-c8y-cli/pkg/mapbuilder"
"github.com/reubenmiller/go-c8y/pkg/c8y"
"github.com/spf13/cobra"
)
type updateDeviceGroupCmd struct {
*baseCmd
}
func newUpdateDeviceGroupCmd() *updateDeviceGroupCmd {
ccmd := &updateDeviceGroupCmd{}
cmd := &cobra.Command{
Use: "updateGroup",
Short: "Update device group",
Long: `Update properties of an existing device group, for example name or any other custom properties.
`,
Example: `
$ c8y devices updateGroup --id 12345
Update device group by id
`,
PreRunE: validateUpdateMode,
RunE: ccmd.updateDeviceGroup,
}
cmd.SilenceUsage = true
cmd.Flags().StringSlice("id", []string{""}, "Device group ID (required)")
cmd.Flags().String("name", "", "Device group name")
addDataFlag(cmd)
addProcessingModeFlag(cmd)
// Required flags
cmd.MarkFlagRequired("id")
ccmd.baseCmd = newBaseCmd(cmd)
return ccmd
}
func (n *updateDeviceGroupCmd) updateDeviceGroup(cmd *cobra.Command, args []string) error {
commonOptions, err := getCommonOptions(cmd)
if err != nil {
return newUserError(fmt.Sprintf("Failed to get common options. err=%s", err))
}
// query parameters
queryValue := url.QueryEscape("")
query := url.Values{}
queryValue, err = url.QueryUnescape(query.Encode())
if err != nil {
return newSystemError("Invalid query parameter")
}
// headers
headers := http.Header{}
if cmd.Flags().Changed("processingMode") {
if v, err := cmd.Flags().GetString("processingMode"); err == nil && v != "" {
headers.Add("X-Cumulocity-Processing-Mode", v)
}
}
// form data
formData := make(map[string]io.Reader)
// body
body := mapbuilder.NewMapBuilder()
body.SetMap(getDataFlag(cmd))
if v, err := cmd.Flags().GetString("name"); err == nil {
if v != "" {
body.Set("name", v)
}
} else {
return newUserError(fmt.Sprintf("Flag [%s] does not exist. %s", "name", err))
}
if err := setDataTemplateFromFlags(cmd, body); err != nil {
return newUserError("Template error. ", err)
}
if err := body.Validate(); err != nil {
return newUserError("Body validation error. ", err)
}
// path parameters
pathParameters := make(map[string]string)
if cmd.Flags().Changed("id") {
idInputValues, idValue, err := getFormattedDeviceGroupSlice(cmd, args, "id")
if err != nil {
return newUserError("no matching device groups found", idInputValues, err)
}
if len(idValue) == 0 {
return newUserError("no matching device groups found", idInputValues)
}
for _, item := range idValue {
if item != "" {
pathParameters["id"] = newIDValue(item).GetID()
}
}
}
path := replacePathParameters("inventory/managedObjects/{id}", pathParameters)
req := c8y.RequestOptions{
Method: "PUT",
Path: path,
Query: queryValue,
Body: body.GetMap(),
FormData: formData,
Header: headers,
IgnoreAccept: false,
DryRun: globalFlagDryRun,
}
return processRequestAndResponse([]c8y.RequestOptions{req}, commonOptions)
}