Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0c4c21b

Browse files
committedJun 5, 2024
add
1 parent dacd39d commit 0c4c21b

11 files changed

+474
-320
lines changed
 

‎tencentcloud/services/cvm/resource_tc_cvm_renew_instance.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func ResourceTencentCloudCvmRenewInstance() *schema.Resource {
4141
"renew_flag": {
4242
Type: schema.TypeString,
4343
Optional: true,
44-
Description: "Auto renewal flag. Valid values:<br><li>NOTIFY_AND_AUTO_RENEW: notify upon expiration and renew automatically<br><li>NOTIFY_AND_MANUAL_RENEW: notify upon expiration but do not renew automatically<br><li>DISABLE_NOTIFY_AND_MANUAL_RENEW: neither notify upon expiration nor renew automatically<br><br>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: This field may return null, indicating that no valid value is found.",
44+
Description: "Auto renewal flag. Valid values:<br><li>NOTIFY_AND_AUTO_RENEWnotify upon expiration and renew automatically<br><li>NOTIFY_AND_MANUAL_RENEWnotify upon expiration but do not renew automatically<br><li>DISABLE_NOTIFY_AND_MANUAL_RENEWneither notify upon expiration nor renew automatically<br><br>Default value: NOTIFY_AND_MANUAL_RENEWIf 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: This field may return null, indicating that no valid value is found.",
4545
},
4646
},
4747
},
@@ -51,7 +51,7 @@ func ResourceTencentCloudCvmRenewInstance() *schema.Resource {
5151
Type: schema.TypeBool,
5252
Optional: true,
5353
ForceNew: true,
54-
Description: "Whether to renew the elastic data disk. Valid values:<br><li>TRUE: Indicates to renew the subscription instance and renew the attached elastic data disk at the same time<br><li>FALSE: Indicates that the subscription instance will be renewed and the elastic data disk attached to it will not be renewed<br><br>Default value: TRUE.",
54+
Description: "Whether to renew the elastic data disk. Valid values:<br><li>TRUEIndicates to renew the subscription instance and renew the attached elastic data disk at the same time<br><li>FALSEIndicates that the subscription instance will be renewed and the elastic data disk attached to it will not be renewed<br><br>Default valueTRUE.",
5555
},
5656
},
5757
}

‎tencentcloud/services/cvm/resource_tc_cvm_security_group_attachment.go

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -94,38 +94,38 @@ func resourceTencentCloudCvmSecurityGroupAttachmentRead(d *schema.ResourceData,
9494
defer tccommon.LogElapsed("resource.tencentcloud_cvm_security_group_attachment.read")()
9595
defer tccommon.InconsistentCheck(d, meta)()
9696

97-
//logId := tccommon.GetLogId(tccommon.ContextNil)
98-
//
99-
//ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
100-
//
101-
//service := CvmService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
102-
//
103-
//idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
104-
//if len(idSplit) != 2 {
105-
// return fmt.Errorf("id is broken,%s", d.Id())
106-
//}
107-
//instanceId := idSplit[0]
108-
//securityGroupId := idSplit[1]
109-
//
110-
//_ = d.Set("instance_id", instanceId)
111-
//
112-
//_ = d.Set("security_group_id", securityGroupId)
113-
114-
//respData, err := service.DescribeCvmSecurityGroupAttachmentById(ctx, instanceId)
115-
//if err != nil {
116-
// return err
117-
//}
118-
//
119-
//if respData == nil {
120-
// d.SetId("")
121-
// log.Printf("[WARN]%s resource `cvm_security_group_attachment` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
122-
// return nil
123-
//}
124-
//if err := resourceTencentCloudCvmSecurityGroupAttachmentReadPostHandleResponse0(ctx, respData); err != nil {
125-
// return err
126-
//}
127-
//
128-
//_ = securityGroupId
97+
logId := tccommon.GetLogId(tccommon.ContextNil)
98+
99+
ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
100+
101+
service := CvmService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
102+
103+
idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
104+
if len(idSplit) != 2 {
105+
return fmt.Errorf("id is broken,%s", d.Id())
106+
}
107+
instanceId := idSplit[0]
108+
securityGroupId := idSplit[1]
109+
110+
_ = d.Set("instance_id", instanceId)
111+
112+
_ = d.Set("security_group_id", securityGroupId)
113+
114+
respData, err := service.DescribeCvmSecurityGroupAttachmentById(ctx, instanceId)
115+
if err != nil {
116+
return err
117+
}
118+
119+
if respData == nil {
120+
d.SetId("")
121+
log.Printf("[WARN]%s resource `cvm_security_group_attachment` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
122+
return nil
123+
}
124+
if err := resourceTencentCloudCvmSecurityGroupAttachmentReadPostHandleResponse0(ctx, respData); err != nil {
125+
return err
126+
}
127+
128+
_ = securityGroupId
129129
return nil
130130
}
131131

@@ -163,10 +163,11 @@ func resourceTencentCloudCvmSecurityGroupAttachmentDelete(d *schema.ResourceData
163163
return nil
164164
})
165165
if err != nil {
166-
log.Printf("[CRITAL]%s create cvm security group attachment failed, reason:%+v", logId, err)
166+
log.Printf("[CRITAL]%s delete cvm security group attachment failed, reason:%+v", logId, err)
167167
return err
168168
}
169169

