Skip to content

Commit 90cec67

Browse files
authored
feat: as - support spot and prepaid charge type (#931)
1 parent 44b21b8 commit 90cec67

File tree

3 files changed

+199
-2
lines changed

3 files changed

+199
-2
lines changed

tencentcloud/resource_tc_as_scaling_config.go

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ resource "tencentcloud_as_scaling_config" "launch_configuration" {
3131
}
3232
```
3333
34+
Using SPOT charge type
35+
```
36+
resource "tencentcloud_as_scaling_config" "launch_configuration" {
37+
configuration_name = "launch-configuration"
38+
image_id = "img-9qabwvbn"
39+
instance_types = ["SA1.SMALL1"]
40+
instance_charge_type = "SPOTPAID"
41+
spot_instance_type = "one-time"
42+
spot_max_price = "1000"
43+
}
44+
```
45+
3446
Import
3547
3648
AutoScaling Configuration can be imported using the id, e.g.
@@ -135,6 +147,37 @@ func resourceTencentCloudAsScalingConfig() *schema.Resource {
135147
},
136148
},
137149
},
150+
// payment
151+
"instance_charge_type": {
152+
Type: schema.TypeString,
153+
Optional: true,
154+
Description: "Charge type of instance. Valid values are `PREPAID`, `POSTPAID_BY_HOUR`, `SPOTPAID`. The default is `POSTPAID_BY_HOUR`. NOTE: `SPOTPAID` instance must set `spot_instance_type` and `spot_max_price` at the same time.",
155+
},
156+
"instance_charge_type_prepaid_period": {
157+
Type: schema.TypeInt,
158+
Optional: true,
159+
ValidateFunc: validateAllowedIntValue(CVM_PREPAID_PERIOD),
160+
Description: "The tenancy (in month) of the prepaid instance, NOTE: it only works when instance_charge_type is set to `PREPAID`. Valid values are `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `11`, `12`, `24`, `36`.",
161+
},
162+
"instance_charge_type_prepaid_renew_flag": {
163+
Type: schema.TypeString,
164+
Optional: true,
165+
Computed: true,
166+
ValidateFunc: validateAllowedStringValue(CVM_PREPAID_RENEW_FLAG),
167+
Description: "Auto renewal flag. Valid values: `NOTIFY_AND_AUTO_RENEW`: notify upon expiration and renew automatically, `NOTIFY_AND_MANUAL_RENEW`: notify upon expiration but do not renew automatically, `DISABLE_NOTIFY_AND_MANUAL_RENEW`: neither notify upon expiration nor renew automatically. Default value: `NOTIFY_AND_MANUAL_RENEW`. If this parameter is specified as `NOTIFY_AND_AUTO_RENEW`, the instance will be automatically renewed on a monthly basis if the account balance is sufficient. NOTE: it only works when instance_charge_type is set to `PREPAID`.",
168+
},
169+
"spot_instance_type": {
170+
Type: schema.TypeString,
171+
Optional: true,
172+
ValidateFunc: validateAllowedStringValue([]string{"one-time"}),
173+
Description: "Type of spot instance, only support `one-time` now. Note: it only works when instance_charge_type is set to `SPOTPAID`.",
174+
},
175+
"spot_max_price": {
176+
Type: schema.TypeString,
177+
Optional: true,
178+
ValidateFunc: validateStringNumber,
179+
Description: "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`.",
180+
},
138181
"internet_charge_type": {
139182
Type: schema.TypeString,
140183
Optional: true,
@@ -362,7 +405,32 @@ func resourceTencentCloudAsScalingConfigCreate(d *schema.ResourceData, meta inte
362405
request.UserData = helper.String(v.(string))
363406
}
364407

365-
chargeType := INSTANCE_CHARGE_TYPE_POSTPAID
408+
chargeType, ok := d.Get("instance_charge_type").(string)
409+
if !ok || chargeType == "" {
410+
chargeType = INSTANCE_CHARGE_TYPE_POSTPAID
411+
}
412+
413+
if chargeType == INSTANCE_CHARGE_TYPE_SPOTPAID {
414+
spotMaxPrice := d.Get("spot_max_price").(string)
415+
spotInstanceType := d.Get("spot_instance_type").(string)
416+
request.InstanceMarketOptions = &as.InstanceMarketOptionsRequest{
417+
MarketType: helper.String("spot"),
418+
SpotOptions: &as.SpotMarketOptions{
419+
MaxPrice: &spotMaxPrice,
420+
SpotInstanceType: &spotInstanceType,
421+
},
422+
}
423+
}
424+
425+
if chargeType == INSTANCE_CHARGE_TYPE_PREPAID {
426+
period := d.Get("instance_charge_type_prepaid_period").(int)
427+
renewFlag := d.Get("instance_charge_type_prepaid_renew_flag").(string)
428+
request.InstanceChargePrepaid = &as.InstanceChargePrepaid{
429+
Period: helper.IntInt64(period),
430+
RenewFlag: &renewFlag,
431+
}
432+
}
433+
366434
request.InstanceChargeType = &chargeType
367435

368436
if v, ok := d.GetOk("instance_types_check_policy"); ok {
@@ -480,6 +548,19 @@ func resourceTencentCloudAsScalingConfigRead(d *schema.ResourceData, meta interf
480548
if config.SystemDisk.DiskType != nil {
481549
_ = d.Set("system_disk_type", *config.SystemDisk.DiskType)
482550
}
551+
552+
if _, ok := d.GetOk("instance_charge_type"); ok || *config.InstanceChargeType != INSTANCE_CHARGE_TYPE_POSTPAID {
553+
_ = d.Set("instance_charge_type", *config.InstanceChargeType)
554+
}
555+
556+
if config.InstanceMarketOptions != nil && config.InstanceMarketOptions.SpotOptions != nil {
557+
_ = d.Set("spot_instance_type", config.InstanceMarketOptions.SpotOptions.SpotInstanceType)
558+
_ = d.Set("spot_max_price", config.InstanceMarketOptions.SpotOptions.MaxPrice)
559+
}
560+
561+
if config.InstanceChargePrepaid != nil {
562+
_ = d.Set("instance_charge_type_prepaid_renew_flag", config.InstanceChargePrepaid.RenewFlag)
563+
}
483564
return nil
484565
})
485566
if err != nil {
@@ -603,7 +684,32 @@ func resourceTencentCloudAsScalingConfigUpdate(d *schema.ResourceData, meta inte
603684
request.UserData = helper.String(v.(string))
604685
}
605686

606-
chargeType := INSTANCE_CHARGE_TYPE_POSTPAID
687+
chargeType, ok := d.Get("instance_charge_type").(string)
688+
if !ok || chargeType == "" {
689+
chargeType = INSTANCE_CHARGE_TYPE_POSTPAID
690+
}
691+
692+
if chargeType == INSTANCE_CHARGE_TYPE_SPOTPAID {
693+
spotMaxPrice := d.Get("spot_max_price").(string)
694+
spotInstanceType := d.Get("spot_instance_type").(string)
695+
request.InstanceMarketOptions = &as.InstanceMarketOptionsRequest{
696+
MarketType: helper.String("spot"),
697+
SpotOptions: &as.SpotMarketOptions{
698+
MaxPrice: &spotMaxPrice,
699+
SpotInstanceType: &spotInstanceType,
700+
},
701+
}
702+
}
703+
704+
if chargeType == INSTANCE_CHARGE_TYPE_PREPAID {
705+
period := d.Get("instance_charge_type_prepaid_period").(int)
706+
renewFlag := d.Get("instance_charge_type_prepaid_renew_flag").(string)
707+
request.InstanceChargePrepaid = &as.InstanceChargePrepaid{
708+
Period: helper.IntInt64(period),
709+
RenewFlag: &renewFlag,
710+
}
711+
}
712+
607713
request.InstanceChargeType = &chargeType
608714

609715
if v, ok := d.GetOk("instance_types_check_policy"); ok {

tencentcloud/resource_tc_as_scaling_config_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,42 @@ func TestAccTencentCloudAsScalingConfig_full(t *testing.T) {
9292
})
9393
}
9494

95+
func TestAccTencentCloudAsScalingConfig_charge(t *testing.T) {
96+
t.Parallel()
97+
resource.Test(t, resource.TestCase{
98+
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_PREPAY) },
99+
Providers: testAccProviders,
100+
CheckDestroy: testAccCheckAsScalingConfigDestroy,
101+
Steps: []resource.TestStep{
102+
{
103+
Config: testAccAsScalingConfig_charge(),
104+
Check: resource.ComposeAggregateTestCheckFunc(
105+
testAccCheckAsScalingConfigExists("tencentcloud_as_scaling_config.launch_configuration"),
106+
resource.TestCheckResourceAttr("tencentcloud_as_scaling_config.launch_configuration", "instance_charge_type", "POSTPAID_BY_HOUR"),
107+
),
108+
},
109+
{
110+
Config: testAccAsScalingConfig_charge_spot(),
111+
Check: resource.ComposeAggregateTestCheckFunc(
112+
testAccCheckAsScalingConfigExists("tencentcloud_as_scaling_config.launch_configuration"),
113+
resource.TestCheckResourceAttr("tencentcloud_as_scaling_config.launch_configuration", "instance_charge_type", "SPOTPAID"),
114+
resource.TestCheckResourceAttr("tencentcloud_as_scaling_config.launch_configuration", "spot_instance_type", "one-time"),
115+
resource.TestCheckResourceAttr("tencentcloud_as_scaling_config.launch_configuration", "spot_max_price", "1000"),
116+
),
117+
},
118+
{
119+
Config: testAccAsScalingConfig_charge_perpaid(),
120+
Check: resource.ComposeAggregateTestCheckFunc(
121+
testAccCheckAsScalingConfigExists("tencentcloud_as_scaling_config.launch_configuration"),
122+
resource.TestCheckResourceAttr("tencentcloud_as_scaling_config.launch_configuration", "instance_charge_type", "PREPAID"),
123+
resource.TestCheckResourceAttr("tencentcloud_as_scaling_config.launch_configuration", "instance_charge_type_prepaid_period", "1"),
124+
resource.TestCheckResourceAttr("tencentcloud_as_scaling_config.launch_configuration", "instance_charge_type_prepaid_renew_flag", "NOTIFY_AND_MANUAL_RENEW"),
125+
),
126+
},
127+
},
128+
})
129+
}
130+
95131
func testAccCheckAsScalingConfigExists(n string) resource.TestCheckFunc {
96132
return func(s *terraform.State) error {
97133
logId := getLogId(contextNil)
@@ -211,3 +247,40 @@ resource "tencentcloud_as_scaling_config" "launch_configuration" {
211247
}
212248
`
213249
}
250+
251+
func testAccAsScalingConfig_charge() string {
252+
return `
253+
resource "tencentcloud_as_scaling_config" "launch_configuration" {
254+
configuration_name = "tf-as-basic"
255+
image_id = "img-2lr9q49h"
256+
instance_types = ["SA1.SMALL1"]
257+
instance_charge_type = "POSTPAID_BY_HOUR"
258+
}
259+
`
260+
}
261+
262+
func testAccAsScalingConfig_charge_spot() string {
263+
return `
264+
resource "tencentcloud_as_scaling_config" "launch_configuration" {
265+
configuration_name = "tf-as-basic"
266+
image_id = "img-2lr9q49h"
267+
instance_types = ["SA1.SMALL1"]
268+
instance_charge_type = "SPOTPAID"
269+
spot_instance_type = "one-time"
270+
spot_max_price = "1000"
271+
}
272+
`
273+
}
274+
275+
func testAccAsScalingConfig_charge_perpaid() string {
276+
return `
277+
resource "tencentcloud_as_scaling_config" "launch_configuration" {
278+
configuration_name = "tf-as-basic"
279+
image_id = "img-2lr9q49h"
280+
instance_types = ["SA1.SMALL1"]
281+
instance_charge_type = "PREPAID"
282+
instance_charge_type_prepaid_period = 1
283+
instance_charge_type_prepaid_renew_flag = "NOTIFY_AND_MANUAL_RENEW"
284+
}
285+
`
286+
}

website/docs/r/as_scaling_config.html.markdown

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ resource "tencentcloud_as_scaling_config" "launch_configuration" {
4141
}
4242
```
4343

44+
Using SPOT charge type
45+
46+
```hcl
47+
resource "tencentcloud_as_scaling_config" "launch_configuration" {
48+
configuration_name = "launch-configuration"
49+
image_id = "img-9qabwvbn"
50+
instance_types = ["SA1.SMALL1"]
51+
instance_charge_type = "SPOTPAID"
52+
spot_instance_type = "one-time"
53+
spot_max_price = "1000"
54+
}
55+
```
56+
4457
## Argument Reference
4558

4659
The following arguments are supported:
@@ -53,6 +66,9 @@ The following arguments are supported:
5366
* `disk_type_policy` - (Optional) Policy of cloud disk type. Valid values: `ORIGINAL` and `AUTOMATIC`. Default is `ORIGINAL`.
5467
* `enhanced_monitor_service` - (Optional) To specify whether to enable cloud monitor service. Default is `TRUE`.
5568
* `enhanced_security_service` - (Optional) To specify whether to enable cloud security service. Default is `TRUE`.
69+
* `instance_charge_type_prepaid_period` - (Optional) The tenancy (in month) of the prepaid instance, NOTE: it only works when instance_charge_type is set to `PREPAID`. Valid values are `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `11`, `12`, `24`, `36`.
70+
* `instance_charge_type_prepaid_renew_flag` - (Optional) Auto renewal flag. Valid values: `NOTIFY_AND_AUTO_RENEW`: notify upon expiration and renew automatically, `NOTIFY_AND_MANUAL_RENEW`: notify upon expiration but do not renew automatically, `DISABLE_NOTIFY_AND_MANUAL_RENEW`: neither notify upon expiration nor renew automatically. Default value: `NOTIFY_AND_MANUAL_RENEW`. If this parameter is specified as `NOTIFY_AND_AUTO_RENEW`, the instance will be automatically renewed on a monthly basis if the account balance is sufficient. NOTE: it only works when instance_charge_type is set to `PREPAID`.
71+
* `instance_charge_type` - (Optional) Charge type of instance. Valid values are `PREPAID`, `POSTPAID_BY_HOUR`, `SPOTPAID`. The default is `POSTPAID_BY_HOUR`. NOTE: `SPOTPAID` instance must set `spot_instance_type` and `spot_max_price` at the same time.
5672
* `instance_name_settings` - (Optional) Settings of CVM instance names.
5773
* `instance_tags` - (Optional) A list of tags used to associate different resources.
5874
* `internet_charge_type` - (Optional) Charge types for network traffic. Valid values: `BANDWIDTH_PREPAID`, `TRAFFIC_POSTPAID_BY_HOUR`, `TRAFFIC_POSTPAID_BY_HOUR` and `BANDWIDTH_PACKAGE`.
@@ -63,6 +79,8 @@ The following arguments are supported:
6379
* `project_id` - (Optional) Specifys to which project the configuration belongs.
6480
* `public_ip_assigned` - (Optional) Specify whether to assign an Internet IP address.
6581
* `security_group_ids` - (Optional) Security groups to which a CVM instance belongs.
82+
* `spot_instance_type` - (Optional) Type of spot instance, only support `one-time` now. Note: it only works when instance_charge_type is set to `SPOTPAID`.
83+
* `spot_max_price` - (Optional) 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`.
6684
* `system_disk_size` - (Optional) Volume of system disk in GB. Default is `50`.
6785
* `system_disk_type` - (Optional) Type of a CVM disk. Valid values: `CLOUD_PREMIUM` and `CLOUD_SSD`. Default is `CLOUD_PREMIUM`. valid when disk_type_policy is ORIGINAL.
6886
* `user_data` - (Optional) ase64-encoded User Data text, the length limit is 16KB.

0 commit comments

Comments
 (0)