Skip to content

Commit 6b172df

Browse files
authored
Merge pull request #1563 from tencentcloudstack/feat/support_create_prepaid_eip
support create prepaid eip
2 parents 74ba2d9 + 6ce4c94 commit 6b172df

File tree

6 files changed

+146
-6
lines changed

6 files changed

+146
-6
lines changed

.changelog/1563.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_eip: support create prepaid eip.
3+
```

tencentcloud/extension_vpc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ var EIP_ANYCAST_ZONE = []string{
8181
EIP_ANYCAST_ZONE_OVERSEAS,
8282
}
8383

84+
var EIP_AVAILABLE_PERIOD = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 24, 36}
85+
8486
// ENI
8587
const (
8688
ENI_DESCRIBE_LIMIT = 100

tencentcloud/resource_tc_eip.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,25 @@ func resourceTencentCloudEip() *schema.Resource {
8484
ForceNew: true,
8585
Description: "The charge type of eip. Valid values: `BANDWIDTH_PACKAGE`, `BANDWIDTH_POSTPAID_BY_HOUR`, `BANDWIDTH_PREPAID_BY_MONTH` and `TRAFFIC_POSTPAID_BY_HOUR`.",
8686
},
87+
88+
"prepaid_period": {
89+
Type: schema.TypeInt,
90+
Optional: true,
91+
ValidateFunc: validateAllowedIntValue(EIP_AVAILABLE_PERIOD),
92+
Description: "Period of instance. Default value: `1`. Valid value: `1`, `2`, `3`, `4`, `6`, `7`, `8`, `9`, `12`, `24`, `36`. NOTES: must set when `internet_charge_type` is `BANDWIDTH_PREPAID_BY_MONTH`.",
93+
},
94+
95+
"auto_renew_flag": {
96+
Type: schema.TypeInt,
97+
Optional: true,
98+
ValidateFunc: validateAllowedIntValue([]int{0, 1, 2}),
99+
Description: "Auto renew flag. 0 - default state (manual renew); 1 - automatic renew; 2 - explicit no automatic renew. NOTES: Only supported prepaid EIP.",
100+
},
101+
87102
"internet_max_bandwidth_out": {
88103
Type: schema.TypeInt,
89104
Optional: true,
105+
Computed: true,
90106
Description: "The bandwidth limit of EIP, unit is Mbps.",
91107
},
92108
"tags": {
@@ -97,6 +113,7 @@ func resourceTencentCloudEip() *schema.Resource {
97113
"bandwidth_package_id": {
98114
Type: schema.TypeString,
99115
Optional: true,
116+
Computed: true,
100117
Description: "ID of bandwidth package, it will set when `internet_charge_type` is `BANDWIDTH_PACKAGE`.",
101118
},
102119
// computed
@@ -125,6 +142,8 @@ func resourceTencentCloudEipCreate(d *schema.ResourceData, meta interface{}) err
125142
tagService := TagService{client: client}
126143
region := client.Region
127144

145+
var internetChargeType string
146+
128147
request := vpc.NewAllocateAddressesRequest()
129148
if v, ok := d.GetOk("type"); ok {
130149
request.AddressType = helper.String(v.(string))
@@ -136,11 +155,22 @@ func resourceTencentCloudEipCreate(d *schema.ResourceData, meta interface{}) err
136155
request.InternetServiceProvider = helper.String(v.(string))
137156
}
138157
if v, ok := d.GetOk("internet_charge_type"); ok {
158+
internetChargeType = v.(string)
139159
request.InternetChargeType = helper.String(v.(string))
140160
}
141161
if v, ok := d.GetOk("internet_max_bandwidth_out"); ok {
142162
request.InternetMaxBandwidthOut = helper.IntInt64(v.(int))
143163
}
164+
165+
if internetChargeType == "BANDWIDTH_PREPAID_BY_MONTH" {
166+
addressChargePrepaid := vpc.AddressChargePrepaid{}
167+
period := d.Get("prepaid_period")
168+
renewFlag := d.Get("auto_renew_flag")
169+
addressChargePrepaid.Period = helper.IntInt64(period.(int))
170+
addressChargePrepaid.AutoRenewFlag = helper.IntInt64(renewFlag.(int))
171+
request.AddressChargePrepaid = &addressChargePrepaid
172+
}
173+
144174
if v := helper.GetTags(d, "tags"); len(v) > 0 {
145175
for tagKey, tagValue := range v {
146176
tag := vpc.Tag{
@@ -264,6 +294,11 @@ func resourceTencentCloudEipRead(d *schema.ResourceData, meta interface{}) error
264294
_ = d.Set("status", eip.AddressStatus)
265295
_ = d.Set("internet_charge_type", eip.InternetChargeType)
266296
_ = d.Set("tags", tags)
297+
298+
if eip.Bandwidth != nil {
299+
_ = d.Set("internet_max_bandwidth_out", eip.Bandwidth)
300+
}
301+
267302
if bgp != nil {
268303
_ = d.Set("bandwidth_package_id", bgp.BandwidthPackageId)
269304
}
@@ -315,6 +350,15 @@ func resourceTencentCloudEipUpdate(d *schema.ResourceData, meta interface{}) err
315350
}
316351
}
317352

353+
if d.HasChange("prepaid_period") || d.HasChange("auto_renew_flag") {
354+
period := d.Get("prepaid_period").(int)
355+
renewFlag := d.Get("auto_renew_flag").(int)
356+
err := vpcService.RenewAddress(ctx, eipId, period, renewFlag)
357+
if err != nil {
358+
return err
359+
}
360+
}
361+
318362
if d.HasChange("tags") {
319363
oldTags, newTags := d.GetChange("tags")
320364
replaceTags, deleteTags := diffTags(oldTags.(map[string]interface{}), newTags.(map[string]interface{}))
@@ -363,6 +407,40 @@ func resourceTencentCloudEipDelete(d *schema.ResourceData, meta interface{}) err
363407
return err
364408
}
365409

410+
var internetChargeType string
411+
if v, ok := d.GetOk("internet_charge_type"); ok {
412+
internetChargeType = v.(string)
413+
}
414+
415+
if internetChargeType == "BANDWIDTH_PREPAID_BY_MONTH" {
416+
// isolated
417+
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
418+
eip, errRet := vpcService.DescribeEipById(ctx, eipId)
419+
if errRet != nil {
420+
return retryError(errRet)
421+
}
422+
if !*eip.IsArrears {
423+
return resource.RetryableError(fmt.Errorf("eip is still isolate"))
424+
}
425+
return nil
426+
})
427+
if err != nil {
428+
return err
429+
}
430+
431+
// release
432+
err = resource.Retry(writeRetryTimeout, func() *resource.RetryError {
433+
errRet := vpcService.DeleteEip(ctx, eipId)
434+
if errRet != nil {
435+
return retryError(errRet, "DesOperation.MutexTaskRunning")
436+
}
437+
return nil
438+
})
439+
if err != nil {
440+
return err
441+
}
442+
}
443+
366444
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
367445
eip, errRet := vpcService.DescribeEipById(ctx, eipId)
368446
if errRet != nil {

tencentcloud/resource_tc_eip_test.go

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func testSweepEipInstance(region string) error {
6666
return nil
6767
}
6868

69-
func TestAccTencentCloudEip_basic(t *testing.T) {
69+
func TestAccTencentCloudEipResource_basic(t *testing.T) {
7070
t.Parallel()
7171
resource.Test(t, resource.TestCase{
7272
PreCheck: func() { testAccPreCheck(t) },
@@ -127,7 +127,7 @@ func TestAccTencentCloudEip_basic(t *testing.T) {
127127
})
128128
}
129129

130-
func TestAccTencentCloudEip_anycast(t *testing.T) {
130+
func TestAccTencentCloudEipResource_anycast(t *testing.T) {
131131
defer func() {
132132
os.Setenv(PROVIDER_REGION, "")
133133
}()
@@ -149,7 +149,7 @@ func TestAccTencentCloudEip_anycast(t *testing.T) {
149149
})
150150
}
151151

152-
func TestAccTencentCloudEip_provider(t *testing.T) {
152+
func TestAccTencentCloudEipResource_provider(t *testing.T) {
153153
t.Parallel()
154154
resource.Test(t, resource.TestCase{
155155
PreCheck: func() { testAccPreCheck(t) },
@@ -168,7 +168,7 @@ func TestAccTencentCloudEip_provider(t *testing.T) {
168168
})
169169
}
170170

171-
func TestAccTencentCloudEip_bandwidth(t *testing.T) {
171+
func TestAccTencentCloudEipResource_bandwidth(t *testing.T) {
172172
t.Parallel()
173173
resource.Test(t, resource.TestCase{
174174
PreCheck: func() { testAccPreCheck(t) },
@@ -187,7 +187,7 @@ func TestAccTencentCloudEip_bandwidth(t *testing.T) {
187187
})
188188
}
189189

190-
func TestAccTencentCloudEip_chargetype(t *testing.T) {
190+
func TestAccTencentCloudEipResource_chargetype(t *testing.T) {
191191
t.Parallel()
192192
resource.Test(t, resource.TestCase{
193193
PreCheck: func() { testAccPreCheck(t) },
@@ -210,6 +210,30 @@ func TestAccTencentCloudEip_chargetype(t *testing.T) {
210210
})
211211
}
212212

213+
func TestAccTencentCloudEipResource_prepaid(t *testing.T) {
214+
t.Parallel()
215+
resource.Test(t, resource.TestCase{
216+
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_PREPAY) },
217+
Providers: testAccProviders,
218+
CheckDestroy: testAccCheckEipDestroy,
219+
Steps: []resource.TestStep{
220+
{
221+
Config: testAccEipPrepaid,
222+
Check: resource.ComposeTestCheckFunc(
223+
testAccCheckEipExists("tencentcloud_eip.foo"),
224+
resource.TestCheckResourceAttr("tencentcloud_eip.foo", "internet_charge_type", "BANDWIDTH_PREPAID_BY_MONTH"),
225+
),
226+
},
227+
{
228+
ResourceName: "tencentcloud_eip.foo",
229+
ImportState: true,
230+
ImportStateVerify: true,
231+
ImportStateVerifyIgnore: []string{"prepaid_period", "auto_renew_flag"},
232+
},
233+
},
234+
})
235+
}
236+
213237
func testAccCheckEipExists(n string) resource.TestCheckFunc {
214238
return func(s *terraform.State) error {
215239
logId := getLogId(contextNil)
@@ -324,7 +348,7 @@ resource "tencentcloud_eip" "foo" {
324348
const testAccEipProvider = `
325349
resource "tencentcloud_eip" "foo" {
326350
name = "eip_provider"
327-
internet_service_provider = "CUCC"
351+
internet_service_provider = "CMCC"
328352
}
329353
`
330354

@@ -342,3 +366,13 @@ resource "tencentcloud_eip" "foo" {
342366
internet_charge_type = "TRAFFIC_POSTPAID_BY_HOUR"
343367
}
344368
`
369+
370+
const testAccEipPrepaid = `
371+
resource "tencentcloud_eip" "foo" {
372+
name = "eip_prepaid"
373+
internet_charge_type = "BANDWIDTH_PREPAID_BY_MONTH"
374+
prepaid_period = 6
375+
auto_renew_flag = 1
376+
internet_max_bandwidth_out = 2
377+
}
378+
`

