44 "context"
55 "fmt"
66 "log"
7- "regexp"
87 "strings"
98
109 tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
@@ -110,13 +109,14 @@ func resourceTencentCloudVpcFlowLogCreate(d *schema.ResourceData, meta interface
110109 defer tccommon .LogElapsed ("resource.tencentcloud_vpc_flow_log.create" )()
111110 defer tccommon .InconsistentCheck (d , meta )()
112111
113- logId := tccommon .GetLogId (tccommon .ContextNil )
114-
115112 var (
113+ logId = tccommon .GetLogId (tccommon .ContextNil )
116114 request = vpc .NewCreateFlowLogRequest ()
117115 response = vpc .NewCreateFlowLogResponse ()
118116 flowLogId string
117+ vpcId string
119118 )
119+
120120 if v , ok := d .GetOk ("flow_log_name" ); ok {
121121 request .FlowLogName = helper .String (v .(string ))
122122 }
@@ -135,6 +135,7 @@ func resourceTencentCloudVpcFlowLogCreate(d *schema.ResourceData, meta interface
135135
136136 if v , ok := d .GetOk ("vpc_id" ); ok {
137137 request .VpcId = helper .String (v .(string ))
138+ vpcId = v .(string )
138139 }
139140
140141 if v , ok := d .GetOk ("flow_log_description" ); ok {
@@ -154,9 +155,11 @@ func resourceTencentCloudVpcFlowLogCreate(d *schema.ResourceData, meta interface
154155 if v , ok := dMap ["storage_id" ]; ok {
155156 flowLogStorage .StorageId = helper .String (v .(string ))
156157 }
158+
157159 if v , ok := dMap ["storage_topic" ]; ok {
158160 flowLogStorage .StorageTopic = helper .String (v .(string ))
159161 }
162+
160163 request .FlowLogStorage = & flowLogStorage
161164 }
162165
@@ -171,9 +174,15 @@ func resourceTencentCloudVpcFlowLogCreate(d *schema.ResourceData, meta interface
171174 } else {
172175 log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
173176 }
177+
178+ if result == nil || result .Response == nil {
179+ return resource .NonRetryableError (fmt .Errorf ("Create vpc flowLog failed, Response is nil." ))
180+ }
181+
174182 response = result
175183 return nil
176184 })
185+
177186 if err != nil {
178187 log .Printf ("[CRITAL]%s create vpc flowLog failed, reason:%+v" , logId , err )
179188 return err
@@ -184,8 +193,7 @@ func resourceTencentCloudVpcFlowLogCreate(d *schema.ResourceData, meta interface
184193 }
185194
186195 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 ))
189197
190198 if tags := helper .GetTags (d , "tags" ); len (tags ) > 0 {
191199 client := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()
@@ -205,19 +213,18 @@ func resourceTencentCloudVpcFlowLogRead(d *schema.ResourceData, meta interface{}
205213 defer tccommon .LogElapsed ("resource.tencentcloud_vpc_flow_log.read" )()
206214 defer tccommon .InconsistentCheck (d , meta )()
207215
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+ )
213221
214222 flowLogId , vpcId , err := resourceTencentCloudGetFlowLogId (d )
215-
216223 if err != nil {
217224 return err
218225 }
219226
220- flowLog , err := service .DescribeVpcFlowLogById (ctx , flowLogId , vpcId )
227+ flowLog , err := service .DescribeVpcFlowLogsById (ctx , flowLogId , vpcId )
221228 if err != nil {
222229 return err
223230 }
@@ -295,13 +302,13 @@ func resourceTencentCloudVpcFlowLogUpdate(d *schema.ResourceData, meta interface
295302 defer tccommon .LogElapsed ("resource.tencentcloud_vpc_flow_log.update" )()
296303 defer tccommon .InconsistentCheck (d , meta )()
297304
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+ )
302310
303311 flowLogId , vpcId , err := resourceTencentCloudGetFlowLogId (d )
304-
305312 if err != nil {
306313 return err
307314 }
@@ -335,14 +342,18 @@ func resourceTencentCloudVpcFlowLogUpdate(d *schema.ResourceData, meta interface
335342 }
336343
337344 request .FlowLogId = & flowLogId
338- request .VpcId = & vpcId
345+ if vpcId != "" {
346+ request .VpcId = & vpcId
347+ }
348+
339349 err = resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
340350 result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseVpcClient ().ModifyFlowLogAttribute (request )
341351 if e != nil {
342352 return tccommon .RetryError (e )
343353 } else {
344354 log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
345355 }
356+
346357 return nil
347358 })
348359
@@ -372,12 +383,13 @@ func resourceTencentCloudVpcFlowLogDelete(d *schema.ResourceData, meta interface
372383 defer tccommon .LogElapsed ("resource.tencentcloud_vpc_flow_log.delete" )()
373384 defer tccommon .InconsistentCheck (d , meta )()
374385
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+ )
377391
378- service := svcvpc .NewVpcService (meta .(tccommon.ProviderMeta ).GetAPIV3Conn ())
379392 flowLogId , vpcId , err := resourceTencentCloudGetFlowLogId (d )
380-
381393 if err != nil {
382394 return err
383395 }
@@ -395,11 +407,6 @@ func resourceTencentCloudSetFlowLogId(d *schema.ResourceData, id, vpcId string)
395407
396408func resourceTencentCloudGetFlowLogId (d * schema.ResourceData ) (id , vpcId string , err error ) {
397409 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- }
403410 ids := strings .Split (rawId , tccommon .FILED_SP )
404411 id = ids [0 ]
405412 vpcId = ids [1 ]
0 commit comments