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