/
console.go
85 lines (74 loc) · 3.33 KB
/
console.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
package commands
import (
"context"
"fmt"
"github.com/ionos-cloud/ionosctl/v6/commands/cloudapi-v6/completer"
"github.com/ionos-cloud/ionosctl/v6/internal/constants"
"github.com/ionos-cloud/ionosctl/v6/internal/core"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/json2table/jsonpaths"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/jsontabwriter"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/tabheaders"
cloudapiv6 "github.com/ionos-cloud/ionosctl/v6/services/cloudapi-v6"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
defaultConsoleCols = []string{"RemoteConsoleUrl"}
)
func ServerConsoleCmd() *core.Command {
ctx := context.TODO()
consoleCmd := &core.Command{
Command: &cobra.Command{
Use: "console",
Aliases: []string{"url"},
Short: "Server Remote Console URL Operations",
Long: `The sub-command of ` + "`" + `ionosctl server console` + "`" + ` allows you to get the URL for Remote Console of a specific Server.`,
TraverseChildren: true,
},
}
/*
Get Command
*/
get := core.NewCommand(ctx, consoleCmd, core.CommandBuilder{
Namespace: "server",
Resource: "console",
Verb: "get",
Aliases: []string{"g"},
ShortDesc: "Get the Remote Console URL to access a Server",
LongDesc: "Use this command to get the Server Remote Console link.\n\nRequired values to run command:\n\n* Data Center Id\n* Server Id",
Example: getConsoleServerExample,
PreCmdRun: PreRunDcServerIds,
CmdRun: RunServerConsoleGet,
InitClient: true,
})
get.AddUUIDFlag(cloudapiv6.ArgDataCenterId, "", "", cloudapiv6.DatacenterId, core.RequiredFlagOption())
_ = get.Command.RegisterFlagCompletionFunc(cloudapiv6.ArgDataCenterId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.DataCentersIds(), cobra.ShellCompDirectiveNoFileComp
})
get.AddUUIDFlag(cloudapiv6.ArgServerId, cloudapiv6.ArgIdShort, "", cloudapiv6.ServerId, core.RequiredFlagOption())
_ = get.Command.RegisterFlagCompletionFunc(cloudapiv6.ArgServerId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.ServersIds(viper.GetString(core.GetFlagName(get.NS, cloudapiv6.ArgDataCenterId))), cobra.ShellCompDirectiveNoFileComp
})
get.AddInt32Flag(cloudapiv6.ArgDepth, cloudapiv6.ArgDepthShort, cloudapiv6.DefaultGetDepth, cloudapiv6.ArgDepthDescription)
return consoleCmd
}
func RunServerConsoleGet(c *core.CommandConfig) error {
dcId := viper.GetString(core.GetFlagName(c.NS, cloudapiv6.ArgDataCenterId))
serverId := viper.GetString(core.GetFlagName(c.NS, cloudapiv6.ArgServerId))
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput(
"Getting Consoler URL for Server with ID: %v from Datacenter with ID: %v...", serverId, dcId))
t, resp, err := c.CloudApiV6Services.Servers().GetRemoteConsoleUrl(dcId, serverId)
if err != nil {
return err
}
if resp != nil {
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput(constants.MessageRequestTime, resp.RequestTime))
}
out, err := jsontabwriter.GenerateOutput("", jsonpaths.Console, t.RemoteConsoleUrl,
tabheaders.GetHeadersAllDefault(defaultConsoleCols, nil))
if err != nil {
return err
}
fmt.Fprintf(c.Command.Command.OutOrStdout(), out)
return nil
}