2018-03-21 20:43:21 +01:00
|
|
|
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
|
2017-11-20 19:08:59 +01:00
|
|
|
|
2017-11-21 19:13:09 +01:00
|
|
|
import * as log from "../log";
|
2017-11-20 19:08:59 +01:00
|
|
|
import { getProject, getStack } from "../metadata";
|
2018-02-05 23:44:23 +01:00
|
|
|
import { ComponentResource, Inputs, Resource } from "../resource";
|
2017-11-26 19:04:15 +01:00
|
|
|
import { getRootResource, setRootResource } from "./settings";
|
2017-11-20 19:08:59 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* rootPulumiStackTypeName is the type name that should be used to construct the root component in the tree of Pulumi
|
|
|
|
* resources allocated by a deployment. This must be kept up to date with
|
|
|
|
* `github.com/pulumi/pulumi/pkg/resource/stack.RootPulumiStackTypeName`.
|
|
|
|
*/
|
|
|
|
export const rootPulumiStackTypeName = "pulumi:pulumi:Stack";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* runInPulumiStack creates a new Pulumi stack resource and executes the callback inside of it. Any outputs
|
|
|
|
* returned by the callback will be stored as output properties on this resulting Stack object.
|
|
|
|
*/
|
|
|
|
export function runInPulumiStack(init: () => any): void {
|
|
|
|
const _ = new Stack(init);
|
|
|
|
}
|
|
|
|
|
|
|
|
class Stack extends ComponentResource {
|
2018-02-05 23:44:23 +01:00
|
|
|
constructor(init: () => Inputs) {
|
2017-11-20 19:08:59 +01:00
|
|
|
super(rootPulumiStackTypeName, `${getProject()}-${getStack()}`);
|
|
|
|
|
2017-11-26 19:04:15 +01:00
|
|
|
if (getRootResource()) {
|
2017-11-20 19:08:59 +01:00
|
|
|
throw new Error("Only one root Pulumi Stack may be active at once");
|
|
|
|
}
|
2018-02-05 23:44:23 +01:00
|
|
|
let outputs: Inputs | undefined;
|
2017-11-20 19:08:59 +01:00
|
|
|
try {
|
2017-11-28 20:03:34 +01:00
|
|
|
setRootResource(this); // install ourselves as the current root.
|
|
|
|
outputs = init(); // run the init code.
|
2017-11-20 19:08:59 +01:00
|
|
|
}
|
|
|
|
finally {
|
2017-11-29 20:27:32 +01:00
|
|
|
super.registerOutputs(outputs); // save the outputs for this component to whatever the init returned.
|
2018-01-19 23:17:28 +01:00
|
|
|
// intentionally not removing the root resource because we want subsequent async turns to parent to it.
|
2017-11-20 19:08:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|