Skip to content

Commit

Permalink
feat(domains): support v1 create
Browse files Browse the repository at this point in the history
  • Loading branch information
Integralist committed Jan 21, 2025
1 parent 50d2e3b commit bf08193
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 5 deletions.
5 changes: 5 additions & 0 deletions pkg/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ type HTTPClient interface {
// Interface models the methods of the Fastly API client that we use.
// It exists to allow for easier testing, in combination with Mock.
type Interface interface {
Delete(p string, ro *fastly.RequestOptions) (*http.Response, error)
Get(p string, ro *fastly.RequestOptions) (*http.Response, error)
PatchJSON(p string, i any, ro *fastly.RequestOptions) (*http.Response, error)
PostJSON(p string, i any, ro *fastly.RequestOptions) (*http.Response, error)

AllIPs() (v4, v6 fastly.IPAddrs, err error)
AllDatacenters() (datacenters []fastly.Datacenter, err error)

Expand Down
48 changes: 43 additions & 5 deletions pkg/commands/domain/create.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package domain

import (
"errors"
"fmt"
"io"
"strings"

"4d63.com/optional"
"github.com/fastly/go-fastly/v9/fastly"
v1 "github.com/fastly/go-fastly/v9/fastly/domains/v1"

Check failure on line 11 in pkg/commands/domain/create.go

View workflow job for this annotation

GitHub Actions / test (0.31.2, 1.22.x, 18, ubuntu-latest)

no required module provides package github.com/fastly/go-fastly/v9/fastly/domains/v1; to add it:

Check failure on line 11 in pkg/commands/domain/create.go

View workflow job for this annotation

GitHub Actions / test (0.31.2, 1.22.x, 18, macos-latest)

no required module provides package github.com/fastly/go-fastly/v9/fastly/domains/v1; to add it:

"github.com/fastly/cli/pkg/argparser"
"github.com/fastly/cli/pkg/errors"
fsterr "github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/text"
)
Expand All @@ -23,6 +27,7 @@ type CreateCommand struct {
apiVersion argparser.OptionalString
autoClone argparser.OptionalAutoClone
comment argparser.OptionalString
fqdn argparser.OptionalString
name argparser.OptionalString
serviceName argparser.OptionalServiceNameID
}
Expand All @@ -37,12 +42,13 @@ func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateComman
c.CmdClause = parent.Command("create", "Create a domain").Alias("add")

// Optional.
c.CmdClause.Flag("api-version", "The Fastly API version").HintOptions(APIVersions...).Action(c.apiVersion.Set).EnumVar(&c.apiVersion.Value, APIVersions...)
c.CmdClause.Flag("api-version", fmt.Sprintf("The Fastly API version (%s)", strings.Join(APIVersions, ","))).HintOptions(APIVersions...).Action(c.apiVersion.Set).EnumVar(&c.apiVersion.Value, APIVersions...)
c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{
Action: c.autoClone.Set,
Dst: &c.autoClone.Value,
})
c.CmdClause.Flag("comment", "A descriptive note").Action(c.comment.Set).StringVar(&c.comment.Value)
c.CmdClause.Flag("fqdn", "The fully qualified domain name (version support: v1)").Action(c.fqdn.Set).StringVar(&c.fqdn.Value)
c.CmdClause.Flag("name", "Domain name").Short('n').Action(c.name.Set).StringVar(&c.name.Value)
c.RegisterFlag(argparser.StringFlagOpts{
Name: argparser.FlagServiceIDName,
Expand All @@ -66,13 +72,45 @@ func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateComman

// Exec invokes the application logic for the command.
func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
if c.apiVersion.WasSet {
c.v1(out)
if c.apiVersion.Value == "v1" {
return c.v1(out)
}
return c.v0(out)
}

func (c *CreateCommand) v1(out io.Writer) error {
if !c.fqdn.WasSet {
return errors.New("--fqdn required when using --api-version")
}
input := &v1.CreateInput{
FQDN: &c.fqdn.Value,
}

serviceID, _, _, err := argparser.ServiceID(c.serviceName, *c.Globals.Manifest, c.Globals.APIClient, c.Globals.ErrLog)
if err == nil {
input.ServiceID = &serviceID
}

fc, ok := c.Globals.APIClient.(*fastly.Client)
if !ok {
return errors.New("failed to convert interface to a fastly client")
}

d, err := v1.Create(fc, input)
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]any{
"FQDN": c.fqdn.Value,
"Service ID": serviceID,
})
return err
}

serviceOutput := ""
if d.ServiceID != nil {
serviceOutput = fmt.Sprintf(" (service-id: %s)", *d.ServiceID)
}

text.Success(out, "Created domain '%s' (domain-id: %s)%s", d.FQDN, d.DomainID, serviceOutput)
return nil
}

Expand All @@ -91,7 +129,7 @@ func (c *CreateCommand) v0(out io.Writer) error {
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]any{
"Service ID": serviceID,
"Service Version": errors.ServiceVersion(serviceVersion),
"Service Version": fsterr.ServiceVersion(serviceVersion),
})
return err
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/mock/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mock

import (
"crypto/ed25519"
"net/http"

"github.com/fastly/go-fastly/v9/fastly"
)
Expand All @@ -10,6 +11,11 @@ import (
// The zero value is useful, but will panic on all methods. Provide function
// implementations for the method(s) your test will call.
type API struct {
DeleteFn func(string, *fastly.RequestOptions) (*http.Response, error)
GetFn func(string, *fastly.RequestOptions) (*http.Response, error)
PatchJSONFn func(string, any, *fastly.RequestOptions) (*http.Response, error)
PostJSONFn func(string, any, *fastly.RequestOptions) (*http.Response, error)

AllDatacentersFn func() (datacenters []fastly.Datacenter, err error)
AllIPsFn func() (v4, v6 fastly.IPAddrs, err error)

Expand Down Expand Up @@ -406,6 +412,26 @@ type API struct {
DeleteObservabilityCustomDashboardFn func(i *fastly.DeleteObservabilityCustomDashboardInput) error
}

// Delete implements Interface.
func (m API) Delete(p string, ro *fastly.RequestOptions) (*http.Response, error) {
return m.DeleteFn(p, ro)
}

// Get implements Interface.
func (m API) Get(p string, ro *fastly.RequestOptions) (*http.Response, error) {
return m.GetFn(p, ro)
}

// PatchJSON implements Interface.
func (m API) PatchJSON(p string, i any, ro *fastly.RequestOptions) (*http.Response, error) {
return m.PatchJSONFn(p, i, ro)
}

// PostJSON implements Interface.
func (m API) PostJSON(p string, i any, ro *fastly.RequestOptions) (*http.Response, error) {
return m.PostJSONFn(p, i, ro)
}

// AllDatacenters implements Interface.
func (m API) AllDatacenters() ([]fastly.Datacenter, error) {
return m.AllDatacentersFn()
Expand Down

0 comments on commit bf08193

Please sign in to comment.