Skip to content

Commit 7da37fb

Browse files
committed
support create prepaid eip
1 parent 2f62c50 commit 7da37fb

File tree

5 files changed

+136
-5
lines changed

5 files changed

+136
-5
lines changed

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: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@ 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,
@@ -125,6 +140,8 @@ func resourceTencentCloudEipCreate(d *schema.ResourceData, meta interface{}) err
125140
tagService := TagService{client: client}
126141
region := client.Region
127142

143+
var internetChargeType string
144+
128145
request := vpc.NewAllocateAddressesRequest()
129146
if v, ok := d.GetOk("type"); ok {
130147
request.AddressType = helper.String(v.(string))
@@ -136,11 +153,22 @@ func resourceTencentCloudEipCreate(d *schema.ResourceData, meta interface{}) err
136153
request.InternetServiceProvider = helper.String(v.(string))
137154
}
138155
if v, ok := d.GetOk("internet_charge_type"); ok {
156+
internetChargeType = v.(string)
139157
request.InternetChargeType = helper.String(v.(string))
140158
}
141159
if v, ok := d.GetOk("internet_max_bandwidth_out"); ok {
142160
request.InternetMaxBandwidthOut = helper.IntInt64(v.(int))
143161
}
162+
163+
if internetChargeType == "BANDWIDTH_PREPAID_BY_MONTH" {
164+
addressChargePrepaid := vpc.AddressChargePrepaid{}
165+
period := d.Get("prepaid_period")
166+
renewFlag := d.Get("auto_renew_flag")
167+
addressChargePrepaid.Period = helper.IntInt64(period.(int))
168+
addressChargePrepaid.AutoRenewFlag = helper.IntInt64(renewFlag.(int))
169+
request.AddressChargePrepaid = &addressChargePrepaid
170+
}
171+
144172
if v := helper.GetTags(d, "tags"); len(v) > 0 {
145173
for tagKey, tagValue := range v {
146174
tag := vpc.Tag{
@@ -263,6 +291,7 @@ func resourceTencentCloudEipRead(d *schema.ResourceData, meta interface{}) error
263291
_ = d.Set("public_ip", eip.AddressIp)
264292
_ = d.Set("status", eip.AddressStatus)
265293
_ = d.Set("internet_charge_type", eip.InternetChargeType)
294+
_ = d.Set("internet_max_bandwidth_out", eip.Bandwidth)
266295
_ = d.Set("tags", tags)
267296
if bgp != nil {
268297
_ = d.Set("bandwidth_package_id", bgp.BandwidthPackageId)
@@ -315,6 +344,15 @@ func resourceTencentCloudEipUpdate(d *schema.ResourceData, meta interface{}) err
315344
}
316345
}
317346

347+
if d.HasChange("prepaid_period") || d.HasChange("auto_renew_flag") {
348+
period := d.Get("prepaid_period").(int)
349+
renewFlag := d.Get("auto_renew_flag").(int)
350+
err := vpcService.RenewAddress(ctx, eipId, period, renewFlag)
351+
if err != nil {
352+
return err
353+
}
354+
}
355+
318356
if d.HasChange("tags") {
319357
oldTags, newTags := d.GetChange("tags")
320358
replaceTags, deleteTags := diffTags(oldTags.(map[string]interface{}), newTags.(map[string]interface{}))
@@ -363,6 +401,40 @@ func resourceTencentCloudEipDelete(d *schema.ResourceData, meta interface{}) err
363401
return err
364402
}
365403

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

tencentcloud/resource_tc_eip_test.go

Lines changed: 39 additions & 5 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)
@@ -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)