This repository was archived by the owner on Dec 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovisioner.ts
57 lines (53 loc) · 2.5 KB
/
provisioner.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
// https://github.com/pulumi/examples/blob/master/aws-ts-ec2-provisioners/provisioners/provisioner.ts
// Copyright 2016-2019, Pulumi Corporation. All rights reserved.
import * as pulumi from "@pulumi/pulumi"
import * as uuid from "uuid"
// Provisioner lets a custom action run the first time a resource has been created. It takes as input
// a dependent property. Anytime its value changes, the resource is replaced and will re-run its logic.
export class Provisioner<T, U> extends pulumi.dynamic.Resource {
dep: pulumi.Output<T>
result: pulumi.Output<U>
constructor(name: string, props: ProvisionerProperties<T, U>, opts?: pulumi.CustomResourceOptions) {
const provider: pulumi.dynamic.ResourceProvider = {
diff: async (_: pulumi.ID, olds: State<T, U>, news: State<T, U>) => {
let replace = false
let replacementProperties = []
if (JSON.stringify(olds.dep) !== JSON.stringify(news.dep)) {
replace = true
replacementProperties.push("dep")
}
// Only trigger replacement due to the `changeToken` property, IFF
// the changeToken still has a value in the new inputs, and it doesn't
// match with the old value. If, say, the user decides to no longer specify
// the changeToken in the new inputs, then we don't trigger a replacement.
if (news.changeToken && olds.changeToken !== news.changeToken) {
replace = true
replacementProperties.push("changeToken")
}
return {
changes: replace,
replaces: replace ? replacementProperties : undefined,
deleteBeforeReplace: true,
}
},
create: async (inputs: State<T, U>) => {
const result = await props.onCreate(inputs.dep)
if (result !== undefined) {
inputs.result = result
}
return { id: uuid.v4(), outs: inputs }
},
}
super(provider, name, { dep: props.dep, changeToken: props.changeToken, result: null }, opts)
}
}
export interface ProvisionerProperties<T, U> {
changeToken: pulumi.Input<string>
dep: pulumi.Input<T>
onCreate: (dep: pulumi.Unwrap<T>) => Promise<pulumi.Unwrap<U>>
}
interface State<T, U> {
dep: pulumi.Unwrap<T>
changeToken: pulumi.Unwrap<string>
result: pulumi.Unwrap<U>
}