170170
_ = response
171+
_ = securityGroupId
171172
return nil
172173
}

‎tencentcloud/services/cvm/resource_tc_cvm_security_group_attachment_extension.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,28 @@ import (
55
"fmt"
66
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
77
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
8+
"log"
89
"strings"
910
)
1011

11-
func resourceTencentCloudCvmSecurityGroupAttachmentReadPostHandleResponse0(ctx context.Context, resp *cvm.Instance) error {
12+
func resourceTencentCloudCvmSecurityGroupAttachmentReadPostHandleResponse0(ctx context.Context, resp *cvm.DescribeInstancesResponseParams) error {
1213
d := tccommon.ResourceDataFromContext(ctx)
14+
logId := tccommon.GetLogId(tccommon.ContextNil)
15+
if len(resp.InstanceSet) < 1 {
16+
d.SetId("")
17+
log.Printf("[WARN]%s resource `CvmSecurityGroupAttachment` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
18+
return nil
19+
}
20+
1321
idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
1422
if len(idSplit) != 2 {
1523
return fmt.Errorf("id is broken,%s", d.Id())
1624
}
1725
instanceId := idSplit[0]
1826
securityGroupId := idSplit[1]
1927

20-
for _, sgId := range resp.SecurityGroupIds {
28+
instanceInfo := resp.InstanceSet[0]
29+
for _, sgId := range instanceInfo.SecurityGroupIds {
2130
if *sgId == securityGroupId {
2231
_ = d.Set("instance_id", instanceId)
2332
_ = d.Set("security_group_id", securityGroupId)

‎tencentcloud/services/cvm/resource_tc_cvm_sync_image.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ func ResourceTencentCloudCvmSyncImage() *schema.Resource {
1717
Create: resourceTencentCloudCvmSyncImageCreate,
1818
Read: resourceTencentCloudCvmSyncImageRead,
1919
Delete: resourceTencentCloudCvmSyncImageDelete,
20-
2120
Schema: map[string]*schema.Schema{
2221
"destination_regions": {
2322
Type: schema.TypeSet,

‎tencentcloud/services/cvm/resource_tc_eip_address_transform.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package cvm
22

33
import (
4+
"context"
45
"log"
5-
"time"
6-
7-
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
8-
svcvpc "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/vpc"
96

107
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
118
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12-
eip "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
9+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
1310

11+
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
1412
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
1513
)
1614

@@ -19,12 +17,11 @@ func ResourceTencentCloudEipAddressTransform() *schema.Resource {
1917
Create: resourceTencentCloudEipAddressTransformCreate,
2018
Read: resourceTencentCloudEipAddressTransformRead,
2119
Delete: resourceTencentCloudEipAddressTransformDelete,
22-
2320
Schema: map[string]*schema.Schema{
2421
"instance_id": {
22+
Type: schema.TypeString,
2523
Required: true,
2624
ForceNew: true,
27-
Type: schema.TypeString,
2825
Description: "the instance ID of a normal public network IP to be operated. eg:ins-23mk45jn.",
2926
},
3027
},
@@ -37,18 +34,26 @@ func resourceTencentCloudEipAddressTransformCreate(d *schema.ResourceData, meta
3734

3835
logId := tccommon.GetLogId(tccommon.ContextNil)
3936

37+
ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
38+
4039
var (
41-
request = eip.NewTransformAddressRequest()
42-
response = eip.NewTransformAddressResponse()
4340
instanceId string
4441
)
42+
var (
43+
request = vpc.NewTransformAddressRequest()
44+
response = vpc.NewTransformAddressResponse()
45+
)
46+
4547
if v, ok := d.GetOk("instance_id"); ok {
4648
instanceId = v.(string)
49+
}
50+
51+
if v, ok := d.GetOk("instance_id"); ok {
4752
request.InstanceId = helper.String(v.(string))
4853
}
4954

5055
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
51-
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseVpcClient().TransformAddress(request)
56+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseVpcClient().TransformAddressWithContext(ctx, request)
5257
if e != nil {
5358
return tccommon.RetryError(e)
5459
} else {
@@ -58,21 +63,18 @@ func resourceTencentCloudEipAddressTransformCreate(d *schema.ResourceData, meta
5863
return nil
5964
})
6065
if err != nil {
61-
log.Printf("[CRITAL]%s operate eip addressTransform failed, reason:%+v", logId, err)
66+
log.Printf("[CRITAL]%s create eip address transform failed, reason:%+v", logId, err)
6267
return err
6368
}
6469

65-
taskId := *response.Response.TaskId
66-
d.SetId(instanceId)
67-
68-
service := svcvpc.NewVpcService(meta.(tccommon.ProviderMeta).GetAPIV3Conn())
70+
_ = response
6971

70-
conf := tccommon.BuildStateChangeConf([]string{}, []string{"SUCCESS"}, 1*tccommon.ReadRetryTimeout, time.Second, service.VpcIpv6AddressStateRefreshFunc(helper.UInt64ToStr(taskId), []string{}))
71-
72-
if _, e := conf.WaitForState(); e != nil {
73-
return e
72+
if err := resourceTencentCloudEipAddressTransformCreatePostHandleResponse0(ctx, response); err != nil {
73+
return err
7474
}
7575

76+
d.SetId(instanceId)
77+
7678
return resourceTencentCloudEipAddressTransformRead(d, meta)
7779
}
7880

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cvm
2+
3+
import (
4+
"context"
5+
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
6+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
7+
svcvpc "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/vpc"
8+
"time"
9+
10+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
11+
)
12+
13+
func resourceTencentCloudEipAddressTransformCreatePostHandleResponse0(ctx context.Context, resp *vpc.TransformAddressResponse) error {
14+
meta := tccommon.ProviderMetaFromContext(ctx)
15+
service := svcvpc.NewVpcService(meta.(tccommon.ProviderMeta).GetAPIV3Conn())
16+
17+
taskId := *resp.Response.TaskId
18+
conf := tccommon.BuildStateChangeConf([]string{}, []string{"SUCCESS"}, 1*tccommon.ReadRetryTimeout, time.Second, service.VpcIpv6AddressStateRefreshFunc(helper.UInt64ToStr(taskId), []string{}))
19+
20+
if _, e := conf.WaitForState(); e != nil {
21+
return e
22+
}
23+
24+
return nil
25+
}

‎tencentcloud/services/cvm/resource_tc_eip_association.go

Lines changed: 51 additions & 247 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,10 @@ package cvm
22

33
import (
44
"context"
5-
"fmt"
65
"log"
7-
"strings"
86

9-
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
10-
svcvpc "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/vpc"
11-
12-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
137
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
14-
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
15-
16-
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/ratelimit"
8+
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
179
)
1810

1911
func ResourceTencentCloudEipAssociation() *schema.Resource {
@@ -26,289 +18,101 @@ func ResourceTencentCloudEipAssociation() *schema.Resource {
2618
},
2719
Schema: map[string]*schema.Schema{
2820
"eip_id": {
29-
Type: schema.TypeString,
30-
Required: true,
31-
ForceNew: true,
32-
ValidateFunc: tccommon.ValidateStringLengthInRange(1, 25),
33-
Description: "The ID of EIP.",
21+
Type: schema.TypeString,
22+
Required: true,
23+
ForceNew: true,
24+
Description: "The ID of EIP.",
3425
},
26+
3527
"instance_id": {
36-
Type: schema.TypeString,
37-
ForceNew: true,
38-
Optional: true,
39-
Computed: true,
40-
ConflictsWith: []string{
41-
"network_interface_id",
42-
"private_ip",
43-
},
44-
ValidateFunc: tccommon.ValidateStringLengthInRange(1, 25),
45-
Description: "The CVM or CLB instance id going to bind with the EIP. This field is conflict with `network_interface_id` and `private_ip fields`.",
28+
Type: schema.TypeString,
29+
Optional: true,
30+
Computed: true,
31+
ForceNew: true,
32+
ConflictsWith: []string{"network_interface_id", "private_ip"},
33+
Description: "The CVM or CLB instance id going to bind with the EIP. This field is conflict with `network_interface_id` and `private_ip fields`.",
4634
},
35+
4736
"network_interface_id": {
48-
Type: schema.TypeString,
49-
ForceNew: true,
50-
Optional: true,
51-
Computed: true,
52-
ValidateFunc: tccommon.ValidateStringLengthInRange(1, 25),
53-
ConflictsWith: []string{
54-
"instance_id",
55-
},
56-
Description: "Indicates the network interface id like `eni-xxxxxx`. This field is conflict with `instance_id`.",
37+
Type: schema.TypeString,
38+
Optional: true,
39+
Computed: true,
40+
ForceNew: true,
41+
ConflictsWith: []string{"instance_id"},
42+
Description: "Indicates the network interface id like `eni-xxxxxx`. This field is conflict with `instance_id`.",
5743
},
44+
5845
"private_ip": {
59-
Type: schema.TypeString,
60-
ForceNew: true,
61-
Optional: true,
62-
Computed: true,
63-
ValidateFunc: tccommon.ValidateStringLengthInRange(7, 25),
64-
ConflictsWith: []string{
65-
"instance_id",
66-
},
67-
Description: "Indicates an IP belongs to the `network_interface_id`. This field is conflict with `instance_id`.",
46+
Type: schema.TypeString,
47+
Optional: true,
48+
Computed: true,
49+
ForceNew: true,
50+
ConflictsWith: []string{"instance_id"},
51+
Description: "Indicates an IP belongs to the `network_interface_id`. This field is conflict with `instance_id`.",
6852
},
6953
},
7054
}
7155
}
7256

7357
func resourceTencentCloudEipAssociationCreate(d *schema.ResourceData, meta interface{}) error {
7458
defer tccommon.LogElapsed("resource.tencentcloud_eip_association.create")()
59+
defer tccommon.InconsistentCheck(d, meta)()
7560

76-
var (
77-
logId = tccommon.GetLogId(tccommon.ContextNil)
78-
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
79-
vpcService = svcvpc.NewVpcService(meta.(tccommon.ProviderMeta).GetAPIV3Conn())
80-
eip *vpc.Address
81-
errRet error
82-
)
83-
84-
eipId := d.Get("eip_id").(string)
85-
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
86-
eip, errRet = vpcService.DescribeEipById(ctx, eipId)
87-
if errRet != nil {
88-
return tccommon.RetryError(errRet, tccommon.InternalError)
89-
}
61+
logId := tccommon.GetLogId(tccommon.ContextNil)
9062

91-
if eip == nil {
92-
return resource.NonRetryableError(fmt.Errorf("eip is not found"))
93-
}
63+
ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
9464

95-
return nil
96-
})
65+
var (
66+
eipId string
67+
)
68+
d.SetId(eipId)
9769

98-
if err != nil {
70+
if err := resourceTencentCloudEipAssociationCreateOnExit(ctx); err != nil {
9971
return err
10072
}
10173

102-
if *eip.AddressStatus != svcvpc.EIP_STATUS_UNBIND {
103-
return fmt.Errorf("eip status is illegal %s", *eip.AddressStatus)
104-
}
105-
106-
if v, ok := d.GetOk("instance_id"); ok {
107-
instanceId := v.(string)
108-
err = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
109-
e := vpcService.AttachEip(ctx, eipId, instanceId)
110-
if e != nil {
111-
return tccommon.RetryError(e)
112-
}
113-
114-
return nil
115-
})
116-
117-
if err != nil {
118-
return err
119-
}
120-
121-
associationId := fmt.Sprintf("%v::%v", eipId, instanceId)
122-
err = resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
123-
eip, errRet = vpcService.DescribeEipById(ctx, eipId)
124-
if errRet != nil {
125-
return tccommon.RetryError(errRet)
126-
}
127-
128-
if eip == nil {
129-
return resource.NonRetryableError(fmt.Errorf("eip is not found"))
130-
}
131-
132-
if *eip.AddressStatus == svcvpc.EIP_STATUS_BIND {
133-
return nil
134-
}
135-
136-
return resource.RetryableError(fmt.Errorf("wait for binding success: %s", *eip.AddressStatus))
137-
})
138-
139-
if err != nil {
140-
return err
141-
}
142-
143-
d.SetId(associationId)
144-
return resourceTencentCloudEipAssociationRead(d, meta)
145-
}
146-
147-
needRequest := false
148-
request := vpc.NewAssociateAddressRequest()
149-
request.AddressId = &eipId
150-
var networkId string
151-
var privateIp string
152-
if v, ok := d.GetOk("network_interface_id"); ok {
153-
needRequest = true
154-
networkId = v.(string)
155-
request.NetworkInterfaceId = &networkId
156-
}
157-
158-
if v, ok := d.GetOk("private_ip"); ok {
159-
needRequest = true
160-
privateIp = v.(string)
161-
request.PrivateIpAddress = &privateIp
162-
}
163-
164-
if needRequest {
165-
err = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
166-
ratelimit.Check(request.GetAction())
167-
response, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseVpcClient().AssociateAddress(request)
168-
if e != nil {
169-
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
170-
logId, request.GetAction(), request.ToJsonString(), e.Error())
171-
return tccommon.RetryError(e)
172-
}
173-
174-
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
175-
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
176-
return nil
177-
})
178-
179-
if err != nil {
180-
return err
181-
}
182-
183-
id := fmt.Sprintf("%v::%v::%v", eipId, networkId, privateIp)
184-
185-
err = resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
186-
eip, errRet = vpcService.DescribeEipById(ctx, eipId)
187-
if errRet != nil {
188-
return tccommon.RetryError(errRet)
189-
}
190-
191-
if eip == nil {
192-
return resource.NonRetryableError(fmt.Errorf("eip is not found"))
193-
}
194-
195-
if *eip.AddressStatus == svcvpc.EIP_STATUS_BIND_ENI || *eip.AddressStatus == svcvpc.EIP_STATUS_BIND {
196-
return nil
197-
}
198-
199-
return resource.RetryableError(fmt.Errorf("wait for binding success: %s", *eip.AddressStatus))
200-
})
201-
202-
if err != nil {
203-
return err
204-
}
205-
206-
d.SetId(id)
207-
return resourceTencentCloudEipAssociationRead(d, meta)
208-
}
209-
210-
return nil
74+
_ = ctx
75+
return resourceTencentCloudEipAssociationRead(d, meta)
21176
}
21277

