|
| 1 | +/* |
| 2 | +Provides a resource to create a vpc classic_link_attachment |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_vpc_classic_link_attachment" "classic_link_attachment" { |
| 8 | + vpc_id = "vpc-hdvfe0g1" |
| 9 | + instance_ids = ["ins-ceynqvnu"] |
| 10 | +} |
| 11 | +``` |
| 12 | +
|
| 13 | +Import |
| 14 | +
|
| 15 | +vpc classic_link_attachment can be imported using the id, e.g. |
| 16 | +
|
| 17 | +``` |
| 18 | +terraform import tencentcloud_vpc_classic_link_attachment.classic_link_attachment classic_link_attachment_id |
| 19 | +``` |
| 20 | +*/ |
| 21 | +package tencentcloud |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "fmt" |
| 26 | + "log" |
| 27 | + "strings" |
| 28 | + |
| 29 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 30 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 31 | + vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" |
| 32 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 33 | +) |
| 34 | + |
| 35 | +func resourceTencentCloudVpcClassicLinkAttachment() *schema.Resource { |
| 36 | + return &schema.Resource{ |
| 37 | + Create: resourceTencentCloudVpcClassicLinkAttachmentCreate, |
| 38 | + Read: resourceTencentCloudVpcClassicLinkAttachmentRead, |
| 39 | + Delete: resourceTencentCloudVpcClassicLinkAttachmentDelete, |
| 40 | + Importer: &schema.ResourceImporter{ |
| 41 | + State: schema.ImportStatePassthrough, |
| 42 | + }, |
| 43 | + Schema: map[string]*schema.Schema{ |
| 44 | + "vpc_id": { |
| 45 | + Required: true, |
| 46 | + ForceNew: true, |
| 47 | + Type: schema.TypeString, |
| 48 | + Description: "VPC instance ID.", |
| 49 | + }, |
| 50 | + |
| 51 | + "instance_ids": { |
| 52 | + Required: true, |
| 53 | + ForceNew: true, |
| 54 | + MaxItems: 1, |
| 55 | + Type: schema.TypeSet, |
| 56 | + Elem: &schema.Schema{ |
| 57 | + Type: schema.TypeString, |
| 58 | + }, |
| 59 | + Description: "CVM instance ID. It only support set one instance now.", |
| 60 | + }, |
| 61 | + }, |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func resourceTencentCloudVpcClassicLinkAttachmentCreate(d *schema.ResourceData, meta interface{}) error { |
| 66 | + defer logElapsed("resource.tencentcloud_vpc_classic_link_attachment.create")() |
| 67 | + defer inconsistentCheck(d, meta)() |
| 68 | + |
| 69 | + logId := getLogId(contextNil) |
| 70 | + |
| 71 | + var ( |
| 72 | + request = vpc.NewAttachClassicLinkVpcRequest() |
| 73 | + vpcId string |
| 74 | + instanceId string |
| 75 | + ) |
| 76 | + if v, ok := d.GetOk("vpc_id"); ok { |
| 77 | + vpcId = v.(string) |
| 78 | + request.VpcId = helper.String(v.(string)) |
| 79 | + } |
| 80 | + |
| 81 | + if v, ok := d.GetOk("instance_ids"); ok { |
| 82 | + instanceIdsSet := v.(*schema.Set).List() |
| 83 | + for i := range instanceIdsSet { |
| 84 | + instanceId = instanceIdsSet[i].(string) |
| 85 | + request.InstanceIds = append(request.InstanceIds, &instanceId) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 90 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseVpcClient().AttachClassicLinkVpc(request) |
| 91 | + if e != nil { |
| 92 | + return retryError(e) |
| 93 | + } else { |
| 94 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 95 | + } |
| 96 | + return nil |
| 97 | + }) |
| 98 | + if err != nil { |
| 99 | + log.Printf("[CRITAL]%s create vpc ClassicLinkAttachment failed, reason:%+v", logId, err) |
| 100 | + return err |
| 101 | + } |
| 102 | + |
| 103 | + d.SetId(vpcId + FILED_SP + instanceId) |
| 104 | + |
| 105 | + return resourceTencentCloudVpcClassicLinkAttachmentRead(d, meta) |
| 106 | +} |
| 107 | + |
| 108 | +func resourceTencentCloudVpcClassicLinkAttachmentRead(d *schema.ResourceData, meta interface{}) error { |
| 109 | + defer logElapsed("resource.tencentcloud_vpc_classic_link_attachment.read")() |
| 110 | + defer inconsistentCheck(d, meta)() |
| 111 | + |
| 112 | + logId := getLogId(contextNil) |
| 113 | + |
| 114 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 115 | + |
| 116 | + service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 117 | + |
| 118 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 119 | + if len(idSplit) != 2 { |
| 120 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 121 | + } |
| 122 | + vpcId := idSplit[0] |
| 123 | + instanceId := idSplit[1] |
| 124 | + |
| 125 | + classicLinkAttachment, err := service.DescribeVpcClassicLinkAttachmentById(ctx, vpcId, instanceId) |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + |
| 130 | + if classicLinkAttachment == nil { |
| 131 | + d.SetId("") |
| 132 | + log.Printf("[WARN]%s resource `VpcClassicLinkAttachment` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 133 | + return nil |
| 134 | + } |
| 135 | + |
| 136 | + if classicLinkAttachment.VpcId != nil { |
| 137 | + _ = d.Set("vpc_id", classicLinkAttachment.VpcId) |
| 138 | + } |
| 139 | + |
| 140 | + if classicLinkAttachment.InstanceId != nil { |
| 141 | + _ = d.Set("instance_ids", []*string{classicLinkAttachment.InstanceId}) |
| 142 | + } |
| 143 | + |
| 144 | + return nil |
| 145 | +} |
| 146 | + |
| 147 | +func resourceTencentCloudVpcClassicLinkAttachmentDelete(d *schema.ResourceData, meta interface{}) error { |
| 148 | + defer logElapsed("resource.tencentcloud_vpc_classic_link_attachment.delete")() |
| 149 | + defer inconsistentCheck(d, meta)() |
| 150 | + |
| 151 | + logId := getLogId(contextNil) |
| 152 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 153 | + |
| 154 | + service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 155 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 156 | + if len(idSplit) != 2 { |
| 157 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 158 | + } |
| 159 | + vpcId := idSplit[0] |
| 160 | + instanceId := idSplit[1] |
| 161 | + |
| 162 | + if err := service.DeleteVpcClassicLinkAttachmentById(ctx, vpcId, instanceId); err != nil { |
| 163 | + return err |
| 164 | + } |
| 165 | + |
| 166 | + return nil |
| 167 | +} |
0 commit comments