WIP: domain listing with Gandi

Rethinking the interface already.
This commit is contained in:
decentral1se 2021-10-18 22:16:29 +02:00
parent 7ea3df45d4
commit 597b4b586e
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
10 changed files with 161 additions and 145 deletions

View File

@ -8,6 +8,7 @@ import (
"coopcloud.tech/abra/cli/app"
"coopcloud.tech/abra/cli/catalogue"
"coopcloud.tech/abra/cli/domain"
"coopcloud.tech/abra/cli/recipe"
"coopcloud.tech/abra/cli/server"
"coopcloud.tech/abra/pkg/config"
@ -59,6 +60,7 @@ func RunApp(version, commit string) {
server.ServerCommand,
recipe.RecipeCommand,
catalogue.CatalogueCommand,
domain.DomainCommand,
VersionCommand,
UpgradeCommand,
},

32
cli/domain/domain.go Normal file
View File

@ -0,0 +1,32 @@
package domain
import (
"github.com/urfave/cli/v2"
)
// DomainCommand supports managing DNS entries.
var DomainCommand = &cli.Command{
Name: "domain",
Usage: "Manage domains via 3rd party providers",
Aliases: []string{"d"},
ArgsUsage: "<domain>",
Description: `
This command supports managing DNS records via 3rd party providers such as
Gandi DNS. It supports listing, creating, updating and removing all types of
DNS records that you might need to manage for managing Co-op Cloud apps.
The following providers are supported:
Gandi DNS https://www.gandi.net
Any new provider can be integrated, we welcome change sets. See the underlying
DNS library documentation for more. It supports many existing providers and
allows to implement new provider support easily.
https://pkg.go.dev/github.com/libdns/libdns
`,
Subcommands: []*cli.Command{
DomainListCommand,
},
}

70
cli/domain/list.go Normal file
View File

@ -0,0 +1,70 @@
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/libdns/gandi"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
// DomainListCommand lists domains.
var DomainListCommand = &cli.Command{
Name: "list",
Usage: "List domains for a server",
Aliases: []string{"ls"},
ArgsUsage: "<zone>",
Flags: []cli.Flag{
internal.DNSProviderFlag,
},
Description: `
`,
Action: func(c *cli.Context) error {
zone := c.Args().First()
if zone == "" {
internal.ShowSubcommandHelpAndError(c, errors.New("no zone provided"))
}
var err error
var provider gandi.Provider
switch internal.DNSProvider {
case "gandi":
provider, err = gandiPkg.New()
if err != nil {
logrus.Fatal(err)
}
}
records, err := provider.GetRecords(c.Context, zone)
if err != nil {
logrus.Fatal(err)
}
tableCol := []string{"type", "name", "value", "TTL", "priority"}
table := abraFormatter.CreateTable(tableCol)
for _, record := range records {
value := record.Value
if len(record.Value) > 30 {
value = fmt.Sprintf("%s...", record.Value[:30])
}
table.Append([]string{
record.Type,
record.Name,
value,
record.TTL.String(),
strconv.Itoa(record.Priority),
})
}
table.Render()
return nil
},
}

View File

@ -61,3 +61,16 @@ var ChaosFlag = &cli.BoolFlag{
Usage: "Deploy uncommitted recipes changes. Use with care!",
Destination: &Chaos,
}
// DNSProvider specifies a DNS provider.
var DNSProvider string
// DNSProviderFlag selects a DNS provider.
var DNSProviderFlag = &cli.StringFlag{
Name: "provider",
Value: "",
Aliases: []string{"p"},
Usage: "DNS provider",
Destination: &DNSProvider,
Required: true,
}

View File

@ -1,91 +0,0 @@
package server
import (
"github.com/urfave/cli/v2"
)
var provider string
var providerFlag = &cli.StringFlag{
Name: "provider",
Value: "",
Aliases: []string{"p"},
Usage: "Choose a DNS provider (options: Gandi)",
Destination: &provider,
}
var serverDnsListCommand = &cli.Command{
Name: "list",
Usage: "List domains for a server",
Aliases: []string{"ls"},
ArgsUsage: "<domain>",
Flags: []cli.Flag{
providerFlag,
},
Description: `
`,
Action: func(c *cli.Context) error {
// domainName := internal.ValidateDomain(c)
return nil
},
}
var serverDnsRemoveCommand = &cli.Command{
Name: "remove",
Usage: "Remove domains for a server",
Aliases: []string{"rm"},
ArgsUsage: "<domain>",
Flags: []cli.Flag{
providerFlag,
},
Description: `
`,
Action: func(c *cli.Context) error {
// domainName := internal.ValidateDomain(c)
return nil
},
}
var serverDnsAddCommand = &cli.Command{
Name: "add",
Usage: "Add domains for a server",
Aliases: []string{"a"},
ArgsUsage: "<domain>",
Flags: []cli.Flag{
providerFlag,
},
Description: `
`,
Action: func(c *cli.Context) error {
// domainName := internal.ValidateDomain(c)
return nil
},
}
var serverDnsUpdateCommand = &cli.Command{
Name: "update",
Usage: "Update domains for a server",
Aliases: []string{"u"},
ArgsUsage: "<domain>",
Flags: []cli.Flag{
providerFlag,
},
Description: `
`,
Action: func(c *cli.Context) error {
// domainName := internal.ValidateDomain(c)
return nil
},
}
var serverDnsCommand = &cli.Command{
Name: "dns",
Aliases: []string{"d"},
Usage: "Manage server domains",
ArgsUsage: "<command>",
Subcommands: []*cli.Command{
serverDnsListCommand,
serverDnsRemoveCommand,
serverDnsAddCommand,
serverDnsUpdateCommand,
},
}

View File

@ -22,6 +22,5 @@ the connections to those servers.
serverAddCommand,
serverListCommand,
serverRemoveCommand,
serverDnsCommand,
},
}

