@@ -3,10 +3,12 @@ package dcg
33import (
44 "context"
55 "fmt"
6+ "log"
67 "strings"
7- "time"
88
9+ vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
910 tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
11+ "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
1012
1113 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1214 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -21,7 +23,6 @@ func ResourceTencentCloudDcGatewayInstance() *schema.Resource {
2123 Importer : & schema.ResourceImporter {
2224 State : schema .ImportStatePassthrough ,
2325 },
24-
2526 Schema : map [string ]* schema.Schema {
2627 "name" : {
2728 Type : schema .TypeString ,
@@ -74,59 +75,84 @@ func ResourceTencentCloudDcGatewayInstance() *schema.Resource {
7475func resourceTencentCloudDcGatewayCreate (d * schema.ResourceData , meta interface {}) error {
7576 defer tccommon .LogElapsed ("resource.tencentcloud_dc_gateway.create" )()
7677
77- logId := tccommon .GetLogId (tccommon .ContextNil )
78- ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
79-
80- service := VpcService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
81-
8278 var (
83- name = d .Get ("name" ).(string )
84- networkType = d .Get ("network_type" ).(string )
85- networkInstanceId = d .Get ("network_instance_id" ).(string )
86- gatewayType = d .Get ("gateway_type" ).(string )
79+ logId = tccommon .GetLogId (tccommon .ContextNil )
80+ request = vpc .NewCreateDirectConnectGatewayRequest ()
81+ response = vpc .NewCreateDirectConnectGatewayResponse ()
82+ networkType string
83+ networkInstanceId string
84+ gatewayType string
8785 )
8886
89- if networkType == DCG_NETWORK_TYPE_VPC &&
90- ! strings .HasPrefix (networkInstanceId , "vpc" ) {
87+ if v , ok := d .GetOk ("name" ); ok {
88+ request .DirectConnectGatewayName = helper .String (v .(string ))
89+ }
9190
92- return fmt .Errorf ("if `network_type` is '%s', the field `network_instance_id` must be a VPC resource" ,
93- DCG_NETWORK_TYPE_VPC )
91+ if v , ok := d .GetOk ("network_type" ); ok {
92+ request .NetworkType = helper .String (v .(string ))
93+ networkType = v .(string )
9494 }
9595
96- if networkType == DCG_NETWORK_TYPE_CCN &&
97- ! strings .HasPrefix (networkInstanceId , "ccn" ) {
96+ if v , ok := d .GetOk ("network_instance_id" ); ok {
97+ request .NetworkInstanceId = helper .String (v .(string ))
98+ networkInstanceId = v .(string )
99+ }
98100
99- return fmt .Errorf ("if `network_type` is '%s', the field `network_instance_id` must be a CCN resource" ,
100- DCG_NETWORK_TYPE_CCN )
101+ if v , ok := d .GetOk ("gateway_type" ); ok {
102+ request .GatewayType = helper .String (v .(string ))
103+ gatewayType = v .(string )
101104 }
102105
103- if networkType == DCG_NETWORK_TYPE_CCN && gatewayType != DCG_GATEWAY_TYPE_NORMAL {
106+ if networkType == DCG_NETWORK_TYPE_VPC && ! strings .HasPrefix (networkInstanceId , "vpc" ) {
107+ return fmt .Errorf ("if `network_type` is '%s', the field `network_instance_id` must be a VPC resource" , DCG_NETWORK_TYPE_VPC )
108+ }
104109
105- return fmt .Errorf ("if `network_type` is '%s', the field `gateway_type` must be '%s'" ,
106- DCG_NETWORK_TYPE_CCN ,
107- DCG_GATEWAY_TYPE_NORMAL )
110+ if networkType == DCG_NETWORK_TYPE_CCN && ! strings .HasPrefix (networkInstanceId , "ccn" ) {
111+ return fmt .Errorf ("if `network_type` is '%s', the field `network_instance_id` must be a CCN resource" , DCG_NETWORK_TYPE_CCN )
108112 }
109113
110- dcgId , err := service .CreateDirectConnectGateway (ctx , name , networkType , networkInstanceId , gatewayType )
114+ if networkType == DCG_NETWORK_TYPE_CCN && gatewayType != DCG_GATEWAY_TYPE_NORMAL {
115+ return fmt .Errorf ("if `network_type` is '%s', the field `gateway_type` must be '%s'" , DCG_NETWORK_TYPE_CCN , DCG_GATEWAY_TYPE_NORMAL )
116+ }
117+
118+ err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
119+ result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseVpcClient ().CreateDirectConnectGateway (request )
120+ if e != nil {
121+ log .Printf ("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n " , logId , request .GetAction (), request .ToJsonString (), e .Error ())
122+ return tccommon .RetryError (e )
123+ }
124+
125+ if result == nil || result .Response == nil || result .Response .DirectConnectGateway == nil {
126+ return resource .NonRetryableError (fmt .Errorf ("Create direct connect gateway failed, Response is nil." ))
127+ }
128+
129+ response = result
130+ return nil
131+ })
132+
111133 if err != nil {
134+ log .Printf ("[CRITAL]%s create direct connect gateway failed, reason:%s\n " , logId , err .Error ())
112135 return err
113136 }
114137
115- d .SetId (dcgId )
138+ if response .Response .DirectConnectGateway .DirectConnectGatewayId == nil {
139+ return fmt .Errorf ("DirectConnectGatewayId is nil." )
140+ }
116141
117- // add sleep protect, either network_instance_id will be set "".
118- time .Sleep (1 * time .Second )
142+ d .SetId (* response .Response .DirectConnectGateway .DirectConnectGatewayId )
119143
120144 return resourceTencentCloudDcGatewayRead (d , meta )
121145}
122146
123147func resourceTencentCloudDcGatewayRead (d * schema.ResourceData , meta interface {}) error {
124148 defer tccommon .LogElapsed ("resource.tencentcloud_dc_gateway.read" )()
125149
126- logId := tccommon .GetLogId (tccommon .ContextNil )
127- ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
150+ var (
151+ logId = tccommon .GetLogId (tccommon .ContextNil )
152+ ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
153+ service = VpcService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
154+ )
128155
129- service := VpcService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
130156 err := resource .Retry (tccommon .ReadRetryTimeout , func () * resource.RetryError {
131157 info , has , e := service .DescribeDirectConnectGateway (ctx , d .Id ())
132158 if e != nil {
@@ -147,22 +173,43 @@ func resourceTencentCloudDcGatewayRead(d *schema.ResourceData, meta interface{})
147173 _ = d .Set ("create_time" , info .createTime )
148174 return nil
149175 })
176+
150177 if err != nil {
151178 return err
152179 }
180+
153181 return nil
154182}
155183
156184func resourceTencentCloudDcGatewayUpdate (d * schema.ResourceData , meta interface {}) error {
157185 defer tccommon .LogElapsed ("resource.tencentcloud_dc_gateway.update" )()
158186
159- logId := tccommon .GetLogId (tccommon .ContextNil )
160- ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
187+ var (
188+ logId = tccommon .GetLogId (tccommon .ContextNil )
189+ dcgId = d .Id ()
190+ )
161191
162- service := VpcService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
163192 if d .HasChange ("name" ) {
164- var name = d .Get ("name" ).(string )
165- return service .ModifyDirectConnectGatewayAttribute (ctx , d .Id (), name )
193+ request := vpc .NewModifyDirectConnectGatewayAttributeRequest ()
194+ request .DirectConnectGatewayId = helper .String (dcgId )
195+ if v , ok := d .GetOk ("name" ); ok {
196+ request .DirectConnectGatewayName = helper .String (v .(string ))
197+ }
198+
199+ err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
200+ _ , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseVpcClient ().ModifyDirectConnectGatewayAttribute (request )
201+ if e != nil {
202+ log .Printf ("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n " , logId , request .GetAction (), request .ToJsonString (), e .Error ())
203+ return tccommon .RetryError (e )
204+ }
205+
206+ return nil
207+ })
208+
209+ if err != nil {
210+ log .Printf ("[CRITAL]%s update direct connect gateway failed, reason:%s\n " , logId , err .Error ())
211+ return err
212+ }
166213 }
167214
168215 return resourceTencentCloudDcGatewayRead (d , meta )
@@ -171,23 +218,27 @@ func resourceTencentCloudDcGatewayUpdate(d *schema.ResourceData, meta interface{
171218func resourceTencentCloudDcGatewayDelete (d * schema.ResourceData , meta interface {}) error {
172219 defer tccommon .LogElapsed ("resource.tencentcloud_dc_gateway.delete" )()
173220
174- logId := tccommon .GetLogId (tccommon .ContextNil )
175- ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
221+ var (
222+ logId = tccommon .GetLogId (tccommon .ContextNil )
223+ request = vpc .NewDeleteDirectConnectGatewayRequest ()
224+ dcgId = d .Id ()
225+ )
176226
177- service := VpcService { client : meta .(tccommon. ProviderMeta ). GetAPIV3Conn ()}
178- err := resource .Retry (tccommon .ReadRetryTimeout , func () * resource.RetryError {
179- _ , has , e := service . DescribeDirectConnectGateway ( ctx , d . Id () )
227+ request . DirectConnectGatewayId = helper . String ( dcgId )
228+ err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
229+ _ , e := meta .(tccommon. ProviderMeta ). GetAPIV3Conn (). UseVpcClient (). DeleteDirectConnectGateway ( request )
180230 if e != nil {
231+ log .Printf ("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n " , logId , request .GetAction (), request .ToJsonString (), e .Error ())
181232 return tccommon .RetryError (e )
182233 }
183234
184- if has == 0 {
185- return nil
186- }
187235 return nil
188236 })
237+
189238 if err != nil {
239+ log .Printf ("[CRITAL]%s delete direct connect gateway failed, reason:%s\n " , logId , err .Error ())
190240 return err
191241 }
192- return service .DeleteDirectConnectGateway (ctx , d .Id ())
242+
243+ return nil
193244}
0 commit comments