Skip to content

Commit f0a46b9

Browse files
authored
[CDTOOL-1243] Support for --no-default-domain flag for Compute Deploy & Publish (#1610)
### Change summary This PR adds support for a `--no-default-domain` flag for the Compute Deploy & Publish commands, allowing users to opt out of the automatic allocation of a domain when one isn't specified. All Submissions: * [x] Have you followed the guidelines in our Contributing document? * [x] Have you checked to ensure there aren't other open [Pull Requests](https://github.com/fastly/cli/pulls) for the same update/change? <!-- You can erase any parts of this template not applicable to your Pull Request. --> ### New Feature Submissions: * [x] Does your submission pass tests? ``` TEST_COMPUTE_DEPLOY=1 go test -v ./pkg/commands/compute -run "TestDeploy/success_with_--no-default-domain_flag_for_new_service" ... --- PASS: TestDeploy (0.70s) --- PASS: TestDeploy/success_with_--no-default-domain_flag_for_new_service (0.68s) TEST_COMPUTE_DEPLOY=1 go test -v ./pkg/commands/compute -run "TestDeploy/success with --no-default-domain but explicit --domain provided" ... --- PASS: TestDeploy (0.10s) --- PASS: TestDeploy/success_with_--no-default-domain_but_explicit_--domain_provided (0.08s) ``` ### Changes to Core Features: * [x] Have you written new tests for your core changes, as applicable? * [x] Have you successfully run tests with your changes locally? ### User Impact Users can now opt out of the automatic allocation of a domain when one isn't specified when deploying a new Compute service. ### Additional Notes Local testing confirms this new flag is working as expected.
1 parent a3be1a3 commit f0a46b9

5 files changed

Lines changed: 121 additions & 24 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- feat(rust): Allow testing with prerelease Rust versions ([#1604](https://github.com/fastly/cli/pull/1604))
99
- feat(compute/hashfiles): remove hashsum subcommand ([#1608](https://github.com/fastly/cli/pull/1608))
1010
- feat(commands/ngwaf/rules): add support for CRUD operations for NGWAF rules ([#1578](https://github.com/fastly/cli/pull/1605))
11+
- feat(compute/deploy): added the `--no-default-domain` flag to allow for the skipping of automatic domain creation when deploying a Compute service([#1610](https://github.com/fastly/cli/pull/1610))
1112

1213
### Bug fixes:
1314

pkg/commands/compute/deploy.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type DeployCommand struct {
5353
Dir string
5454
Domain string
5555
Env string
56+
NoDefaultDomain argparser.OptionalBool
5657
PackagePath string
5758
ServiceName argparser.OptionalServiceNameID
5859
ServiceVersion argparser.OptionalServiceVersion
@@ -93,6 +94,7 @@ func NewDeployCommand(parent argparser.Registerer, g *global.Data) *DeployComman
9394
c.CmdClause.Flag("dir", "Project directory (default: current directory)").Short('C').StringVar(&c.Dir)
9495
c.CmdClause.Flag("domain", "The name of the domain associated to the package").StringVar(&c.Domain)
9596
c.CmdClause.Flag("env", "The manifest environment config to use (e.g. 'stage' will attempt to read 'fastly.stage.toml')").StringVar(&c.Env)
97+
c.CmdClause.Flag("no-default-domain", "Skip default domain creation").Action(c.NoDefaultDomain.Set).BoolVar(&c.NoDefaultDomain.Value)
9698
c.CmdClause.Flag("package", "Path to a package tar.gz").Short('p').StringVar(&c.PackagePath)
9799
c.CmdClause.Flag("status-check-code", "Set the expected status response for the service availability check").IntVar(&c.StatusCheckCode)
98100
c.CmdClause.Flag("status-check-off", "Disable the service availability check").BoolVar(&c.StatusCheckOff)
@@ -233,16 +235,17 @@ func (c *DeployCommand) Exec(in io.Reader, out io.Writer) (err error) {
233235
// It's part of the implementation so that we can utilise the same interface.
234236
// A domain is required regardless of whether it's a new service or existing.
235237
sr.domains = &setup.Domains{
236-
APIClient: c.Globals.APIClient,
237-
AcceptDefaults: c.Globals.Flags.AcceptDefaults,
238-
NonInteractive: c.Globals.Flags.NonInteractive,
239-
PackageDomain: c.Domain,
240-
RetryLimit: 5,
241-
ServiceID: serviceID,
242-
ServiceVersion: serviceVersionNumber,
243-
Stdin: in,
244-
Stdout: out,
245-
Verbose: c.Globals.Verbose(),
238+
APIClient: c.Globals.APIClient,
239+
AcceptDefaults: c.Globals.Flags.AcceptDefaults,
240+
NoDefaultDomain: c.NoDefaultDomain.WasSet,
241+
NonInteractive: c.Globals.Flags.NonInteractive,
242+
PackageDomain: c.Domain,
243+
RetryLimit: 5,
244+
ServiceID: serviceID,
245+
ServiceVersion: serviceVersionNumber,
246+
Stdin: in,
247+
Stdout: out,
248+
Verbose: c.Globals.Verbose(),
246249
}
247250
if err = sr.domains.Validate(); err != nil {
248251
errLogService(c.Globals.ErrLog, err, serviceID, serviceVersionNumber)
@@ -304,7 +307,7 @@ func (c *DeployCommand) Exec(in io.Reader, out io.Writer) (err error) {
304307
return err
305308
}
306309

307-
if !c.StatusCheckOff && noExistingService {
310+
if !c.StatusCheckOff && noExistingService && serviceURL != "" {
308311
c.StatusCheck(serviceURL, spinner, out)
309312
}
310313

@@ -345,7 +348,9 @@ func (c *DeployCommand) StatusCheck(serviceURL string, spinner text.Spinner, out
345348

346349
func displayDeployOutput(out io.Writer, manageServiceBaseURL, serviceID, serviceURL string, serviceVersion int) {
347350
text.Description(out, "Manage this service at", fmt.Sprintf("%s%s", manageServiceBaseURL, serviceID))
348-
text.Description(out, "View this service at", serviceURL)
351+
if serviceURL != "" {
352+
text.Description(out, "View this service at", serviceURL)
353+
}
349354
text.Success(out, "Deployed package (service %s, version %v)", serviceID, serviceVersion)
350355
}
351356

@@ -1065,6 +1070,9 @@ func (c *DeployCommand) GetServiceURL(serviceID string, serviceVersion int) (str
10651070
if err != nil {
10661071
return "", err
10671072
}
1073+
if len(latestDomains) == 0 {
1074+
return "", nil
1075+
}
10681076
name := fastly.ToValue(latestDomains[0].Name)
10691077
if segs := strings.Split(name, "*."); len(segs) > 1 {
10701078
name = segs[1]

pkg/commands/compute/deploy_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,79 @@ func TestDeploy(t *testing.T) {
697697
"Deployed package (service 123, version 4)",
698698
},
699699
},
700+
{
701+
name: "success with --no-default-domain flag for new service",
702+
args: args("compute deploy --no-default-domain --token 123"),
703+
api: mock.API{
704+
ActivateVersionFn: activateVersionOk,
705+
CreateBackendFn: createBackendOK,
706+
CreateServiceFn: createServiceOK,
707+
DeleteServiceFn: deleteServiceOK,
708+
GetPackageFn: getPackageOk,
709+
ListDomainsFn: listDomainsNone,
710+
UpdatePackageFn: updatePackageOk,
711+
},
712+
stdin: []string{
713+
"Y", // when prompted to create a new service
714+
},
715+
wantOutput: []string{
716+
"Deployed package (service 12345, version 1)",
717+
},
718+
dontWantOutput: []string{
719+
"Creating domain",
720+
"Domain:",
721+
},
722+
},
723+
{
724+
name: "success with --no-default-domain but explicit --domain provided",
725+
args: args("compute deploy --token 123 --no-default-domain --domain example.com"),
726+
api: mock.API{
727+
ActivateVersionFn: activateVersionOk,
728+
CreateBackendFn: createBackendOK,
729+
CreateDomainFn: createDomainOK,
730+
CreateServiceFn: createServiceOK,
731+
GetPackageFn: getPackageOk,
732+
ListDomainsFn: listDomainsNone,
733+
UpdatePackageFn: updatePackageOk,
734+
},
735+
httpClientRes: []*http.Response{
736+
mock.NewHTTPResponse(http.StatusNoContent, nil, nil),
737+
mock.NewHTTPResponse(http.StatusOK, nil, io.NopCloser(strings.NewReader("success"))),
738+
},
739+
httpClientErr: []error{
740+
nil,
741+
nil,
742+
},
743+
stdin: []string{
744+
"Y", // when prompted to create a new service
745+
},
746+
wantOutput: []string{
747+
"Creating domain 'example.com'",
748+
"Deployed package (service 12345, version 1)",
749+
},
750+
},
751+
{
752+
name: "success with --no-default-domain and existing service",
753+
args: args("compute deploy --service-id 123 --token 123 --no-default-domain"),
754+
api: mock.API{
755+
ActivateVersionFn: activateVersionOk,
756+
CloneVersionFn: testutil.CloneVersionResult(4),
757+
GetPackageFn: getPackageOk,
758+
GetServiceDetailsFn: getServiceDetailsWasm,
759+
GetServiceFn: getServiceOK,
760+
ListDomainsFn: listDomainsOk,
761+
ListVersionsFn: testutil.ListVersions,
762+
UpdatePackageFn: updatePackageOk,
763+
},
764+
wantOutput: []string{
765+
"Uploading package",
766+
"Activating service",
767+
"Deployed package (service 123, version 4)",
768+
},
769+
dontWantOutput: []string{
770+
"Creating domain",
771+
},
772+
},
700773
// The following test doesn't provide a Service ID by either a flag nor the
701774
// manifest, so this will result in the deploy script attempting to create
702775
// a new service. Our fastly.toml is configured with a [setup] section so

pkg/commands/compute/publish.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type PublishCommand struct {
3030
comment argparser.OptionalString
3131
domain argparser.OptionalString
3232
env argparser.OptionalString
33+
noDefaultDomain argparser.OptionalBool
3334
pkg argparser.OptionalString
3435
serviceName argparser.OptionalServiceNameID
3536
serviceVersion argparser.OptionalServiceVersion
@@ -55,6 +56,7 @@ func NewPublishCommand(parent argparser.Registerer, g *global.Data, build *Build
5556
c.CmdClause.Flag("domain", "The name of the domain associated to the package").Action(c.domain.Set).StringVar(&c.domain.Value)
5657
c.CmdClause.Flag("env", "The manifest environment config to use (e.g. 'stage' will attempt to read 'fastly.stage.toml')").Action(c.env.Set).StringVar(&c.env.Value)
5758
c.CmdClause.Flag("include-source", "Include source code in built package").Action(c.includeSrc.Set).BoolVar(&c.includeSrc.Value)
59+
c.CmdClause.Flag("no-default-domain", "Skip default domain creation").Action(c.noDefaultDomain.Set).BoolVar(&c.noDefaultDomain.Value)
5860
c.CmdClause.Flag("language", "Language type").Action(c.lang.Set).StringVar(&c.lang.Value)
5961
c.CmdClause.Flag("metadata-disable", "Disable Wasm binary metadata annotations").Action(c.metadataDisable.Set).BoolVar(&c.metadataDisable.Value)
6062
c.CmdClause.Flag("metadata-filter-envvars", "Redact specified environment variables from [scripts.env_vars] using comma-separated list").Action(c.metadataFilterEnvVars.Set).StringVar(&c.metadataFilterEnvVars.Value)
@@ -187,6 +189,9 @@ func (c *PublishCommand) Deploy(in io.Reader, out io.Writer) error {
187189
if c.env.WasSet {
188190
c.deploy.Env = c.env.Value
189191
}
192+
if c.noDefaultDomain.WasSet {
193+
c.deploy.NoDefaultDomain = c.noDefaultDomain
194+
}
190195
if c.comment.WasSet {
191196
c.deploy.Comment = c.comment
192197
}

pkg/commands/compute/setup/domain.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,18 @@ var domainNameRegEx = regexp.MustCompile(`(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])
2626
// NOTE: It implements the setup.Interface interface.
2727
type Domains struct {
2828
// Public
29-
APIClient api.Interface
30-
AcceptDefaults bool
31-
NonInteractive bool
32-
PackageDomain string
33-
Spinner text.Spinner
34-
RetryLimit int
35-
ServiceID string
36-
ServiceVersion int
37-
Stdin io.Reader
38-
Stdout io.Writer
39-
Verbose bool
29+
APIClient api.Interface
30+
AcceptDefaults bool
31+
NoDefaultDomain bool
32+
NonInteractive bool
33+
PackageDomain string
34+
Spinner text.Spinner
35+
RetryLimit int
36+
ServiceID string
37+
ServiceVersion int
38+
Stdin io.Reader
39+
Stdout io.Writer
40+
Verbose bool
4041

4142
// Private
4243
available []*fastly.Domain
@@ -54,6 +55,12 @@ type Domain struct {
5455
//
5556
// NOTE: If --domain flag is used we'll use that as the domain to create.
5657
func (d *Domains) Configure() error {
58+
// Don't generate a domain if --no-default-domain is set and no domain is provided
59+
if d.NoDefaultDomain && d.PackageDomain == "" {
60+
d.missing = false
61+
return nil
62+
}
63+
5764
// PackageDomain is the --domain flag value.
5865
if d.PackageDomain != "" {
5966
d.required = append(d.required, Domain{
@@ -134,7 +141,10 @@ func (d *Domains) Validate() error {
134141
}
135142
d.available = available
136143
if len(d.available) == 0 {
137-
d.missing = true
144+
// Only mark as missing if we're not intentionally skipping domain creation
145+
if !d.NoDefaultDomain || d.PackageDomain != "" {
146+
d.missing = true
147+
}
138148
}
139149
return nil
140150
}

0 commit comments

Comments
 (0)