/
index.ts
72 lines (61 loc) 路 2.87 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
import * as pulumi from "@pulumi/pulumi";
class Resource extends pulumi.ComponentResource {
constructor(name: string, opts?: pulumi.ComponentResourceOptions) {
super("my:module:Resource", name, {}, opts);
}
}
// Scenario #2 - adopt a resource into a component. The component author is the same as the component user, and changes
// the component to be able to adopt the resource that was previously defined separately...
class Component extends pulumi.ComponentResource {
resource: Resource;
constructor(name: string, opts?: pulumi.ComponentResourceOptions) {
super("my:module:Component", name, {}, opts);
// The resource creation was moved from top level to inside the component.
this.resource = new Resource(`${name}-child`, {
// With a new parent
parent: this,
// But with an alias provided based on knowing where the resource existing before - in this case at top
// level. We use an absolute URN instead of a relative `Alias` because we are referencing a fixed resource
// that was in some arbitrary other location in the hierarchy prior to being adopted into this component.
aliases: [pulumi.createUrn("res2", "my:module:Resource")],
});
}
}
// The creation of the component is unchanged.
const comp2 = new Component("comp2");
// Scenario 3: adopt this resource into a new parent.
class Component2 extends pulumi.ComponentResource {
constructor(name: string, opts?: pulumi.ComponentResourceOptions) {
super("my:module:Component2", name, {}, opts);
}
}
// validate that "parent: undefined" means "i didn't have a parent previously"
new Component2("unparented", {
aliases: [{ parent: pulumi.rootStackResource }],
parent: comp2,
});
// Scenario 4: Make a child resource that is parented by opts instead of 'this'. Fix
// in the next step to be parented by this. Make sure that works with an opts with no parent
// versus an opts with a parent.
class Component3 extends pulumi.ComponentResource {
constructor(name: string, opts: pulumi.ComponentResourceOptions = {}) {
super("my:module:Component3", name, {}, opts);
new Component2(name + "-child", { aliases: [{ parent: opts.parent }], parent: this });
}
}
new Component3("parentedbystack");
new Component3("parentedbycomponent", { parent: comp2 });
// Scenario 5: Allow multiple aliases to the same resource.
class Component4 extends pulumi.ComponentResource {
constructor(name: string, opts: pulumi.ComponentResourceOptions = {}) {
super("my:module:Component4", name, {}, {
aliases: [
{ parent: pulumi.rootStackResource },
{ parent: pulumi.rootStackResource },
],
...opts,
});
}
}
new Component4("duplicateAliases", { parent: comp2 });