/
switch.go
185 lines (155 loc) · 4.95 KB
/
switch.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package cmd
import (
"fmt"
"log"
"sort"
"strings"
"github.com/metal-stack/metal-go/api/client/switch_operations"
"github.com/metal-stack/metal-go/api/models"
"github.com/metal-stack/metal-lib/pkg/genericcli"
"github.com/metal-stack/metal-lib/pkg/genericcli/printers"
"github.com/metal-stack/metal-lib/pkg/pointer"
"github.com/metal-stack/metalctl/cmd/sorters"
"github.com/metal-stack/metalctl/cmd/tableprinters"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type switchCmd struct {
*config
}
func newSwitchCmd(c *config) *cobra.Command {
w := switchCmd{
config: c,
}
cmdsConfig := &genericcli.CmdsConfig[any, *models.V1SwitchUpdateRequest, *models.V1SwitchResponse]{
BinaryName: binaryName,
GenericCLI: genericcli.NewGenericCLI[any, *models.V1SwitchUpdateRequest, *models.V1SwitchResponse](w).WithFS(c.fs),
OnlyCmds: genericcli.OnlyCmds(
genericcli.ListCmd,
genericcli.DescribeCmd,
genericcli.UpdateCmd,
genericcli.DeleteCmd,
genericcli.EditCmd,
),
Singular: "switch",
Plural: "switches",
Description: "switch are the leaf switches in the data center that are controlled by metal-stack.",
Sorter: sorters.SwitchSorter(),
DescribePrinter: func() printers.Printer { return c.describePrinter },
ListPrinter: func() printers.Printer { return c.listPrinter },
}
switchDetailCmd := &cobra.Command{
Use: "detail",
Short: "switch details",
RunE: func(cmd *cobra.Command, args []string) error {
return w.switchDetail()
},
}
switchReplaceCmd := &cobra.Command{
Use: "replace <switchID>",
Short: "puts a switch in replace mode in preparation for physical replacement",
RunE: func(cmd *cobra.Command, args []string) error {
return w.switchReplace(args)
},
}
switchDetailCmd.Flags().StringP("filter", "F", "", "filter for site, rack, ID")
return genericcli.NewCmds(cmdsConfig, switchDetailCmd, switchReplaceCmd)
}
func (c switchCmd) Get(id string) (*models.V1SwitchResponse, error) {
resp, err := c.client.SwitchOperations().FindSwitch(switch_operations.NewFindSwitchParams().WithID(id), nil)
if err != nil {
return nil, err
}
return resp.Payload, nil
}
func (c switchCmd) List() ([]*models.V1SwitchResponse, error) {
resp, err := c.client.SwitchOperations().ListSwitches(switch_operations.NewListSwitchesParams(), nil)
if err != nil {
return nil, err
}
for _, s := range resp.Payload {
s := s
sort.SliceStable(s.Connections, func(i, j int) bool {
return pointer.SafeDeref(pointer.SafeDeref((pointer.SafeDeref(s.Connections[i])).Nic).Name) < pointer.SafeDeref(pointer.SafeDeref((pointer.SafeDeref(s.Connections[j])).Nic).Name)
})
}
return resp.Payload, nil
}
func (c switchCmd) Delete(id string) (*models.V1SwitchResponse, error) {
resp, err := c.client.SwitchOperations().DeleteSwitch(switch_operations.NewDeleteSwitchParams().WithID(id), nil)
if err != nil {
return nil, err
}
return resp.Payload, nil
}
func (c switchCmd) Create(rq any) (*models.V1SwitchResponse, error) {
return nil, fmt.Errorf("switch entity does not support create operation")
}
func (c switchCmd) Update(rq *models.V1SwitchUpdateRequest) (*models.V1SwitchResponse, error) {
resp, err := c.client.SwitchOperations().UpdateSwitch(switch_operations.NewUpdateSwitchParams().WithBody(rq), nil)
if err != nil {
return nil, err
}
return resp.Payload, nil
}
func (c switchCmd) ToCreate(r *models.V1SwitchResponse) (any, error) {
return nil, fmt.Errorf("switch entity does not support create operation")
}
func (c switchCmd) ToUpdate(r *models.V1SwitchResponse) (*models.V1SwitchUpdateRequest, error) {
return switchResponseToUpdate(r), nil
}
func switchResponseToUpdate(r *models.V1SwitchResponse) *models.V1SwitchUpdateRequest {
return &models.V1SwitchUpdateRequest{
Description: r.Description,
ID: r.ID,
Mode: r.Mode,
Name: r.Name,
RackID: r.RackID,
}
}
// non-generic command handling
func (c *switchCmd) switchDetail() error {
resp, err := c.List()
if err != nil {
return err
}
var result []*tableprinters.SwitchDetail
filter := viper.GetString("filter")
for _, s := range resp {
partitionID := ""
if s.Partition != nil {
partitionID = *s.Partition.ID
}
if strings.Contains(*s.ID, filter) ||
strings.Contains(partitionID, filter) ||
strings.Contains(*s.RackID, filter) {
result = append(result, &tableprinters.SwitchDetail{V1SwitchResponse: s})
}
}
if len(result) < 1 {
log.Printf("no switch detail for filter: %s", filter)
return nil
}
return c.listPrinter.Print(result)
}
func (c *switchCmd) switchReplace(args []string) error {
id, err := genericcli.GetExactlyOneArg(args)
if err != nil {
return err
}
resp, err := c.Get(id)
if err != nil {
return err
}
uresp, err := c.Update(&models.V1SwitchUpdateRequest{
ID: resp.ID,
Name: resp.Name,
Description: resp.Description,
RackID: resp.RackID,
Mode: "replace",
})
if err != nil {
return err
}
return c.describePrinter.Print(uresp)
}