Skip to content

Commit 615b6d8

Browse files
eip: detect eip unbind out of terraform
1 parent f0db0a9 commit 615b6d8

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

tencentcloud/resource_tc_eip_association.go

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ func resourceTencentCloudEipAssociation() *schema.Resource {
6565
Type: schema.TypeString,
6666
ForceNew: true,
6767
Optional: true,
68-
Computed: true,
6968
ConflictsWith: []string{
7069
"network_interface_id",
7170
"private_ip",
@@ -77,7 +76,6 @@ func resourceTencentCloudEipAssociation() *schema.Resource {
7776
Type: schema.TypeString,
7877
ForceNew: true,
7978
Optional: true,
80-
Computed: true,
8179
ValidateFunc: validateStringLengthInRange(1, 25),
8280
ConflictsWith: []string{
8381
"instance_id",
@@ -88,7 +86,6 @@ func resourceTencentCloudEipAssociation() *schema.Resource {
8886
Type: schema.TypeString,
8987
ForceNew: true,
9088
Optional: true,
91-
Computed: true,
9289
ValidateFunc: validateStringLengthInRange(7, 25),
9390
ConflictsWith: []string{
9491
"instance_id",
@@ -239,24 +236,38 @@ func resourceTencentCloudEipAssociationRead(d *schema.ResourceData, meta interfa
239236
if errRet != nil {
240237
return retryError(errRet)
241238
}
242-
if eip == nil {
239+
if eip == nil || *eip.AddressStatus == EIP_STATUS_UNBIND {
243240
d.SetId("")
241+
return nil
242+
}
243+
// there is no easy way to ensure eip been bound to desired resource as all the InstanceId/NetworkInterfaceId/PrivateAddressIp fields been populated once bound
244+
// eg: user want bind eip to cvm, but it's bound to the secondary eni of the same cvm instead, the only way to distinguish is by checking whether the eni is primary or not
245+
d.Set("eip_id", *eip.AddressId)
246+
// associate with instance
247+
if len(association.InstanceId) > 0 {
248+
if eip.InstanceId != nil {
249+
d.Set("instance_id", *eip.InstanceId)
250+
} else {
251+
d.Set("instance_id", "")
252+
}
253+
} else {
254+
if eip.NetworkInterfaceId != nil {
255+
d.Set("network_interface_id", *eip.NetworkInterfaceId)
256+
} else {
257+
d.Set("network_interface_id", "")
258+
}
259+
if eip.PrivateAddressIp != nil {
260+
d.Set("private_ip", *eip.PrivateAddressIp)
261+
} else {
262+
d.Set("private_ip", "")
263+
}
244264
}
245265
return nil
246266
})
247267
if err != nil {
248268
return err
249269
}
250270

251-
_ = d.Set("eip_id", association.EipId)
252-
// associate with instance
253-
if len(association.InstanceId) > 0 {
254-
_ = d.Set("instance_id", association.InstanceId)
255-
return nil
256-
}
257-
258-
_ = d.Set("network_interface_id", association.NetworkInterfaceId)
259-
_ = d.Set("private_ip", association.PrivateIp)
260271
return nil
261272
}
262273

0 commit comments

Comments
 (0)