Skip to content

Commit c10a3c8

Browse files
authored
fix: use 'security-group-id' for update server (#696)
1 parent 0e26d90 commit c10a3c8

File tree

5 files changed

+1226
-2
lines changed

5 files changed

+1226
-2
lines changed

internal/core/arg_specs.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ func (s ArgSpecs) GetByName(name string) *ArgSpec {
1313
return nil
1414
}
1515

16+
func (s *ArgSpecs) DeleteByName(name string) {
17+
for i, spec := range *s {
18+
if spec.Name == name {
19+
*s = append((*s)[:i], (*s)[i+1:]...)
20+
return
21+
}
22+
}
23+
}
24+
1625
type ArgSpec struct {
1726
// Name of the argument.
1827
Name string

internal/namespaces/instance/v1/custom_server.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ func serverUpdateBuilder(c *core.Command) *core.Command {
178178
*instance.UpdateServerRequest
179179
IP *instance.NullableStringValue
180180
PlacementGroupID *instance.NullableStringValue
181+
SecurityGroupID string
181182
}
182183

183184
IPArgSpec := &core.ArgSpec{
@@ -189,12 +190,17 @@ func serverUpdateBuilder(c *core.Command) *core.Command {
189190
c.ArgsType = reflect.TypeOf(instanceUpdateServerRequestCustom{})
190191

191192
c.ArgSpecs = append(c.ArgSpecs, IPArgSpec)
193+
c.ArgSpecs.DeleteByName("security-group.name")
194+
c.ArgSpecs.GetByName("security-group.id").Name = "security-group-id"
192195

193196
c.Run = func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
194197
customRequest := argsI.(*instanceUpdateServerRequestCustom)
195198

196199
updateServerRequest := customRequest.UpdateServerRequest
197200
updateServerRequest.PlacementGroup = customRequest.PlacementGroupID
201+
updateServerRequest.SecurityGroup = &instance.SecurityGroupTemplate{
202+
ID: customRequest.SecurityGroupID,
203+
}
198204

199205
attachIPRequest := (*instance.UpdateIPRequest)(nil)
200206

internal/namespaces/instance/v1/custom_server_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,29 @@ func Test_ServerUpdateCustom(t *testing.T) {
208208
return nil
209209
},
210210
}))
211+
212+
t.Run("Update server security-group-id from server with security-group-id", core.Test(&core.TestConfig{
213+
Commands: GetCommands(),
214+
BeforeFunc: func(ctx *core.BeforeFuncCtx) error {
215+
ctx.Meta["SecurityGroupResponse"] = ctx.ExecuteCmd("scw instance security-group create")
216+
ctx.Meta["SecurityGroupResponse2"] = ctx.ExecuteCmd("scw instance security-group create")
217+
ctx.Meta["Server"] = ctx.ExecuteCmd("scw instance server create stopped=true image=ubuntu-bionic security-group-id={{ .SecurityGroupResponse.SecurityGroup.ID }}")
218+
return nil
219+
},
220+
Cmd: "scw instance server update server-id={{ .Server.ID }} security-group-id={{ .SecurityGroupResponse2.SecurityGroup.ID }}",
221+
Check: core.TestCheckCombine(
222+
core.TestCheckExitCode(0),
223+
func(t *testing.T, ctx *core.CheckFuncCtx) {
224+
assert.Equal(t,
225+
ctx.Meta["SecurityGroupResponse2"].(*instance.CreateSecurityGroupResponse).SecurityGroup.ID,
226+
ctx.Result.(*instance.UpdateServerResponse).Server.SecurityGroup.ID)
227+
},
228+
),
229+
AfterFunc: func(ctx *core.AfterFuncCtx) error {
230+
ctx.ExecuteCmd("scw instance server delete server-id={{ .Server.ID }} delete-ip=true delete-volumes=true")
231+
ctx.ExecuteCmd("scw instance security-group delete security-group-id={{ .SecurityGroupResponse.SecurityGroup.ID }}")
232+
ctx.ExecuteCmd("scw instance security-group delete security-group-id={{ .SecurityGroupResponse2.SecurityGroup.ID }}")
233+
return nil
234+
},
235+
}))
211236
}

0 commit comments

Comments
 (0)