-
Notifications
You must be signed in to change notification settings - Fork 6
/
create.go
125 lines (101 loc) · 3.91 KB
/
create.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 servergroup
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/namedargs"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/ui"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
"github.com/spf13/pflag"
)
// CreateCommand creates the "servergroup create" command
func CreateCommand() commands.Command {
return &createCommand{
BaseCommand: commands.New(
"create",
"Create a server group",
`upctl servergroup create \
--title my-server-group \
--anti-affinity-policy yes \
--server 1fdfda29-ead1-4855-b71f-a432179800ab \
--server my-server`,
`upctl servergroup create \
--title my-server-group \
--anti-affinity-policy yes \
--label env=dev`,
`upctl servergroup create \
--title my-server-group \
--anti-affinity-policy strict \
--label env=dev \
--label owner=operations`,
),
}
}
type createParams struct {
request.CreateServerGroupRequest
antiAffinityPolicy string
labels []string
servers []string
}
var defaultCreateParams = &createParams{
CreateServerGroupRequest: request.CreateServerGroupRequest{},
antiAffinityPolicy: string(upcloud.ServerGroupAntiAffinityPolicyBestEffort),
}
func (p *createParams) processParams(exec commands.Executor) error {
p.AntiAffinityPolicy = upcloud.ServerGroupAntiAffinityPolicy(p.antiAffinityPolicy)
if len(p.labels) > 0 {
labelSlice, err := labels.StringsToUpCloudLabelSlice(p.labels)
if err != nil {
return err
}
p.Labels = labelSlice
}
if len(p.servers) > 0 {
servers, err := stringsToServerUUIDSlice(exec, p.servers)
if err != nil {
return err
}
p.Members = servers
}
return nil
}
type createCommand struct {
*commands.BaseCommand
params createParams
}
// InitCommand implements Command.InitCommand
func (c *createCommand) InitCommand() {
fs := &pflag.FlagSet{}
c.params = createParams{CreateServerGroupRequest: request.CreateServerGroupRequest{}}
fs.StringVar(&c.params.Title, "title", defaultCreateParams.Title, "Server group title.")
fs.StringVar(&c.params.antiAffinityPolicy, "anti-affinity-policy", defaultCreateParams.antiAffinityPolicy, "Anti-affinity policy. Valid values are `yes` (best effort), `strict` and `no`. Will take effect upon server start.")
fs.StringArrayVar(&c.params.labels, "label", defaultCreateParams.labels, "Labels to describe the server group in `key=value` format, multiple can be declared.\nUsage: --label env=dev\n\n--label owner=operations")
fs.StringArrayVar(&c.params.servers, "server", defaultCreateParams.servers, "Servers to be added to the server group, multiple can be declared.\nUsage: --server my-server\n\n--server 00333d1b-3a4a-4b75-820a-4a56d70395dd")
c.AddFlags(fs)
_ = c.Cobra().MarkFlagRequired("title")
}
func (c *createCommand) InitCommandWithConfig(cfg *config.Config) {
_ = c.Cobra().RegisterFlagCompletionFunc("server", namedargs.CompletionFunc(completion.Server{}, cfg))
}
// ExecuteWithoutArguments implements commands.NoArgumentCommand
func (c *createCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) {
svc := exec.All()
if err := c.params.processParams(exec); err != nil {
return nil, err
}
msg := fmt.Sprintf("Creating server group %s", c.params.Title)
exec.PushProgressStarted(msg)
r := c.params.CreateServerGroupRequest
res, err := svc.CreateServerGroup(exec.Context(), &r)
if err != nil {
return commands.HandleError(exec, msg, err)
}
exec.PushProgressSuccess(msg)
return output.MarshaledWithHumanDetails{Value: res, Details: []output.DetailRow{
{Title: "UUID", Value: res.UUID, Colour: ui.DefaultUUUIDColours},
}}, nil
}