21378
func resourceTencentCloudEipAssociationRead(d *schema.ResourceData, meta interface{}) error {
21479
defer tccommon.LogElapsed("resource.tencentcloud_eip_association.read")()
21580
defer tccommon.InconsistentCheck(d, meta)()
21681

217-
var (
218-
logId = tccommon.GetLogId(tccommon.ContextNil)
219-
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
220-
vpcService = svcvpc.NewVpcService(meta.(tccommon.ProviderMeta).GetAPIV3Conn())
221-
id = d.Id()
222-
)
82+
logId := tccommon.GetLogId(tccommon.ContextNil)
22383

224-
association, err := ParseEipAssociationId(id)
225-
if err != nil {
226-
return err
227-
}
84+
ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
22885

229-
err = resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
230-
eip, errRet := vpcService.DescribeEipById(ctx, association.EipId)
231-
if errRet != nil {
232-
return tccommon.RetryError(errRet)
233-
}
86+
service := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
23487

235-
if eip == nil {
236-
d.SetId("")
237-
}
88+
eipId := d.Id()
23889

239-
return nil
240-
})
90+
_ = d.Set("eip_id", eipId)
24191

92+
respData, err := service.DescribeEipAssociationById(ctx, eipId)
24293
if err != nil {
24394
return err
24495
}
24596

