-
Notifications
You must be signed in to change notification settings - Fork 7
/
createPassthrough.auto.go
193 lines (167 loc) · 5.18 KB
/
createPassthrough.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Code generated from specification version 1.0.0: DO NOT EDIT
package create_passthrough
import (
"io"
"net/http"
"github.com/MakeNowJust/heredoc/v2"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/c8yfetcher"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/cmd/subcommand"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/cmderrors"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/cmdutil"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/completion"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/flags"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/mapbuilder"
"github.com/reubenmiller/go-c8y/pkg/c8y"
"github.com/spf13/cobra"
)
// CreatePassthroughCmd command
type CreatePassthroughCmd struct {
*subcommand.SubCommand
factory *cmdutil.Factory
}
// NewCreatePassthroughCmd creates a command to Create passthrough configuration
func NewCreatePassthroughCmd(f *cmdutil.Factory) *CreatePassthroughCmd {
ccmd := &CreatePassthroughCmd{
factory: f,
}
cmd := &cobra.Command{
Use: "create-passthrough",
Short: "Create passthrough configuration",
Long: `Create a passthrough configuration which enables you to connect
directly to the device (via Cumulocity IoT) using a native client such as ssh.
After a passthrough connection has been added, you can open a proxy to it using
one of the following commands:
* c8y remoteaccess server
* c8y remoteaccess connect ssh
`,
Example: heredoc.Doc(`
$ c8y remoteaccess configurations create-passthrough --device device01
Create a SSH passthrough configuration to the localhost
$ c8y remoteaccess configurations create-passthrough --device device01 --hostname customhost --port 1234 --name "My custom configuration"
Create a SSH passthrough configuration with custom details
`),
PreRunE: func(cmd *cobra.Command, args []string) error {
return f.CreateModeEnabled()
},
RunE: ccmd.RunE,
}
cmd.SilenceUsage = true
cmd.Flags().StringSlice("device", []string{""}, "Device (accepts pipeline)")
cmd.Flags().String("name", "passthrough", "Connection name")
cmd.Flags().String("hostname", "127.0.0.1", "Hostname")
cmd.Flags().Int("port", 22, "Port")
cmd.Flags().String("protocol", "PASSTHROUGH", "Protocol")
completion.WithOptions(
cmd,
completion.WithDevice("device", func() (*c8y.Client, error) { return ccmd.factory.Client() }),
completion.WithValidateSet("protocol", "PASSTHROUGH"),
)
flags.WithOptions(
cmd,
flags.WithProcessingMode(),
flags.WithExtendedPipelineSupport("device", "device", false, "deviceId", "source.id", "managedObject.id", "id"),
flags.WithPipelineAliases("device", "deviceId", "source.id", "managedObject.id", "id"),
)
// Required flags
ccmd.SubCommand = subcommand.NewSubCommand(cmd)
return ccmd
}
// RunE executes the command
func (n *CreatePassthroughCmd) RunE(cmd *cobra.Command, args []string) error {
cfg, err := n.factory.Config()
if err != nil {
return err
}
// Runtime flag options
flags.WithOptions(
cmd,
flags.WithRuntimePipelineProperty(),
)
client, err := n.factory.Client()
if err != nil {
return err
}
inputIterators, err := cmdutil.NewRequestInputIterators(cmd, cfg)
if err != nil {
return err
}
// query parameters
query := flags.NewQueryTemplate()
err = flags.WithQueryParameters(
cmd,
query,
inputIterators,
flags.WithCustomStringSlice(func() ([]string, error) { return cfg.GetQueryParameters(), nil }, "custom"),
)
if err != nil {
return cmderrors.NewUserError(err)
}
queryValue, err := query.GetQueryUnescape(true)
if err != nil {
return cmderrors.NewSystemError("Invalid query parameter")
}
// headers
headers := http.Header{}
err = flags.WithHeaders(
cmd,
headers,
inputIterators,
flags.WithCustomStringSlice(func() ([]string, error) { return cfg.GetHeader(), nil }, "header"),
flags.WithProcessingModeValue(),
)
if err != nil {
return cmderrors.NewUserError(err)
}
// form data
formData := make(map[string]io.Reader)
err = flags.WithFormDataOptions(
cmd,
formData,
inputIterators,
)
if err != nil {
return cmderrors.NewUserError(err)
}
// body
body := mapbuilder.NewInitializedMapBuilder(true)
err = flags.WithBody(
cmd,
body,
inputIterators,
flags.WithDataFlagValue(),
flags.WithStringValue("name", "name"),
flags.WithStringValue("hostname", "hostname"),
flags.WithIntValue("port", "port"),
flags.WithStringValue("protocol", "protocol"),
flags.WithDefaultTemplateString(`
{credentialsType:'NONE'}`),
cmdutil.WithTemplateValue(n.factory),
flags.WithTemplateVariablesValue(),
flags.WithRequiredProperties("hostname", "port", "protocol", "name"),
)
if err != nil {
return cmderrors.NewUserError(err)
}
// path parameters
path := flags.NewStringTemplate("/service/remoteaccess/devices/{device}/configurations")
err = flags.WithPathParameters(
cmd,
path,
inputIterators,
c8yfetcher.WithDeviceByNameFirstMatch(n.factory, args, "device", "device"),
)
if err != nil {
return err
}
req := c8y.RequestOptions{
Method: "POST",
Path: path.GetTemplate(),
Query: queryValue,
Body: body,
FormData: formData,
Header: headers,
IgnoreAccept: cfg.IgnoreAcceptHeader(),
DryRun: cfg.ShouldUseDryRun(cmd.CommandPath()),
}
return n.factory.RunWithWorkers(client, cmd, &req, inputIterators)
}