|
| 1 | +package vpc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" |
| 11 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 12 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 13 | +) |
| 14 | + |
| 15 | +func ResourceTencentCloudVpcPrivateNatGateway() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + Create: resourceTencentCloudVpcPrivateNatGatewayCreate, |
| 18 | + Read: resourceTencentCloudVpcPrivateNatGatewayRead, |
| 19 | + Update: resourceTencentCloudVpcPrivateNatGatewayUpdate, |
| 20 | + Delete: resourceTencentCloudVpcPrivateNatGatewayDelete, |
| 21 | + Importer: &schema.ResourceImporter{ |
| 22 | + State: schema.ImportStatePassthrough, |
| 23 | + }, |
| 24 | + Schema: map[string]*schema.Schema{ |
| 25 | + "nat_gateway_name": { |
| 26 | + Required: true, |
| 27 | + Type: schema.TypeString, |
| 28 | + Description: "Private network gateway name.", |
| 29 | + }, |
| 30 | + |
| 31 | + "vpc_id": { |
| 32 | + Optional: true, |
| 33 | + Computed: true, |
| 34 | + Type: schema.TypeString, |
| 35 | + Description: "Private Cloud instance ID. This parameter is required when creating a VPC type private network NAT gateway or a private network NAT gateway of private network gateway.", |
| 36 | + }, |
| 37 | + |
| 38 | + "cross_domain": { |
| 39 | + Optional: true, |
| 40 | + Computed: true, |
| 41 | + Type: schema.TypeBool, |
| 42 | + Description: "Cross-domain parameters. Cross-domain binding of VPCs is supported only when the value is True.", |
| 43 | + }, |
| 44 | + |
| 45 | + "vpc_type": { |
| 46 | + Optional: true, |
| 47 | + Computed: true, |
| 48 | + Type: schema.TypeBool, |
| 49 | + Description: "VPC type private network NAT gateway. Only when the value is True will a VPC type private network NAT gateway be created.", |
| 50 | + }, |
| 51 | + |
| 52 | + "ccn_id": { |
| 53 | + Optional: true, |
| 54 | + Computed: true, |
| 55 | + Type: schema.TypeString, |
| 56 | + Description: "Cloud Connect Network type The Cloud Connect Network instance ID required to be bound to the private network NAT gateway.", |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func resourceTencentCloudVpcPrivateNatGatewayCreate(d *schema.ResourceData, meta interface{}) error { |
| 63 | + defer tccommon.LogElapsed("resource.tencentcloud_vpc_private_nat_gateway.create")() |
| 64 | + defer tccommon.InconsistentCheck(d, meta)() |
| 65 | + |
| 66 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 67 | + ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 68 | + |
| 69 | + var ( |
| 70 | + request = vpc.NewCreatePrivateNatGatewayRequest() |
| 71 | + response = vpc.NewCreatePrivateNatGatewayResponse() |
| 72 | + instanceId string |
| 73 | + ) |
| 74 | + if v, ok := d.GetOk("nat_gateway_name"); ok { |
| 75 | + request.NatGatewayName = helper.String(v.(string)) |
| 76 | + } |
| 77 | + |
| 78 | + if v, ok := d.GetOk("vpc_id"); ok { |
| 79 | + request.VpcId = helper.String(v.(string)) |
| 80 | + } |
| 81 | + |
| 82 | + if v, ok := d.GetOkExists("cross_domain"); ok { |
| 83 | + request.CrossDomain = helper.Bool(v.(bool)) |
| 84 | + } |
| 85 | + |
| 86 | + if v, ok := d.GetOkExists("vpc_type"); ok { |
| 87 | + request.VpcType = helper.Bool(v.(bool)) |
| 88 | + } |
| 89 | + |
| 90 | + if v, ok := d.GetOk("ccn_id"); ok { |
| 91 | + request.CcnId = helper.String(v.(string)) |
| 92 | + } |
| 93 | + |
| 94 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 95 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseVpcClient().CreatePrivateNatGateway(request) |
| 96 | + if e != nil { |
| 97 | + return tccommon.RetryError(e) |
| 98 | + } else { |
| 99 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 100 | + } |
| 101 | + response = result |
| 102 | + return nil |
| 103 | + }) |
| 104 | + if err != nil { |
| 105 | + log.Printf("[CRITAL]%s create vpc privateNatGateway failed, reason:%+v", logId, err) |
| 106 | + return err |
| 107 | + } |
| 108 | + |
| 109 | + if response.Response != nil && len(response.Response.PrivateNatGatewaySet) > 0 { |
| 110 | + privateNatGateway := response.Response.PrivateNatGatewaySet[0] |
| 111 | + if privateNatGateway.NatGatewayId != nil { |
| 112 | + instanceId = *response.Response.PrivateNatGatewaySet[0].NatGatewayId |
| 113 | + } |
| 114 | + } |
| 115 | + if instanceId == "" { |
| 116 | + d.SetId("") |
| 117 | + return fmt.Errorf("instanceId is nil") |
| 118 | + } |
| 119 | + |
| 120 | + service := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 121 | + err = resource.Retry(5*tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 122 | + privateNatGateway, errRet := service.DescribeVpcPrivateNatGatewayById(ctx, instanceId) |
| 123 | + if errRet != nil { |
| 124 | + return tccommon.RetryError(errRet) |
| 125 | + } |
| 126 | + if privateNatGateway.Status == nil { |
| 127 | + return resource.RetryableError(fmt.Errorf("waiting for instance create")) |
| 128 | + } |
| 129 | + if *privateNatGateway.Status != "AVAILABLE" { |
| 130 | + return resource.RetryableError(fmt.Errorf("waiting for instance create")) |
| 131 | + } |
| 132 | + return nil |
| 133 | + }) |
| 134 | + if err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + d.SetId(instanceId) |
| 138 | + |
| 139 | + return resourceTencentCloudVpcPrivateNatGatewayRead(d, meta) |
| 140 | +} |
| 141 | + |
| 142 | +func resourceTencentCloudVpcPrivateNatGatewayRead(d *schema.ResourceData, meta interface{}) error { |
| 143 | + defer tccommon.LogElapsed("resource.tencentcloud_vpc_private_nat_gateway.read")() |
| 144 | + defer tccommon.InconsistentCheck(d, meta)() |
| 145 | + |
| 146 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 147 | + |
| 148 | + ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 149 | + |
| 150 | + service := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 151 | + |
| 152 | + instanceId := d.Id() |
| 153 | + |
| 154 | + privateNatGateway, err := service.DescribeVpcPrivateNatGatewayById(ctx, instanceId) |
| 155 | + if err != nil { |
| 156 | + return err |
| 157 | + } |
| 158 | + |
| 159 | + if privateNatGateway == nil { |
| 160 | + d.SetId("") |
| 161 | + log.Printf("[WARN]%s resource `VpcPrivateNatGateway` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 162 | + return nil |
| 163 | + } |
| 164 | + |
| 165 | + if privateNatGateway.NatGatewayName != nil { |
| 166 | + _ = d.Set("nat_gateway_name", privateNatGateway.NatGatewayName) |
| 167 | + } |
| 168 | + |
| 169 | + if privateNatGateway.VpcId != nil { |
| 170 | + _ = d.Set("vpc_id", privateNatGateway.VpcId) |
| 171 | + } |
| 172 | + |
| 173 | + if privateNatGateway.CrossDomain != nil { |
| 174 | + _ = d.Set("cross_domain", privateNatGateway.CrossDomain) |
| 175 | + } |
| 176 | + |
| 177 | + if privateNatGateway.VpcType != nil { |
| 178 | + _ = d.Set("vpc_type", privateNatGateway.VpcType) |
| 179 | + } |
| 180 | + |
| 181 | + if privateNatGateway.CcnId != nil { |
| 182 | + _ = d.Set("ccn_id", privateNatGateway.CcnId) |
| 183 | + } |
| 184 | + |
| 185 | + return nil |
| 186 | +} |
| 187 | + |
| 188 | +func resourceTencentCloudVpcPrivateNatGatewayUpdate(d *schema.ResourceData, meta interface{}) error { |
| 189 | + defer tccommon.LogElapsed("resource.tencentcloud_vpc_private_nat_gateway.update")() |
| 190 | + defer tccommon.InconsistentCheck(d, meta)() |
| 191 | + |
| 192 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 193 | + ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 194 | + |
| 195 | + request := vpc.NewModifyPrivateNatGatewayAttributeRequest() |
| 196 | + |
| 197 | + instanceId := d.Id() |
| 198 | + |
| 199 | + request.NatGatewayId = &instanceId |
| 200 | + |
| 201 | + immutableArgs := []string{"vpc_id", "cross_domain", "vpc_type", "ccn_id"} |
| 202 | + |
| 203 | + for _, v := range immutableArgs { |
| 204 | + if d.HasChange(v) { |
| 205 | + return fmt.Errorf("argument `%s` cannot be changed", v) |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + if d.HasChange("nat_gateway_name") { |
| 210 | + if v, ok := d.GetOk("nat_gateway_name"); ok { |
| 211 | + request.NatGatewayName = helper.String(v.(string)) |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 216 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseVpcClient().ModifyPrivateNatGatewayAttribute(request) |
| 217 | + if e != nil { |
| 218 | + return tccommon.RetryError(e) |
| 219 | + } else { |
| 220 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 221 | + } |
| 222 | + return nil |
| 223 | + }) |
| 224 | + if err != nil { |
| 225 | + log.Printf("[CRITAL]%s update vpc privateNatGateway failed, reason:%+v", logId, err) |
| 226 | + return err |
| 227 | + } |
| 228 | + |
| 229 | + service := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 230 | + err = resource.Retry(5*tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 231 | + privateNatGateway, errRet := service.DescribeVpcPrivateNatGatewayById(ctx, instanceId) |
| 232 | + if errRet != nil { |
| 233 | + return tccommon.RetryError(errRet) |
| 234 | + } |
| 235 | + if privateNatGateway.Status == nil { |
| 236 | + return resource.RetryableError(fmt.Errorf("waiting for instance update")) |
| 237 | + } |
| 238 | + if *privateNatGateway.Status != "AVAILABLE" { |
| 239 | + return resource.RetryableError(fmt.Errorf("waiting for instance update")) |
| 240 | + } |
| 241 | + return nil |
| 242 | + }) |
| 243 | + if err != nil { |
| 244 | + return err |
| 245 | + } |
| 246 | + return resourceTencentCloudVpcPrivateNatGatewayRead(d, meta) |
| 247 | +} |
| 248 | + |
| 249 | +func resourceTencentCloudVpcPrivateNatGatewayDelete(d *schema.ResourceData, meta interface{}) error { |
| 250 | + defer tccommon.LogElapsed("resource.tencentcloud_vpc_private_nat_gateway.delete")() |
| 251 | + defer tccommon.InconsistentCheck(d, meta)() |
| 252 | + |
| 253 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 254 | + ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 255 | + |
| 256 | + service := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 257 | + instanceId := d.Id() |
| 258 | + |
| 259 | + if err := service.DeleteVpcPrivateNatGatewayById(ctx, instanceId); err != nil { |
| 260 | + return err |
| 261 | + } |
| 262 | + |
| 263 | + err := resource.Retry(5*tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 264 | + privateNatGateway, errRet := service.DescribeVpcPrivateNatGatewayById(ctx, instanceId) |
| 265 | + if errRet != nil { |
| 266 | + return tccommon.RetryError(errRet) |
| 267 | + } |
| 268 | + if privateNatGateway != nil { |
| 269 | + return resource.RetryableError(fmt.Errorf("waiting for instance delete")) |
| 270 | + } |
| 271 | + return nil |
| 272 | + }) |
| 273 | + |
| 274 | + if err != nil { |
| 275 | + return err |
| 276 | + } |
| 277 | + return nil |
| 278 | +} |
0 commit comments