pulumi/cmd/state_delete.go
Matt Ellis 59a54f1802
Add --stack argument to a few missing commands (#2278)
As of this change, all of the stack specific commands for `pulumi` now
allow passing `--stack` to operate on a different stack from the
default one.

Fixes #1648
2018-12-10 10:10:43 -08:00

87 lines
2.9 KiB
Go

// Copyright 2016-2018, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"fmt"
"github.com/pulumi/pulumi/pkg/diag"
"github.com/pulumi/pulumi/pkg/resource"
"github.com/pulumi/pulumi/pkg/resource/deploy"
"github.com/pulumi/pulumi/pkg/resource/edit"
"github.com/pulumi/pulumi/pkg/util/cmdutil"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
func newStateDeleteCommand() *cobra.Command {
var force bool // Force deletion of protected resources
var stack string
cmd := &cobra.Command{
Use: "delete",
Short: "Deletes a resource from a stack's state",
Long: `Deletes a resource from a stack's state
This command deletes a resource from a stack's state, as long as it is safe to do so. Resources can't be deleted if
there exist other resources that depend on it or are parented to it. Protected resources will not be deleted unless
it is specifically requested using the --force flag.`,
Args: cmdutil.ExactArgs(1),
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
urn := resource.URN(args[0])
err := runStateEdit(stack, urn, func(snap *deploy.Snapshot, res *resource.State) error {
if !force {
return edit.DeleteResource(snap, res)
}
if res.Protect {
cmdutil.Diag().Warningf(diag.RawMessage("" /*urn*/, "deleting protected resource due to presence of --force"))
res.Protect = false
}
return edit.DeleteResource(snap, res)
})
if err != nil {
switch e := err.(type) {
case edit.ResourceHasDependenciesError:
message := "This resource can't be safely deleted because the following resources depend on it:\n"
for _, dependentResource := range e.Dependencies {
depUrn := dependentResource.URN
message += fmt.Sprintf(" * %-15q (%s)\n", depUrn.Name(), depUrn)
}
message += "\nDelete those resources first before deleting this one."
return errors.New(message)
case edit.ResourceProtectedError:
return errors.New(
"This resource can't be safely deleted because it is protected. " +
"Re-run this command with --force to force deletion")
default:
return err
}
}
fmt.Println("Resource deleted successfully")
return nil
}),
}
cmd.PersistentFlags().StringVarP(
&stack, "stack", "s", "",
"The name of the stack to operate on. Defaults to the current stack")
cmd.Flags().BoolVar(&force, "force", false, "Force deletion of protected resources")
return cmd
}