Skip to content

Commit

Permalink
add remote access configuration completion and named lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
reubenmiller committed May 18, 2024
1 parent af7be5b commit c92e50c
Show file tree
Hide file tree
Showing 18 changed files with 221 additions and 31 deletions.
21 changes: 15 additions & 6 deletions api/spec/json/remoteAccessConfigurations.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@
},
{
"name": "id",
"type": "string",
"type": "remoteaccessconfiguration",
"description": "Connection",
"pipeline": true
"pipeline": true,
"dependsOn": [
"device"
]
}
]
},
Expand Down Expand Up @@ -131,9 +134,12 @@
},
{
"name": "id",
"type": "string",
"type": "remoteaccessconfiguration",
"description": "Connection",
"pipeline": true
"pipeline": true,
"dependsOn": [
"device"
]
}
],
"body": [
Expand Down Expand Up @@ -185,9 +191,12 @@
},
{
"name": "id",
"type": "string",
"type": "remoteaccessconfiguration",
"description": "Connection",
"pipeline": true
"pipeline": true,
"dependsOn": [
"device"
]
}
]
},
Expand Down
1 change: 1 addition & 0 deletions api/spec/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@
"devicegroup[]",
"usergroup[]",
"userself[]",
"remoteaccessconfiguration",
"roleself[]",
"role[]",
"user[]",
Expand Down
23 changes: 9 additions & 14 deletions api/spec/yaml/remoteAccessConfigurations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ commands:
pipeline: false

- name: id
type: string
type: remoteaccessconfiguration
description: Connection
pipeline: true
dependsOn:
- device

- name: update
# Hide for now, as updating requires to PUT the entire existing state into the body plus the desired changes
Expand Down Expand Up @@ -94,9 +96,11 @@ commands:
pipeline: false

- name: id
type: string
type: remoteaccessconfiguration
description: Connection
pipeline: true
dependsOn:
- device
body:
- name: newName
type: string
Expand Down Expand Up @@ -130,9 +134,11 @@ commands:
pipeline: false

- name: id
type: string
type: remoteaccessconfiguration
description: Connection
pipeline: true
dependsOn:
- device

- name: create-passthrough
description: Create passthrough configuration
Expand Down Expand Up @@ -306,17 +312,6 @@ commands:
- protocol
- name
- credentialsType
# bodyTemplates:
# - type: jsonnet
# template: |

# {
# local credType = if std.objectHas($, "privateKey") && std.objectHas($, "publicKey") then "USER_PASS" else if std.objectHas($, "password") else,
# local credValue = {

# },
# credentialsType: credType
# }

- name: create-vnc
description: Create vnc configuration
Expand Down
23 changes: 23 additions & 0 deletions pkg/c8yfetcher/c8yfetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -1068,3 +1068,26 @@ func WithExternalCommandByNameFirstMatch(factory *cmdutil.Factory, args []string
return opt(cmd, inputIterators)
}
}

// WithRemoteAccessConfigurationFirstMatch returns the first matching remote access configuration for a device
func WithRemoteAccessConfigurationFirstMatch(factory *cmdutil.Factory, flagDevice string, args []string, opts ...string) flags.GetOption {
return func(cmd *cobra.Command, inputIterators *flags.RequestInputIterators) (string, interface{}, error) {
mo_id := ""
// Note: Lookup of device does not work if "device" is piped input
if values, err := cmd.Flags().GetStringSlice(flagDevice); err == nil && len(values) > 0 {
formattedValues, err := lookupEntity(NewDeviceFetcher(factory), values, false, "")
if err != nil {
return "", nil, err
}
if len(formattedValues) > 0 {
mo_id = formattedValues[0].ID
}
}
if mo_id == "" {
return "", nil, fmt.Errorf("device not found")
}

opt := WithReferenceByNameFirstMatch(factory, NewRemoteAccessFetcher(factory, mo_id), args, opts...)
return opt(cmd, inputIterators)
}
}
11 changes: 11 additions & 0 deletions pkg/c8yfetcher/remoteaccessFetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ type RemoteAccessFetcher struct {
}

