pulumi/sdk/go/auto
Zach Buchheit 0ebfde2629
Add support for `--all` parameter of the `stack ls` command to the Automation API (#16266)
<!--- 
Thanks so much for your contribution! If this is your first time
contributing, please ensure that you have read the
[CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md)
documentation.
-->

# Description

Adds Go/Nodejs/Python automation API support for pulumi stack ls --all.

Fixes: [#14226](https://github.com/pulumi/pulumi/issues/14226)

## Checklist

- [x] I have run `make tidy` to update any new dependencies
- [x] I have run `make lint` to verify my code passes the lint check
  - [x] I have formatted my code using `gofumpt`

<!--- Please provide details if the checkbox below is to be left
unchecked. -->
- [ ] I have added tests that prove my fix is effective or that my
feature works
<!--- 
User-facing changes require a CHANGELOG entry.
-->
- [x] I have run `make changelog` and committed the
`changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the
Pulumi Cloud,
then the service should honor older versions of the CLI where this
change would not exist.
You must then bump the API version in
/pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi
Cloud API version
<!-- @Pulumi employees: If yes, you must submit corresponding changes in
the service repo. -->

---------

Co-authored-by: Thomas Gummerer <t.gummerer@gmail.com>
2024-06-03 15:53:43 +00:00
..
debug feat(automation): Add options to configure logging, tracing (#10338) 2022-08-11 23:30:45 -07:00
events [breaking] Changing the version of go.mod in sdk / pkg to be v3 2021-04-14 19:32:18 +01:00
optdestroy Add support for continue-on-error parameter of the destroy command to the Automation API (#15921) 2024-04-12 07:58:38 +00:00
opthistory chore: Update doc comments, coding style, fix lint 2022-10-13 13:50:49 -07:00
optlist Add support for `--all` parameter of the `stack ls` command to the Automation API (#16266) 2024-06-03 15:53:43 +00:00
optpreview add support for --import-file when using the Automation API (#16071) 2024-04-30 11:31:43 +00:00
optrefresh Support suppress-progress and suppress-outputs options (#15596) 2024-03-07 09:29:24 +00:00
optremotedestroy [auto/go] Support for remote operations 2022-10-28 12:56:11 -07:00
optremotepreview [auto/go] Support for remote operations 2022-10-28 12:56:11 -07:00
optremoterefresh [auto/go] Support for remote operations 2022-10-28 12:56:11 -07:00
optremoteup [auto/go] Support for remote operations 2022-10-28 12:56:11 -07:00
optremove Add force flag for RemoveStack (#7523) 2021-08-02 12:54:46 -07:00
optup Add ContinueOnError() method for optup (#16291) 2024-05-31 07:51:01 +00:00
test Update github.com/cloudflare/circl to v1.3.7 (#15151) 2024-01-16 08:59:57 +00:00
README.md Remove references to automation api being in alpha (#6828) 2021-04-21 08:44:04 -07:00
cmd.go upgrade to latest version of golangci-lint (#15977) 2024-04-19 06:20:33 +00:00
cmd_test.go Use the full path when running pulumi from a custom CLI install (#15802) 2024-03-27 21:12:00 +00:00
errors.go Rename filestate to DIY (#15314) 2024-01-30 15:53:10 +00:00
errors_test.go [Python] Move existing dependency installation and python command invocation to a Toolchain interface (#16243) 2024-06-03 13:52:27 +00:00
example_test.go Use slice.Prealloc instead of make([]T, 0, ...) 2023-06-29 11:27:50 +01:00
git.go Fix datarace in setupGitRepo (#14643) 2023-11-24 14:34:43 +00:00
git_test.go Use EqualError/ErrorContains instead of Error (#14737) 2023-12-08 06:40:14 +00:00
local_workspace.go Add support for `--all` parameter of the `stack ls` command to the Automation API (#16266) 2024-06-03 15:53:43 +00:00
local_workspace_test.go Add support for `--all` parameter of the `stack ls` command to the Automation API (#16266) 2024-06-03 15:53:43 +00:00
minimum_version.go Add user agent to the CLI, Go and Nodejs Automation API SDKs (#6935) 2021-04-30 07:26:23 -07:00
remote_stack.go Rename filestate to DIY (#15314) 2024-01-30 15:53:10 +00:00
remote_workspace.go Use new API for deployments (#15684) 2024-04-16 23:23:56 +00:00
remote_workspace_test.go Use new API for deployments (#15684) 2024-04-16 23:23:56 +00:00
stack.go add support for --import-file when using the Automation API (#16071) 2024-04-30 11:31:43 +00:00
stack_test.go automation: only read complete lines before trying to deserialize (#15778) 2024-03-26 14:32:56 +00:00
workspace.go Add support for `--all` parameter of the `stack ls` command to the Automation API (#16266) 2024-06-03 15:53:43 +00:00

README.md

Automation API

Programmatic infrastructure.

Godocs

See the full godocs for the most extensive and up to date information including full examples coverage:

https://pkg.go.dev/github.com/pulumi/pulumi/sdk/v3/go/auto?tab=doc

Examples

Multiple full working examples with detailed walkthroughs can be found in this repo:

https://github.com/pulumi/automation-api-examples

Overview

Package auto contains the Pulumi Automation API, the programmatic interface for driving Pulumi programs without the CLI. Generally this can be thought of as encapsulating the functionality of the CLI (pulumi up, pulumi preview, pulumi destroy, pulumi stack init, etc.) but with more flexibility. This still requires a CLI binary to be installed and available on your $PATH.

In addition to fine-grained building blocks, Automation API provides three out of the box ways to work with Stacks:

  1. Programs locally available on-disk and addressed via a filepath (NewStackLocalSource)
    stack, err := NewStackLocalSource(ctx, "myOrg/myProj/myStack", filepath.Join("..", "path", "to", "project"))
  1. Programs fetched from a Git URL (NewStackRemoteSource)
	stack, err := NewStackRemoteSource(ctx, "myOrg/myProj/myStack", GitRepo{
		URL:         "https:github.com/pulumi/test-repo.git",
		ProjectPath: filepath.Join("project", "path", "repo", "root", "relative"),
    })
  1. Programs defined as a function alongside your Automation API code (NewStackInlineSource)
	 stack, err := NewStackInlineSource(ctx, "myOrg/myProj/myStack", "myProj", func(pCtx *pulumi.Context) error {
		bucket, err := s3.NewBucket(pCtx, "bucket", nil)
		if err != nil {
			return err
		}
		pCtx.Export("bucketName", bucket.Bucket)
		return nil
     })

Each of these creates a stack with access to the full range of Pulumi lifecycle methods (up/preview/refresh/destroy), as well as methods for managing config, stack, and project settings.

	 err := stack.SetConfig(ctx, "key", ConfigValue{ Value: "value", Secret: true })
	 preRes, err := stack.Preview(ctx)
	 detailed info about results
     fmt.Println(preRes.prev.Steps[0].URN)

The Automation API provides a natural way to orchestrate multiple stacks, feeding the output of one stack as an input to the next as shown in the package-level example below. The package can be used for a number of use cases:

  • Driving pulumi deployments within CI/CD workflows
  • Integration testing
  • Multi-stage deployments such as blue-green deployment patterns
  • Deployments involving application code like database migrations
  • Building higher level tools, custom CLIs over pulumi, etc
  • Using pulumi behind a REST or GRPC API
  • Debugging Pulumi programs (by using a single main entrypoint with "inline" programs)

To enable a broad range of runtime customization the API defines a Workspace interface. A Workspace is the execution context containing a single Pulumi project, a program, and multiple stacks. Workspaces are used to manage the execution environment, providing various utilities such as plugin installation, environment configuration ($PULUMI_HOME), and creation, deletion, and listing of Stacks. Every Stack including those in the above examples are backed by a Workspace which can be accessed via:

	 w = stack.Workspace()
     err := w.InstallPlugin("aws", "v3.2.0")

Workspaces can be explicitly created and customized beyond the three Stack creation helpers noted above:

	 w, err := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "project", "path"), PulumiHome("~/.pulumi"))
     s := NewStack(ctx, "org/proj/stack", w)

A default implementation of workspace is provided as LocalWorkspace. This implementation relies on Pulumi.yaml and Pulumi..yaml as the intermediate format for Project and Stack settings. Modifying ProjectSettings will alter the Workspace Pulumi.yaml file, and setting config on a Stack will modify the Pulumi..yaml file. This is identical to the behavior of Pulumi CLI driven workspaces. Custom Workspace implementations can be used to store Project and Stack settings as well as Config in a different format, such as an in-memory data structure, a shared persistent SQL database, or cloud object storage. Regardless of the backing Workspace implementation, the Pulumi SaaS Console will still be able to display configuration applied to updates as it does with the local version of the Workspace today.

The Automation API also provides error handling utilities to detect common cases such as concurrent update conflicts:

	uRes, err :=stack.Up(ctx)
	if err != nil && IsConcurrentUpdateError(err) { /* retry logic here */ }

Developing the Godocs

This repo has extensive examples and godoc content. To test out your changes locally you can do the following:

  1. enlist in the appropriate pulumi branch:
  2. cd $GOPATH/src/github.com/pulumi/pulumi/sdk/go/auto
  3. godoc -http=:6060
  4. Navigate to http://localhost:6060/pkg/github.com/pulumi/pulumi/sdk/v3/go/auto/

Known Issues

Please upvote issues, add comments, and open new ones to help prioritize our efforts: https://github.com/pulumi/pulumi/issues?q=is%3Aissue+is%3Aopen+label%3Aarea%2Fautomation-api