Skip to content

Commit 2ef64cd

Browse files
tongyimingmikatong
andauthored
fix: cvm support orderly sg (#1430)
* fix: cvm support orderly sg * add changelog * update unit test Co-authored-by: mikatong <mikatong@tencent.com>
1 parent 7e48726 commit 2ef64cd

File tree

4 files changed

+143
-21
lines changed

4 files changed

+143
-21
lines changed

.changelog/1430.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_instance: support orderly security group
3+
```

tencentcloud/resource_tc_instance.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,22 @@ func resourceTencentCloudInstance() *schema.Resource {
325325
},
326326
// security group
327327
"security_groups": {
328-
Type: schema.TypeSet,
329-
Elem: &schema.Schema{Type: schema.TypeString},
330-
Optional: true,
331-
Computed: true,
332-
Description: "A list of security group IDs to associate with.",
328+
Type: schema.TypeSet,
329+
Elem: &schema.Schema{Type: schema.TypeString},
330+
Optional: true,
331+
Computed: true,
332+
ConflictsWith: []string{"orderly_security_groups"},
333+
Description: "A list of security group IDs to associate with.",
334+
Deprecated: "It will be deprecated. Use `orderly_security_groups` instead.",
335+
},
336+
337+
"orderly_security_groups": {
338+
Type: schema.TypeList,
339+
Elem: &schema.Schema{Type: schema.TypeString},
340+
Optional: true,
341+
Computed: true,
342+
ConflictsWith: []string{"security_groups"},
343+
Description: "A list of orderly security group IDs to associate with.",
333344
},
334345
// storage
335346
"system_disk_type": {
@@ -632,6 +643,14 @@ func resourceTencentCloudInstanceCreate(d *schema.ResourceData, meta interface{}
632643
}
633644
}
634645

646+
if v, ok := d.GetOk("orderly_security_groups"); ok {
647+
securityGroups := v.([]interface{})
648+
request.SecurityGroupIds = make([]*string, 0, len(securityGroups))
649+
for _, securityGroup := range securityGroups {
650+
request.SecurityGroupIds = append(request.SecurityGroupIds, helper.String(securityGroup.(string)))
651+
}
652+
}
653+
635654
// storage
636655
request.SystemDisk = &cvm.SystemDisk{}
637656
if v, ok := d.GetOk("system_disk_type"); ok {
@@ -929,7 +948,8 @@ func resourceTencentCloudInstanceRead(d *schema.ResourceData, meta interface{})
929948
_ = d.Set("internet_max_bandwidth_out", instance.InternetAccessible.InternetMaxBandwidthOut)
930949
_ = d.Set("vpc_id", instance.VirtualPrivateCloud.VpcId)
931950
_ = d.Set("subnet_id", instance.VirtualPrivateCloud.SubnetId)
932-
_ = d.Set("security_groups", helper.StringsInterfaces(instance.SecurityGroupIds))
951+
_ = d.Set("security_groups", instance.SecurityGroupIds)
952+
_ = d.Set("orderly_security_groups", instance.SecurityGroupIds)
933953
_ = d.Set("system_disk_type", instance.SystemDisk.DiskType)
934954
_ = d.Set("system_disk_size", instance.SystemDisk.DiskSize)
935955
_ = d.Set("system_disk_id", instance.SystemDisk.DiskId)
@@ -1154,6 +1174,19 @@ func resourceTencentCloudInstanceUpdate(d *schema.ResourceData, meta interface{}
11541174
d.SetPartial("security_groups")
11551175
}
11561176

1177+
if d.HasChange("orderly_security_groups") {
1178+
orderlySecurityGroups := d.Get("orderly_security_groups").([]interface{})
1179+
orderlySecurityGroupIds := make([]*string, 0, len(orderlySecurityGroups))
1180+
for _, securityGroup := range orderlySecurityGroups {
1181+
orderlySecurityGroupIds = append(orderlySecurityGroupIds, helper.String(securityGroup.(string)))
1182+
}
1183+
err := cvmService.ModifySecurityGroups(ctx, instanceId, orderlySecurityGroupIds)
1184+
if err != nil {
1185+
return err
1186+
}
1187+
d.SetPartial("orderly_security_groups")
1188+
}
1189+
11571190
if d.HasChange("project_id") {
11581191
projectId := d.Get("project_id").(int)
11591192
err := cvmService.ModifyProjectId(ctx, instanceId, int64(projectId))

tencentcloud/resource_tc_instance_test.go

Lines changed: 99 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func testSweepCvmInstance(region string) error {
6161
return nil
6262
}
6363

64-
func TestAccTencentCloudInstanceBasic(t *testing.T) {
64+
func TestAccTencentCloudInstanceResource_Basic(t *testing.T) {
6565
t.Parallel()
6666

6767
id := "tencentcloud_instance.foo"
@@ -103,7 +103,7 @@ func TestAccTencentCloudInstanceBasic(t *testing.T) {
103103
})
104104
}
105105

106-
func TestAccTencentCloudInstanceWithDataDisk(t *testing.T) {
106+
func TestAccTencentCloudInstanceResource_WithDataDisk(t *testing.T) {
107107
t.Parallel()
108108

109109
id := "tencentcloud_instance.foo"
@@ -149,7 +149,7 @@ func TestAccTencentCloudInstanceWithDataDisk(t *testing.T) {
149149
})
150150
}
151151

152-
func TestAccTencentCloudInstanceWithNetwork(t *testing.T) {
152+
func TestAccTencentCloudInstanceResource_WithNetwork(t *testing.T) {
153153
t.Parallel()
154154

155155
id := "tencentcloud_instance.foo"
@@ -184,7 +184,7 @@ func TestAccTencentCloudInstanceWithNetwork(t *testing.T) {
184184
})
185185
}
186186

187-
func TestAccTencentCloudInstanceWithPrivateIP(t *testing.T) {
187+
func TestAccTencentCloudInstanceResource_WithPrivateIP(t *testing.T) {
188188
t.Parallel()
189189
id := "tencentcloud_instance.foo"
190190
resource.Test(t, resource.TestCase{
@@ -206,7 +206,7 @@ func TestAccTencentCloudInstanceWithPrivateIP(t *testing.T) {
206206
})
207207
}
208208

209-
func TestAccTencentCloudInstanceWithKeyPairs(t *testing.T) {
209+
func TestAccTencentCloudInstanceResource_WithKeyPairs(t *testing.T) {
210210
id := "tencentcloud_instance.foo"
211211
resource.Test(t, resource.TestCase{
212212
PreCheck: func() { testAccPreCheck(t) },
@@ -243,7 +243,7 @@ func TestAccTencentCloudInstanceWithKeyPairs(t *testing.T) {
243243
})
244244
}
245245

246-
func TestAccTencentCloudInstanceWithPassword(t *testing.T) {
246+
func TestAccTencentCloudInstanceResource_WithPassword(t *testing.T) {
247247
t.Parallel()
248248

249249
id := "tencentcloud_instance.foo"
@@ -280,7 +280,7 @@ func TestAccTencentCloudInstanceWithPassword(t *testing.T) {
280280
})
281281
}
282282

283-
func TestAccTencentCloudInstanceWithImageLogin(t *testing.T) {
283+
func TestAccTencentCloudInstanceResource_WithImageLogin(t *testing.T) {
284284

285285
id := "tencentcloud_instance.foo"
286286
resource.Test(t, resource.TestCase{
@@ -303,7 +303,7 @@ func TestAccTencentCloudInstanceWithImageLogin(t *testing.T) {
303303
})
304304
}
305305

306-
func TestAccTencentCloudInstanceWithName(t *testing.T) {
306+
func TestAccTencentCloudInstanceResource_WithName(t *testing.T) {
307307
t.Parallel()
308308

309309
id := "tencentcloud_instance.foo"
@@ -337,7 +337,7 @@ func TestAccTencentCloudInstanceWithName(t *testing.T) {
337337
})
338338
}
339339

340-
func TestAccTencentCloudInstanceWithHostname(t *testing.T) {
340+
func TestAccTencentCloudInstanceResource_WithHostname(t *testing.T) {
341341
t.Parallel()
342342

343343
id := "tencentcloud_instance.foo"
@@ -361,7 +361,7 @@ func TestAccTencentCloudInstanceWithHostname(t *testing.T) {
361361
})
362362
}
363363

364-
func TestAccTencentCloudInstanceWithSecurityGroup(t *testing.T) {
364+
func TestAccTencentCloudInstanceResource_WithSecurityGroup(t *testing.T) {
365365
t.Parallel()
366366

367367
instanceId := "tencentcloud_instance.foo"
@@ -412,7 +412,63 @@ func TestAccTencentCloudInstanceWithSecurityGroup(t *testing.T) {
412412
})
413413
}
414414

415-
func TestAccTencentCloudInstanceWithTags(t *testing.T) {
415+
func TestAccTencentCloudInstanceResource_WithOrderlySecurityGroup(t *testing.T) {
416+
t.Parallel()
417+
418+
var sgId1, sgId2, sgId3 string
419+
instanceId := "tencentcloud_instance.cvm_with_orderly_sg"
420+
orderlySecurityGroupId1 := "tencentcloud_security_group.orderly_security_group1"
421+
orderlySecurityGroupId2 := "tencentcloud_security_group.orderly_security_group2"
422+
orderlySecurityGroupId3 := "tencentcloud_security_group.orderly_security_group3"
423+
424+
resource.Test(t, resource.TestCase{
425+
PreCheck: func() { testAccPreCheck(t) },
426+
IDRefreshName: instanceId,
427+
Providers: testAccProviders,
428+
CheckDestroy: testAccCheckInstanceDestroy,
429+
Steps: []resource.TestStep{
430+
{
431+
PreConfig: func() { testAccStepPreConfigSetTempAKSK(t, ACCOUNT_TYPE_COMMON) },
432+
Config: testAccTencentCloudInstanceOrderlySecurityGroups(`[
433+
tencentcloud_security_group.orderly_security_group1.id,
434+
tencentcloud_security_group.orderly_security_group2.id,
435+
tencentcloud_security_group.orderly_security_group3.id
436+
]`),
437+
Check: resource.ComposeTestCheckFunc(
438+
testAccCheckTencentCloudInstanceExists(instanceId),
439+
testAccCheckSecurityGroupExists(orderlySecurityGroupId1, &sgId1),
440+
testAccCheckSecurityGroupExists(orderlySecurityGroupId2, &sgId2),
441+
testAccCheckSecurityGroupExists(orderlySecurityGroupId3, &sgId3),
442+
443+
resource.TestCheckResourceAttrPtr(instanceId, "orderly_security_groups.0", &sgId1),
444+
resource.TestCheckResourceAttrPtr(instanceId, "orderly_security_groups.1", &sgId2),
445+
resource.TestCheckResourceAttrPtr(instanceId, "orderly_security_groups.2", &sgId3),
446+
),
447+
},
448+
449+
{
450+
PreConfig: func() { testAccStepPreConfigSetTempAKSK(t, ACCOUNT_TYPE_COMMON) },
451+
Config: testAccTencentCloudInstanceOrderlySecurityGroups(`[
452+
tencentcloud_security_group.orderly_security_group3.id,
453+
tencentcloud_security_group.orderly_security_group2.id,
454+
tencentcloud_security_group.orderly_security_group1.id
455+
]`),
456+
Check: resource.ComposeTestCheckFunc(
457+
testAccCheckTencentCloudInstanceExists(instanceId),
458+
testAccCheckSecurityGroupExists(orderlySecurityGroupId1, &sgId1),
459+
testAccCheckSecurityGroupExists(orderlySecurityGroupId2, &sgId2),
460+
testAccCheckSecurityGroupExists(orderlySecurityGroupId3, &sgId3),
461+
462+
resource.TestCheckResourceAttrPtr(instanceId, "orderly_security_groups.0", &sgId3),
463+
resource.TestCheckResourceAttrPtr(instanceId, "orderly_security_groups.1", &sgId2),
464+
resource.TestCheckResourceAttrPtr(instanceId, "orderly_security_groups.2", &sgId1),
465+
),
466+
},
467+
},
468+
})
469+
}
470+
471+
func TestAccTencentCloudInstanceResource_WithTags(t *testing.T) {
416472
t.Parallel()
417473

418474
id := "tencentcloud_instance.foo"
@@ -453,7 +509,7 @@ func TestAccTencentCloudInstanceWithTags(t *testing.T) {
453509
})
454510
}
455511

456-
func TestAccTencentCloudInstanceWithPlacementGroup(t *testing.T) {
512+
func TestAccTencentCloudInstanceResource_WithPlacementGroup(t *testing.T) {
457513
t.Parallel()
458514

459515
id := "tencentcloud_instance.foo"
@@ -475,7 +531,7 @@ func TestAccTencentCloudInstanceWithPlacementGroup(t *testing.T) {
475531
})
476532
}
477533

478-
func TestAccTencentCloudInstanceWithSpotpaid(t *testing.T) {
534+
func TestAccTencentCloudInstanceResource_WithSpotpaid(t *testing.T) {
479535
t.Parallel()
480536

481537
id := "tencentcloud_instance.foo"
@@ -531,7 +587,7 @@ func TestAccTencentCloudNeedFixInstancePostpaidToPrepaid(t *testing.T) {
531587
})
532588
}
533589

534-
func TestAccTencentCloudInstancePrepaidFallbackToPostpaid(t *testing.T) {
590+
func TestAccTencentCloudInstanceResource_PrepaidFallbackToPostpaid(t *testing.T) {
535591

536592
id := "tencentcloud_instance.foo"
537593
resource.Test(t, resource.TestCase{
@@ -1062,3 +1118,32 @@ resource "tencentcloud_instance" "foo" {
10621118
spot_max_price = "0.5"
10631119
}
10641120
`
1121+
1122+
func testAccTencentCloudInstanceOrderlySecurityGroups(sgs string) string {
1123+
1124+
return fmt.Sprintf(defaultInstanceVariable+`
1125+
resource "tencentcloud_security_group" "orderly_security_group1" {
1126+
name = "test-cvm-orderly-sg1"
1127+
description = "test-cvm-orderly-sg1"
1128+
}
1129+
1130+
resource "tencentcloud_security_group" "orderly_security_group2" {
1131+
name = "test-cvm-orderly-sg2"
1132+
description = "test-cvm-orderly-sg2"
1133+
}
1134+
1135+
resource "tencentcloud_security_group" "orderly_security_group3" {
1136+
name = "test-cvm-orderly-sg3"
1137+
description = "test-cvm-orderly-sg3"
1138+
}
1139+
1140+
resource "tencentcloud_instance" "cvm_with_orderly_sg" {
1141+
instance_name = "test-orderly-sg-cvm"
1142+
availability_zone = var.availability_cvm_zone
1143+
image_id = data.tencentcloud_images.default.images.0.image_id
1144+
instance_type = data.tencentcloud_instance_types.default.instance_types.0.instance_type
1145+
system_disk_type = "CLOUD_PREMIUM"
1146+
orderly_security_groups = %s
1147+
}
1148+
`, sgs)
1149+
}

website/docs/r/instance.html.markdown

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,13 @@ The following arguments are supported:
162162
* `keep_image_login` - (Optional, Bool) Whether to keep image login or not, default is `false`. When the image type is private or shared or imported, this parameter can be set `true`. Modifying will cause the instance reset.
163163
* `key_ids` - (Optional, Set: [`String`]) The key pair to use for the instance, it looks like `skey-16jig7tx`. Modifying will cause the instance reset.
164164
* `key_name` - (Optional, String, **Deprecated**) Please use `key_ids` instead. The key pair to use for the instance, it looks like `skey-16jig7tx`. Modifying will cause the instance reset.
165+
* `orderly_security_groups` - (Optional, List: [`String`]) A list of orderly security group IDs to associate with.
165166
* `password` - (Optional, String) Password for the instance. In order for the new password to take effect, the instance will be restarted after the password change. Modifying will cause the instance reset.
166167
* `placement_group_id` - (Optional, String, ForceNew) The ID of a placement group.
167168
* `private_ip` - (Optional, String) The private IP to be assigned to this instance, must be in the provided subnet and available.
168169
* `project_id` - (Optional, Int) The project the instance belongs to, default to 0.
169170
* `running_flag` - (Optional, Bool) Set instance to running or stop. Default value is true, the instance will shutdown when this flag is false.
170-
* `security_groups` - (Optional, Set: [`String`]) A list of security group IDs to associate with.
171+
* `security_groups` - (Optional, Set: [`String`], **Deprecated**) It will be deprecated. Use `orderly_security_groups` instead. A list of security group IDs to associate with.
171172
* `spot_instance_type` - (Optional, String) Type of spot instance, only support `ONE-TIME` now. Note: it only works when instance_charge_type is set to `SPOTPAID`.
172173
* `spot_max_price` - (Optional, String, ForceNew) Max price of a spot instance, is the format of decimal string, for example "0.50". Note: it only works when instance_charge_type is set to `SPOTPAID`.
173174
* `stopped_mode` - (Optional, String) Billing method of a pay-as-you-go instance after shutdown. Available values: `KEEP_CHARGING`,`STOP_CHARGING`. Default `KEEP_CHARGING`.

0 commit comments

Comments
 (0)