diff --git a/commands/previewPush.go b/commands/previewPush.go index f1d188d2c..f02daecb8 100644 --- a/commands/previewPush.go +++ b/commands/previewPush.go @@ -15,6 +15,8 @@ import ( "github.com/StackExchange/dnscontrol/v3/pkg/notifications" "github.com/StackExchange/dnscontrol/v3/pkg/printer" "github.com/StackExchange/dnscontrol/v3/providers" + + "golang.org/x/exp/slices" ) var _ = cmd(catMain, func() *cli.Command { @@ -36,6 +38,7 @@ type PreviewArgs struct { FilterArgs Notify bool WarnChanges bool + NoPopulate bool } func (args *PreviewArgs) flags() []cli.Flag { @@ -52,6 +55,11 @@ func (args *PreviewArgs) flags() []cli.Flag { Destination: &args.WarnChanges, Usage: `set to true for non-zero return code if there are changes`, }) + flags = append(flags, &cli.BoolFlag{ + Name: "no-populate", + Destination: &args.NoPopulate, + Usage: `Use this flag to not auto-create non-existing zones at the provider`, + }) return flags } @@ -128,6 +136,27 @@ DomainLoop: domain.Nameservers = nsList nameservers.AddNSRecords(domain) for _, provider := range domain.DNSProviderInstances { + + if !args.NoPopulate { + // preview run: check if zone is already there, if not print a warning + if lister, ok := provider.Driver.(providers.ZoneLister); ok && !push { + zones, err := lister.ListZones() + if err != nil { + return err + } + if !slices.Contains(zones, domain.Name) { + out.Warnf("Domain '%s' does not exist in the '%s' profile and will be added automatically.\n", domain.Name, provider.Name) + continue // continue with next provider, as we can not determine corrections without an existing zone + } + } else if creator, ok := provider.Driver.(providers.DomainCreator); ok && push { + // this is the actual push, ensure domain exists at DSP + if err := creator.EnsureDomainExists(domain.Name); err != nil { + out.Warnf("Error creating domain: %s\n", err) + continue // continue with next provider, as we couldn't create this one + } + } + } + dc, err := domain.Copy() if err != nil { return err diff --git a/go.mod b/go.mod index fabfb2746..77142b364 100644 --- a/go.mod +++ b/go.mod @@ -60,7 +60,10 @@ require ( gopkg.in/yaml.v2 v2.4.0 ) -require github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 +require ( + github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 + golang.org/x/exp v0.0.0-20220602145555-4a0574d9293f +) require ( cloud.google.com/go/compute v1.6.1 // indirect diff --git a/go.sum b/go.sum index 0ede5f639..924c50cae 100644 --- a/go.sum +++ b/go.sum @@ -664,6 +664,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0 golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= +golang.org/x/exp v0.0.0-20220602145555-4a0574d9293f h1:KK6mxegmt5hGJRcAnEDjSNLxIRhZxDcgwMbcO/lMCRM= +golang.org/x/exp v0.0.0-20220602145555-4a0574d9293f/go.mod h1:yh0Ynu2b5ZUe3MQfp2nM0ecK7wsgouWTDN0FNeJuIys= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= diff --git a/providers/powerdns/powerdnsProvider.go b/providers/powerdns/powerdnsProvider.go index d710f8ed4..742c0258f 100644 --- a/providers/powerdns/powerdnsProvider.go +++ b/providers/powerdns/powerdnsProvider.go @@ -113,7 +113,7 @@ func (api *powerdnsProvider) ListZones() ([]string, error) { return result, err } for _, zone := range myZones { - result = append(result, zone.Name) + result = append(result, strings.TrimSuffix(zone.Name, ".")) } return result, nil }