/
partition.go
187 lines (160 loc) · 6.72 KB
/
partition.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
186
187
package cmd
import (
"errors"
"fmt"
"strings"
"github.com/metal-stack/metal-go/api/client/partition"
"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/spf13/cobra"
"github.com/spf13/viper"
)
type partitionCmd struct {
*config
}
func newPartitionCmd(c *config) *cobra.Command {
w := partitionCmd{
config: c,
}
cmdsConfig := &genericcli.CmdsConfig[*models.V1PartitionCreateRequest, *models.V1PartitionUpdateRequest, *models.V1PartitionResponse]{
BinaryName: binaryName,
GenericCLI: genericcli.NewGenericCLI[*models.V1PartitionCreateRequest, *models.V1PartitionUpdateRequest, *models.V1PartitionResponse](w).WithFS(c.fs),
Singular: "partition",
Plural: "partitions",
Description: "a partition is a failure domain in the data center.",
ValidArgsFn: c.comp.PartitionListCompletion,
AvailableSortKeys: sorters.PartitionSortKeys(),
DescribePrinter: func() printers.Printer { return c.describePrinter },
ListPrinter: func() printers.Printer { return c.listPrinter },
CreateRequestFromCLI: func() (*models.V1PartitionCreateRequest, error) {
return &models.V1PartitionCreateRequest{
ID: pointer.Pointer(viper.GetString("id")),
Description: viper.GetString("description"),
Name: viper.GetString("name"),
Mgmtserviceaddress: viper.GetString("mgmtserver"),
Bootconfig: &models.V1PartitionBootConfiguration{
Commandline: viper.GetString("cmdline"),
Imageurl: viper.GetString("imageurl"),
Kernelurl: viper.GetString("kernelurl"),
},
}, nil
},
CreateCmdMutateFn: func(cmd *cobra.Command) {
cmd.Flags().StringP("id", "", "", "ID of the partition. [required]")
cmd.Flags().StringP("name", "n", "", "Name of the partition. [optional]")
cmd.Flags().StringP("description", "d", "", "Description of the partition. [required]")
cmd.Flags().StringP("mgmtserver", "", "", "management server address in the partition. [required]")
cmd.Flags().StringP("cmdline", "", "", "kernel commandline for the metal-hammer in the partition. [required]")
cmd.Flags().StringP("imageurl", "", "", "initrd for the metal-hammer in the partition. [required]")
cmd.Flags().StringP("kernelurl", "", "", "kernel url for the metal-hammer in the partition. [required]")
},
}
partitionCapacityCmd := &cobra.Command{
Use: "capacity",
Short: "show partition capacity",
RunE: func(cmd *cobra.Command, args []string) error {
return w.partitionCapacity()
},
}
partitionCapacityCmd.Flags().StringP("id", "", "", "filter on partition id. [optional]")
partitionCapacityCmd.Flags().StringP("size", "", "", "filter on size id. [optional]")
partitionCapacityCmd.Flags().StringSlice("order", []string{}, fmt.Sprintf("order by (comma separated) column(s), sort direction can be changed by appending :asc or :desc behind the column identifier. possible values: %s", strings.Join(sorters.PartitionCapacitySortKeys(), "|")))
must(partitionCapacityCmd.RegisterFlagCompletionFunc("id", c.comp.PartitionListCompletion))
must(partitionCapacityCmd.RegisterFlagCompletionFunc("size", c.comp.SizeListCompletion))
must(partitionCapacityCmd.RegisterFlagCompletionFunc("order", cobra.FixedCompletions(sorters.PartitionCapacitySortKeys(), cobra.ShellCompDirectiveNoFileComp)))
return genericcli.NewCmds(cmdsConfig, partitionCapacityCmd)
}
func (c partitionCmd) Get(id string) (*models.V1PartitionResponse, error) {
resp, err := c.client.Partition().FindPartition(partition.NewFindPartitionParams().WithID(id), nil)
if err != nil {
return nil, err
}
return resp.Payload, nil
}
func (c partitionCmd) List() ([]*models.V1PartitionResponse, error) {
resp, err := c.client.Partition().ListPartitions(partition.NewListPartitionsParams(), nil)
if err != nil {
return nil, err
}
err = sorters.PartitionSort(resp.Payload)
if err != nil {
return nil, err
}
return resp.Payload, nil
}
func (c partitionCmd) Delete(id string) (*models.V1PartitionResponse, error) {
resp, err := c.client.Partition().DeletePartition(partition.NewDeletePartitionParams().WithID(id), nil)
if err != nil {
return nil, err
}
return resp.Payload, nil
}
func (c partitionCmd) Create(rq *models.V1PartitionCreateRequest) (*models.V1PartitionResponse, error) {
resp, err := c.client.Partition().CreatePartition(partition.NewCreatePartitionParams().WithBody(rq), nil)
if err != nil {
var r *partition.CreatePartitionConflict
if errors.As(err, &r) {
return nil, genericcli.AlreadyExistsError()
}
return nil, err
}
return resp.Payload, nil
}
func (c partitionCmd) Update(rq *models.V1PartitionUpdateRequest) (*models.V1PartitionResponse, error) {
resp, err := c.client.Partition().UpdatePartition(partition.NewUpdatePartitionParams().WithBody(rq), nil)
if err != nil {
return nil, err
}
return resp.Payload, nil
}
func (c partitionCmd) ToCreate(r *models.V1PartitionResponse) (*models.V1PartitionCreateRequest, error) {
return partitionResponseToCreate(r), nil
}
func (c partitionCmd) ToUpdate(r *models.V1PartitionResponse) (*models.V1PartitionUpdateRequest, error) {
return partitionResponseToUpdate(r), nil
}
func partitionResponseToCreate(r *models.V1PartitionResponse) *models.V1PartitionCreateRequest {
return &models.V1PartitionCreateRequest{
Bootconfig: &models.V1PartitionBootConfiguration{
Commandline: r.Bootconfig.Commandline,
Imageurl: r.Bootconfig.Imageurl,
Kernelurl: r.Bootconfig.Kernelurl,
},
Description: r.Description,
ID: r.ID,
Mgmtserviceaddress: r.Mgmtserviceaddress,
Name: r.Name,
Privatenetworkprefixlength: r.Privatenetworkprefixlength,
}
}
func partitionResponseToUpdate(r *models.V1PartitionResponse) *models.V1PartitionUpdateRequest {
return &models.V1PartitionUpdateRequest{
Bootconfig: &models.V1PartitionBootConfiguration{
Commandline: r.Bootconfig.Commandline,
Imageurl: r.Bootconfig.Imageurl,
Kernelurl: r.Bootconfig.Kernelurl,
},
Description: r.Description,
ID: r.ID,
Mgmtserviceaddress: r.Mgmtserviceaddress,
Name: r.Name,
}
}
// non-generic command handling
func (c *partitionCmd) partitionCapacity() error {
resp, err := c.client.Partition().PartitionCapacity(partition.NewPartitionCapacityParams().WithBody(&models.V1PartitionCapacityRequest{
ID: viper.GetString("id"),
Sizeid: viper.GetString("size"),
}), nil)
if err != nil {
return err
}
err = sorters.PartitionCapacitySort(resp.Payload)
if err != nil {
return err
}
return c.listPrinter.Print(resp.Payload)
}