Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compute/deploy: clear Service ID from manifest when service is deleted #278

Merged
merged 2 commits into from May 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion pkg/compute/compute_integration_test.go
Expand Up @@ -889,25 +889,36 @@ func TestDeploy(t *testing.T) {
manifest: "name = \"package\"\nservice_id = \"123\"\n",
wantError: "error fetching service backends: fixture error",
},
// The following test doesn't just validate the package API error behaviour
// but as a side effect it validates that when deleting the created
// service, the Service ID is also cleared out from the manifest.
{
name: "package API error",
args: []string{"compute", "deploy", "--token", "123"},
in: strings.NewReader(""),
api: mock.API{
GetServiceFn: getServiceOK,
ListVersionsFn: listVersionsActiveOk,
CloneVersionFn: cloneVersionOk,
ListDomainsFn: listDomainsOk,
ListBackendsFn: listBackendsOk,
CreateServiceFn: createServiceOK,
CreateDomainFn: createDomainOK,
CreateBackendFn: createBackendOK,
GetPackageFn: getPackageOk,
UpdatePackageFn: updatePackageError,
DeleteBackendFn: deleteBackendOK,
DeleteDomainFn: deleteDomainOK,
DeleteServiceFn: deleteServiceOK,
},
manifest: "name = \"package\"\nservice_id = \"123\"\n",
manifest: `name = "package"`,
wantError: "error uploading package: fixture error",
wantOutput: []string{
"Reading package manifest...",
"Validating package...",
"Uploading package...",
},
manifestIncludes: `service_id = ""`,
},
// The following test doesn't provide a Service ID by either a flag nor the
// manifest, so this will result in the deploy script attempting to create
Expand Down
26 changes: 22 additions & 4 deletions pkg/compute/deploy.go
Expand Up @@ -189,6 +189,10 @@ func (c *DeployCommand) Exec(in io.Reader, out io.Writer) (err error) {
return err
}

undoStack.Push(func() error {
return clearServiceID(&c.manifest.File, ManifestFilename)
})

undoStack.Push(func() error {
return c.Globals.Client.DeleteService(&fastly.DeleteServiceInput{
ID: serviceID,
Expand Down Expand Up @@ -242,7 +246,7 @@ func (c *DeployCommand) Exec(in io.Reader, out io.Writer) (err error) {
// provided by the flag and not the file, then we'll also store that ID
// within the manifest.
if sidSrc == manifest.SourceUndefined || sidSrc != manifest.SourceFile {
err = updateManifestServiceID(ManifestFilename, progress, serviceID)
err = updateManifestServiceID(&c.manifest.File, ManifestFilename, progress, serviceID)
if err != nil {
return err
}
Expand Down Expand Up @@ -294,6 +298,22 @@ func (c *DeployCommand) Exec(in io.Reader, out io.Writer) (err error) {
return nil
}

// clearServiceID removes the Service ID from the manifest.
//
// This needs to happen when there is an error in the deploy flow, otherwise
// the service itself will be deleted while the manifest will continue to hold
// a reference to it.
func clearServiceID(m *manifest.File, manifestFilename string) error {
if err := m.Read(manifestFilename); err != nil {
return fmt.Errorf("error reading package manifest: %w", err)
}
m.ServiceID = ""
if err := m.Write(manifestFilename); err != nil {
return fmt.Errorf("error updating package manifest: %w", err)
}
return nil
}

// pkgPath generates a path that points to a package tar inside the pkg
// directory if the `path` flag was not set by the user.
func pkgPath(path string, progress text.Progress, name string, source manifest.Source) (string, error) {
Expand Down Expand Up @@ -434,9 +454,7 @@ func createService(progress text.Progress, client api.Interface, name string, de
}

// updateManifestServiceID updates the Service ID in the manifest.
func updateManifestServiceID(manifestFilename string, progress text.Progress, serviceID string) error {
var m manifest.File

func updateManifestServiceID(m *manifest.File, manifestFilename string, progress text.Progress, serviceID string) error {
if err := m.Read(manifestFilename); err != nil {
return fmt.Errorf("error reading package manifest: %w", err)
}
Expand Down