feat: domain CRUD complete with Gandi provider

This commit is contained in:
decentral1se 2021-10-20 16:52:19 +02:00
parent da8d72620a
commit 02d24104e1
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
3 changed files with 130 additions and 1 deletions

View File

@ -30,7 +30,7 @@ This command creates a new domain name record for a specific zone.
You must specify a zone (e.g. example.com) under which your domain name records You must specify a zone (e.g. example.com) under which your domain name records
are listed. This zone must already be created on your provider account. are listed. This zone must already be created on your provider account.
Examples: Example:
abra domain create -p gandi foo.com A myapp 192.168.178.44 abra domain create -p gandi foo.com A myapp 192.168.178.44
@ -128,6 +128,7 @@ Which means you can then deploy an app against "myapp.foo.com" successfully.
strconv.Itoa(createdRecord.Priority), strconv.Itoa(createdRecord.Priority),
}) })
table.SetCaption(true, "record created")
table.Render() table.Render()
return nil return nil

View File

@ -33,5 +33,6 @@ allows to implement new provider support easily.
Subcommands: []*cli.Command{ Subcommands: []*cli.Command{
DomainListCommand, DomainListCommand,
DomainCreateCommand, DomainCreateCommand,
DomainRemoveCommand,
}, },
} }

127
cli/domain/remove.go Normal file
View File

@ -0,0 +1,127 @@
package domain
import (
"errors"
"fmt"
"strconv"
abraFormatter "coopcloud.tech/abra/cli/formatter"
"coopcloud.tech/abra/cli/internal"
gandiPkg "coopcloud.tech/abra/pkg/dns/gandi"
"github.com/AlecAivazis/survey/v2"
"github.com/libdns/gandi"
"github.com/libdns/libdns"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
// DomainRemoveCommand lists domains.
var DomainRemoveCommand = &cli.Command{
Name: "remove",
Usage: "Remove domain name records for a zone",
Aliases: []string{"rm"},
ArgsUsage: "<zone> <type> <name>",
Flags: []cli.Flag{
internal.DNSProviderFlag,
},
Description: `
This command removes a new domain name record for a specific zone.
You must specify a zone (e.g. example.com) under which your domain name records
are listed. This zone must already be created on your provider account.
The "<id>" can be retrieved by running "abra domain list <zone>". This can be
used then in this command for deletion of the record.
Example:
abra domain remove -p gandi foo.com A myapp
Which means that the domain name record of type A for myapp.foo.com will be deleted.
`,
Action: func(c *cli.Context) error {
zone := c.Args().First()
if zone == "" {
internal.ShowSubcommandHelpAndError(c, errors.New("no zone provided"))
}
recordType := c.Args().Get(1)
recordName := c.Args().Get(2)
if recordType == "" {
internal.ShowSubcommandHelpAndError(c, errors.New("no type provided"))
}
if recordName == "" {
internal.ShowSubcommandHelpAndError(c, errors.New("no name provided"))
}
var err error
var provider gandi.Provider
switch internal.DNSProvider {
case "gandi":
provider, err = gandiPkg.New()
if err != nil {
logrus.Fatal(err)
}
default:
logrus.Fatalf("'%s' is not a supported DNS provider", internal.DNSProvider)
}
records, err := provider.GetRecords(c.Context, zone)
if err != nil {
logrus.Fatal(err)
}
var toDelete libdns.Record
for _, record := range records {
if record.Type == recordType && record.Name == recordName {
toDelete = record
break
}
}
if (libdns.Record{}) == toDelete {
logrus.Fatal("provider library reports no matching record?")
}
tableCol := []string{"type", "name", "value", "TTL", "priority"}
table := abraFormatter.CreateTable(tableCol)
value := toDelete.Value
if len(toDelete.Value) > 30 {
value = fmt.Sprintf("%s...", toDelete.Value[:30])
}
table.Append([]string{
toDelete.Type,
toDelete.Name,
value,
toDelete.TTL.String(),
strconv.Itoa(toDelete.Priority),
})
table.Render()
response := false
prompt := &survey.Confirm{
Message: "continue with record deletion?",
}
if err := survey.AskOne(prompt, &response); err != nil {
return err
}
if !response {
logrus.Fatal("exiting as requested")
}
_, err = provider.DeleteRecords(c.Context, zone, []libdns.Record{toDelete})
if err != nil {
logrus.Fatal(err)
}
logrus.Info("record successfully deleted")
return nil
},
}