pulumi/sdk/nodejs/runtime/invoke.ts
joeduffy a2ae4accf4 Switch to parent pointers; display components nicely
This change switches from child lists to parent pointers, in the
way resource ancestries are represented.  This cleans up a fair bit
of the old parenting logic, including all notion of ambient parent
scopes (and will notably address pulumi/pulumi#435).

This lets us show a more parent/child display in the output when
doing planning and updating.  For instance, here is an update of
a lambda's text, which is logically part of a cloud timer:

    * cloud:timer:Timer: (same)
          [urn=urn:pulumi:malta::lm-cloud:☁️timer:Timer::lm-cts-malta-job-CleanSnapshots]
        * cloud:function:Function: (same)
              [urn=urn:pulumi:malta::lm-cloud:☁️function:Function::lm-cts-malta-job-CleanSnapshots]
            * aws:serverless:Function: (same)
                  [urn=urn:pulumi:malta::lm-cloud::aws:serverless:Function::lm-cts-malta-job-CleanSnapshots]
                ~ aws:lambda/function:Function: (modify)
                      [id=lm-cts-malta-job-CleanSnapshots-fee4f3bf41280741]
                      [urn=urn:pulumi:malta::lm-cloud::aws:lambda/function:Function::lm-cts-malta-job-CleanSnapshots]
                    - code            : archive(assets:2092f44) {
                        // etc etc etc

Note that we still get walls of text, but this will be actually
quite nice when combined with pulumi/pulumi#454.

I've also suppressed printing properties that didn't change during
updates when --detailed was not passed, and also suppressed empty
strings and zero-length arrays (since TF uses these as defaults in
many places and it just makes creation and deletion quite verbose).

Note that this is a far cry from everything we can possibly do
here as part of pulumi/pulumi#340 (and even pulumi/pulumi#417).
But it's a good start towards taming some of our output spew.
2017-11-26 08:14:01 -08:00

76 lines
3.3 KiB
TypeScript

// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
import * as log from "../log";
import { ComputedValues } from "../resource";
import { debuggablePromise } from "./debuggable";
import { deserializeProperties, PropertyTransfer, transferProperties } from "./rpc";
import { excessiveDebugOutput, getMonitor, options, rpcKeepAlive, serialize } from "./settings";
const resproto = require("../proto/resource_pb.js");
/**
* invoke dynamically invokes the function, tok, which is offered by a provider plugin. The inputs can be a bag of
* computed values (Ts or Promise<T>s), and the result is a Promise<any> that resolves when the invoke finishes.
*/
export function invoke(tok: string, props: ComputedValues | undefined): Promise<any> {
log.debug(`Invoking function: tok=${tok}` +
excessiveDebugOutput ? `, props=${JSON.stringify(props)}` : ``);
// Pre-allocate an error so we have a clean stack to print even if an asynchronous operation occurs.
const invokeError: Error = new Error(`Invoke of '${tok}' failed`);
// Now "transfer" all input properties; this simply awaits any promises and resolves when they all do.
const transfer: Promise<PropertyTransfer> = debuggablePromise(
transferProperties(undefined, `invoke:${tok}`, props, undefined));
const done: () => void = rpcKeepAlive();
return new Promise<any>(async (resolve, reject) => {
// Wait for all values to be available, and then perform the RPC.
try {
const result: PropertyTransfer = await transfer;
const obj: any = result.obj;
log.debug(`Invoke RPC prepared: tok=${tok}` + excessiveDebugOutput ? `, obj=${JSON.stringify(obj)}` : ``);
// Fetch the monitor and make an RPC request.
const monitor: any = getMonitor();
if (monitor) {
const req = new resproto.InvokeRequest();
req.setTok(tok);
req.setArgs(obj);
const resp: any = await debuggablePromise(new Promise((innerResolve, innerReject) => {
monitor.invoke(req, (err: Error, innerResponse: any) => {
log.debug(`Invoke RPC finished: tok=${tok}; err: ${err}, resp: ${innerResponse}`);
if (err) {
innerReject(err);
}
else {
innerResolve(innerResponse);
}
});
}));
// If there were failures, propagate them.
const failures: any = resp.getFailuresList();
if (failures && failures.length) {
throw new Error(`Invoke of '${tok}' failed: ${failures[0].reason} (${failures[0].property})`);
}
// Finally propagate any other properties that were given to us as outputs.
resolve(deserializeProperties(resp.getReturn()));
}
else {
// If the monitor doesn't exist, still make sure to resolve all properties to undefined.
log.debug(`Not sending Invoke RPC to monitor -- it doesn't exist: invoke tok=${tok}`);
resolve(undefined);
}
}
catch (err) {
reject(err);
}
finally {
done();
}
});
}