246-
_ = d.Set("eip_id", association.EipId)
247-
// associate with instance
248-
if len(association.InstanceId) > 0 {
249-
_ = d.Set("instance_id", association.InstanceId)
97+
if respData == nil {
98+
d.SetId("")
99+
log.Printf("[WARN]%s resource `eip_association` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
250100
return nil
251101
}
252102

253-
_ = d.Set("network_interface_id", association.NetworkInterfaceId)
254-
_ = d.Set("private_ip", association.PrivateIp)
255103
return nil
256104
}
257105

258106
func resourceTencentCloudEipAssociationDelete(d *schema.ResourceData, meta interface{}) error {
259107
defer tccommon.LogElapsed("resource.tencentcloud_eip_association.delete")()
108+
defer tccommon.InconsistentCheck(d, meta)()
260109

261-
var (
262-
logId = tccommon.GetLogId(tccommon.ContextNil)
263-
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
264-
vpcService = svcvpc.NewVpcService(meta.(tccommon.ProviderMeta).GetAPIV3Conn())
265-
id = d.Id()
266-
)
267-
268-
association, err := ParseEipAssociationId(id)
269-
if err != nil {
270-
return err
271-
}
272-
273-
err = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
274-
e := vpcService.UnattachEip(ctx, association.EipId)
275-
if e != nil {
276-
return tccommon.RetryError(e, "DesOperation.MutexTaskRunning")
277-
}
110+
logId := tccommon.GetLogId(tccommon.ContextNil)
111+
ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
278112

279-
return nil
280-
})
281-
282-
if err != nil {
283-
return err
284-
}
113+
eipId := d.Id()
285114

