/
requestDeviceCredentialsCmd.auto.go
108 lines (87 loc) · 2.9 KB
/
requestDeviceCredentialsCmd.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
// 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 requestDeviceCredentialsCmd struct {
*baseCmd
}
func newRequestDeviceCredentialsCmd() *requestDeviceCredentialsCmd {
ccmd := &requestDeviceCredentialsCmd{}
cmd := &cobra.Command{
Use: "requestDeviceCredentials",
Short: "Request credentials for a new device",
Long: `Device credentials can be enquired by devices that do not have credentials for accessing a tenant yet. Since the device does not have credentials yet, a set of fixed credentials is used for this API. The credentials can be obtained by contacting support. Do not use your tenant credentials with this API.`,
Example: `
$ c8y devices requestDeviceCredentials --id "device-AD76-matrixer"
Request credentials for a new device
`,
PreRunE: validateCreateMode,
RunE: ccmd.requestDeviceCredentials,
}
cmd.SilenceUsage = true
cmd.Flags().String("id", "", "Device identifier. Max: 1000 characters. E.g. IMEI (required)")
addProcessingModeFlag(cmd)
// Required flags
cmd.MarkFlagRequired("id")
ccmd.baseCmd = newBaseCmd(cmd)
return ccmd
}
func (n *requestDeviceCredentialsCmd) requestDeviceCredentials(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("id"); err == nil {
if v != "" {
body.Set("id", v)
}
} else {
return newUserError(fmt.Sprintf("Flag [%s] does not exist. %s", "id", 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)
path := replacePathParameters("devicecontrol/deviceCredentials", pathParameters)
req := c8y.RequestOptions{
Method: "POST",
Path: path,
Query: queryValue,
Body: body.GetMap(),
FormData: formData,
Header: headers,
IgnoreAccept: false,
DryRun: globalFlagDryRun,
}
return processRequestAndResponse([]c8y.RequestOptions{req}, commonOptions)
}