Skip to content

Commit 8db01a6

Browse files
authored
fix/eip (#1990)
* fix/eip * fix/eip * add
1 parent 7b0ee55 commit 8db01a6

File tree

4 files changed

+249
-50
lines changed

4 files changed

+249
-50
lines changed

.changelog/1990.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_association: Fix the issue of binding EIP timeout.
3+
```

tencentcloud/resource_tc_eip_association.go

Lines changed: 152 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,104 @@ Provides an eip resource associated with other resource like CVM, ENI and CLB.
55
66
Example Usage
77
8+
Bind elastic public IP By Instance ID
9+
810
```hcl
9-
resource "tencentcloud_eip_association" "foo" {
10-
eip_id = "eip-xxxxxx"
11-
instance_id = "ins-xxxxxx"
11+
data "tencentcloud_availability_zones" "zones" {}
12+
13+
data "tencentcloud_images" "image" {
14+
image_type = ["PUBLIC_IMAGE"]
15+
image_name_regex = "Final"
16+
}
17+
18+
data "tencentcloud_instance_types" "instance_types" {
19+
filter {
20+
name = "zone"
21+
values = [data.tencentcloud_availability_zones.zones.zones.0.name]
22+
}
23+
24+
filter {
25+
name = "instance-family"
26+
values = ["S5"]
27+
}
28+
29+
cpu_core_count = 2
30+
exclude_sold_out = true
31+
}
32+
33+
resource "tencentcloud_vpc" "vpc" {
34+
name = "example-vpc"
35+
cidr_block = "10.0.0.0/16"
36+
}
37+
38+
resource "tencentcloud_subnet" "subnet" {
39+
availability_zone = data.tencentcloud_availability_zones.zones.zones.0.name
40+
name = "example-vpc"
41+
vpc_id = tencentcloud_vpc.vpc.id
42+
cidr_block = "10.0.0.0/16"
43+
is_multicast = false
44+
}
45+
46+
resource "tencentcloud_eip" "eip" {
47+
name = "example-eip"
48+
internet_charge_type = "TRAFFIC_POSTPAID_BY_HOUR"
49+
type = "EIP"
50+
}
51+
52+
resource "tencentcloud_instance" "example" {
53+
instance_name = "example-cvm"
54+
availability_zone = data.tencentcloud_availability_zones.zones.zones.0.name
55+
image_id = data.tencentcloud_images.image.images.0.image_id
56+
instance_type = data.tencentcloud_instance_types.instance_types.instance_types.0.instance_type
57+
system_disk_type = "CLOUD_PREMIUM"
58+
disable_security_service = true
59+
disable_monitor_service = true
60+
vpc_id = tencentcloud_vpc.vpc.id
61+
subnet_id = tencentcloud_subnet.subnet.id
62+
}
63+
64+
resource "tencentcloud_eip_association" "example" {
65+
eip_id = tencentcloud_eip.eip.id
66+
instance_id = tencentcloud_instance.example.id
1267
}
1368
```
1469
15-
or
70+
Bind elastic public IP By elastic network card
1671
1772
```hcl
18-
resource "tencentcloud_eip_association" "bar" {
19-
eip_id = "eip-xxxxxx"
20-
network_interface_id = "eni-xxxxxx"
21-
private_ip = "10.0.1.22"
73+
data "tencentcloud_availability_zones" "zones" {}
74+
75+
resource "tencentcloud_vpc" "vpc" {
76+
name = "example-vpc"
77+
cidr_block = "10.0.0.0/16"
78+
}
79+
80+
resource "tencentcloud_subnet" "subnet" {
81+
availability_zone = data.tencentcloud_availability_zones.zones.zones.0.name
82+
name = "example-vpc"
83+
vpc_id = tencentcloud_vpc.vpc.id
84+
cidr_block = "10.0.0.0/16"
85+
is_multicast = false
86+
}
87+
88+
resource "tencentcloud_eni" "eni" {
89+
name = "example-eni"
90+
vpc_id = tencentcloud_vpc.vpc.id
91+
subnet_id = tencentcloud_subnet.subnet.id
92+
description = "eni desc"
93+
ipv4_count = 1
94+
}
95+
96+
resource "tencentcloud_eip" "eip" {
97+
name = "example-eip"
98+
internet_charge_type = "TRAFFIC_POSTPAID_BY_HOUR"
99+
type = "EIP"
100+
}
101+
102+
resource "tencentcloud_eip_association" "example" {
103+
eip_id = tencentcloud_eip.eip.id
104+
network_interface_id = tencentcloud_eni.eni.id
105+
private_ip = tencentcloud_eni.eni.ipv4_info[0].ip
22106
}
23107
```
24108
@@ -52,7 +136,6 @@ func resourceTencentCloudEipAssociation() *schema.Resource {
52136
Importer: &schema.ResourceImporter{
53137
State: schema.ImportStatePassthrough,
54138
},
55-
56139
Schema: map[string]*schema.Schema{
57140
"eip_id": {
58141
Type: schema.TypeString,
@@ -101,63 +184,75 @@ func resourceTencentCloudEipAssociation() *schema.Resource {
101184

102185
func resourceTencentCloudEipAssociationCreate(d *schema.ResourceData, meta interface{}) error {
103186
defer logElapsed("resource.tencentcloud_eip_association.create")()
104-
logId := getLogId(contextNil)
105-
ctx := context.WithValue(context.TODO(), logIdKey, logId)
106-
vpcService := VpcService{
107-
client: meta.(*TencentCloudClient).apiV3Conn,
108-
}
187+
188+
var (
189+
logId = getLogId(contextNil)
190+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
191+
vpcService = VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
192+
eip *vpc.Address
193+
errRet error
194+
)
109195

110196
eipId := d.Get("eip_id").(string)
111-
var eip *vpc.Address
112-
var errRet error
113197
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
114198
eip, errRet = vpcService.DescribeEipById(ctx, eipId)
115199
if errRet != nil {
116200
return retryError(errRet, InternalError)
117201
}
202+
118203
if eip == nil {
119204
return resource.NonRetryableError(fmt.Errorf("eip is not found"))
120205
}
206+
121207
return nil
122208
})
209+
123210
if err != nil {
124211
return err
125212
}
213+
126214
if *eip.AddressStatus != EIP_STATUS_UNBIND {
127215
return fmt.Errorf("eip status is illegal %s", *eip.AddressStatus)
128216
}
129217

130218
if v, ok := d.GetOk("instance_id"); ok {
131219
instanceId := v.(string)
132220
err = resource.Retry(writeRetryTimeout, func() *resource.RetryError {
133-
err := vpcService.AttachEip(ctx, eipId, instanceId)
134-
if err != nil {
135-
return retryError(err)
221+
e := vpcService.AttachEip(ctx, eipId, instanceId)
222+
if e != nil {
223+
return retryError(e)
136224
}
225+
137226
return nil
138227
})
228+
139229
if err != nil {
140230
return err
141231
}
142-
associationId := fmt.Sprintf("%v::%v", eipId, instanceId)
143-
d.SetId(associationId)
144232

233+
associationId := fmt.Sprintf("%v::%v", eipId, instanceId)
145234
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
146235
eip, errRet = vpcService.DescribeEipById(ctx, eipId)
147236
if errRet != nil {
148237
return retryError(errRet)
149238
}
239+
150240
if eip == nil {
151241
return resource.NonRetryableError(fmt.Errorf("eip is not found"))
152242
}
243+
153244
if *eip.AddressStatus == EIP_STATUS_BIND {
154245
return nil
155246
}
247+
156248
return resource.RetryableError(fmt.Errorf("wait for binding success: %s", *eip.AddressStatus))
157249
})
250+
158251
if err != nil {
159252
return err
160253
}
254+
255+
d.SetId(associationId)
161256
return resourceTencentCloudEipAssociationRead(d, meta)
162257
}
163258

@@ -171,47 +266,56 @@ func resourceTencentCloudEipAssociationCreate(d *schema.ResourceData, meta inter
171266
networkId = v.(string)
172267
request.NetworkInterfaceId = &networkId
173268
}
269+
174270
if v, ok := d.GetOk("private_ip"); ok {
175271
needRequest = true
176272
privateIp = v.(string)
177273
request.PrivateIpAddress = &privateIp
178274
}
275+
179276
if needRequest {
180277
err = resource.Retry(writeRetryTimeout, func() *resource.RetryError {
181278
ratelimit.Check(request.GetAction())
182-
response, err := meta.(*TencentCloudClient).apiV3Conn.UseVpcClient().AssociateAddress(request)
183-
if err != nil {
279+
response, e := meta.(*TencentCloudClient).apiV3Conn.UseVpcClient().AssociateAddress(request)
280+
if e != nil {
184281
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
185-
logId, request.GetAction(), request.ToJsonString(), err.Error())
186-
return retryError(err)
282+
logId, request.GetAction(), request.ToJsonString(), e.Error())
283+
return retryError(e)
187284
}
285+
188286
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
189287
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
190288
return nil
191289
})
290+
192291
if err != nil {
193292
return err
194293
}
294+
195295
id := fmt.Sprintf("%v::%v::%v", eipId, networkId, privateIp)
196-
d.SetId(id)
197296

198297
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
199298
eip, errRet = vpcService.DescribeEipById(ctx, eipId)
200299
if errRet != nil {
201300
return retryError(errRet)
202301
}
302+
203303
if eip == nil {
204304
return resource.NonRetryableError(fmt.Errorf("eip is not found"))
205305
}
206-
if *eip.AddressStatus == EIP_STATUS_BIND_ENI {
306+
307+
if *eip.AddressStatus == EIP_STATUS_BIND_ENI || *eip.AddressStatus == EIP_STATUS_BIND {
207308
return nil
208309
}
310+
209311
return resource.RetryableError(fmt.Errorf("wait for binding success: %s", *eip.AddressStatus))
210312
})
313+
211314
if err != nil {
212315
return err
213316
}
214317

318+
d.SetId(id)
215319
return resourceTencentCloudEipAssociationRead(d, meta)
216320
}
217321

@@ -222,13 +326,13 @@ func resourceTencentCloudEipAssociationRead(d *schema.ResourceData, meta interfa
222326
defer logElapsed("resource.tencentcloud_eip_association.read")()
223327
defer inconsistentCheck(d, meta)()
224328

225-
logId := getLogId(contextNil)
226-
ctx := context.WithValue(context.TODO(), logIdKey, logId)
227-
vpcService := VpcService{
228-
client: meta.(*TencentCloudClient).apiV3Conn,
229-
}
329+
var (
330+
logId = getLogId(contextNil)
331+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
332+
vpcService = VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
333+
id = d.Id()
334+
)
230335

231-
id := d.Id()
232336
association, err := parseEipAssociationId(id)
233337
if err != nil {
234338
return err
@@ -239,11 +343,14 @@ func resourceTencentCloudEipAssociationRead(d *schema.ResourceData, meta interfa
239343
if errRet != nil {
240344
return retryError(errRet)
241345
}
346+
242347
if eip == nil {
243348
d.SetId("")
244349
}
350+
245351
return nil
246352
})
353+
247354
if err != nil {
248355
return err
249356
}
@@ -262,25 +369,28 @@ func resourceTencentCloudEipAssociationRead(d *schema.ResourceData, meta interfa
262369

263370
func resourceTencentCloudEipAssociationDelete(d *schema.ResourceData, meta interface{}) error {
264371
defer logElapsed("resource.tencentcloud_eip_association.delete")()
265-
logId := getLogId(contextNil)
266-
ctx := context.WithValue(context.TODO(), logIdKey, logId)
267-
vpcService := VpcService{
268-
client: meta.(*TencentCloudClient).apiV3Conn,
269-
}
270372

271-
id := d.Id()
373+
var (
374+
logId = getLogId(contextNil)
375+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
376+
vpcService = VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
377+
id = d.Id()
378+
)
379+
272380
association, err := parseEipAssociationId(id)
273381
if err != nil {
274382
return err
275383
}
276384

277385
err = resource.Retry(writeRetryTimeout, func() *resource.RetryError {
278-
err := vpcService.UnattachEip(ctx, association.EipId)
279-
if err != nil {
280-
return retryError(err, "DesOperation.MutexTaskRunning")
386+
e := vpcService.UnattachEip(ctx, association.EipId)
387+
if e != nil {
388+
return retryError(e, "DesOperation.MutexTaskRunning")
281389
}
390+
282391
return nil
283392
})
393+
284394
if err != nil {
285395
return err
286396
}

tencentcloud/resource_tc_eip_association_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1010
)
1111

12+
// go test -i; go test -test.run TestAccTencentCloudEipAssociationWithInstance -v
1213
func TestAccTencentCloudEipAssociationWithInstance(t *testing.T) {
1314
t.Parallel()
1415
id := "tencentcloud_eip_association.foo"
@@ -40,6 +41,7 @@ func TestAccTencentCloudEipAssociationWithInstance(t *testing.T) {
4041
})
4142
}
4243

44+
// go test -i; go test -test.run TestAccTencentCloudEipAssociationWithNetworkInterface -v
4345
func TestAccTencentCloudEipAssociationWithNetworkInterface(t *testing.T) {
4446
t.Parallel()
4547
id := "tencentcloud_eip_association.foo"

0 commit comments

Comments
 (0)