115+
_ = eipId
116+
_ = ctx
286117
return nil
287118
}
288-
289-
type EipAssociationId struct {
290-
EipId string
291-
InstanceId string
292-
NetworkInterfaceId string
293-
PrivateIp string
294-
}
295-
296-
func ParseEipAssociationId(associationId string) (association EipAssociationId, errRet error) {
297-
ids := strings.Split(associationId, "::")
298-
if len(ids) < 2 || len(ids) > 3 {
299-
errRet = fmt.Errorf("Invalid eip association ID: %v", associationId)
300-
return
301-
}
302-
association.EipId = ids[0]
303-
304-
// associate with instance
305-
if len(ids) == 2 {
306-
association.InstanceId = ids[1]
307-
return
308-
}
309-
310-
// associate with network interface
311-
association.NetworkInterfaceId = ids[1]
312-
association.PrivateIp = ids[2]
313-
return
314-
}
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
package cvm
2+
3+
import (
4+
"context"
5+
"fmt"
6+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
7+
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
8+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/ratelimit"
9+
svcvpc "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/vpc"
10+
"log"
11+
"strings"
12+
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
14+
)
15+
16+
type EipAssociationId struct {
17+
EipId string
18+
InstanceId string
19+
NetworkInterfaceId string
20+
PrivateIp string
21+
}
22+
23+
func resourceTencentCloudEipAssociationCreateOnExit(ctx context.Context) error {
24+
var (
25+
logId = tccommon.GetLogId(tccommon.ContextNil)
26+
meta = tccommon.ProviderMetaFromContext(ctx)
27+
vpcService = svcvpc.NewVpcService(meta.(tccommon.ProviderMeta).GetAPIV3Conn())
28+
d = tccommon.ResourceDataFromContext(ctx)
29+
eip *vpc.Address
30+
errRet error
31+
)
32+
33+
eipId := d.Get("eip_id").(string)
34+
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
35+
eip, errRet = vpcService.DescribeEipById(ctx, eipId)
36+
if errRet != nil {
37+
return tccommon.RetryError(errRet, tccommon.InternalError)
38+
}
39+
40+
if eip == nil {
41+
return resource.NonRetryableError(fmt.Errorf("eip is not found"))
42+
}
43+
44+
return nil
45+
})
46+
47+
if err != nil {
48+
return err
49+
}
50+
51+
if *eip.AddressStatus != svcvpc.EIP_STATUS_UNBIND {
52+
return fmt.Errorf("eip status is illegal %s", *eip.AddressStatus)
53+
}
54+
55+
if v, ok := d.GetOk("instance_id"); ok {
56+
instanceId := v.(string)
57+
err = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
58+
e := vpcService.AttachEip(ctx, eipId, instanceId)
59+
if e != nil {
60+
return tccommon.RetryError(e)
61+
}
62+
63+
return nil
64+
})
65+
66+
if err != nil {
67+
return err
68+
}
69+
70+
associationId := fmt.Sprintf("%v::%v", eipId, instanceId)
71+
err = resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
72+
eip, errRet = vpcService.DescribeEipById(ctx, eipId)
73+
if errRet != nil {
74+
return tccommon.RetryError(errRet)
75+
}
76+
77+
if eip == nil {
78+
return resource.NonRetryableError(fmt.Errorf("eip is not found"))
79+
}
80+
81+
if *eip.AddressStatus == svcvpc.EIP_STATUS_BIND {
82+
return nil
83+
}
84+
85+
return resource.RetryableError(fmt.Errorf("wait for binding success: %s", *eip.AddressStatus))
86+
})
87+
88+
if err != nil {
89+
return err
90+
}
91+
92+
d.SetId(associationId)
93+
return resourceTencentCloudEipAssociationRead(d, meta)
94+
}
95+
96+
needRequest := false
97+
request := vpc.NewAssociateAddressRequest()
98+
request.AddressId = &eipId
99+
var networkId string
100+
var privateIp string
101+
if v, ok := d.GetOk("network_interface_id"); ok {
102+
needRequest = true
103+
networkId = v.(string)
104+
request.NetworkInterfaceId = &networkId
105+
}
106+
107+
if v, ok := d.GetOk("private_ip"); ok {
108+
needRequest = true
109+
privateIp = v.(string)
110+
request.PrivateIpAddress = &privateIp
111+
}
112+
113+
if needRequest {
114+
err = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
115+
ratelimit.Check(request.GetAction())
116+
response, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseVpcClient().AssociateAddress(request)
117+
if e != nil {
118+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
119+
logId, request.GetAction(), request.ToJsonString(), e.Error())
120+
return tccommon.RetryError(e)
121+
}
122+
123+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
124+
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
125+
return nil
126+
})
127+
128+
if err != nil {
129+
return err
130+
}
131+
132+
id := fmt.Sprintf("%v::%v::%v", eipId, networkId, privateIp)
133+
134+
err = resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
135+
eip, errRet = vpcService.DescribeEipById(ctx, eipId)
136+
if errRet != nil {
137+
return tccommon.RetryError(errRet)
138+
}
139+
140+
if eip == nil {
141+
return resource.NonRetryableError(fmt.Errorf("eip is not found"))
142+
}
143+
144+
if *eip.AddressStatus == svcvpc.EIP_STATUS_BIND_ENI || *eip.AddressStatus == svcvpc.EIP_STATUS_BIND {
145+
return nil
146+
}
147+
148+
return resource.RetryableError(fmt.Errorf("wait for binding success: %s", *eip.AddressStatus))
149+
})
150+
151+
if err != nil {
152+
return err
153+
}
154+
155+
d.SetId(id)
156+
return resourceTencentCloudEipAssociationRead(d, meta)
157+
}
158+
159+
return nil
160+
}
161+
162+
func resourceTencentCloudEipAssociationReadPreRequest0(ctx context.Context, req *vpc.DescribeAddressesRequest) error {
163+
d := tccommon.ResourceDataFromContext(ctx)
164+
id := d.Id()
165+
association, err := ParseEipAssociationId(id)
166+
if err != nil {
167+
return err
168+
}
169+
170+
_ = d.Set("eip_id", association.EipId)
171+
if len(association.InstanceId) > 0 {
172+
_ = d.Set("instance_id", association.InstanceId)
173+
return nil
174+
}
175+
176+
_ = d.Set("network_interface_id", association.NetworkInterfaceId)
177+
_ = d.Set("private_ip", association.PrivateIp)
178+
179+
req.AddressIds = []*string{&association.EipId}
180+
return nil
181+
}
182+
183+
func ParseEipAssociationId(associationId string) (association EipAssociationId, errRet error) {
184+
ids := strings.Split(associationId, "::")
185+
if len(ids) < 2 || len(ids) > 3 {
186+
errRet = fmt.Errorf("Invalid eip association ID: %v", associationId)
187+
return
188+
}
189+
association.EipId = ids[0]
190+
191+
// associate with instance
192+
if len(ids) == 2 {
193+
association.InstanceId = ids[1]
194+
return
195+
}
196+
197+
// associate with network interface
198+
association.NetworkInterfaceId = ids[1]
199+
association.PrivateIp = ids[2]
200+
return
201+
}

