Skip to content

Commit 730321a

Browse files
authored
add as operation (#1897)
* add as operation * add changelog
1 parent 877b36e commit 730321a

15 files changed

+870
-0
lines changed

.changelog/1897.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
```release-note:new-resource
2+
tencentcloud_as_scaling_group_status
3+
```
4+
5+
```release-note:new-resource
6+
tencentcloud_as_scale_in_instances
7+
```
8+
9+
```release-note:new-resource
10+
tencentcloud_as_scale_out_instances
11+
```
12+
13+
```release-note:new-resource
14+
tencentcloud_as_execute_scaling_policy
15+
```

tencentcloud/provider.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ Auto Scaling(AS)
138138
Resource
139139
tencentcloud_as_scaling_config
140140
tencentcloud_as_scaling_group
141+
tencentcloud_as_scaling_group_status
141142
tencentcloud_as_attachment
142143
tencentcloud_as_scaling_policy
143144
tencentcloud_as_schedule
@@ -147,6 +148,9 @@ Auto Scaling(AS)
147148
tencentcloud_as_protect_instances
148149
tencentcloud_as_start_instances
149150
tencentcloud_as_stop_instances
151+
tencentcloud_as_scale_in_instances
152+
tencentcloud_as_scale_out_instances
153+
tencentcloud_as_execute_scaling_policy
150154
151155
Content Delivery Network(CDN)
152156
Data Source
@@ -2268,6 +2272,7 @@ func Provider() *schema.Provider {
22682272
"tencentcloud_as_load_balancer": resourceTencentCloudAsLoadBalancer(),
22692273
"tencentcloud_as_scaling_config": resourceTencentCloudAsScalingConfig(),
22702274
"tencentcloud_as_scaling_group": resourceTencentCloudAsScalingGroup(),
2275+
"tencentcloud_as_scaling_group_status": resourceTencentCloudAsScalingGroupStatus(),
22712276
"tencentcloud_as_attachment": resourceTencentCloudAsAttachment(),
22722277
"tencentcloud_as_scaling_policy": resourceTencentCloudAsScalingPolicy(),
22732278
"tencentcloud_as_schedule": resourceTencentCloudAsSchedule(),
@@ -2277,6 +2282,9 @@ func Provider() *schema.Provider {
22772282
"tencentcloud_as_protect_instances": resourceTencentCloudAsProtectInstances(),
22782283
"tencentcloud_as_start_instances": resourceTencentCloudAsStartInstances(),
22792284
"tencentcloud_as_stop_instances": resourceTencentCloudAsStopInstances(),
2285+
"tencentcloud_as_scale_in_instances": resourceTencentCloudAsScaleInInstances(),
2286+
"tencentcloud_as_scale_out_instances": resourceTencentCloudAsScaleOutInstances(),
2287+
"tencentcloud_as_execute_scaling_policy": resourceTencentCloudAsExecuteScalingPolicy(),
22802288
"tencentcloud_mongodb_instance": resourceTencentCloudMongodbInstance(),
22812289
"tencentcloud_mongodb_sharding_instance": resourceTencentCloudMongodbShardingInstance(),
22822290
"tencentcloud_mongodb_instance_account": resourceTencentCloudMongodbInstanceAccount(),
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
Provides a resource to create a as execute_scaling_policy
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_as_execute_scaling_policy" "execute_scaling_policy" {
8+
auto_scaling_policy_id = "asp-519acdug"
9+
honor_cooldown = false
10+
trigger_source = "API"
11+
}
12+
```
13+
14+
Import
15+
16+
as execute_scaling_policy can be imported using the id, e.g.
17+
18+
```
19+
terraform import tencentcloud_as_execute_scaling_policy.execute_scaling_policy execute_scaling_policy_id
20+
```
21+
*/
22+
package tencentcloud
23+
24+
import (
25+
"log"
26+
27+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
28+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
29+
as "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as/v20180419"
30+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
31+
)
32+
33+
func resourceTencentCloudAsExecuteScalingPolicy() *schema.Resource {
34+
return &schema.Resource{
35+
Create: resourceTencentCloudAsExecuteScalingPolicyCreate,
36+
Read: resourceTencentCloudAsExecuteScalingPolicyRead,
37+
Delete: resourceTencentCloudAsExecuteScalingPolicyDelete,
38+
Importer: &schema.ResourceImporter{
39+
State: schema.ImportStatePassthrough,
40+
},
41+
Schema: map[string]*schema.Schema{
42+
"auto_scaling_policy_id": {
43+
Required: true,
44+
ForceNew: true,
45+
Type: schema.TypeString,
46+
Description: "Auto-scaling policy ID. This parameter is not available to a target tracking policy.",
47+
},
48+
49+
"honor_cooldown": {
50+
Optional: true,
51+
ForceNew: true,
52+
Type: schema.TypeBool,
53+
Description: "Whether to check if the auto scaling group is in the cooldown period. Default value: false.",
54+
},
55+
56+
"trigger_source": {
57+
Optional: true,
58+
ForceNew: true,
59+
Type: schema.TypeString,
60+
Description: "Source that triggers the scaling policy. Valid values: API and CLOUD_MONITOR. Default value: API. The value CLOUD_MONITOR is specific to the Cloud Monitor service.",
61+
},
62+
},
63+
}
64+
}
65+
66+
func resourceTencentCloudAsExecuteScalingPolicyCreate(d *schema.ResourceData, meta interface{}) error {
67+
defer logElapsed("resource.tencentcloud_as_execute_scaling_policy.create")()
68+
defer inconsistentCheck(d, meta)()
69+
70+
logId := getLogId(contextNil)
71+
72+
var (
73+
request = as.NewExecuteScalingPolicyRequest()
74+
response = as.NewExecuteScalingPolicyResponse()
75+
activityId string
76+
)
77+
if v, ok := d.GetOk("auto_scaling_policy_id"); ok {
78+
request.AutoScalingPolicyId = helper.String(v.(string))
79+
}
80+
81+
if v, ok := d.GetOkExists("honor_cooldown"); ok {
82+
request.HonorCooldown = helper.Bool(v.(bool))
83+
}
84+
85+
if v, ok := d.GetOk("trigger_source"); ok {
86+
request.TriggerSource = helper.String(v.(string))
87+
}
88+
89+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
90+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseAsClient().ExecuteScalingPolicy(request)
91+
if e != nil {
92+
return retryError(e)
93+
} else {
94+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
95+
}
96+
response = result
97+
return nil
98+
})
99+
if err != nil {
100+
log.Printf("[CRITAL]%s operate as executeScalingPolicy failed, reason:%+v", logId, err)
101+
return err
102+
}
103+
104+
activityId = *response.Response.ActivityId
105+
d.SetId(activityId)
106+
107+
return resourceTencentCloudAsExecuteScalingPolicyRead(d, meta)
108+
}
109+
110+
func resourceTencentCloudAsExecuteScalingPolicyRead(d *schema.ResourceData, meta interface{}) error {
111+
defer logElapsed("resource.tencentcloud_as_execute_scaling_policy.read")()
112+
defer inconsistentCheck(d, meta)()
113+
114+
return nil
115+
}
116+
117+
func resourceTencentCloudAsExecuteScalingPolicyDelete(d *schema.ResourceData, meta interface{}) error {
118+
defer logElapsed("resource.tencentcloud_as_execute_scaling_policy.delete")()
119+
defer inconsistentCheck(d, meta)()
120+
121+
return nil
122+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudNeedFixAsExecuteScalingPolicyResource_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: testAccAsExecuteScalingPolicy,
19+
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_as_execute_scaling_policy.execute_scaling_policy", "id")),
20+
},
21+
{
22+
ResourceName: "tencentcloud_as_execute_scaling_policy.execute_scaling_policy",
23+
ImportState: true,
24+
ImportStateVerify: true,
25+
},
26+
},
27+
})
28+
}
29+
30+
const testAccAsExecuteScalingPolicy = `
31+
32+
resource "tencentcloud_as_execute_scaling_policy" "execute_scaling_policy" {
33+
auto_scaling_policy_id = "asp-519acdug"
34+
honor_cooldown = false
35+
trigger_source = "API"
36+
}
37+
38+
`
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
Provides a resource to create a as scale_in_instances
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_as_scale_in_instances" "scale_in_instances" {
8+
auto_scaling_group_id = "asg-519acdug"
9+
scale_in_number = 1
10+
}
11+
```
12+
13+
Import
14+
15+
as scale_in_instances can be imported using the id, e.g.
16+
17+
```
18+
terraform import tencentcloud_as_scale_in_instances.scale_in_instances scale_in_instances_id
19+
```
20+
*/
21+
package tencentcloud
22+
23+
import (
24+
"log"
25+
26+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
27+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
28+
as "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as/v20180419"
29+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
30+
)
31+
32+
func resourceTencentCloudAsScaleInInstances() *schema.Resource {
33+
return &schema.Resource{
34+
Create: resourceTencentCloudAsScaleInInstancesCreate,
35+
Read: resourceTencentCloudAsScaleInInstancesRead,
36+
Delete: resourceTencentCloudAsScaleInInstancesDelete,
37+
Schema: map[string]*schema.Schema{
38+
"auto_scaling_group_id": {
39+
Required: true,
40+
ForceNew: true,
41+
Type: schema.TypeString,
42+
Description: "Scaling group ID.",
43+
},
44+
45+
"scale_in_number": {
46+
Required: true,
47+
ForceNew: true,
48+
Type: schema.TypeInt,
49+
Description: "Number of instances to be reduced.",
50+
},
51+
},
52+
}
53+
}
54+
55+
func resourceTencentCloudAsScaleInInstancesCreate(d *schema.ResourceData, meta interface{}) error {
56+
defer logElapsed("resource.tencentcloud_as_scale_in_instances.create")()
57+
defer inconsistentCheck(d, meta)()
58+
59+
logId := getLogId(contextNil)
60+
61+
var (
62+
request = as.NewScaleInInstancesRequest()
63+
response = as.NewScaleInInstancesResponse()
64+
activityId string
65+
)
66+
if v, ok := d.GetOk("auto_scaling_group_id"); ok {
67+
request.AutoScalingGroupId = helper.String(v.(string))
68+
}
69+
70+
if v, ok := d.GetOkExists("scale_in_number"); ok {
71+
request.ScaleInNumber = helper.IntUint64(v.(int))
72+
}
73+
74+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
75+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseAsClient().ScaleInInstances(request)
76+
if e != nil {
77+
return retryError(e)
78+
} else {
79+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
80+
}
81+
response = result
82+
return nil
83+
})
84+
if err != nil {
85+
log.Printf("[CRITAL]%s operate as scaleInInstances failed, reason:%+v", logId, err)
86+
return err
87+
}
88+
89+
activityId = *response.Response.ActivityId
90+
d.SetId(activityId)
91+
92+
return resourceTencentCloudAsScaleInInstancesRead(d, meta)
93+
}
94+
95+
func resourceTencentCloudAsScaleInInstancesRead(d *schema.ResourceData, meta interface{}) error {
96+
defer logElapsed("resource.tencentcloud_as_scale_in_instances.read")()
97+
defer inconsistentCheck(d, meta)()
98+
99+
return nil
100+
}
101+
102+
func resourceTencentCloudAsScaleInInstancesDelete(d *schema.ResourceData, meta interface{}) error {
103+
defer logElapsed("resource.tencentcloud_as_scale_in_instances.delete")()
104+
defer inconsistentCheck(d, meta)()
105+
106+
return nil
107+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudNeedFixAsScaleInInstancesResource_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: testAccAsScaleInInstances,
19+
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_as_scale_in_instances.scale_in_instances", "id")),
20+
},
21+
{
22+
ResourceName: "tencentcloud_as_scale_in_instances.scale_in_instances",
23+
ImportState: true,
24+
ImportStateVerify: true,
25+
},
26+
},
27+
})
28+
}
29+
30+
const testAccAsScaleInInstances = `
31+
32+
resource "tencentcloud_as_scale_in_instances" "scale_in_instances" {
33+
auto_scaling_group_id = "asg-519acdug"
34+
scale_in_number = 1
35+
}
36+
37+
`

0 commit comments

Comments
 (0)