|
| 1 | +import { Property, RelationshipRef, Resource, RichProperty, SpecDatabase } from '@aws-cdk/service-spec-types'; |
| 2 | +import { namespaceFromResource, referenceInterfaceName, referenceInterfaceAttributeName, referencePropertyName, typeAliasPrefixFromResource } from '../naming'; |
| 3 | +import { getReferenceProps } from './reference-props'; |
| 4 | +import { createModuleDefinitionFromCfnNamespace } from '../cfn2ts/pkglint'; |
| 5 | +import { log } from '../util'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Represents a cross-service property relationship that enables references |
| 9 | + * between resources from different AWS services. |
| 10 | + */ |
| 11 | +export interface Relationship { |
| 12 | + /** The TypeScript interface type that provides the reference (e.g. "IRoleRef") */ |
| 13 | + readonly referenceType: string; |
| 14 | + /** The property name on the reference interface that holds the reference object (e.g. "roleRef") */ |
| 15 | + readonly referenceName: string; |
| 16 | + /** The property to extract from the reference object (e.g. "roleArn") */ |
| 17 | + readonly propName: string; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Represents a selective import statement for cross-module type references. |
| 22 | + * Used to import specific types from other CDK modules when relationships |
| 23 | + * are between different modules. |
| 24 | + */ |
| 25 | +export interface SelectiveImport { |
| 26 | + /** The module name to import from */ |
| 27 | + readonly moduleName: string; |
| 28 | + /** Array of types that need to be imported */ |
| 29 | + readonly types: { |
| 30 | + /** The original type name in the source module */ |
| 31 | + originalType: string; |
| 32 | + /** The aliased name to avoid naming conflicts */ |
| 33 | + aliasedType: string; |
| 34 | + }[]; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Extracts resource relationship information from the database for cross-service property references. |
| 39 | + */ |
| 40 | +export class RelationshipDecider { |
| 41 | + private readonly namespace: string; |
| 42 | + public readonly imports = new Array<SelectiveImport>(); |
| 43 | + |
| 44 | + constructor(readonly resource: Resource, private readonly db: SpecDatabase) { |
| 45 | + this.namespace = namespaceFromResource(resource); |
| 46 | + } |
| 47 | + |
| 48 | + private registerRequiredImport({ namespace, originalType, aliasedType }: { |
| 49 | + namespace: string; |
| 50 | + originalType: string; |
| 51 | + aliasedType: string; |
| 52 | + }) { |
| 53 | + const moduleName = createModuleDefinitionFromCfnNamespace(namespace).moduleName; |
| 54 | + const moduleImport = this.imports.find(i => i.moduleName === moduleName); |
| 55 | + if (!moduleImport) { |
| 56 | + this.imports.push({ |
| 57 | + moduleName, |
| 58 | + types: [{ originalType, aliasedType }], |
| 59 | + }); |
| 60 | + } else { |
| 61 | + if (!moduleImport.types.find(t => |
| 62 | + t.originalType === originalType && t.aliasedType === aliasedType, |
| 63 | + )) { |
| 64 | + moduleImport.types.push({ originalType, aliasedType }); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Retrieves the target resource for a relationship. |
| 71 | + * Returns undefined if the target property cannot be found in the reference |
| 72 | + * properties as a relationship can only target a primary identifier or arn |
| 73 | + */ |
| 74 | + private findTargetResource(sourcePropName: string, relationship: RelationshipRef) { |
| 75 | + const targetResource = this.db.lookup('resource', 'cloudFormationType', 'equals', relationship.cloudFormationType).only(); |
| 76 | + const refProps = getReferenceProps(targetResource); |
| 77 | + const expectedPropName = referencePropertyName(relationship.propertyName, targetResource.name); |
| 78 | + const prop = refProps.find(p => p.declaration.name === expectedPropName); |
| 79 | + if (!prop) { |
| 80 | + log.debug( |
| 81 | + 'Could not find target prop for relationship:', |
| 82 | + `${this.resource.cloudFormationType} ${sourcePropName}`, |
| 83 | + `=> ${targetResource.cloudFormationType} ${relationship.propertyName}`, |
| 84 | + ); |
| 85 | + return undefined; |
| 86 | + } |
| 87 | + return targetResource; |
| 88 | + } |
| 89 | + |
| 90 | + public parseRelationship(sourcePropName: string, relationships?: RelationshipRef[]) { |
| 91 | + const parsedRelationships: Relationship[] = []; |
| 92 | + if (!relationships) { |
| 93 | + return parsedRelationships; |
| 94 | + } |
| 95 | + for (const relationship of relationships) { |
| 96 | + const targetResource = this.findTargetResource(sourcePropName, relationship); |
| 97 | + if (!targetResource) { |
| 98 | + continue; |
| 99 | + } |
| 100 | + // Ignore the suffix part because it's an edge case that happens only for one module |
| 101 | + const interfaceName = referenceInterfaceName(targetResource.name); |
| 102 | + const refPropStructName = referenceInterfaceAttributeName(targetResource.name); |
| 103 | + |
| 104 | + const targetNamespace = namespaceFromResource(targetResource); |
| 105 | + let aliasedTypeName = undefined; |
| 106 | + if (this.namespace !== targetNamespace) { |
| 107 | + // If this is not in our namespace we need to alias the import type |
| 108 | + aliasedTypeName = `${typeAliasPrefixFromResource(targetResource)}${interfaceName}`; |
| 109 | + this.registerRequiredImport({ namespace: targetNamespace, originalType: interfaceName, aliasedType: aliasedTypeName }); |
| 110 | + } |
| 111 | + parsedRelationships.push({ |
| 112 | + referenceType: aliasedTypeName ?? interfaceName, |
| 113 | + referenceName: refPropStructName, |
| 114 | + propName: referencePropertyName(relationship.propertyName, targetResource.name), |
| 115 | + }); |
| 116 | + } |
| 117 | + return parsedRelationships; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Extracts the referenced type from a property's type, for direct refs and array element refs. |
| 122 | + */ |
| 123 | + private getReferencedType(prop: Property) { |
| 124 | + // Use the oldest type for backwards compatibility |
| 125 | + const type = new RichProperty(prop).types()[0]; |
| 126 | + if (type.type === 'ref') { |
| 127 | + return this.db.get('typeDefinition', type.reference.$ref); |
| 128 | + } else if (type.type === 'array' && type.element.type === 'ref') { |
| 129 | + return this.db.get('typeDefinition', type.element.reference.$ref); |
| 130 | + } |
| 131 | + return undefined; |
| 132 | + } |
| 133 | + |
| 134 | + private hasValidRelationships(sourcePropName: string, relationships?: RelationshipRef[]): boolean { |
| 135 | + if (!relationships) { |
| 136 | + return false; |
| 137 | + } |
| 138 | + return relationships.some(rel => this.findTargetResource(sourcePropName, rel) !== undefined); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Checks if a given property needs a flattening function or not |
| 143 | + */ |
| 144 | + public needsFlatteningFunction(propName: string, prop: Property, visited = new Set<string>()): boolean { |
| 145 | + if (this.hasValidRelationships(propName, prop.relationshipRefs)) { |
| 146 | + return true; |
| 147 | + } |
| 148 | + |
| 149 | + const referencedTypeDef = this.getReferencedType(prop); |
| 150 | + if (!referencedTypeDef) { |
| 151 | + return false; |
| 152 | + } |
| 153 | + |
| 154 | + if (visited.has(referencedTypeDef.$id)) { |
| 155 | + return false; |
| 156 | + } |
| 157 | + visited.add(referencedTypeDef.$id); |
| 158 | + |
| 159 | + for (const [nestedPropName, nestedProp] of Object.entries(referencedTypeDef.properties)) { |
| 160 | + if (this.needsFlatteningFunction(nestedPropName, nestedProp, visited)) { |
| 161 | + return true; |
| 162 | + } |
| 163 | + } |
| 164 | + return false; |
| 165 | + } |
| 166 | +} |
0 commit comments