From 44652140c91460be45162d69f48cab4088338e76 Mon Sep 17 00:00:00 2001 From: aq17 Date: Fri, 18 Nov 2022 09:44:09 -0800 Subject: [PATCH] attempt to select stack then create --- ...-create-as-fallback-on-createorselect.yaml | 4 ++++ .../Pulumi.Automation/WorkspaceStack.cs | 6 ++--- sdk/go/auto/stack.go | 24 +++++++------------ sdk/nodejs/automation/stack.ts | 8 +++---- 4 files changed, 19 insertions(+), 23 deletions(-) create mode 100644 changelog/pending/20221117--sdk-nodejs--attempt-to-select-stack-then-create-as-fallback-on-createorselect.yaml diff --git a/changelog/pending/20221117--sdk-nodejs--attempt-to-select-stack-then-create-as-fallback-on-createorselect.yaml b/changelog/pending/20221117--sdk-nodejs--attempt-to-select-stack-then-create-as-fallback-on-createorselect.yaml new file mode 100644 index 000000000000..f379efe02e5f --- /dev/null +++ b/changelog/pending/20221117--sdk-nodejs--attempt-to-select-stack-then-create-as-fallback-on-createorselect.yaml @@ -0,0 +1,4 @@ +changes: +- type: fix + scope: sdk/nodejs,go,dotnet + description: Attempt to select stack then create as fallback on 'createOrSelect' diff --git a/sdk/dotnet/Pulumi.Automation/WorkspaceStack.cs b/sdk/dotnet/Pulumi.Automation/WorkspaceStack.cs index b77a1371af0e..4351dcacb208 100644 --- a/sdk/dotnet/Pulumi.Automation/WorkspaceStack.cs +++ b/sdk/dotnet/Pulumi.Automation/WorkspaceStack.cs @@ -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}") diff --git a/sdk/go/auto/stack.go b/sdk/go/auto/stack.go index 5584f6fe8a5e..6db7d0de2d28 100644 --- a/sdk/go/auto/stack.go +++ b/sdk/go/auto/stack.go @@ -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 diff --git a/sdk/nodejs/automation/stack.ts b/sdk/nodejs/automation/stack.ts index 9dbd021d6f01..d1339039d37e 100644 --- a/sdk/nodejs/automation/stack.ts +++ b/sdk/nodejs/automation/stack.ts @@ -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"; @@ -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; });