@@ -40,7 +40,7 @@ type API interface {
4040 GetDNSZones (ctx context.Context , publish types.PublishingStrategy ) ([]DNSZoneResponse , error )
4141 GetDNSInstancePermittedNetworks (ctx context.Context , dnsID string , dnsZone string ) ([]string , error )
4242 GetDNSCustomResolverIP (ctx context.Context , dnsID string , vpcID string ) (string , error )
43- CreateDNSRecord (ctx context.Context , crnstr string , baseDomain string , hostname string , cname string ) error
43+ CreateDNSRecord (ctx context.Context , publish types. PublishingStrategy , crnstr string , baseDomain string , hostname string , cname string ) error
4444
4545 // VPC
4646 GetVPCByName (ctx context.Context , vpcName string ) (* vpcv1.VPC , error )
@@ -416,8 +416,19 @@ func (c *Client) GetDNSInstancePermittedNetworks(ctx context.Context, dnsID stri
416416}
417417
418418// CreateDNSRecord Creates a DNS CNAME record in the given base domain and CRN.
419- func (c * Client ) CreateDNSRecord (ctx context.Context , crnstr string , baseDomain string , hostname string , cname string ) error {
420- logrus .Debugf ("CreateDNSRecord: crnstr = %s, hostname = %s, cname = %s" , crnstr , hostname , cname )
419+ func (c * Client ) CreateDNSRecord (ctx context.Context , publish types.PublishingStrategy , crnstr string , baseDomain string , hostname string , cname string ) error {
420+ switch publish {
421+ case types .InternalPublishingStrategy :
422+ return c .createPrivateDNSRecord (ctx , crnstr , baseDomain , hostname , cname )
423+ case types .ExternalPublishingStrategy :
424+ return c .createPublicDNSRecord (ctx , crnstr , baseDomain , hostname , cname )
425+ default :
426+ return fmt .Errorf ("publish strategy %q not supported" , publish )
427+ }
428+ }
429+
430+ func (c * Client ) createPublicDNSRecord (ctx context.Context , crnstr string , baseDomain string , hostname string , cname string ) error {
431+ logrus .Debugf ("createDNSRecord: crnstr = %s, hostname = %s, cname = %s" , crnstr , hostname , cname )
421432
422433 var (
423434 zoneID string
@@ -433,7 +444,7 @@ func (c *Client) CreateDNSRecord(ctx context.Context, crnstr string, baseDomain
433444 logrus .Errorf ("c.GetDNSZoneIDByName returns %v" , err )
434445 return err
435446 }
436- logrus .Debugf ("CreateDNSRecord : zoneID = %s" , zoneID )
447+ logrus .Debugf ("CreatePublicDNSRecord : zoneID = %s" , zoneID )
437448
438449 authenticator = & core.IamAuthenticator {
439450 ApiKey : c .APIKey ,
@@ -448,7 +459,7 @@ func (c *Client) CreateDNSRecord(ctx context.Context, crnstr string, baseDomain
448459 logrus .Errorf ("dnsrecordsv1.NewDnsRecordsV1 returns %v" , err )
449460 return err
450461 }
451- logrus .Debugf ("CreateDNSRecord : dnsRecordService = %+v" , dnsRecordService )
462+ logrus .Debugf ("CreatePublicDNSRecord : dnsRecordService = %+v" , dnsRecordService )
452463
453464 createOptions := dnsRecordService .NewCreateDnsRecordOptions ()
454465 createOptions .SetName (hostname )
@@ -460,7 +471,41 @@ func (c *Client) CreateDNSRecord(ctx context.Context, crnstr string, baseDomain
460471 logrus .Errorf ("dnsRecordService.CreateDnsRecord returns %v" , err )
461472 return err
462473 }
463- logrus .Debugf ("CreateDNSRecord: Result.ID = %v, RawResult = %v" , * result .Result .ID , response .RawResult )
474+ logrus .Debugf ("createPublicDNSRecord: Result.ID = %v, RawResult = %v" , * result .Result .ID , response .RawResult )
475+
476+ return nil
477+ }
478+
479+ func (c * Client ) createPrivateDNSRecord (ctx context.Context , crnstr string , baseDomain string , hostname string , cname string ) error {
480+ logrus .Debugf ("createPrivateDNSRecord: crnstr = %s, hostname = %s, cname = %s" , crnstr , hostname , cname )
481+
482+ zoneID , err := c .GetDNSZoneIDByName (ctx , baseDomain , types .InternalPublishingStrategy )
483+ if err != nil {
484+ logrus .Errorf ("c.GetDNSZoneIDByName returns %v" , err )
485+ return err
486+ }
487+ logrus .Debugf ("createPrivateDNSRecord: zoneID = %s" , zoneID )
488+
489+ dnsCRN , err := crn .Parse (crnstr )
490+ if err != nil {
491+ return fmt .Errorf ("failed to parse DNSInstanceCRN: %w" , err )
492+ }
493+
494+ rdataCnameRecord , err := c .dnsServicesAPI .NewResourceRecordInputRdataRdataCnameRecord (cname )
495+ if err != nil {
496+ return fmt .Errorf ("NewResourceRecordInputRdataRdataCnameRecord failed: %w" , err )
497+ }
498+ createOptions := c .dnsServicesAPI .NewCreateResourceRecordOptions (dnsCRN .ServiceInstance , zoneID )
499+ createOptions .SetRdata (rdataCnameRecord )
500+ createOptions .SetTTL (120 )
501+ createOptions .SetName (hostname )
502+ createOptions .SetType ("CNAME" )
503+ result , resp , err := c .dnsServicesAPI .CreateResourceRecord (createOptions )
504+ if err != nil {
505+ logrus .Errorf ("dnsRecordService.CreateResourceRecord returns %v" , err )
506+ return err
507+ }
508+ logrus .Debugf ("createPrivateDNSRecord: result.ID = %v, resp.RawResult = %v" , * result .ID , resp .RawResult )
464509
465510 return nil
466511}
0 commit comments