pulumi/examples/dynamic-provider/simple/index.ts
Pat Gavlin f848090479 Return all computed inputs from Provider.Check.
As documented in issue #616, the inputs/defaults/outputs model we have
today has fundamental problems. The crux of the issue is that our
current design requires that defaults present in the old state of a
resource are applied to the new inputs for that resource.
Unfortunately, it is not possible for the engine to decide which
defaults remain applicable and which do not; only the provider has that
knowledge.

These changes take a more tactical approach to resolving this issue than
that originally proposed in #616 that avoids breaking compatibility with
existing checkpoints. Rather than treating the Pulumi inputs as the
provider input properties for a resource, these inputs are first
translated by `Check`. In order to accommodate provider defaults that
were chosen for the old resource but should not change for the new,
`Check` now takes the old provider inputs as well as the new Pulumi
inputs. Rather than the Pulumi inputs and provider defaults, the
provider inputs returned by `Check` are recorded in the checkpoint file.

Put simply, these changes remove defaults as a first-class concept
(except inasmuch as is required to retain the ability to read old
checkpoint files) and move the responsibilty for manging and
merging defaults into the provider that supplies them.

Fixes #616.
2017-12-03 09:33:16 -08:00

86 lines
3.4 KiB
TypeScript

// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
import * as pulumi from "pulumi";
import * as dynamic from "pulumi/dynamic";
class OperatorProvider implements dynamic.ResourceProvider {
private op: (l: number, r: number) => any;
constructor(op: (l: number, r: number) => any) {
this.op = op;
}
check = (olds: any, news: any) => Promise.resolve(new dynamic.CheckResult(news, []));
diff = (id: pulumi.ID, olds: any, news: any) => Promise.resolve(new dynamic.DiffResult([], []));
delete = (id: pulumi.ID, props: any) => Promise.resolve();
create = (inputs: any) => Promise.resolve(new dynamic.CreateResult("0", this.op(Number(inputs.left), Number(inputs.right))));
update = (id: string, olds: any, news: any) => Promise.resolve(new dynamic.UpdateResult(this.op(Number(news.left), Number(news.right))));
}
class DivProvider extends OperatorProvider {
constructor() {
super((left: number, right: number) => <any>{ quotient: Math.floor(left / right), remainder: left % right });
}
check = (olds: any, news: any) => Promise.resolve(new dynamic.CheckResult(news, news.right == 0 ? [ new dynamic.CheckFailure("right", "divisor must be non-zero") ] : []));
}
class Add extends dynamic.Resource {
public readonly sum: pulumi.Computed<number>;
private static provider = new OperatorProvider((left: number, right: number) => <any>{ sum: left + right });
constructor(name: string, left: pulumi.ComputedValue<number>, right: pulumi.ComputedValue<number>) {
super(Add.provider, name, {left: left, right: right, sum: undefined}, undefined);
}
}
class Mul extends dynamic.Resource {
public readonly product: pulumi.Computed<number>;
private static provider = new OperatorProvider((left: number, right: number) => <any>{ product: left * right });
constructor(name: string, left: pulumi.ComputedValue<number>, right: pulumi.ComputedValue<number>) {
super(Mul.provider, name, {left: left, right: right, product: undefined}, undefined);
}
}
class Sub extends dynamic.Resource {
public readonly difference: pulumi.Computed<number>;
private static provider = new OperatorProvider((left: number, right: number) => <any>{ difference: left - right });
constructor(name: string, left: pulumi.ComputedValue<number>, right: pulumi.ComputedValue<number>) {
super(Sub.provider, name, {left: left, right: right, difference: undefined}, undefined);
}
}
class Div extends dynamic.Resource {
public readonly quotient: pulumi.Computed<number>;
public readonly remainder: pulumi.Computed<number>;
private static provider = new DivProvider();
constructor(name: string, left: pulumi.ComputedValue<number>, right: pulumi.ComputedValue<number>) {
super(Div.provider, name, {left: left, right: right, quotient: undefined, remainder: undefined}, undefined);
}
}
let run = async () => {
let config = new pulumi.Config("simple:config");
let w = Number(config.require("w")), x = Number(config.require("x")), y = Number(config.require("y"));
let sum = new Add("sum", x, y);
let square = new Mul("square", sum.sum, sum.sum);
let diff = new Sub("diff", square.product, w);
let divrem = new Div("divrem", diff.difference, sum.sum);
let result = new Add("result", divrem.quotient, divrem.remainder);
console.log(`((x + y)^2 - w) / (x + y) + ((x + y)^2 - w) %% (x + y) = ${await result.sum}`);
};
run();