2
go.mod
View File

@ -33,7 +33,7 @@ require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/libdns/gandi v1.0.2
github.com/libdns/libdns v0.2.1
github.com/libdns/libdns v0.2.1 // indirect
github.com/moby/sys/mount v0.2.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/runc v1.0.2 // indirect

28
pkg/dns/common.go Normal file
View File

@ -0,0 +1,28 @@
package dns
import (
"fmt"
"os"
"github.com/AlecAivazis/survey/v2"
"github.com/sirupsen/logrus"
)
// NewToken constructs a new DNS provider token.
func NewToken(provider, providerTokenEnvVar string) (string, error) {
if token, present := os.LookupEnv(providerTokenEnvVar); present {
return token, nil
}
logrus.Debugf("no %s in environment, asking via stdin", providerTokenEnvVar)
var token string
prompt := &survey.Input{
Message: fmt.Sprintf("%s API token?", provider),
}
if err := survey.AskOne(prompt, &token); err != nil {
return "", err
}
return token, nil
}

View File

@ -1,52 +0,0 @@
package gandi
import (
"os"
"github.com/AlecAivazis/survey/v2"
"github.com/libdns/gandi"
"github.com/libdns/libdns"
"github.com/sirupsen/logrus"
)
func getToken() (string, error) {
if token, present := os.LookupEnv("GANDI_TOKEN"); present {
return token, nil
}
logrus.Debugf("no GANDI_TOKEN in environment, asking via stdin")
var token string
prompt := &survey.Input{
Message: "Gandi API token?",
}
if err := survey.AskOne(prompt, &token); err != nil {
return "", err
}
return token, nil
}
func getProvider() (gandi.Provider, error) {
token, err := getToken()
if err != nil {
return gandi.Provider{}, err
}
return gandi.Provider{APIToken: token}, nil
}
func ListDomains() ([]libdns.Record, error) {
return []libdns.Record{}, nil
}
func RemoveDomain() (bool, error) {
return false, nil
}
func AddDomain() (bool, error) {
return false, nil
}
func UpdateDomain() (bool, error) {
return false, nil
}

15
pkg/dns/gandi/gandi.go Normal file
View File

@ -0,0 +1,15 @@
package gandi
import (
"coopcloud.tech/abra/pkg/dns"
"github.com/libdns/gandi"
)
// New constructs a new DNs provider.
func New() (gandi.Provider, error) {
token, err := dns.NewToken("Gandi", "GANDI_TOKEN")
if err != nil {
return gandi.Provider{}, err
}
return gandi.Provider{APIToken: token}, nil
}