@@ -378,6 +378,7 @@ func resourceTencentCloudVpnConnectionCreate(d *schema.ResourceData, meta interf
378378 if err != nil {
379379 return err
380380 }
381+
381382 if ! has {
382383 return fmt .Errorf ("[CRITAL] vpn_gateway_id %s doesn't exist" , d .Get ("vpn_gateway_id" ).(string ))
383384 }
@@ -396,16 +397,19 @@ func resourceTencentCloudVpnConnectionCreate(d *schema.ResourceData, meta interf
396397 }
397398 request .VpcId = helper .String ("" )
398399 }
400+
399401 request .VpnGatewayId = helper .String (d .Get ("vpn_gateway_id" ).(string ))
400402 request .CustomerGatewayId = helper .String (d .Get ("customer_gateway_id" ).(string ))
401403 request .PreShareKey = helper .String (d .Get ("pre_share_key" ).(string ))
402404 if v , ok := d .GetOk ("dpd_enable" ); ok {
403405 dpdEnable := v .(int )
404406 request .DpdEnable = helper .IntInt64 (dpdEnable )
405407 }
408+
406409 if v , ok := d .GetOk ("dpd_action" ); ok {
407410 request .DpdAction = helper .String (v .(string ))
408411 }
412+
409413 if v , ok := d .GetOk ("dpd_timeout" ); ok {
410414 request .DpdTimeout = helper .String (strconv .Itoa (v .(int )))
411415 }
@@ -418,22 +422,26 @@ func resourceTencentCloudVpnConnectionCreate(d *schema.ResourceData, meta interf
418422 request .NegotiationType = helper .String (v .(string ))
419423 }
420424
421- //set up SecurityPolicyDatabases
425+ //set up SecurityPolicyDatabases
422426 if v , ok := d .GetOk ("security_group_policy" ); ok {
423- sgps := v .(* schema.Set ).List ()
424- request .SecurityPolicyDatabases = make ([]* vpc.SecurityPolicyDatabase , 0 , len (sgps ))
425- for _ , v := range sgps {
426- m := v .(map [string ]interface {})
427- var sgp vpc.SecurityPolicyDatabase
428- local := m ["local_cidr_block" ].(string )
429- sgp .LocalCidrBlock = & local
430- // list
431- remoteCidrBlocks := m ["remote_cidr_block" ].(* schema.Set ).List ()
432- for _ , vv := range remoteCidrBlocks {
433- remoteCidrBlock := vv .(string )
434- sgp .RemoteCidrBlock = append (sgp .RemoteCidrBlock , & remoteCidrBlock )
427+ for _ , item := range v .(* schema.Set ).List () {
428+ if dMap , ok := item .(map [string ]interface {}); ok && dMap != nil {
429+ var sgp vpc.SecurityPolicyDatabase
430+ if v , ok := dMap ["local_cidr_block" ].(string ); ok && v != "" {
431+ sgp .LocalCidrBlock = & v
432+ }
433+
434+ if v , ok := dMap ["remote_cidr_block" ].(* schema.Set ); ok {
435+ remoteCidrBlocks := v .List ()
436+ for _ , rcb := range remoteCidrBlocks {
437+ if v , ok := rcb .(string ); ok && v != "" {
438+ sgp .RemoteCidrBlock = append (sgp .RemoteCidrBlock , & v )
439+ }
440+ }
441+ }
442+
443+ request .SecurityPolicyDatabases = append (request .SecurityPolicyDatabases , & sgp )
435444 }
436- request .SecurityPolicyDatabases = append (request .SecurityPolicyDatabases , & sgp )
437445 }
438446 }
439447
@@ -457,6 +465,7 @@ func resourceTencentCloudVpnConnectionCreate(d *schema.ResourceData, meta interf
457465 return fmt .Errorf ("ike_local_fqdn_name need to be set when ike_local_identity is `FQDN`" )
458466 }
459467 }
468+
460469 if * ikeOptionsSpecification .LocalIdentity == svcvpc .VPN_IKE_IDENTITY_ADDRESS {
461470 if v , ok := d .GetOk ("ike_remote_address" ); ok {
462471 ikeOptionsSpecification .RemoteAddress = helper .String (v .(string ))
@@ -493,9 +502,11 @@ func resourceTencentCloudVpnConnectionCreate(d *schema.ResourceData, meta interf
493502 if v , ok := d .GetOk ("enable_health_check" ); ok {
494503 request .EnableHealthCheck = helper .Bool (v .(bool ))
495504 }
505+
496506 if v , ok := d .GetOk ("health_check_local_ip" ); ok {
497507 request .HealthCheckLocalIp = helper .String (v .(string ))
498508 }
509+
499510 if v , ok := d .GetOk ("health_check_remote_ip" ); ok {
500511 request .HealthCheckRemoteIp = helper .String (v .(string ))
501512 }
@@ -564,20 +575,27 @@ func resourceTencentCloudVpnConnectionCreate(d *schema.ResourceData, meta interf
564575 err = resource .Retry (tccommon .ReadRetryTimeout , func () * resource.RetryError {
565576 result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseVpcClient ().CreateVpnConnection (request )
566577 if e != nil {
567- log .Printf ("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n " ,
568- logId , request .GetAction (), request .ToJsonString (), e .Error ())
578+ log .Printf ("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n " , logId , request .GetAction (), request .ToJsonString (), e .Error ())
569579 return tccommon .RetryError (e )
580+ } else {
581+ log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
570582 }
583+
584+ if result == nil || result .Response == nil {
585+ return resource .NonRetryableError (fmt .Errorf ("Create VPN connection failed, Response is nil." ))
586+ }
587+
571588 response = result
572589 return nil
573590 })
591+
574592 if err != nil {
575593 log .Printf ("[CRITAL]%s create VPN connection failed, reason:%s\n " , logId , err .Error ())
576594 return err
577595 }
578596
579597 if response .Response .VpnConnection == nil {
580- return fmt .Errorf ("VPN connection id is nil" )
598+ return fmt .Errorf ("VpnConnection is nil. " )
581599 }
582600
583601 vpnConnectionId := ""
@@ -589,28 +607,31 @@ func resourceTencentCloudVpnConnectionCreate(d *schema.ResourceData, meta interf
589607 if v , ok := d .GetOk ("vpn_gateway_id" ); ok {
590608 params ["vpn-gateway-id" ] = v .(string )
591609 }
610+
592611 if v , ok := d .GetOk ("vpc_id" ); ok && * gateway .Type != "CCN" {
593612 params ["vpc-id" ] = v .(string )
594613 }
614+
595615 if v , ok := d .GetOk ("customer_gateway_id" ); ok {
596616 params ["customer-gateway-id" ] = v .(string )
597617 }
618+
598619 for k , v := range params {
599620 filter := & vpc.Filter {
600621 Name : helper .String (k ),
601622 Values : []* string {helper .String (v )},
602623 }
624+
603625 idRequest .Filters = append (idRequest .Filters , filter )
604626 }
627+
605628 offset := uint64 (0 )
606629 idRequest .Offset = & offset
607630
608631 err = resource .Retry (tccommon .ReadRetryTimeout , func () * resource.RetryError {
609632 result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseVpcClient ().DescribeVpnConnections (idRequest )
610-
611633 if e != nil {
612- log .Printf ("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n " ,
613- logId , idRequest .GetAction (), idRequest .ToJsonString (), e .Error ())
634+ log .Printf ("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n " , logId , idRequest .GetAction (), idRequest .ToJsonString (), e .Error ())
614635 return tccommon .RetryError (e , tccommon .InternalError )
615636 } else {
616637 if len (result .Response .VpnConnectionSet ) == 0 || * result .Response .VpnConnectionSet [0 ].VpnConnectionId == "" {
@@ -629,7 +650,7 @@ func resourceTencentCloudVpnConnectionCreate(d *schema.ResourceData, meta interf
629650 }
630651
631652 if vpnConnectionId == "" {
632- return fmt .Errorf ("VPN connection id is nil" )
653+ return fmt .Errorf ("VPN connection id is nil. " )
633654 }
634655
635656 d .SetId (vpnConnectionId )
0 commit comments