Skip to content

Commit

Permalink
add static LocalWorkspace methods for New/Select/Upsert Stack w/ loca…
Browse files Browse the repository at this point in the history
…l and inline programs
  • Loading branch information
EvanBoyle committed Sep 22, 2020
1 parent 3329ea1 commit 0cef250
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 12 deletions.
84 changes: 79 additions & 5 deletions sdk/nodejs/x/automation/localWorkspace.ts
Expand Up @@ -19,16 +19,81 @@ import * as upath from "upath";
import { CommandResult, runPulumiCmd } from "./cmd";
import { ConfigMap, ConfigValue } from "./config";
import { ProjectSettings } from "./projectSettings";
import { Stack } from "./stack";
import { StackSettings } from "./stackSettings";
import { StackSummary, Workspace } from "./workspace";
import { PulumiFn, StackSummary, Workspace } from "./workspace";

export class LocalWorkspace implements Workspace {
ready: Promise<any[]>;
private workDir: string;
private pulumiHome?: string;
private program?: () => void;
private program?: PulumiFn;
private envVars: { [key: string]: string };
private secretsProvider?: string;
public static async NewStackLocalSource(
stackName: string, workDir: string, opts?: LocalWorkspaceOpts,
): Promise<Stack> {
return this.localSourceStackHelper(stackName, workDir, Stack.Create, opts);
}
public static async UpsertStackLocalSource(
stackName: string, workDir: string, opts?: LocalWorkspaceOpts,
): Promise<Stack> {
return this.localSourceStackHelper(stackName, workDir, Stack.Upsert, opts);
}
public static async SelectStackLocalSource(
stackName: string, workDir: string, opts?: LocalWorkspaceOpts,
): Promise<Stack> {
return this.localSourceStackHelper(stackName, workDir, Stack.Select, opts);
}
private static async localSourceStackHelper(
stackName: string, workDir: string, initFn: stackInitFunc, opts?: LocalWorkspaceOpts,
): Promise<Stack> {
let wsOpts = { workDir };
if (opts) {
wsOpts = { ...opts, workDir };
}

const ws = new LocalWorkspace(opts);
await ws.ready;

const stack = await initFn(stackName, ws);

return Promise.resolve(stack);
}
public static async NewStackInlineSource(
stackName: string, projectName: string, program: PulumiFn, opts?: LocalWorkspaceOpts,
): Promise<Stack> {
return this.inlineSourceStackHelper(stackName, projectName, program, Stack.Create, opts);
}
public static async UpsertStackInlinSource(
stackName: string, projectName: string, program: PulumiFn, opts?: LocalWorkspaceOpts,
): Promise<Stack> {
return this.inlineSourceStackHelper(stackName, projectName, program, Stack.Upsert, opts);
}
public static async SelectStackInlineSource(
stackName: string, projectName: string, program: PulumiFn, opts?: LocalWorkspaceOpts,
): Promise<Stack> {
return this.inlineSourceStackHelper(stackName, projectName, program, Stack.Select, opts);
}
private static async inlineSourceStackHelper(
stackName: string, projectName: string, program: PulumiFn, initFn: stackInitFunc, opts?: LocalWorkspaceOpts,
): Promise<Stack> {
let wsOpts: LocalWorkspaceOpts = { program };
if (opts) {
wsOpts = { ...opts, program };
}

if (!wsOpts.projectSettings) {
wsOpts.projectSettings = defaultProject(projectName);
}

const ws = new LocalWorkspace(opts);
await ws.ready;

const stack = await initFn(stackName, ws);

return Promise.resolve(stack);
}
constructor(opts?: LocalWorkspaceOpts) {
let dir = "";
let envs = {};
Expand Down Expand Up @@ -236,10 +301,10 @@ export class LocalWorkspace implements Workspace {
const stacks: StackSummary[] = JSON.parse(result.stdout);
return Promise.resolve(stacks);
}
getProgram(): (() => void) | undefined {
getProgram(): PulumiFn | undefined {
return this.program;
}
setProgram(program: () => void): void {
setProgram(program: PulumiFn): void {
this.program = program;
}
serializeArgsForOp(_: string): Promise<string[]> {
Expand All @@ -265,7 +330,7 @@ export class LocalWorkspace implements Workspace {
export type LocalWorkspaceOpts = {
workDir?: string,
pulumiHome?: string,
program?: () => void,
program?: PulumiFn,
envVars?: { [key: string]: string },
secretsProvider?: string,
projectSettings?: ProjectSettings,
Expand All @@ -281,3 +346,12 @@ const getStackSettingsName = (name: string): string => {
}
return parts[parts.length - 1];
};

type stackInitFunc = (name: string, workspace: Workspace) => Promise<Stack>;

const defaultProject = (projectName: string) => {
const settings = new ProjectSettings();
settings.name = projectName;
settings.runtime.name = "nodejs";
return settings;
};
11 changes: 6 additions & 5 deletions sdk/nodejs/x/automation/stack.ts
Expand Up @@ -14,7 +14,7 @@

import { CommandResult, runPulumiCmd } from "./cmd";
import { ConfigMap, ConfigValue } from "./config";
import { Workspace } from "./workspace";
import { PulumiFn, Workspace } from "./workspace";

export type StackInitMode = "create" | "select" | "upsert";

Expand Down Expand Up @@ -49,6 +49,7 @@ export class Stack {
this.ready = workspace.selectStack(name);
return this;
case "upsert":
// TODO update this based on structured errors (check for 409)
this.ready = workspace.createStack(name).catch(() => {
return workspace.selectStack(name);
});
Expand All @@ -60,7 +61,7 @@ export class Stack {
async up(opts?: UpOptions): Promise<UpResult> {
const args = ["up", "--yes", "--skip-preview"];
let kind = execKind.local;
let program: (() => void) | undefined = this.workspace.getProgram();
let program: PulumiFn | undefined = this.workspace.getProgram();
await this.workspace.selectStack(this.name);

if (opts) {
Expand Down Expand Up @@ -112,7 +113,7 @@ export class Stack {
// TODO JSON
const args = ["preview"];
let kind = execKind.local;
let program: (() => void) | undefined = this.workspace.getProgram();
let program: PulumiFn | undefined = this.workspace.getProgram();
await this.workspace.selectStack(this.name);

if (opts) {
Expand Down Expand Up @@ -359,7 +360,7 @@ export type UpOptions = {
target?: string[];
targetDependents?: boolean;
onOutput?: (out: string) => void;
program?: () => void;
program?: PulumiFn;
};

export type PreviewOptions = {
Expand All @@ -369,7 +370,7 @@ export type PreviewOptions = {
replace?: string[];
target?: string[];
targetDependents?: boolean;
program?: () => void;
program?: PulumiFn;
};

export type RefreshOptions = {
Expand Down
6 changes: 4 additions & 2 deletions sdk/nodejs/x/automation/workspace.ts
Expand Up @@ -42,8 +42,8 @@ export interface Workspace {
selectStack(stackName: string): Promise<void>;
removeStack(stackName: string): Promise<void>;
listStacks(): Promise<StackSummary[]>;
getProgram(): (() => void) | undefined;
setProgram(program: () => void): void;
getProgram(): PulumiFn | undefined;
setProgram(program: PulumiFn): void;
// TODO import/export
}

Expand All @@ -55,3 +55,5 @@ export type StackSummary = {
resourceCount?: number,
url?: string,
};

export type PulumiFn = () => void;

0 comments on commit 0cef250

Please sign in to comment.