-
Notifications
You must be signed in to change notification settings - Fork 6
/
show.go
126 lines (111 loc) · 3.56 KB
/
show.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
package servergroup
import (
"strconv"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/completion"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/format"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/labels"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/output"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/resolver"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/ui"
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud/request"
)
// ShowCommand creates the "servergroup show" command
func ShowCommand() commands.Command {
return &showCommand{
BaseCommand: commands.New(
"show",
"Show server group details",
"upctl servergroup show 8abc8009-4325-4b23-4321-b1232cd81231",
"upctl servergroup show my-server-group",
),
}
}
type showCommand struct {
*commands.BaseCommand
resolver.CachingServerGroup
completion.ServerGroup
}
// Execute implements commands.MultipleArgumentCommand
func (c *showCommand) Execute(exec commands.Executor, uuid string) (output.Output, error) {
svc := exec.All()
serverGroup, err := svc.GetServerGroup(exec.Context(), &request.GetServerGroupRequest{UUID: uuid})
if err != nil {
return nil, err
}
groupStatus := notApplicable
statusMap := make(map[string]string, 0)
if serverGroup.AntiAffinityPolicy != upcloud.ServerGroupAntiAffinityPolicyOff {
for _, serverState := range serverGroup.AntiAffinityStatus {
status := string(serverState.Status)
statusMap[serverState.ServerUUID] = status
if groupStatus != unMet {
groupStatus = status
}
}
}
serverRows := []output.TableRow{}
for _, serverUUID := range serverGroup.Members {
serverDetails, err := svc.GetServerDetails(exec.Context(), &request.GetServerDetailsRequest{UUID: serverUUID})
if err != nil {
return nil, err
}
status, ok := statusMap[serverUUID]
if !ok {
status = notApplicable
}
host := strconv.Itoa(serverDetails.Host)
if host == "0" {
host = notApplicable
}
serverRows = append(serverRows, output.TableRow{
serverDetails.UUID,
serverDetails.Hostname,
serverDetails.Zone,
host,
status,
serverDetails.State,
})
}
serverColumns := []output.TableColumn{
{Key: "uuid", Header: "UUID", Colour: ui.DefaultUUUIDColours},
{Key: "hostname", Header: "Hostname"},
{Key: "zone", Header: "Zone"},
{Key: "host", Header: "Host"},
{Key: "anti_affinity_state", Header: "Anti-affinity state", Format: format.ServerGroupAntiAffinityState},
{Key: "state", Header: "State", Format: format.ServerState},
}
// For JSON and YAML output, passthrough API response
return output.MarshaledWithHumanOutput{
Value: serverGroup,
Output: output.Combined{
output.CombinedSection{
Contents: output.Details{
Sections: []output.DetailSection{
{
Title: "Overview:",
Rows: []output.DetailRow{
{Title: "UUID:", Value: serverGroup.UUID, Colour: ui.DefaultUUUIDColours},
{Title: "Title:", Value: serverGroup.Title},
{Title: "Anti-affinity policy:", Value: serverGroup.AntiAffinityPolicy},
{Title: "Anti-affinity state:", Value: groupStatus, Format: format.ServerGroupAntiAffinityState},
{Title: "Server count:", Value: len(serverGroup.Members)},
},
},
},
},
},
labels.GetLabelsSection(serverGroup.Labels),
output.CombinedSection{
Key: "servers",
Title: "Servers:",
Contents: output.Table{
Columns: serverColumns,
Rows: serverRows,
EmptyMessage: "No servers in this server group.",
},
},
},
}, nil
}