pulumi/cmd/stack_select.go
Matt Ellis 56d7f8eb24 Support new stack identity for the cloud backend
This change introduces support for using the cloud backend when
`pulumi init` has not been run. When this is the case, we use the new
identity model, where a stack is referenced by an owner and a stack
name only.

There are a few things going on here:

- We add a new `--owner` flag to `pulumi stack init` that lets you
  control what account a stack is created in.

- When listing stacks, we show stacks owned by you and any
  organizations you are a member of. So, for example, I can do:

  * `pulumi stack init my-great-stack`
  * `pulumi stack init --owner pulumi my-great-stack`

  To create a stack owned by my user and one owned by my
  organization. When `pulumi stack ls` is run, you'll see both
  stacks (since they are part of the same project).

- When spelling a stack on the CLI, an owner can be optionally
  specified by prefixing the stack name with an owner name. For
  example `my-great-stack` means the stack `my-great-stack` owned by
  the current logged in user, where-as `pulumi/my-great-stack` would
  be the stack owned by the `pulumi` organization

- `--all` can be passed to `pulumi stack ls` to see *all* stacks you
  have access to, not just stacks tied to the current project.
2018-04-18 04:54:02 -07:00

61 lines
1.8 KiB
Go

// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
package cmd
import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/pulumi/pulumi/pkg/backend/state"
"github.com/pulumi/pulumi/pkg/util/cmdutil"
)
// newStackSelectCmd handles both the "local" and "cloud" scenarios in its implementation.
func newStackSelectCmd() *cobra.Command {
var cloud string
cmd := &cobra.Command{
Use: "select [<stack>]",
Short: "Switch the current workspace to the given stack",
Long: "Switch the current workspace to the given stack.\n" +
"\n" +
"Selecting a stack allows you to use commands like `config`, `preview`, and `update`\n" +
"without needing to type the stack name each time.\n" +
"\n" +
"If no <stack> argument is supplied, you will be prompted to select one interactively.",
Args: cmdutil.MaximumNArgs(1),
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
b, err := currentBackend()
if err != nil {
return err
}
if len(args) > 0 {
// A stack was given, ask all known backends about it
stackRef, stackErr := b.ParseStackReference(args[0], nil)
if stackErr != nil {
return stackErr
}
stack, stackErr := b.GetStack(stackRef)
if stackErr != nil {
return stackErr
} else if stack != nil {
return state.SetCurrentStack(stackRef.String())
}
return errors.Errorf("no stack named '%s' found", stackRef)
}
// If no stack was given, prompt the user to select a name from the available ones.
stack, err := chooseStack(b, true)
if err != nil {
return err
}
return state.SetCurrentStack(stack.Name().String())
}),
}
cmd.PersistentFlags().StringVarP(
&cloud, "cloud", "c", "", "A URL for the Pulumi Cloud containing the stack to be selected")
return cmd
}