‎tencentcloud/services/cvm/resource_tc_eip_normal_address_return.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package cvm
22

33
import (
4+
"context"
45
"log"
56

6-
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
7-
87
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
98
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
109
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
10+
11+
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
1112
)
1213

1314
func ResourceTencentCloudEipNormalAddressReturn() *schema.Resource {
@@ -17,13 +18,13 @@ func ResourceTencentCloudEipNormalAddressReturn() *schema.Resource {
1718
Delete: resourceTencentCloudEipNormalAddressReturnDelete,
1819
Schema: map[string]*schema.Schema{
1920
"address_ips": {
20-
Optional: true,
21-
ForceNew: true,
22-
Type: schema.TypeSet,
21+
Type: schema.TypeSet,
22+
Optional: true,
23+
ForceNew: true,
24+
Description: "The IP address of the EIP, example: 101.35.139.183.",
2325
Elem: &schema.Schema{
2426
Type: schema.TypeString,
2527
},
26-
Description: "The IP address of the EIP, example: 101.35.139.183.",
2728
},
2829
},
2930
}
@@ -35,35 +36,43 @@ func resourceTencentCloudEipNormalAddressReturnCreate(d *schema.ResourceData, me
3536

3637
logId := tccommon.GetLogId(tccommon.ContextNil)
3738

39+
ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
40+
3841
var (
39-
request = vpc.NewReturnNormalAddressesRequest()
4042
addressIps string
4143
)
42-
if v, ok := d.GetOk("address_ips"); ok {
43-
addressIpsSet := v.(*schema.Set).List()
44-
for i := range addressIpsSet {
45-
addressIp := addressIpsSet[i].(string)
46-
request.AddressIps = append(request.AddressIps, &addressIp)
47-
addressIps = addressIp + tccommon.FILED_SP
48-
}
44+
var (
45+
request = vpc.NewReturnNormalAddressesRequest()
46+
response = vpc.NewReturnNormalAddressesResponse()
47+
)
48+
49+
if err := resourceTencentCloudEipNormalAddressReturnCreatePostFillRequest0(ctx, request); err != nil {
50+
return err
4951
}
5052

5153
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
52-
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseVpcClient().ReturnNormalAddresses(request)
54+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseVpcClient().ReturnNormalAddressesWithContext(ctx, request)
5355
if e != nil {
5456
return tccommon.RetryError(e)
5557
} else {
5658
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
5759
}
60+
response = result
5861
return nil
5962
})
6063
if err != nil {
61-
log.Printf("[CRITAL]%s operate vpc normalAddressReturn failed, reason:%+v", logId, err)
64+
log.Printf("[CRITAL]%s create eip normal address return failed, reason:%+v", logId, err)
6265
return err
6366
}
6467

68+
_ = response
69+
6570
d.SetId(addressIps)
6671

72+
if err := resourceTencentCloudEipNormalAddressReturnCreateOnExit(ctx); err != nil {
73+
return err
74+
}
75+
6776
return resourceTencentCloudEipNormalAddressReturnRead(d, meta)
6877
}
6978

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package cvm
2+
3+
import (
4+
"context"
5+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
6+
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
7+
8+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
9+
)
10+
11+
func resourceTencentCloudEipNormalAddressReturnCreatePostFillRequest0(ctx context.Context, req *vpc.ReturnNormalAddressesRequest) error {
12+
d := tccommon.ResourceDataFromContext(ctx)
13+
var (
14+
addressIps string
15+
)
16+
17+
if v, ok := d.GetOk("address_ips"); ok {
18+
addressIpsSet := v.(*schema.Set).List()
19+
for i := range addressIpsSet {
20+
addressIp := addressIpsSet[i].(string)
21+
req.AddressIps = append(req.AddressIps, &addressIp)
22+
addressIps = addressIp + tccommon.FILED_SP
23+
}
24+
}
25+
26+
_ = addressIps
27+
return nil
28+
}
29+
30+
func resourceTencentCloudEipNormalAddressReturnCreateOnExit(ctx context.Context) error {
31+
d := tccommon.ResourceDataFromContext(ctx)
32+
var (
33+
addressIps string
34+
)
35+
36+
if v, ok := d.GetOk("address_ips"); ok {
37+
addressIpsSet := v.(*schema.Set).List()
38+
for i := range addressIpsSet {
39+
addressIp := addressIpsSet[i].(string)
40+
addressIps = addressIp + tccommon.FILED_SP
41+
}
42+
}
43+
44+
d.SetId(addressIps)
45+
return nil
46+
}

