-
Notifications
You must be signed in to change notification settings - Fork 7
/
modify.go
125 lines (105 loc) · 5.54 KB
/
modify.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
package server
import (
"fmt"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/config"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/labels"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
"github.com/spf13/pflag"
)
// ModifyCommand creates the "server modify" command
func ModifyCommand() commands.Command {
return &modifyCommand{
BaseCommand: commands.New(
"modify",
"Modifies the configuration of an existing server",
"upctl server modify 00bb4617-c592-4b32-b869-35a60b323b18 --plan 1xCPU-1GB",
"upctl server modify 00bb4617-c592-4b32-b869-35a60b323b18 0053a6f5-e6d1-4b0b-b9dc-b90d0894e8d0 --plan 1xCPU-1GB",
"upctl server modify my_server1 --plan 1xCPU-2GB",
"upctl server modify myapp --hostname superapp",
),
}
}
type modifyCommand struct {
*commands.BaseCommand
params modifyParams
setMetadata config.OptionalBoolean
remoteAccess config.OptionalBoolean
firewall config.OptionalBoolean
resolver.CachingServer
completion.Server
}
type modifyParams struct {
request.ModifyServerRequest
labels []string
}
var defaultModifyParams = modifyParams{
ModifyServerRequest: request.ModifyServerRequest{},
}
// InitCommand implements Command.InitCommand
func (s *modifyCommand) InitCommand() {
s.params = modifyParams{ModifyServerRequest: request.ModifyServerRequest{}}
flags := &pflag.FlagSet{}
flags.StringVar(&s.params.BootOrder, "boot-order", defaultModifyParams.BootOrder, "The boot device order, disk / cdrom / network or comma separated combination.")
flags.IntVar(&s.params.CoreNumber, "cores", defaultModifyParams.CoreNumber, "Number of cores. Sets server plan to custom.")
flags.StringVar(&s.params.Hostname, "hostname", defaultModifyParams.Hostname, "Hostname.")
config.AddEnableDisableFlags(flags, &s.firewall, "firewall", "firewall")
// flags.StringVar(&s.params.Firewall, "firewall", defaultModifyParams.Firewall, "Enables or disables firewall on the server. You can manage firewall rules with the firewall command.\nAvailable: true, false")
flags.StringArrayVar(&s.params.labels, "label", defaultModifyParams.labels, "Labels to describe the server in `key=value` format, multiple can be declared. If set, all the existing labels will be replaced with provided ones.\nUsage: --label env=dev\n\n--label owner=operations")
flags.IntVar(&s.params.MemoryAmount, "memory", defaultModifyParams.MemoryAmount, "Memory amount in MiB. Sets server plan to custom.")
config.AddEnableDisableFlags(flags, &s.setMetadata, "metadata", "metadata service")
// flags.StringVar(&s.params.metadata, "metadata", defaultModifyParams.metadata, "Enable metadata service.")
flags.StringVar(&s.params.Plan, "plan", defaultModifyParams.Plan, "Server plan to use.")
flags.StringVar(&s.params.SimpleBackup, "simple-backup", defaultModifyParams.SimpleBackup, "Simple backup rule. Format (HHMM,{dailies,weeklies,monthlies}).\nExample: 2300,dailies")
flags.StringVar(&s.params.Title, "title", defaultModifyParams.Title, "A short, informational description.")
flags.StringVar(&s.params.TimeZone, "time-zone", defaultModifyParams.TimeZone, "Time zone to set the RTC to.")
flags.StringVar(&s.params.VideoModel, "video-model", defaultModifyParams.VideoModel, "Video interface model of the server.\nAvailable: vga,cirrus")
config.AddEnableDisableFlags(flags, &s.remoteAccess, "remote-access", "remote access")
// flags.StringVar(&s.params.remoteAccessEnabled, "remote-access-enabled", defaultModifyParams.remoteAccessEnabled, "Enables or disables the remote access.\nAvailable: true, false")
flags.StringVar(&s.params.RemoteAccessType, "remote-access-type", defaultModifyParams.RemoteAccessType, "The remote access type.")
flags.StringVar(&s.params.RemoteAccessPassword, "remote-access-password", defaultModifyParams.RemoteAccessPassword, "The remote access password.")
s.AddFlags(flags)
}
// Execute implements commands.MultipleArgumentCommand
func (s *modifyCommand) Execute(exec commands.Executor, uuid string) (output.Output, error) {
svc := exec.Server()
// TODO: refactor out when go-api actually supports not-set upcloud.Booleans in requests
// ref: https://app.asana.com/0/1191419140326561/1200258914439524
serverDetails, err := svc.GetServerDetails(exec.Context(), &request.GetServerDetailsRequest{UUID: uuid})
if err != nil {
return nil, err
}
s.params.RemoteAccessEnabled = s.remoteAccess.OverrideNotSet(serverDetails.RemoteAccessEnabled.Bool()).AsUpcloudBoolean()
s.params.Metadata = s.setMetadata.OverrideNotSet(serverDetails.Metadata.Bool()).AsUpcloudBoolean()
// TODO: refactor when go-api parameter is refactored
switch s.firewall {
case config.True:
s.params.Firewall = "on"
case config.False:
s.params.Firewall = "off"
// nb. no handling for not set, just pass in an empty string in the request
}
if s.params.CoreNumber != 0 || s.params.MemoryAmount != 0 {
s.params.Plan = "custom" // Valid for all custom plans.
}
if len(s.params.labels) > 0 {
labelSlice, err := labels.StringsToUpCloudLabelSlice(s.params.labels)
if err != nil {
return nil, err
}
s.params.Labels = labelSlice
}
msg := fmt.Sprintf("Modifying server %v", uuid)
exec.PushProgressStarted(msg)
req := s.params.ModifyServerRequest
req.UUID = uuid
res, err := svc.ModifyServer(exec.Context(), &req)
if err != nil {
return commands.HandleError(exec, msg, err)
}
exec.PushProgressSuccess(msg)
return output.OnlyMarshaled{Value: res}, nil
}