Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Delete deployment (CLI changes, GCR support) #2

Merged
merged 6 commits into from
Jun 3, 2020
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
9 changes: 9 additions & 0 deletions builtin/google/cloudrun/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ func (d *Deployment) apiName() string {
)
}

// apiRevisionName returns the GCP API "name" string format for API calls
// to the revisons api.
func (d *Deployment) apiRevisionName() string {
return fmt.Sprintf("namespaces/%s/revisions/%s",
d.Resource.Project,
d.RevisionId,
)
}

// apiService returns the API service for GCP client usage.
func (d *Deployment) apiService(ctx context.Context) (*run.APIService, error) {
result, err := run.NewService(ctx,
Expand Down
28 changes: 28 additions & 0 deletions builtin/google/cloudrun/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func (p *Platform) DeployFunc() interface{} {
return p.Deploy
}

// DestroyFunc implements component.Destroyer
func (p *Platform) DestroyFunc() interface{} {
return p.Destroy
}

// Deploy deploys an image to GCR.
func (p *Platform) Deploy(
ctx context.Context,
Expand Down Expand Up @@ -178,6 +183,29 @@ func (p *Platform) Deploy(
return result, nil
}

// Destroy deletes the cloud run revision
func (p *Platform) Destroy(
ctx context.Context,
log hclog.Logger,
deployment *Deployment,
ui terminal.UI,
) error {
// We'll update the user in real time
st := ui.Status()
defer st.Close()

apiService, err := deployment.apiService(ctx)
if err != nil {
return err
}
client := run.NewNamespacesRevisionsService(apiService)

st.Update("Deleting deployment...")

_, err = client.Delete(deployment.apiRevisionName()).Context(ctx).Do()
return err
}

// Config is the configuration structure for the Platform.
type Config struct {
// Project is the project to deploy to.
Expand Down
20 changes: 12 additions & 8 deletions internal/cli/deployment_destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,29 @@ func (c *DeploymentDestroyCommand) Run(args []string) int {
}

args = flags.Args()
if len(args) != 1 {
c.ui.Output("you must supply a single deployment ID", terminal.WithErrorStyle)
return 1
}

client := c.project.Client()
err := c.DoApp(c.Ctx, func(ctx context.Context, app *core.App) error {
// Get the latest deployment
resp, err := client.ListDeployments(ctx, &pb.ListDeploymentsRequest{
Limit: 1,
Order: pb.ListDeploymentsRequest_COMPLETE_TIME,
OrderDesc: true,
// Get the deployment
deployment, err := client.GetDeployment(ctx, &pb.GetDeploymentRequest{
DeploymentId: args[0],
})
if err != nil {
app.UI.Output(err.Error(), terminal.WithErrorStyle())
return ErrSentinel
}
if len(resp.Deployments) == 0 {
app.UI.Output("No successful deployments found.", terminal.WithErrorStyle())

// Can't destroy a deployment that was not successful
if deployment.Status.GetState() != pb.Status_SUCCESS {
app.UI.Output("Cannot destroy deployment that is not successful", terminal.WithErrorStyle())
return ErrSentinel
}

if err := app.DestroyDeploy(ctx, resp.Deployments[0]); err != nil {
if err := app.DestroyDeploy(ctx, deployment); err != nil {
app.UI.Output("Error destroying the deployment: %s", err.Error(), terminal.WithErrorStyle())
return ErrSentinel
}
Expand Down
Loading