Skip to content

Commit

Permalink
Merge #11402
Browse files Browse the repository at this point in the history
11402: Attempt to select stack then create as fallback r=aq17 a=aq17

<!--- 
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

<!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. -->
Fixes #11392

If a user does not have write permission (i.e. read-only), attempting to create the stack first will fail before the stack select fallback is triggered. Flip the logic to select first, then create.

7695311 changed it for python auto API already, this PR does the same for TS, Go, C#

## Checklist

<!--- 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.
-->
- [ ] 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 Service,
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 Service API version
  <!-- `@Pulumi` employees: If yes, you must submit corresponding changes in the service repo. -->


Co-authored-by: aq17 <aqiu@pulumi.com>
  • Loading branch information
bors[bot] and aq17 committed Nov 18, 2022
2 parents 107c29c + 56d3a2e commit 70192a5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 23 deletions.
@@ -0,0 +1,4 @@
changes:
- type: fix
scope: sdk/nodejs,go,dotnet
description: Attempt to select stack then create as fallback on 'createOrSelect'
6 changes: 3 additions & 3 deletions sdk/dotnet/Pulumi.Automation/WorkspaceStack.cs
Expand Up @@ -130,11 +130,11 @@ public sealed class WorkspaceStack : IDisposable
{
try
{
await workspace.CreateStackAsync(name, cancellationToken).ConfigureAwait(false);
await workspace.SelectStackAsync(name, cancellationToken).ConfigureAwait(false);
}
catch (StackAlreadyExistsException)
catch (StackNotFoundException)
{
await workspace.SelectStackAsync(name, cancellationToken).ConfigureAwait(false);
await workspace.CreateStackAsync(name, cancellationToken).ConfigureAwait(false);
}
}),
_ => throw new InvalidOperationException($"Unexpected Stack creation mode: {mode}")
Expand Down
24 changes: 8 additions & 16 deletions sdk/go/auto/stack.go
Expand Up @@ -184,24 +184,16 @@ func SelectStack(ctx context.Context, stackName string, ws Workspace) (Stack, er
return s, nil
}

// UpsertStack tries to create a new stack using the given workspace and
// stack name if the stack does not already exist,
// or falls back to selecting the existing stack. If the stack does not exist,
// it will be created and selected.
// UpsertStack tries to select a stack using the given workspace and
// stack name, or falls back to trying to create the stack if
// it does not exist.
func UpsertStack(ctx context.Context, stackName string, ws Workspace) (Stack, error) {
s, err := NewStack(ctx, stackName, ws)
// error for all failures except if the stack already exists, as we'll
// just select the stack if it exists.
if err != nil && !IsCreateStack409Error(err) {
return s, err
s, err := SelectStack(ctx, stackName, ws)
// If the stack is not found, attempt to create it.
if err != nil && IsSelectStack404Error(err) {
return NewStack(ctx, stackName, ws)
}

err = ws.SelectStack(ctx, stackName)
if err != nil {
return s, err
}

return s, nil
return s, err
}

// Name returns the stack name
Expand Down
8 changes: 4 additions & 4 deletions sdk/nodejs/automation/stack.ts
Expand Up @@ -24,7 +24,7 @@ import TailFile from "@logdna/tail-file";
import * as log from "../log";
import { CommandResult, runPulumiCmd } from "./cmd";
import { ConfigMap, ConfigValue } from "./config";
import { StackAlreadyExistsError } from "./errors";
import { StackNotFoundError } from "./errors";
import { EngineEvent, SummaryEvent } from "./events";
import { LanguageServer, maxRPCMessageSize } from "./server";
import { Deployment, PulumiFn, Workspace } from "./workspace";
Expand Down Expand Up @@ -105,9 +105,9 @@ export class Stack {
this.ready = workspace.selectStack(name);
return this;
case "createOrSelect":
this.ready = workspace.createStack(name).catch((err) => {
if (err instanceof StackAlreadyExistsError) {
return workspace.selectStack(name);
this.ready = workspace.selectStack(name).catch((err) => {
if (err instanceof StackNotFoundError) {
return workspace.createStack(name);
}
throw err;
});
Expand Down

0 comments on commit 70192a5

Please sign in to comment.