func DetectRemoteAccessConfiguration(client *c8y.Client, mo_id string, name string) (*c8y.RemoteAccessConfiguration, error) {
if c8y.IsID(name) {
config, _, err := client.RemoteAccess.GetConfiguration(
c8y.WithDisabledDryRunContext(context.Background()),
mo_id,
name,
)
if err == nil {
return config, err
}
// Fall through if it could not be found (as the name might just look like an id)
}
configs, _, err := client.RemoteAccess.GetConfigurations(
c8y.WithDisabledDryRunContext(context.Background()),
mo_id,
Expand Down
5 changes: 3 additions & 2 deletions pkg/cmd/remoteaccess/configurations/delete/delete.auto.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pkg/cmd/remoteaccess/configurations/get/get.auto.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pkg/cmd/remoteaccess/configurations/update/update.auto.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 15 additions & 4 deletions pkg/cmd/remoteaccess/connect/ssh/ssh.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,17 @@ func NewCmdSSH(f *cmdutil.Factory) *CmdSSH {
Use: "ssh",
Short: "Connect to a device via ssh",
Long: heredoc.Doc(`
Connect to a device via ssh
Connect to a device via ssh
Additional arguments can be passed to the ssh shell by using the "--" convention where everything
after the "--" will be passed untouched to the ssh shell. In this mode, the shell will not be
interactive, and it will return upon completion of the command.
You can set the default ssh user to use for all ssh connections for your current c8y session file
using:
c8y settings update remoteaccess.sshuser root
Additional arguments can be passed to the ssh shell by using the "--" convention where everything
after the "--" will be passed untouched to the ssh shell. In this mode, the shell will not be
interactive, and it will return upon completion of the command.
`),
Example: heredoc.Doc(`
$ c8y remoteaccess ssh --device 12345
Expand All @@ -70,6 +76,7 @@ func NewCmdSSH(f *cmdutil.Factory) *CmdSSH {
completion.WithOptions(
cmd,
completion.WithDevice("device", func() (*c8y.Client, error) { return ccmd.factory.Client() }),
completion.WithRemoteAccessPassthroughConfiguration("configuration", "device", func() (*c8y.Client, error) { return ccmd.factory.Client() }),
)

flags.WithOptions(
Expand Down Expand Up @@ -165,6 +172,10 @@ func (n *CmdSSH) RunE(cmd *cobra.Command, args []string) error {
}

sshTarget := host
if n.user == "" {
// Use default user (if set)
n.user = cfg.GetRemoteAccessDefaultSSHUser()
}
if n.user != "" {
sshTarget = fmt.Sprintf("%s@%s", n.user, host)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/remoteaccess/server/server.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func NewCmdServer(f *cmdutil.Factory) *CmdServer {
completion.WithOptions(
cmd,
completion.WithDevice("device", func() (*c8y.Client, error) { return ccmd.factory.Client() }),
completion.WithRemoteAccessPassthroughConfiguration("configuration", "device", func() (*c8y.Client, error) { return ccmd.factory.Client() }),
)

flags.WithOptions(
Expand Down
5 changes: 5 additions & 0 deletions pkg/cmd/settings/update/update.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,11 @@ var updateSettingsOptions = map[string]argumentHandler{

// extensions
"extensions.datadir": {"extensions.datadir", "string", config.SettingsExtensionDataDir, []string{}, nil, cobra.ShellCompDirectiveDefault},

// remote access
"remoteaccess.sshuser": {"remoteaccess.sshuser", "string", config.SettingsRemoteAccessDefaultSSHUser, []string{
"root",
}, nil, cobra.ShellCompDirectiveNoFileComp},
}

// NewCmdUpdate returns a new command used to update session settings
Expand Down
5 changes: 5 additions & 0 deletions pkg/cmdparser/cmdparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ func GetCompletionOptions(cmd *CmdOptions, p *models.Parameter, factory *cmdutil
return completion.WithNotification2SubscriptionName(p.Name, func() (*c8y.Client, error) { return factory.Client() })
case "subscriptionId":
return completion.WithNotification2SubscriptionId(p.Name, func() (*c8y.Client, error) { return factory.Client() })
case "remoteaccessconfiguration":
return completion.WithRemoteAccessConfiguration(p.Name, p.GetDependentProperty(0, "device"), func() (*c8y.Client, error) { return factory.Client() })
}
return nil
}
Expand Down Expand Up @@ -494,6 +496,9 @@ func GetOption(cmd *CmdOptions, p *models.Parameter, factory *cmdutil.Factory, a

case "usergroup[]":
opts = append(opts, c8yfetcher.WithUserGroupByNameFirstMatch(factory, args, p.Name, targetProp, p.Format))

case "remoteaccessconfiguration":
opts = append(opts, c8yfetcher.WithRemoteAccessConfigurationFirstMatch(factory, p.GetDependentProperty(0, "device"), args, p.Name, targetProp))
}

return opts
Expand Down
Loading

0 comments on commit c92e50c

Please sign in to comment.