tencentcloud/service_tencentcloud_vpc.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2325,6 +2325,27 @@ func (me *VpcService) ModifyEipBandwidthOut(ctx context.Context, eipId string, b
23252325
return nil
23262326
}
23272327

2328+
func (me *VpcService) RenewAddress(ctx context.Context, eipId string, period int, renewFlag int) error {
2329+
logId := getLogId(ctx)
2330+
request := vpc.NewRenewAddressesRequest()
2331+
request.AddressIds = []*string{&eipId}
2332+
addressChargePrepaid := vpc.AddressChargePrepaid{}
2333+
addressChargePrepaid.AutoRenewFlag = helper.IntInt64(renewFlag)
2334+
addressChargePrepaid.Period = helper.IntInt64(period)
2335+
request.AddressChargePrepaid = &addressChargePrepaid
2336+
ratelimit.Check(request.GetAction())
2337+
response, err := me.client.UseVpcClient().RenewAddresses(request)
2338+
if err != nil {
2339+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
2340+
logId, request.GetAction(), request.ToJsonString(), err.Error())
2341+
return err
2342+
}
2343+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
2344+
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
2345+
2346+
return nil
2347+
}
2348+
23282349
func (me *VpcService) DeleteEip(ctx context.Context, eipId string) error {
23292350
logId := getLogId(ctx)
23302351
request := vpc.NewReleaseAddressesRequest()

website/docs/r/eip.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ The following arguments are supported:
2828

2929
* `anycast_zone` - (Optional, String, ForceNew) The zone of anycast. Valid value: `ANYCAST_ZONE_GLOBAL` and `ANYCAST_ZONE_OVERSEAS`.
3030
* `applicable_for_clb` - (Optional, Bool, **Deprecated**) It has been deprecated from version 1.27.0. Indicates whether the anycast eip can be associated to a CLB.
31+
* `auto_renew_flag` - (Optional, Int) Auto renew flag. 0 - default state (manual renew); 1 - automatic renew; 2 - explicit no automatic renew. NOTES: Only supported prepaid EIP.
3132
* `bandwidth_package_id` - (Optional, String) ID of bandwidth package, it will set when `internet_charge_type` is `BANDWIDTH_PACKAGE`.
3233
* `internet_charge_type` - (Optional, String, ForceNew) The charge type of eip. Valid values: `BANDWIDTH_PACKAGE`, `BANDWIDTH_POSTPAID_BY_HOUR`, `BANDWIDTH_PREPAID_BY_MONTH` and `TRAFFIC_POSTPAID_BY_HOUR`.
3334
* `internet_max_bandwidth_out` - (Optional, Int) The bandwidth limit of EIP, unit is Mbps.
3435
* `internet_service_provider` - (Optional, String, ForceNew) Internet service provider of eip. Valid value: `BGP`, `CMCC`, `CTCC` and `CUCC`.
3536
* `name` - (Optional, String) The name of eip.
37+
* `prepaid_period` - (Optional, Int) Period of instance. Default value: `1`. Valid value: `1`, `2`, `3`, `4`, `6`, `7`, `8`, `9`, `12`, `24`, `36`. NOTES: must set when `internet_charge_type` is `BANDWIDTH_PREPAID_BY_MONTH`.
3638
* `tags` - (Optional, Map) The tags of eip.
3739
* `type` - (Optional, String, ForceNew) The type of eip. Valid value: `EIP` and `AnycastEIP` and `HighQualityEIP`. Default is `EIP`.
3840

0 commit comments

Comments
 (0)