|
1 | 1 | import { Resource } from '@aws-cdk/service-spec-types'; |
2 | | -import { $E, expr, Expression, PropertySpec, Type } from '@cdklabs/typewriter'; |
| 2 | +import { $this, expr, Expression, PropertySpec, Type } from '@cdklabs/typewriter'; |
3 | 3 | import { attributePropertyName, referencePropertyName } from '../naming'; |
4 | | -import { findNonIdentifierArnProperty } from './arn'; |
| 4 | +import { extractResourceVariablesFromArnFormat, findArnProperty, findNonIdentifierArnProperty } from './arn'; |
5 | 5 | import { CDK_CORE } from './cdk'; |
6 | 6 |
|
7 | 7 | export interface ReferenceProp { |
8 | 8 | readonly declaration: PropertySpec; |
9 | 9 | readonly cfnValue: Expression; |
10 | 10 | } |
11 | 11 |
|
12 | | -// Convenience typewriter builder |
13 | | -const $this = $E(expr.this_()); |
14 | | - |
15 | | -export function getReferenceProps(resource: Resource): ReferenceProp[] { |
16 | | - const referenceProps = []; |
17 | | - // Primary identifier. We assume all parts are strings. |
18 | | - const primaryIdentifier = resource.primaryIdentifier ?? []; |
19 | | - if (primaryIdentifier.length === 1) { |
20 | | - referenceProps.push({ |
21 | | - declaration: { |
22 | | - name: referencePropertyName(primaryIdentifier[0], resource.name), |
23 | | - type: Type.STRING, |
24 | | - immutable: true, |
25 | | - docs: { |
26 | | - summary: `The ${primaryIdentifier[0]} of the ${resource.name} resource.`, |
| 12 | +export class ResourceReference { |
| 13 | + public readonly resource: Resource; |
| 14 | + public readonly arnPropertyName?: string; |
| 15 | + |
| 16 | + public get referenceProps(): ReferenceProp[] { |
| 17 | + return [...this._referenceProps.values()]; |
| 18 | + } |
| 19 | + |
| 20 | + public get arnVariables(): Record<string, string> | undefined { |
| 21 | + return this._arnVariables.length ? Object.fromEntries(this._arnVariables) : undefined; |
| 22 | + } |
| 23 | + |
| 24 | + public get hasArnGetter(): boolean { |
| 25 | + return this.arnPropertyName != null || this.arnVariables != null; |
| 26 | + } |
| 27 | + |
| 28 | + private _arnVariables: [string, string][] = []; |
| 29 | + private readonly _referenceProps = new FirstOccurrenceMap<string, ReferenceProp>(); |
| 30 | + |
| 31 | + public constructor(resource: Resource) { |
| 32 | + this.resource = resource; |
| 33 | + this.arnPropertyName = this.findArnPropertyName(); |
| 34 | + this._referenceProps = new FirstOccurrenceMap<string, ReferenceProp>(); |
| 35 | + this.collectReferencesProps(); |
| 36 | + } |
| 37 | + |
| 38 | + private findArnPropertyName(): string | undefined { |
| 39 | + const arnProp = findArnProperty(this.resource); |
| 40 | + if (!arnProp) { |
| 41 | + return; |
| 42 | + } |
| 43 | + return referencePropertyName(arnProp, this.resource.name); |
| 44 | + } |
| 45 | + |
| 46 | + private collectReferencesProps() { |
| 47 | + // Primary identifier. We assume all parts are strings. |
| 48 | + const primaryIdentifier = this.resource.primaryIdentifier ?? []; |
| 49 | + if (primaryIdentifier.length === 1) { |
| 50 | + const name = referencePropertyName(primaryIdentifier[0], this.resource.name); |
| 51 | + this._referenceProps.setIfAbsent(name, { |
| 52 | + declaration: { |
| 53 | + name, |
| 54 | + type: Type.STRING, |
| 55 | + immutable: true, |
| 56 | + docs: { |
| 57 | + summary: `The ${primaryIdentifier[0]} of the ${this.resource.name} resource.`, |
| 58 | + }, |
27 | 59 | }, |
28 | | - }, |
29 | | - cfnValue: $this.ref, |
30 | | - }); |
31 | | - } else if (primaryIdentifier.length > 1) { |
32 | | - for (const [i, cfnName] of enumerate(primaryIdentifier)) { |
33 | | - referenceProps.push({ |
| 60 | + cfnValue: $this.ref, |
| 61 | + }); |
| 62 | + } else if (primaryIdentifier.length > 1) { |
| 63 | + for (const [i, cfnName] of primaryIdentifier.entries()) { |
| 64 | + const name = referencePropertyName(cfnName, this.resource.name); |
| 65 | + this._referenceProps.setIfAbsent(name, { |
| 66 | + declaration: { |
| 67 | + name, |
| 68 | + type: Type.STRING, |
| 69 | + immutable: true, |
| 70 | + docs: { |
| 71 | + summary: `The ${cfnName} of the ${this.resource.name} resource.`, |
| 72 | + }, |
| 73 | + }, |
| 74 | + cfnValue: splitSelect('|', i, $this.ref), |
| 75 | + }); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // Arn identifier |
| 80 | + const arnProp = findNonIdentifierArnProperty(this.resource); |
| 81 | + if (arnProp) { |
| 82 | + const name = referencePropertyName(arnProp, this.resource.name); |
| 83 | + this._referenceProps.setIfAbsent(name, { |
34 | 84 | declaration: { |
35 | | - name: referencePropertyName(cfnName, resource.name), |
| 85 | + name, |
36 | 86 | type: Type.STRING, |
37 | 87 | immutable: true, |
38 | 88 | docs: { |
39 | | - summary: `The ${cfnName} of the ${resource.name} resource.`, |
| 89 | + summary: `The ARN of the ${this.resource.name} resource.`, |
40 | 90 | }, |
41 | 91 | }, |
42 | | - cfnValue: splitSelect('|', i, $this.ref), |
| 92 | + cfnValue: $this[attributePropertyName(arnProp)], |
43 | 93 | }); |
44 | 94 | } |
| 95 | + |
| 96 | + // If we do not have an ARN prop, see if we can construct it from the arn template |
| 97 | + if (this.resource.arnTemplate && !this.arnPropertyName) { |
| 98 | + const variables = extractResourceVariablesFromArnFormat(this.resource.arnTemplate); |
| 99 | + const allResolved = variables.every(v => this.tryFindUsableValue(v)); |
| 100 | + if (allResolved) { |
| 101 | + for (const variable of variables) { |
| 102 | + const access = this.tryFindUsableValue(variable)!; |
| 103 | + const refPropName = referencePropertyName(variable, this.resource.name); |
| 104 | + this._arnVariables.push([variable, refPropName]); |
| 105 | + this._referenceProps.setIfAbsent(refPropName, { |
| 106 | + declaration: { |
| 107 | + name: refPropName, |
| 108 | + type: Type.STRING, |
| 109 | + immutable: true, |
| 110 | + docs: { |
| 111 | + summary: `The ${variable} of the ${this.resource.name} resource.`, |
| 112 | + }, |
| 113 | + }, |
| 114 | + cfnValue: access, |
| 115 | + }); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
45 | 119 | } |
46 | 120 |
|
47 | | - const arnProp = findNonIdentifierArnProperty(resource); |
48 | | - if (arnProp) { |
49 | | - referenceProps.push({ |
50 | | - declaration: { |
51 | | - name: referencePropertyName(arnProp, resource.name), |
52 | | - type: Type.STRING, |
53 | | - immutable: true, |
54 | | - docs: { |
55 | | - summary: `The ARN of the ${resource.name} resource.`, |
56 | | - }, |
57 | | - }, |
58 | | - cfnValue: $this[attributePropertyName(arnProp)], |
59 | | - }); |
| 121 | + public tryFindUsableValue(name: string): Expression| undefined { |
| 122 | + const refPropName = referencePropertyName(name, this.resource.name); |
| 123 | + |
| 124 | + // an already discovered reference prop |
| 125 | + if (this._referenceProps.has(refPropName)) { |
| 126 | + return this._referenceProps.get(refPropName)!.cfnValue; |
| 127 | + } |
| 128 | + // an attribute |
| 129 | + if (this.resource.attributes[name]) { |
| 130 | + return $this[attributePropertyName(name)]; |
| 131 | + } |
| 132 | + // a required prop |
| 133 | + if (this.resource.properties[name]?.required) { |
| 134 | + return $this[refPropName]; |
| 135 | + } |
| 136 | + |
| 137 | + // we don't have a matching value |
| 138 | + return undefined; |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +class FirstOccurrenceMap<K, V> extends Map<K, V> { |
| 143 | + setIfAbsent(key: K, value: V): void { |
| 144 | + if (!this.has(key)) this.set(key, value); |
60 | 145 | } |
61 | | - return referenceProps; |
62 | 146 | } |
63 | 147 |
|
64 | 148 | function splitSelect(sep: string, n: number, base: Expression) { |
65 | 149 | return CDK_CORE.Fn.select(expr.lit(n), CDK_CORE.Fn.split(expr.lit(sep), base)); |
66 | 150 | } |
67 | 151 |
|
68 | | -function enumerate<A>(xs: A[]): Array<[number, A]> { |
69 | | - return xs.map((x, i) => [i, x]); |
70 | | -} |
|
0 commit comments