Skip to content

Commit 021aeee

Browse files
hellertangmikatong
authored andcommitted
add as operation
1 parent 707becb commit 021aeee

13 files changed

+627
-10
lines changed

tencentcloud/provider.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ Auto Scaling(AS)
126126
tencentcloud_as_lifecycle_hook
127127
tencentcloud_as_notification
128128
tencentcloud_as_remove_instances
129+
tencentcloud_as_protect_instances
130+
tencentcloud_as_start_instances
131+
tencentcloud_as_stop_instances
129132
130133
Content Delivery Network(CDN)
131134
Data Source
@@ -1268,6 +1271,9 @@ func Provider() terraform.ResourceProvider {
12681271
"tencentcloud_as_lifecycle_hook": resourceTencentCloudAsLifecycleHook(),
12691272
"tencentcloud_as_notification": resourceTencentCloudAsNotification(),
12701273
"tencentcloud_as_remove_instances": resourceTencentCloudAsRemoveInstances(),
1274+
"tencentcloud_as_protect_instances": resourceTencentCloudAsProtectInstances(),
1275+
"tencentcloud_as_start_instances": resourceTencentCloudAsStartInstances(),
1276+
"tencentcloud_as_stop_instances": resourceTencentCloudAsStopInstances(),
12711277
"tencentcloud_mongodb_instance": resourceTencentCloudMongodbInstance(),
12721278
"tencentcloud_mongodb_sharding_instance": resourceTencentCloudMongodbShardingInstance(),
12731279
"tencentcloud_dayu_cc_http_policy": resourceTencentCloudDayuCCHttpPolicy(),
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
Provides a resource to create a as protect_instances
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_as_protect_instances" "protect_instances" {
8+
auto_scaling_group_id = ""
9+
instance_ids = ""
10+
protected_from_scale_in = ""
11+
}
12+
```
13+
14+
*/
15+
package tencentcloud
16+
17+
import (
18+
"log"
19+
20+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
21+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
22+
as "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as/v20180419"
23+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
24+
)
25+
26+
func resourceTencentCloudAsProtectInstances() *schema.Resource {
27+
return &schema.Resource{
28+
Create: resourceTencentCloudAsProtectInstancesCreate,
29+
Read: resourceTencentCloudAsProtectInstancesRead,
30+
Delete: resourceTencentCloudAsProtectInstancesDelete,
31+
Schema: map[string]*schema.Schema{
32+
"auto_scaling_group_id": {
33+
Required: true,
34+
ForceNew: true,
35+
Type: schema.TypeString,
36+
Description: "Launch configuration ID.",
37+
},
38+
39+
"instance_ids": {
40+
Required: true,
41+
ForceNew: true,
42+
Type: schema.TypeSet,
43+
Elem: &schema.Schema{
44+
Type: schema.TypeString,
45+
},
46+
Description: "List of cvm instances to remove.",
47+
},
48+
49+
"protected_from_scale_in": {
50+
Required: true,
51+
ForceNew: true,
52+
Type: schema.TypeBool,
53+
Description: "If instances need protect.",
54+
},
55+
},
56+
}
57+
}
58+
59+
func resourceTencentCloudAsProtectInstancesCreate(d *schema.ResourceData, meta interface{}) error {
60+
defer logElapsed("data_source.tencentcloud_as_protect_instances.read")()
61+
defer inconsistentCheck(d, meta)()
62+
63+
logId := getLogId(contextNil)
64+
65+
var (
66+
request = as.NewSetInstancesProtectionRequest()
67+
response = as.NewSetInstancesProtectionResponse()
68+
//instanceIds string
69+
)
70+
if v, ok := d.GetOk("auto_scaling_group_id"); ok {
71+
request.AutoScalingGroupId = helper.String(v.(string))
72+
}
73+
74+
if v, ok := d.GetOk("instance_ids"); ok {
75+
instanceIdsSet := v.(*schema.Set).List()
76+
for i := range instanceIdsSet {
77+
instanceIds := instanceIdsSet[i].(string)
78+
request.InstanceIds = append(request.InstanceIds, &instanceIds)
79+
}
80+
}
81+
82+
if v, _ := d.GetOk("protected_from_scale_in"); v != nil {
83+
request.ProtectedFromScaleIn = helper.Bool(v.(bool))
84+
}
85+
86+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
87+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseAsClient().SetInstancesProtection(request)
88+
if e != nil {
89+
return retryError(e)
90+
} else {
91+
log.Println("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
92+
}
93+
response = result
94+
return nil
95+
})
96+
if err != nil {
97+
log.Println("[CRITAL]%s operate as protectInstances failed, reason:%+v", logId, err)
98+
return nil
99+
}
100+
101+
// 需要 setId,可以通过InstancesId作为ID
102+
d.SetId("")
103+
104+
return resourceTencentCloudAsProtectInstancesRead(d, meta)
105+
}
106+
107+
func resourceTencentCloudAsProtectInstancesRead(d *schema.ResourceData, meta interface{}) error {
108+
defer logElapsed("resource.tencentcloud_as_protect_instances.read")()
109+
defer inconsistentCheck(d, meta)()
110+
111+
return nil
112+
}
113+
114+
func resourceTencentCloudAsProtectInstancesDelete(d *schema.ResourceData, meta interface{}) error {
115+
defer logElapsed("resource.tencentcloud_as_protect_instances.delete")()
116+
defer inconsistentCheck(d, meta)()
117+
118+
return nil
119+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
func TestTencentCloudAsProtectInstancesResource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() {
13+
testAccPreCheck(t)
14+
},
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccAsProtectInstances,
19+
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_as_protect_instances.protect_instances", "id")),
20+
},
21+
},
22+
})
23+
}
24+
25+
const testAccAsProtectInstances = `
26+
27+
resource "tencentcloud_as_protect_instances" "protect_instances" {
28+
auto_scaling_group_id = ""
29+
instance_ids = ""
30+
protected_from_scale_in = ""
31+
}
32+
33+
`

tencentcloud/resource_tc_as_remove_instances.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,19 @@ terraform import tencentcloud_as_remove_instances.remove_instances remove_instan
2121
package tencentcloud
2222

2323
import (
24+
"log"
25+
2426
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
2527
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
2628
as "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as/v20180419"
2729
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
28-
"log"
2930
)
3031

3132
func resourceTencentCloudAsRemoveInstances() *schema.Resource {
3233
return &schema.Resource{
3334
Create: resourceTencentCloudAsRemoveInstancesCreate,
3435
Read: resourceTencentCloudAsRemoveInstancesRead,
3536
Delete: resourceTencentCloudAsRemoveInstancesDelete,
36-
Importer: &schema.ResourceImporter{
37-
State: schema.ImportStatePassthrough,
38-
},
3937
Schema: map[string]*schema.Schema{
4038
"auto_scaling_group_id": {
4139
Required: true,

tencentcloud/resource_tc_as_remove_instances_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package tencentcloud
22

33
import (
4-
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
54
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
67
)
78

89
func TestTencentCloudAsRemoveInstancesResource_basic(t *testing.T) {
@@ -17,11 +18,6 @@ func TestTencentCloudAsRemoveInstancesResource_basic(t *testing.T) {
1718
Config: testAccAsRemoveInstances,
1819
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_as_remove_instances.remove_instances", "id")),
1920
},
20-
{
21-
ResourceName: "tencentcloud_as_remove_instances.remove_instances",
22-
ImportState: true,
23-
ImportStateVerify: true,
24-
},
2521
},
2622
})
2723
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
Provides a resource to create a as start_instances
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_as_start_instances" "start_instances" {
8+
auto_scaling_group_id = ""
9+
instance_ids = ""
10+
}
11+
```
12+
13+
*/
14+
package tencentcloud
15+
16+
import (
17+
"context"
18+
"fmt"
19+
"log"
20+
21+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
22+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
23+
as "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as/v20180419"
24+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
25+
)
26+
27+
func resourceTencentCloudAsStartInstances() *schema.Resource {
28+
return &schema.Resource{
29+
Create: resourceTencentCloudAsStartInstancesCreate,
30+
Read: resourceTencentCloudAsStartInstancesRead,
31+
Delete: resourceTencentCloudAsStartInstancesDelete,
32+
Schema: map[string]*schema.Schema{
33+
"auto_scaling_group_id": {
34+
Required: true,
35+
ForceNew: true,
36+
Type: schema.TypeString,
37+
Description: "Launch configuration ID.",
38+
},
39+
40+
"instance_ids": {
41+
Required: true,
42+
ForceNew: true,
43+
Type: schema.TypeSet,
44+
Elem: &schema.Schema{
45+
Type: schema.TypeString,
46+
},
47+
Description: "List of cvm instances to start.",
48+
},
49+
},
50+
}
51+
}
52+
53+
func resourceTencentCloudAsStartInstancesCreate(d *schema.ResourceData, meta interface{}) error {
54+
defer logElapsed("data_source.tencentcloud_as_start_instances.read")()
55+
defer inconsistentCheck(d, meta)()
56+
57+
logId := getLogId(contextNil)
58+
59+
var (
60+
request = as.NewStartAutoScalingInstancesRequest()
61+
response = as.NewStartAutoScalingInstancesResponse()
62+
activityId string
63+
)
64+
if v, ok := d.GetOk("auto_scaling_group_id"); ok {
65+
request.AutoScalingGroupId = helper.String(v.(string))
66+
}
67+
68+
if v, ok := d.GetOk("instance_ids"); ok {
69+
instanceIdsSet := v.(*schema.Set).List()
70+
for i := range instanceIdsSet {
71+
instanceIds := instanceIdsSet[i].(string)
72+
request.InstanceIds = append(request.InstanceIds, &instanceIds)
73+
}
74+
}
75+
76+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
77+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseAsClient().StartAutoScalingInstances(request)
78+
if e != nil {
79+
return retryError(e)
80+
} else {
81+
log.Println("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
82+
}
83+
response = result
84+
return nil
85+
})
86+
if err != nil {
87+
log.Println("[CRITAL]%s operate as startInstances failed, reason:%+v", logId, err)
88+
return nil
89+
}
90+
91+
activityId = *response.Response.ActivityId
92+
93+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
94+
service := AsService{
95+
client: meta.(*TencentCloudClient).apiV3Conn,
96+
}
97+
98+
err = resource.Retry(4*readRetryTimeout, func() *resource.RetryError {
99+
status, err := service.DescribeActivityById(ctx, activityId)
100+
if err != nil {
101+
return resource.NonRetryableError(err)
102+
}
103+
if status == SCALING_GROUP_ACTIVITY_STATUS_INIT || status == SCALING_GROUP_ACTIVITY_STATUS_RUNNING {
104+
return resource.RetryableError(fmt.Errorf("remove status is running(%s)", status))
105+
}
106+
if status == SCALING_GROUP_ACTIVITY_STATUS_SUCCESSFUL {
107+
return nil
108+
}
109+
return resource.NonRetryableError(fmt.Errorf("remove status is failed(%s)", status))
110+
})
111+
if err != nil {
112+
return err
113+
}
114+
115+
d.SetId(activityId)
116+
117+
return resourceTencentCloudAsStartInstancesRead(d, meta)
118+
}
119+
120+
func resourceTencentCloudAsStartInstancesRead(d *schema.ResourceData, meta interface{}) error {
121+
defer logElapsed("resource.tencentcloud_as_start_instances.read")()
122+
defer inconsistentCheck(d, meta)()
123+
124+
return nil
125+
}
126+
127+
func resourceTencentCloudAsStartInstancesDelete(d *schema.ResourceData, meta interface{}) error {
128+
defer logElapsed("resource.tencentcloud_as_start_instances.delete")()
129+
defer inconsistentCheck(d, meta)()
130+
131+
return nil
132+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
func TestTencentCloudAsStartInstancesResource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() {
13+
testAccPreCheck(t)
14+
},
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccAsStartInstances,
19+
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_as_start_instances.start_instances", "id")),
20+
},
21+
},
22+
})
23+
}
24+
25+
const testAccAsStartInstances = `
26+
27+
resource "tencentcloud_as_start_instances" "start_instances" {
28+
auto_scaling_group_id = ""
29+
instance_ids = ""
30+
}
31+
32+
`

0 commit comments

Comments
 (0)