Skip to content

Commit

Permalink
attempt to select stack then create
Browse files Browse the repository at this point in the history
  • Loading branch information
aq17 committed Nov 18, 2022
1 parent befe190 commit 5733dfb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 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
17 changes: 8 additions & 9 deletions sdk/go/auto/stack.go
Expand Up @@ -184,19 +184,18 @@ 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) {
s, err := SelectStack(ctx, stackName, ws)
// error for all failures except if the stack is not found, as we'll
// attempt to create it.
if err != nil && !IsSelectStack404Error(err) {
return s, err
}

err = ws.SelectStack(ctx, stackName)
s, err = NewStack(ctx, stackName, ws)
if err != nil {
return s, err
}
Expand Down
14 changes: 7 additions & 7 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,12 +105,12 @@ 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);
}
throw err;
});
this.ready = workspace.selectStack(name).catch((err) => {
if (err instanceof StackNotFoundError) {
return workspace.createStack(name);
}
throw err;
})
return this;
default:
throw new Error(`unexpected Stack creation mode: ${mode}`);
Expand Down

0 comments on commit 5733dfb

Please sign in to comment.