@@ -3,10 +3,12 @@ package dcg
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "log"
6
7
"strings"
7
- "time"
8
8
9
+ vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
9
10
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
11
+ "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
10
12
11
13
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12
14
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -21,7 +23,6 @@ func ResourceTencentCloudDcGatewayInstance() *schema.Resource {
21
23
Importer : & schema.ResourceImporter {
22
24
State : schema .ImportStatePassthrough ,
23
25
},
24
-
25
26
Schema : map [string ]* schema.Schema {
26
27
"name" : {
27
28
Type : schema .TypeString ,
@@ -74,59 +75,84 @@ func ResourceTencentCloudDcGatewayInstance() *schema.Resource {
74
75
func resourceTencentCloudDcGatewayCreate (d * schema.ResourceData , meta interface {}) error {
75
76
defer tccommon .LogElapsed ("resource.tencentcloud_dc_gateway.create" )()
76
77
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
-
82
78
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
87
85
)
88
86
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
+ }
91
90
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 )
94
94
}
95
95
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
+ }
98
100
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 )
101
104
}
102
105
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
+ }
104
109
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 )
108
112
}
109
113
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
+
111
133
if err != nil {
134
+ log .Printf ("[CRITAL]%s create direct connect gateway failed, reason:%s\n " , logId , err .Error ())
112
135
return err
113
136
}
114
137
115
- d .SetId (dcgId )
138
+ if response .Response .DirectConnectGateway .DirectConnectGatewayId == nil {
139
+ return fmt .Errorf ("DirectConnectGatewayId is nil." )
140
+ }
116
141
117
- // add sleep protect, either network_instance_id will be set "".
118
- time .Sleep (1 * time .Second )
142
+ d .SetId (* response .Response .DirectConnectGateway .DirectConnectGatewayId )
119
143
120
144
return resourceTencentCloudDcGatewayRead (d , meta )
121
145
}
122
146
123
147
func resourceTencentCloudDcGatewayRead (d * schema.ResourceData , meta interface {}) error {
124
148
defer tccommon .LogElapsed ("resource.tencentcloud_dc_gateway.read" )()
125
149
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
+ )
128
155
129
- service := VpcService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
130
156
err := resource .Retry (tccommon .ReadRetryTimeout , func () * resource.RetryError {
131
157
info , has , e := service .DescribeDirectConnectGateway (ctx , d .Id ())
132
158
if e != nil {
@@ -147,22 +173,43 @@ func resourceTencentCloudDcGatewayRead(d *schema.ResourceData, meta interface{})
147
173
_ = d .Set ("create_time" , info .createTime )
148
174
return nil
149
175
})
176
+
150
177
if err != nil {
151
178
return err
152
179
}
180
+
153
181
return nil
154
182
}
155
183
156
184
func resourceTencentCloudDcGatewayUpdate (d * schema.ResourceData , meta interface {}) error {
157
185
defer tccommon .LogElapsed ("resource.tencentcloud_dc_gateway.update" )()
158
186
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
+ )
161
191
162
- service := VpcService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
163
192
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
+ }
166
213
}
167
214
168
215
return resourceTencentCloudDcGatewayRead (d , meta )
@@ -171,23 +218,27 @@ func resourceTencentCloudDcGatewayUpdate(d *schema.ResourceData, meta interface{
171
218
func resourceTencentCloudDcGatewayDelete (d * schema.ResourceData , meta interface {}) error {
172
219
defer tccommon .LogElapsed ("resource.tencentcloud_dc_gateway.delete" )()
173
220
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
+ )
176
226
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 )
180
230
if e != nil {
231
+ log .Printf ("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n " , logId , request .GetAction (), request .ToJsonString (), e .Error ())
181
232
return tccommon .RetryError (e )
182
233
}
183
234
184
- if has == 0 {
185
- return nil
186
- }
187
235
return nil
188
236
})
237
+
189
238
if err != nil {
239
+ log .Printf ("[CRITAL]%s delete direct connect gateway failed, reason:%s\n " , logId , err .Error ())
190
240
return err
191
241
}
192
- return service .DeleteDirectConnectGateway (ctx , d .Id ())
242
+
243
+ return nil
193
244
}
0 commit comments