Convert resource to pojo objects when used as stack outputs. (#2311)
This commit is contained in:
parent
f49fb626bb
commit
87e5a441f5
|
@ -6,6 +6,8 @@
|
|||
|
||||
## 0.16.9 (Released December 24th, 2018)
|
||||
|
||||
- Exporting a Resource from an application Stack now exports it as a rich recursive pojo instead of just being an opaque URN (fixes https://github.com/pulumi/pulumi/issues/1858).
|
||||
|
||||
### Improvements
|
||||
|
||||
- Update the error message when When `pulumi` commands fail to detect your project to mention that `pulumi new` can be used to create a new project (fixes [pulumi/pulumi#2234](https://github.com/pulumi/pulumi/issues/2234))
|
||||
|
|
|
@ -12,8 +12,9 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
import * as asset from "../asset";
|
||||
import { getProject, getStack } from "../metadata";
|
||||
import { ComponentResource, Inputs, Output, output } from "../resource";
|
||||
import { ComponentResource, Inputs, Output, output, Resource } from "../resource";
|
||||
import { getRootResource, setRootResource } from "./settings";
|
||||
|
||||
/**
|
||||
|
@ -64,9 +65,103 @@ class Stack extends ComponentResource {
|
|||
try {
|
||||
outputs = init();
|
||||
} finally {
|
||||
super.registerOutputs(outputs);
|
||||
// We want to expose stack outputs as simple pojo objects (including Resources). This
|
||||
// helps ensure that outputs can point to resources, and that that is stored and
|
||||
// presented as something reasonable, and not as just an id/urn in the case of
|
||||
// Resources.
|
||||
const massaged = output(outputs).apply(v => massage(v, new Set()));
|
||||
super.registerOutputs(massaged);
|
||||
}
|
||||
|
||||
return outputs;
|
||||
}
|
||||
}
|
||||
|
||||
async function massage(prop: any, seenObjects: Set<any>): Promise<any> {
|
||||
if (prop === undefined ||
|
||||
prop === null ||
|
||||
typeof prop === "boolean" ||
|
||||
typeof prop === "number" ||
|
||||
typeof prop === "string") {
|
||||
|
||||
return prop;
|
||||
}
|
||||
|
||||
if (prop instanceof Promise) {
|
||||
return await massage(await prop, seenObjects);
|
||||
}
|
||||
|
||||
if (Output.isInstance(prop)) {
|
||||
return await massage(await prop.promise(), seenObjects);
|
||||
}
|
||||
|
||||
// from this point on, we have complex objects. If we see them again, we don't want to emit
|
||||
// them again fully or else we'd loop infinitely.
|
||||
if (seenObjects.has(prop)) {
|
||||
// Note: for Resources we hit again, emit their urn so cycles can be easily understood
|
||||
// in the pojo objects.
|
||||
if (Resource.isInstance(prop)) {
|
||||
return await massage(prop.urn, seenObjects);
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
seenObjects.add(prop);
|
||||
|
||||
if (asset.Asset.isInstance(prop)) {
|
||||
if ((<asset.FileAsset>prop).path !== undefined) {
|
||||
return { path: (<asset.FileAsset>prop).path };
|
||||
}
|
||||
else if ((<asset.RemoteAsset>prop).uri !== undefined) {
|
||||
return { uri: (<asset.RemoteAsset>prop).uri };
|
||||
}
|
||||
else if ((<asset.StringAsset>prop).text !== undefined) {
|
||||
return { text: "..." };
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (asset.Archive.isInstance(prop)) {
|
||||
if ((<asset.AssetArchive>prop).assets) {
|
||||
return { assets: massage((<asset.AssetArchive>prop).assets, seenObjects) };
|
||||
}
|
||||
else if ((<asset.FileArchive>prop).path !== undefined) {
|
||||
return { path: (<asset.FileArchive>prop).path };
|
||||
}
|
||||
else if ((<asset.RemoteArchive>prop).uri !== undefined) {
|
||||
return { uri: (<asset.RemoteArchive>prop).uri };
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (Resource.isInstance(prop)) {
|
||||
// Emit a resource as a normal pojo. But filter out all our internal properties so that
|
||||
// they don't clutter the display/checkpoint with values not relevant to the application.
|
||||
return serializeAllKeys(n => !n.startsWith("__"));
|
||||
}
|
||||
|
||||
if (prop instanceof Array) {
|
||||
const result = [];
|
||||
for (let i = 0; i < prop.length; i++) {
|
||||
result[i] = await massage(prop[i], seenObjects);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return await serializeAllKeys(n => true);
|
||||
|
||||
async function serializeAllKeys(include: (name: string) => boolean) {
|
||||
const obj: Record<string, any> = {};
|
||||
for (const k of Object.keys(prop)) {
|
||||
if (include(k)) {
|
||||
obj[k] = await massage(prop[k], seenObjects);
|
||||
}
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue