4
4
"context"
5
5
"fmt"
6
6
"log"
7
- "regexp"
8
7
"strings"
9
8
10
9
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
@@ -110,13 +109,14 @@ func resourceTencentCloudVpcFlowLogCreate(d *schema.ResourceData, meta interface
110
109
defer tccommon .LogElapsed ("resource.tencentcloud_vpc_flow_log.create" )()
111
110
defer tccommon .InconsistentCheck (d , meta )()
112
111
113
- logId := tccommon .GetLogId (tccommon .ContextNil )
114
-
115
112
var (
113
+ logId = tccommon .GetLogId (tccommon .ContextNil )
116
114
request = vpc .NewCreateFlowLogRequest ()
117
115
response = vpc .NewCreateFlowLogResponse ()
118
116
flowLogId string
117
+ vpcId string
119
118
)
119
+
120
120
if v , ok := d .GetOk ("flow_log_name" ); ok {
121
121
request .FlowLogName = helper .String (v .(string ))
122
122
}
@@ -135,6 +135,7 @@ func resourceTencentCloudVpcFlowLogCreate(d *schema.ResourceData, meta interface
135
135
136
136
if v , ok := d .GetOk ("vpc_id" ); ok {
137
137
request .VpcId = helper .String (v .(string ))
138
+ vpcId = v .(string )
138
139
}
139
140
140
141
if v , ok := d .GetOk ("flow_log_description" ); ok {
@@ -154,9 +155,11 @@ func resourceTencentCloudVpcFlowLogCreate(d *schema.ResourceData, meta interface
154
155
if v , ok := dMap ["storage_id" ]; ok {
155
156
flowLogStorage .StorageId = helper .String (v .(string ))
156
157
}
158
+
157
159
if v , ok := dMap ["storage_topic" ]; ok {
158
160
flowLogStorage .StorageTopic = helper .String (v .(string ))
159
161
}
162
+
160
163
request .FlowLogStorage = & flowLogStorage
161
164
}
162
165
@@ -171,9 +174,15 @@ func resourceTencentCloudVpcFlowLogCreate(d *schema.ResourceData, meta interface
171
174
} else {
172
175
log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
173
176
}
177
+
178
+ if result == nil || result .Response == nil {
179
+ return resource .NonRetryableError (fmt .Errorf ("Create vpc flowLog failed, Response is nil." ))
180
+ }
181
+
174
182
response = result
175
183
return nil
176
184
})
185
+
177
186
if err != nil {
178
187
log .Printf ("[CRITAL]%s create vpc flowLog failed, reason:%+v" , logId , err )
179
188
return err
@@ -184,8 +193,7 @@ func resourceTencentCloudVpcFlowLogCreate(d *schema.ResourceData, meta interface
184
193
}
185
194
186
195
flowLogId = * response .Response .FlowLog [0 ].FlowLogId
187
-
188
- resourceTencentCloudSetFlowLogId (d , flowLogId , d .Get ("vpc_id" ).(string ))
196
+ d .SetId (strings .Join ([]string {flowLogId , vpcId }, tccommon .FILED_SP ))
189
197
190
198
if tags := helper .GetTags (d , "tags" ); len (tags ) > 0 {
191
199
client := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()
@@ -205,19 +213,18 @@ func resourceTencentCloudVpcFlowLogRead(d *schema.ResourceData, meta interface{}
205
213
defer tccommon .LogElapsed ("resource.tencentcloud_vpc_flow_log.read" )()
206
214
defer tccommon .InconsistentCheck (d , meta )()
207
215
208
- logId := tccommon . GetLogId ( tccommon . ContextNil )
209
-
210
- ctx : = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
211
-
212
- service := svcvpc . NewVpcService ( meta .(tccommon. ProviderMeta ). GetAPIV3Conn () )
216
+ var (
217
+ logId = tccommon . GetLogId ( tccommon . ContextNil )
218
+ ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
219
+ service = svcvpc . NewVpcService ( meta .(tccommon. ProviderMeta ). GetAPIV3Conn ())
220
+ )
213
221
214
222
flowLogId , vpcId , err := resourceTencentCloudGetFlowLogId (d )
215
-
216
223
if err != nil {
217
224
return err
218
225
}
219
226
220
- flowLog , err := service .DescribeVpcFlowLogById (ctx , flowLogId , vpcId )
227
+ flowLog , err := service .DescribeVpcFlowLogsById (ctx , flowLogId , vpcId )
221
228
if err != nil {
222
229
return err
223
230
}
@@ -295,13 +302,13 @@ func resourceTencentCloudVpcFlowLogUpdate(d *schema.ResourceData, meta interface
295
302
defer tccommon .LogElapsed ("resource.tencentcloud_vpc_flow_log.update" )()
296
303
defer tccommon .InconsistentCheck (d , meta )()
297
304
298
- logId := tccommon .GetLogId (tccommon .ContextNil )
299
- ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
300
-
301
- request := vpc .NewModifyFlowLogAttributeRequest ()
305
+ var (
306
+ logId = tccommon .GetLogId (tccommon .ContextNil )
307
+ ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
308
+ request = vpc .NewModifyFlowLogAttributeRequest ()
309
+ )
302
310
303
311
flowLogId , vpcId , err := resourceTencentCloudGetFlowLogId (d )
304
-
305
312
if err != nil {
306
313
return err
307
314
}
@@ -335,14 +342,18 @@ func resourceTencentCloudVpcFlowLogUpdate(d *schema.ResourceData, meta interface
335
342
}
336
343
337
344
request .FlowLogId = & flowLogId
338
- request .VpcId = & vpcId
345
+ if vpcId != "" {
346
+ request .VpcId = & vpcId
347
+ }
348
+
339
349
err = resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
340
350
result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseVpcClient ().ModifyFlowLogAttribute (request )
341
351
if e != nil {
342
352
return tccommon .RetryError (e )
343
353
} else {
344
354
log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
345
355
}
356
+
346
357
return nil
347
358
})
348
359
@@ -372,12 +383,13 @@ func resourceTencentCloudVpcFlowLogDelete(d *schema.ResourceData, meta interface
372
383
defer tccommon .LogElapsed ("resource.tencentcloud_vpc_flow_log.delete" )()
373
384
defer tccommon .InconsistentCheck (d , meta )()
374
385
375
- logId := tccommon .GetLogId (tccommon .ContextNil )
376
- ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
386
+ var (
387
+ logId = tccommon .GetLogId (tccommon .ContextNil )
388
+ ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
389
+ service = svcvpc .NewVpcService (meta .(tccommon.ProviderMeta ).GetAPIV3Conn ())
390
+ )
377
391
378
- service := svcvpc .NewVpcService (meta .(tccommon.ProviderMeta ).GetAPIV3Conn ())
379
392
flowLogId , vpcId , err := resourceTencentCloudGetFlowLogId (d )
380
-
381
393
if err != nil {
382
394
return err
383
395
}
@@ -395,11 +407,6 @@ func resourceTencentCloudSetFlowLogId(d *schema.ResourceData, id, vpcId string)
395
407
396
408
func resourceTencentCloudGetFlowLogId (d * schema.ResourceData ) (id , vpcId string , err error ) {
397
409
rawId := d .Id ()
398
- rawIdRE := regexp .MustCompile (`^fl-[0-9a-z]{8}#vpc-[0-9a-z]{8}$` )
399
- if ! rawIdRE .MatchString (rawId ) {
400
- err = fmt .Errorf ("invalid id format %s, expect `fl-xxxxxxxx#vpc-xxxxxxxx`" , rawId )
401
- return
402
- }
403
410
ids := strings .Split (rawId , tccommon .FILED_SP )
404
411
id = ids [0 ]
405
412
vpcId = ids [1 ]
0 commit comments