Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/commands/compute/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -792,10 +792,12 @@ func constructSetupObjects(
AcceptDefaults: c.Globals.Flags.AcceptDefaults,
NonInteractive: c.Globals.Flags.NonInteractive,
PackageDomain: c.Domain,
RetryLimit: 5,
ServiceID: serviceID,
ServiceVersion: serviceVersion,
Stdin: in,
Stdout: out,
Verbose: c.Globals.Verbose(),
}

err = domains.Validate()
Expand Down
2 changes: 0 additions & 2 deletions pkg/commands/compute/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,6 @@ func TestDeploy(t *testing.T) {
wantError: fmt.Sprintf("error creating domain: %s", testutil.Err.Error()),
wantOutput: []string{
"Creating service...",
"Creating domain '",
},
},
// The following test doesn't provide a Service ID by either a flag nor the
Expand Down Expand Up @@ -395,7 +394,6 @@ func TestDeploy(t *testing.T) {
},
wantError: fmt.Sprintf("error configuring the service: %s", testutil.Err.Error()),
wantOutput: []string{
"Creating domain '",
"Cleaning up service",
"Removing Service ID from fastly.toml",
"Cleanup complete",
Expand Down
81 changes: 66 additions & 15 deletions pkg/commands/compute/setup/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"
"io"
"math/rand"
"net/http"
"regexp"
"strings"
"time"

petname "github.com/dustinkirkland/golang-petname"
Expand All @@ -28,10 +30,12 @@ type Domains struct {
NonInteractive bool
PackageDomain string
Progress text.Progress
RetryLimit int
ServiceID string
ServiceVersion int
Stdin io.Reader
Stdout io.Writer
Verbose bool

// Private
available []*fastly.Domain
Expand All @@ -57,11 +61,7 @@ func (d *Domains) Configure() error {
return nil
}

// IMPORTANT: go1.20 deprecates rand.Seed
// The global random number generator (RNG) is now automatically seeded.
// If not seeded, the same domain name is repeated on each run.
rand.Seed(time.Now().UnixNano())
defaultDomain := fmt.Sprintf("%s.%s", petname.Generate(3, "-"), defaultTopLevelDomain)
defaultDomain := generateDomainName()

var (
domain string
Expand Down Expand Up @@ -95,19 +95,12 @@ func (d *Domains) Create() error {
}

for _, domain := range d.required {
d.Progress.Step(fmt.Sprintf("Creating domain '%s'...", domain.Name))

_, err := d.APIClient.CreateDomain(&fastly.CreateDomainInput{
ServiceID: d.ServiceID,
ServiceVersion: d.ServiceVersion,
Name: &domain.Name,
})
if err != nil {
d.Progress.Fail()
return fmt.Errorf("error creating domain: %w", err)
if err := d.createDomain(domain.Name, 1); err != nil {
return err
}
}

d.Progress.Done()
return nil
}

Expand Down Expand Up @@ -157,3 +150,61 @@ func (d *Domains) validateDomain(input string) error {
}
return nil
}

func (d *Domains) createDomain(name string, attempt int) error {
if attempt > 1 {
d.Progress = text.ResetProgress(d.Stdout, d.Verbose)
d.Progress.Step(fmt.Sprintf("Creating domain '%s'...", name))
}

_, err := d.APIClient.CreateDomain(&fastly.CreateDomainInput{
ServiceID: d.ServiceID,
ServiceVersion: d.ServiceVersion,
Name: &name,
})
if err != nil {
if attempt > d.RetryLimit {
return fmt.Errorf("too many attempts")
}

// We have to stop the ticker so we can now prompt the user.
d.Progress.Fail()

if e, ok := err.(*fastly.HTTPError); ok {
if e.StatusCode == http.StatusBadRequest {
for _, he := range e.Errors {
// NOTE: In case the domain is already used by another customer.
// We'll give the user one additional chance to correct the domain.
if strings.Contains(he.Detail, "by another customer") {
var domain string
defaultDomain := generateDomainName()
if !d.AcceptDefaults && !d.NonInteractive {
text.Break(d.Stdout)
domain, err = text.Input(d.Stdout, text.BoldYellow(fmt.Sprintf("Domain already taken, please choose another (attempt %d of %d): [%s] ", attempt, d.RetryLimit, defaultDomain)), d.Stdin, d.validateDomain)
if err != nil {
return fmt.Errorf("error reading input %w", err)
}
text.Break(d.Stdout)
}
if domain == "" {
domain = defaultDomain
}
return d.createDomain(domain, attempt+1)
}
}
}
}

return fmt.Errorf("error creating domain: %w", err)
}

return nil
}

func generateDomainName() string {
// IMPORTANT: go1.20 deprecates rand.Seed
// The global random number generator (RNG) is now automatically seeded.
// If not seeded, the same domain name is repeated on each run.
rand.Seed(time.Now().UnixNano())
return fmt.Sprintf("%s.%s", petname.Generate(3, "-"), defaultTopLevelDomain)
}