pulumi/cmd/stack_init.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

70 lines
1.9 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/cloud"
"github.com/pulumi/pulumi/pkg/util/cmdutil"
)
func newStackInitCmd() *cobra.Command {
var owner string
var ppc string
cmd := &cobra.Command{
Use: "init <stack-name>",
Args: cmdutil.MaximumNArgs(1),
Short: "Create an empty stack with the given name, ready for updates",
Long: "Create an empty stack with the given name, ready for updates\n" +
"\n" +
"This command creates an empty stack with the given name. It has no resources,\n" +
"but afterwards it can become the target of a deployment using the `update` command.",
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
b, err := currentBackend()
if err != nil {
return err
}
var parseOpts interface{}
var createOpts interface{}
if _, ok := b.(cloud.Backend); ok {
createOpts = cloud.CreateStackOptions{
CloudName: ppc,
}
parseOpts = cloud.StackReferenceParseOptions{
DefaultOwner: owner,
}
}
var stackName string
if len(args) > 0 {
stackName = args[0]
} else if cmdutil.Interactive() {
name, nameErr := cmdutil.ReadConsole("Enter a stack name")
if nameErr != nil {
return nameErr
}
stackName = name
}
if stackName == "" {
return errors.New("missing stack name")
}
stackRef, err := b.ParseStackReference(stackName, parseOpts)
if err != nil {
return err
}
_, err = createStack(b, stackRef, createOpts)
return err
}),
}
cmd.PersistentFlags().StringVarP(
&owner, "owner", "o", "", "The owner for the new stack (defaults to current user)")
cmd.PersistentFlags().StringVarP(
&ppc, "ppc", "p", "", "An optional Pulumi Private Cloud (PPC) name to initialize this stack in")
return cmd
}