pulumi/pkg/backend/state/stacks.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

41 lines
882 B
Go

// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
package state
import (
"github.com/pulumi/pulumi/pkg/backend"
"github.com/pulumi/pulumi/pkg/workspace"
)
// CurrentStack reads the current stack and returns an instance connected to its backend provider.
func CurrentStack(backend backend.Backend) (backend.Stack, error) {
w, err := workspace.New()
if err != nil {
return nil, err
}
stackName := w.Settings().Stack
if stackName == "" {
return nil, nil
}
ref, err := backend.ParseStackReference(stackName, nil)
if err != nil {
return nil, err
}
return backend.GetStack(ref)
}
// SetCurrentStack changes the current stack to the given stack name.
func SetCurrentStack(name string) error {
// Switch the current workspace to that stack.
w, err := workspace.New()
if err != nil {
return err
}
w.Settings().Stack = name
return w.Save()
}