‎tencentcloud/services/cvm/service_tencentcloud_cvm.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,3 +2150,61 @@ func (me *CvmService) DescribeInstancesByFilter(ctx context.Context, param map[s
21502150

21512151
return
21522152
}
2153+
2154+
func (me *CvmService) DescribeCvmSecurityGroupAttachmentById(ctx context.Context, instanceId string) (ret *cvm.DescribeInstancesResponseParams, errRet error) {
2155+
logId := tccommon.GetLogId(ctx)
2156+
2157+
request := cvm.NewDescribeInstancesRequest()
2158+
request.InstanceIds = []*string{helper.String(instanceId)}
2159+
2160+
defer func() {
2161+
if errRet != nil {
2162+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
2163+
}
2164+
}()
2165+
2166+
ratelimit.Check(request.GetAction())
2167+
2168+
response, err := me.client.UseCvmClient().DescribeInstances(request)
2169+
if err != nil {
2170+
errRet = err
2171+
return
2172+
}
2173+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
2174+
2175+
ret = response.Response
2176+
return
2177+
}
2178+
2179+
func (me *VpcService) DescribeEipAssociationById(ctx context.Context, eipId string) (ret *vpc.Address, errRet error) {
2180+
logId := tccommon.GetLogId(ctx)
2181+
2182+
request := vpc.NewDescribeAddressesRequest()
2183+
request.AddressIds = []*string{helper.String(eipId)}
2184+
2185+
defer func() {
2186+
if errRet != nil {
2187+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
2188+
}
2189+
}()
2190+
2191+
ratelimit.Check(request.GetAction())
2192+
2193+
if err := resourceTencentCloudEipAssociationReadPreRequest0(ctx, request); err != nil {
2194+
return nil, err
2195+
}
2196+
2197+
response, err := me.client.UseVpcClient().DescribeAddresses(request)
2198+
if err != nil {
2199+
errRet = err
2200+
return
2201+
}
2202+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
2203+
2204+
if len(response.Response.AddressSet) < 1 {
2205+
return
2206+
}
2207+
2208+
ret = response.Response.AddressSet[0]
2209+
return
2210+
}

0 commit comments

Comments
 (0)
Please sign in to comment.