Skip to content

Commit b10d11f

Browse files
committed
change the flag validation
1 parent d60abe3 commit b10d11f

File tree

3 files changed

+54
-35
lines changed

3 files changed

+54
-35
lines changed

internal/cmd/beta/server/create/create.go

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -109,38 +109,6 @@ func NewCmd(p *print.Printer) *cobra.Command {
109109
`$ stackit beta server create --machine-type t1.1 --name server1 --boot-volume-source-id xxx --boot-volume-source-type image --boot-volume-size 64 --user-data @path/to/file.yaml")`,
110110
),
111111
),
112-
PreRun: func(cmd *cobra.Command, _ []string) {
113-
bootVolumeSourceId, _ := cmd.Flags().GetString(bootVolumeSourceIdFlag)
114-
bootVolumeSourceType, _ := cmd.Flags().GetString(bootVolumeSourceTypeFlag)
115-
bootVolumeSize, _ := cmd.Flags().GetInt64(bootVolumeSizeFlag)
116-
imageId, _ := cmd.Flags().GetString(imageIdFlag)
117-
118-
if imageId == "" && bootVolumeSourceId == "" && bootVolumeSourceType == "" {
119-
p.Warn("Either Image ID or boot volume flags must be provided.\n")
120-
}
121-
122-
if imageId == "" {
123-
err := flags.MarkFlagsRequired(cmd, bootVolumeSourceIdFlag, bootVolumeSourceTypeFlag)
124-
cobra.CheckErr(err)
125-
}
126-
127-
if bootVolumeSourceType == "image" {
128-
if bootVolumeSize == 0 {
129-
p.Warn("Boot volume size must be provided when `source_type` is `image`.\n")
130-
}
131-
err := cmd.MarkFlagRequired(bootVolumeSizeFlag)
132-
cobra.CheckErr(err)
133-
}
134-
135-
if bootVolumeSourceId == "" && bootVolumeSourceType == "" {
136-
err := cmd.MarkFlagRequired(imageIdFlag)
137-
cobra.CheckErr(err)
138-
}
139-
140-
if imageId != "" && (bootVolumeSourceId != "" || bootVolumeSourceType != "") {
141-
p.Warn("Image ID flag cannot be used together with any of the boot volume flags.\n")
142-
}
143-
},
144112
RunE: func(cmd *cobra.Command, _ []string) error {
145113
ctx := context.Background()
146114
model, err := parseInput(p, cmd)
@@ -215,6 +183,9 @@ func configureFlags(cmd *cobra.Command) {
215183
cmd.Flags().StringSlice(volumesFlag, []string{}, "The list of volumes attached to the server")
216184

217185
err := flags.MarkFlagsRequired(cmd, nameFlag, machineTypeFlag)
186+
cmd.MarkFlagsMutuallyExclusive(imageIdFlag, bootVolumeSourceIdFlag)
187+
cmd.MarkFlagsMutuallyExclusive(imageIdFlag, bootVolumeSourceTypeFlag)
188+
cmd.MarkFlagsMutuallyExclusive(networkIdFlag, networkInterfaceIdsFlag)
218189
cobra.CheckErr(err)
219190
}
220191

@@ -224,6 +195,35 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
224195
return nil, &cliErr.ProjectIdError{}
225196
}
226197

198+
bootVolumeSourceId, _ := cmd.Flags().GetString(bootVolumeSourceIdFlag)
199+
bootVolumeSourceType, _ := cmd.Flags().GetString(bootVolumeSourceTypeFlag)
200+
bootVolumeSize, _ := cmd.Flags().GetInt64(bootVolumeSizeFlag)
201+
imageId, _ := cmd.Flags().GetString(imageIdFlag)
202+
203+
if imageId == "" && bootVolumeSourceId == "" && bootVolumeSourceType == "" {
204+
return nil, &cliErr.ServerCreateMissingFlagsError{
205+
Cmd: cmd,
206+
}
207+
}
208+
209+
if imageId == "" {
210+
err := flags.MarkFlagsRequired(cmd, bootVolumeSourceIdFlag, bootVolumeSourceTypeFlag)
211+
cobra.CheckErr(err)
212+
}
213+
214+
if bootVolumeSourceType == "image" && bootVolumeSize == 0 {
215+
err := cmd.MarkFlagRequired(bootVolumeSizeFlag)
216+
cobra.CheckErr(err)
217+
return nil, &cliErr.ServerCreateError{
218+
Cmd: cmd,
219+
}
220+
}
221+
222+
if bootVolumeSourceId == "" && bootVolumeSourceType == "" {
223+
err := cmd.MarkFlagRequired(imageIdFlag)
224+
cobra.CheckErr(err)
225+
}
226+
227227
model := inputModel{
228228
GlobalFlagModel: globalFlags,
229229
Name: flags.FlagToStringPointer(p, cmd, nameFlag),
@@ -303,7 +303,8 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
303303
payload.Networking.CreateServerNetworkingWithNics = &iaas.CreateServerNetworkingWithNics{
304304
NicIds: model.NetworkInterfaceIds,
305305
}
306-
} else if model.NetworkId != nil {
306+
}
307+
if model.NetworkId != nil {
307308
payload.Networking.CreateServerNetworking = &iaas.CreateServerNetworking{
308309
NetworkId: model.NetworkId,
309310
}

internal/cmd/beta/server/create/create_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ func TestParseInput(t *testing.T) {
167167
delete(flagValues, bootVolumeSizeFlag)
168168
delete(flagValues, bootVolumePerformanceClassFlag)
169169
delete(flagValues, bootVolumeDeleteOnTerminationFlag)
170-
delete(flagValues, imageIdFlag)
171170
delete(flagValues, keypairNameFlag)
172171
delete(flagValues, networkIdFlag)
173172
delete(flagValues, networkInterfaceIdsFlag)
@@ -186,7 +185,6 @@ func TestParseInput(t *testing.T) {
186185
model.BootVolumeSize = nil
187186
model.BootVolumePerformanceClass = nil
188187
model.BootVolumeDeleteOnTermination = nil
189-
model.ImageId = nil
190188
model.KeypairName = nil
191189
model.NetworkId = nil
192190
model.NetworkInterfaceIds = nil

internal/pkg/errors/errors.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,28 @@ The profile name can only contain lowercase letters, numbers, and "-" and cannot
138138
139139
To enable it, run:
140140
$ stackit %s enable`
141+
142+
IAAS_SERVER_MISSING_VOLUME_SIZE = `Boot volume size must be provided when "source_type" is "image".`
143+
144+
IAAS_SERVER_MISSING_IMAGE_OR_VOLUME_FLAGS = `Either Image ID or boot volume flags must be provided.`
141145
)
142146

147+
type ServerCreateMissingFlagsError struct {
148+
Cmd *cobra.Command
149+
}
150+
151+
func (e *ServerCreateMissingFlagsError) Error() string {
152+
return fmt.Sprintf(IAAS_SERVER_MISSING_IMAGE_OR_VOLUME_FLAGS)
153+
}
154+
155+
type ServerCreateError struct {
156+
Cmd *cobra.Command
157+
}
158+
159+
func (e *ServerCreateError) Error() string {
160+
return fmt.Sprintf(IAAS_SERVER_MISSING_VOLUME_SIZE)
161+
}
162+
143163
type ProjectIdError struct{}
144164

145165
func (e *ProjectIdError) Error() string {

0 commit comments

Comments
 (0)