From a2e60dcf973f4c6e66f8bdec54648c8651dadd84 Mon Sep 17 00:00:00 2001 From: SevenEarth <391613297@qq.com> Date: Mon, 20 Jan 2025 18:13:44 +0800 Subject: [PATCH 1/2] add --- .../services/dcg/resource_tc_dc_gateway.go | 139 ++++++++++++------ .../services/dcg/resource_tc_dc_gateway.md | 42 +++++- website/docs/r/dc_gateway.html.markdown | 42 +++++- 3 files changed, 165 insertions(+), 58 deletions(-) diff --git a/tencentcloud/services/dcg/resource_tc_dc_gateway.go b/tencentcloud/services/dcg/resource_tc_dc_gateway.go index 86626e1539..9db99c26e1 100644 --- a/tencentcloud/services/dcg/resource_tc_dc_gateway.go +++ b/tencentcloud/services/dcg/resource_tc_dc_gateway.go @@ -3,10 +3,12 @@ package dcg import ( "context" "fmt" + "log" "strings" - "time" + vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -21,7 +23,6 @@ func ResourceTencentCloudDcGatewayInstance() *schema.Resource { Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, }, - Schema: map[string]*schema.Schema{ "name": { Type: schema.TypeString, @@ -74,48 +75,71 @@ func ResourceTencentCloudDcGatewayInstance() *schema.Resource { func resourceTencentCloudDcGatewayCreate(d *schema.ResourceData, meta interface{}) error { defer tccommon.LogElapsed("resource.tencentcloud_dc_gateway.create")() - logId := tccommon.GetLogId(tccommon.ContextNil) - ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) - - service := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} - var ( - name = d.Get("name").(string) - networkType = d.Get("network_type").(string) - networkInstanceId = d.Get("network_instance_id").(string) - gatewayType = d.Get("gateway_type").(string) + logId = tccommon.GetLogId(tccommon.ContextNil) + request = vpc.NewCreateDirectConnectGatewayRequest() + response = vpc.NewCreateDirectConnectGatewayResponse() + networkType string + networkInstanceId string + gatewayType string ) - if networkType == DCG_NETWORK_TYPE_VPC && - !strings.HasPrefix(networkInstanceId, "vpc") { + if v, ok := d.GetOk("name"); ok { + request.DirectConnectGatewayName = helper.String(v.(string)) + } - return fmt.Errorf("if `network_type` is '%s', the field `network_instance_id` must be a VPC resource", - DCG_NETWORK_TYPE_VPC) + if v, ok := d.GetOk("network_type"); ok { + request.NetworkType = helper.String(v.(string)) + networkType = v.(string) } - if networkType == DCG_NETWORK_TYPE_CCN && - !strings.HasPrefix(networkInstanceId, "ccn") { + if v, ok := d.GetOk("network_instance_id"); ok { + request.NetworkInstanceId = helper.String(v.(string)) + networkInstanceId = v.(string) + } - return fmt.Errorf("if `network_type` is '%s', the field `network_instance_id` must be a CCN resource", - DCG_NETWORK_TYPE_CCN) + if v, ok := d.GetOk("gateway_type"); ok { + request.GatewayType = helper.String(v.(string)) + gatewayType = v.(string) } - if networkType == DCG_NETWORK_TYPE_CCN && gatewayType != DCG_GATEWAY_TYPE_NORMAL { + if networkType == DCG_NETWORK_TYPE_VPC && !strings.HasPrefix(networkInstanceId, "vpc") { + return fmt.Errorf("if `network_type` is '%s', the field `network_instance_id` must be a VPC resource", DCG_NETWORK_TYPE_VPC) + } - return fmt.Errorf("if `network_type` is '%s', the field `gateway_type` must be '%s'", - DCG_NETWORK_TYPE_CCN, - DCG_GATEWAY_TYPE_NORMAL) + if networkType == DCG_NETWORK_TYPE_CCN && !strings.HasPrefix(networkInstanceId, "ccn") { + return fmt.Errorf("if `network_type` is '%s', the field `network_instance_id` must be a CCN resource", DCG_NETWORK_TYPE_CCN) } - dcgId, err := service.CreateDirectConnectGateway(ctx, name, networkType, networkInstanceId, gatewayType) + if networkType == DCG_NETWORK_TYPE_CCN && gatewayType != DCG_GATEWAY_TYPE_NORMAL { + return fmt.Errorf("if `network_type` is '%s', the field `gateway_type` must be '%s'", DCG_NETWORK_TYPE_CCN, DCG_GATEWAY_TYPE_NORMAL) + } + + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseVpcClient().CreateDirectConnectGateway(request) + if e != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), e.Error()) + return tccommon.RetryError(e) + } + + if result == nil || result.Response == nil || result.Response.DirectConnectGateway == nil { + return resource.NonRetryableError(fmt.Errorf("Create direct connect gateway failed, Response is nil.")) + } + + response = result + return nil + }) + if err != nil { + log.Printf("[CRITAL]%s create direct connect gateway failed, reason:%s\n", logId, err.Error()) return err } - d.SetId(dcgId) + if response.Response.DirectConnectGateway.DirectConnectGatewayId == nil { + return fmt.Errorf("DirectConnectGatewayId is nil.") + } - // add sleep protect, either network_instance_id will be set "". - time.Sleep(1 * time.Second) + d.SetId(*response.Response.DirectConnectGateway.DirectConnectGatewayId) return resourceTencentCloudDcGatewayRead(d, meta) } @@ -123,10 +147,12 @@ func resourceTencentCloudDcGatewayCreate(d *schema.ResourceData, meta interface{ func resourceTencentCloudDcGatewayRead(d *schema.ResourceData, meta interface{}) error { defer tccommon.LogElapsed("resource.tencentcloud_dc_gateway.read")() - logId := tccommon.GetLogId(tccommon.ContextNil) - ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) + var ( + logId = tccommon.GetLogId(tccommon.ContextNil) + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) + service = VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} + ) - service := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { info, has, e := service.DescribeDirectConnectGateway(ctx, d.Id()) if e != nil { @@ -147,22 +173,43 @@ func resourceTencentCloudDcGatewayRead(d *schema.ResourceData, meta interface{}) _ = d.Set("create_time", info.createTime) return nil }) + if err != nil { return err } + return nil } func resourceTencentCloudDcGatewayUpdate(d *schema.ResourceData, meta interface{}) error { defer tccommon.LogElapsed("resource.tencentcloud_dc_gateway.update")() - logId := tccommon.GetLogId(tccommon.ContextNil) - ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) + var ( + logId = tccommon.GetLogId(tccommon.ContextNil) + dcgId = d.Id() + ) - service := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} if d.HasChange("name") { - var name = d.Get("name").(string) - return service.ModifyDirectConnectGatewayAttribute(ctx, d.Id(), name) + request := vpc.NewModifyDirectConnectGatewayAttributeRequest() + request.DirectConnectGatewayId = helper.String(dcgId) + if v, ok := d.GetOk("name"); ok { + request.DirectConnectGatewayName = helper.String(v.(string)) + } + + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { + _, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseVpcClient().ModifyDirectConnectGatewayAttribute(request) + if e != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), e.Error()) + return tccommon.RetryError(e) + } + + return nil + }) + + if err != nil { + log.Printf("[CRITAL]%s update direct connect gateway failed, reason:%s\n", logId, err.Error()) + return err + } } return resourceTencentCloudDcGatewayRead(d, meta) @@ -171,23 +218,27 @@ func resourceTencentCloudDcGatewayUpdate(d *schema.ResourceData, meta interface{ func resourceTencentCloudDcGatewayDelete(d *schema.ResourceData, meta interface{}) error { defer tccommon.LogElapsed("resource.tencentcloud_dc_gateway.delete")() - logId := tccommon.GetLogId(tccommon.ContextNil) - ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) + var ( + logId = tccommon.GetLogId(tccommon.ContextNil) + request = vpc.NewDeleteDirectConnectGatewayRequest() + dcgId = d.Id() + ) - service := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} - err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { - _, has, e := service.DescribeDirectConnectGateway(ctx, d.Id()) + request.DirectConnectGatewayId = helper.String(dcgId) + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { + _, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseVpcClient().DeleteDirectConnectGateway(request) if e != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), e.Error()) return tccommon.RetryError(e) } - if has == 0 { - return nil - } return nil }) + if err != nil { + log.Printf("[CRITAL]%s delete direct connect gateway failed, reason:%s\n", logId, err.Error()) return err } - return service.DeleteDirectConnectGateway(ctx, d.Id()) + + return nil } diff --git a/tencentcloud/services/dcg/resource_tc_dc_gateway.md b/tencentcloud/services/dcg/resource_tc_dc_gateway.md index 82849848c5..6a3ad6bff7 100644 --- a/tencentcloud/services/dcg/resource_tc_dc_gateway.md +++ b/tencentcloud/services/dcg/resource_tc_dc_gateway.md @@ -2,17 +2,45 @@ Provides a resource to creating direct connect gateway instance. Example Usage +If network_type is VPC + ```hcl -resource "tencentcloud_vpc" "main" { - name = "ci-vpc-instance-test" +// create vpc +resource "tencentcloud_vpc" "vpc" { cidr_block = "10.0.0.0/16" + name = "vpc" } -resource "tencentcloud_dc_gateway" "vpc_main" { - name = "ci-cdg-vpc-test" - network_instance_id = tencentcloud_vpc.main.id +// create dc gateway +resource "tencentcloud_dc_gateway" "example" { + name = "tf-example" + network_instance_id = tencentcloud_vpc.vpc.id network_type = "VPC" - gateway_type = "NAT" + gateway_type = "NORMAL" +} +``` + +If network_type is CCN + +```hcl +// create ccn +resource "tencentcloud_ccn" "ccn" { + name = "tf-example" + description = "desc." + qos = "AG" + charge_type = "PREPAID" + bandwidth_limit_type = "INTER_REGION_LIMIT" + tags = { + createBy = "terraform" + } +} + +// create dc gateway +resource "tencentcloud_dc_gateway" "example" { + name = "tf-example" + network_instance_id = tencentcloud_ccn.ccn.id + network_type = "CCN" + gateway_type = "NORMAL" } ``` @@ -21,5 +49,5 @@ Import Direct connect gateway instance can be imported, e.g. ``` -$ terraform import tencentcloud_dc_gateway.instance dcg-id +$ terraform import tencentcloud_dc_gateway.example dcg-dr1y0hu7 ``` \ No newline at end of file diff --git a/website/docs/r/dc_gateway.html.markdown b/website/docs/r/dc_gateway.html.markdown index 5d80e12363..e4821c38af 100644 --- a/website/docs/r/dc_gateway.html.markdown +++ b/website/docs/r/dc_gateway.html.markdown @@ -13,17 +13,45 @@ Provides a resource to creating direct connect gateway instance. ## Example Usage +### If network_type is VPC + ```hcl -resource "tencentcloud_vpc" "main" { - name = "ci-vpc-instance-test" +// create vpc +resource "tencentcloud_vpc" "vpc" { cidr_block = "10.0.0.0/16" + name = "vpc" } -resource "tencentcloud_dc_gateway" "vpc_main" { - name = "ci-cdg-vpc-test" - network_instance_id = tencentcloud_vpc.main.id +// create dc gateway +resource "tencentcloud_dc_gateway" "example" { + name = "tf-example" + network_instance_id = tencentcloud_vpc.vpc.id network_type = "VPC" - gateway_type = "NAT" + gateway_type = "NORMAL" +} +``` + +### If network_type is CCN + +```hcl +// create ccn +resource "tencentcloud_ccn" "ccn" { + name = "tf-example" + description = "desc." + qos = "AG" + charge_type = "PREPAID" + bandwidth_limit_type = "INTER_REGION_LIMIT" + tags = { + createBy = "terraform" + } +} + +// create dc gateway +resource "tencentcloud_dc_gateway" "example" { + name = "tf-example" + network_instance_id = tencentcloud_ccn.ccn.id + network_type = "CCN" + gateway_type = "NORMAL" } ``` @@ -51,6 +79,6 @@ In addition to all arguments above, the following attributes are exported: Direct connect gateway instance can be imported, e.g. ``` -$ terraform import tencentcloud_dc_gateway.instance dcg-id +$ terraform import tencentcloud_dc_gateway.example dcg-dr1y0hu7 ``` From 3f023b99c12d31cdfe181fad2f9688c3a25c7c7e Mon Sep 17 00:00:00 2001 From: SevenEarth <391613297@qq.com> Date: Mon, 20 Jan 2025 18:15:10 +0800 Subject: [PATCH 2/2] add --- .changelog/3091.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/3091.txt diff --git a/.changelog/3091.txt b/.changelog/3091.txt new file mode 100644 index 0000000000..72aff9a923 --- /dev/null +++ b/.changelog/3091.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/tencentcloud_dc_gateway: update code +```