Skip to content

Commit d79cbcc

Browse files
committed
fix: generate addArnFor<Resource> method for more resources
1 parent 10de047 commit d79cbcc

20 files changed

Lines changed: 490 additions & 146 deletions

File tree

aws-cdk.code-workspace

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,14 @@
77
"type": "on-demand"
88
},
99
"jest.virtualFolders": [
10-
{
11-
"name": "aws-cdk",
12-
"rootPath": "packages/aws-cdk"
13-
},
1410
{
1511
"name": "aws-cdk-lib",
1612
"rootPath": "packages/aws-cdk-lib"
1713
},
18-
{
19-
"name": "cli-lib-alpha",
20-
"rootPath": "packages/@aws-cdk/cli-lib-alpha"
21-
},
22-
{
23-
"name": "cloudformation-diff",
24-
"rootPath": "packages/@aws-cdk/cloudformation-diff"
25-
},
2614
{
2715
"name": "custom-resource-handlers",
2816
"rootPath": "packages/@aws-cdk/custom-resource-handlers"
2917
},
30-
{
31-
"name": "integ-runner",
32-
"rootPath": "packages/@aws-cdk/integ-runner"
33-
},
3418
{
3519
"name": "integ-tests-alpha",
3620
"rootPath": "packages/@aws-cdk/integ-tests-alpha"
@@ -39,17 +23,13 @@
3923
"name": "aws-custom-resource-sdk-adapter",
4024
"rootPath": "packages/@aws-cdk/aws-custom-resource-sdk-adapter"
4125
},
42-
{
43-
"name": "toolkit",
44-
"rootPath": "packages/@aws-cdk/toolkit"
45-
},
46-
{
47-
"name": "user-input-gen",
48-
"rootPath": "tools/@aws-cdk/user-input-gen"
49-
},
5026
{
5127
"name": "mixins",
5228
"rootPath": "packages/@aws-cdk/mixins-preview"
29+
},
30+
{
31+
"name": "spec2cdk",
32+
"rootPath": "tools/@aws-cdk/spec2cdk"
5333
}
5434
]
5535
},
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './cfn-props-mixins.generated';
2+
export * from './logs-delivery-mixins.generated';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './events.generated';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export * as events from './events';
12
export * as mixins from './mixins';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './cfn-props-mixins.generated';
2+
export * from './logs-delivery-mixins.generated';

packages/@aws-cdk/mixins-preview/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@
486486
"./aws_s3vectors": "./lib/services/aws-s3vectors/index.js",
487487
"./aws_s3vectors/mixins": "./lib/services/aws-s3vectors/mixins.js",
488488
"./aws_sagemaker": "./lib/services/aws-sagemaker/index.js",
489+
"./aws_sagemaker/events": "./lib/services/aws-sagemaker/events.js",
489490
"./aws_sagemaker/mixins": "./lib/services/aws-sagemaker/mixins.js",
490491
"./aws_sam": "./lib/services/aws-sam/index.js",
491492
"./aws_sam/mixins": "./lib/services/aws-sam/mixins.js",

packages/@aws-cdk/mixins-preview/scripts/spec2eventbridge/builder.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import { LibraryBuilder } from '@aws-cdk/spec2cdk/lib/cdk/library-builder';
99
import type { LocatedModule, ServiceSubmoduleProps } from '@aws-cdk/spec2cdk/lib/cdk/service-submodule';
1010
import { BaseServiceSubmodule } from '@aws-cdk/spec2cdk/lib/cdk/service-submodule';
1111
import { eventNamespaceName } from '@aws-cdk/spec2cdk/lib/naming';
12-
import type { ReferenceProp } from '@aws-cdk/spec2cdk/lib/cdk/reference-props';
13-
import { getReferenceProps } from '@aws-cdk/spec2cdk/lib/cdk/reference-props';
12+
import { ResourceReference, type ReferenceProp } from '@aws-cdk/spec2cdk/lib/cdk/reference-props';
1413
import { log } from '@aws-cdk/spec2cdk/lib/util';
1514

1615
/**
@@ -136,7 +135,7 @@ class EventBridgeEventsClass extends ClassType {
136135

137136
this.refInterface = Type.fromName(constructLibModule, naming.referenceInterfaceName(resource.name));
138137
this.referenceName = naming.referenceInterfaceAttributeName(resource.name);
139-
this.referenceProps = getReferenceProps(resource);
138+
this.referenceProps = new ResourceReference(resource).referenceProps;
140139
}
141140

142141
public build() {

packages/@aws-cdk/mixins-preview/scripts/spec2logs/builder.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,7 @@ import { BaseServiceSubmodule, relativeImportPath } from '@aws-cdk/spec2cdk/lib/
99
import type { AddServiceProps, LibraryBuilderProps } from '@aws-cdk/spec2cdk/lib/cdk/library-builder';
1010
import { LibraryBuilder } from '@aws-cdk/spec2cdk/lib/cdk/library-builder';
1111
import { MIXINS_CORE } from '../spec2mixins/helpers';
12-
13-
// we cannot currently get an Arn for these
14-
const EXCLUDE: string[] = [
15-
'AWS::BedrockAgentCore::Runtime',
16-
'AWS::ElastiCache::CacheCluster',
17-
'AWS::Grafana::Workspace',
18-
'AWS::RUM::AppMonitor',
19-
'AWS::SageMaker::Workteam',
20-
];
12+
import { ResourceReference } from '@aws-cdk/spec2cdk/lib/cdk/reference-props';
2113

2214
class LogsDeliveryBuilderServiceModule extends BaseServiceSubmodule {
2315
public readonly constructLibModule: ExternalModule;
@@ -47,7 +39,8 @@ export class LogsDeliveryBuilder extends LibraryBuilder<LogsDeliveryBuilderServi
4739
}
4840

4941
protected addResourceToSubmodule(submodule: LogsDeliveryBuilderServiceModule, resource: Resource, _props?: AddServiceProps): void {
50-
if (resource.vendedLogs && !EXCLUDE.includes(resource.cloudFormationType)) {
42+
const resourceReference = new ResourceReference(resource);
43+
if (resource.vendedLogs && resourceReference.hasArnGetter) {
5144
const service = this.db.incoming('hasResource', resource).only().entity;
5245
const logsModule = this.obtainLogsDeliveryModule(submodule, service);
5346

packages/@aws-cdk/mixins-preview/test/events/l1-l2.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ describe.each([
5656
const asgEvents = AutoScalingGroupEvents.fromAutoScalingGroup(new class extends Construct {
5757
public readonly autoScalingGroupRef: AutoScalingGroupReference = {
5858
autoScalingGroupName: 'asdf',
59+
autoScalingGroupArn: 'arn:aws:autoscaling:us-west-2:123456789012:autoScalingGroup:1a2b3c4d-5e6f-7a8b-9c0d-123456789012:autoScalingGroupName/MyAutoScalingGroup',
5960
};
6061
public readonly env = { account: '11111111111', region: 'us-east-1' };
6162
}(stack, 'Group'));

tools/@aws-cdk/spec2cdk/lib/cdk/arn.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,27 @@ export function findNonIdentifierArnProperty(resource: Resource) {
1010
return findArnProperty(resource, (name) => !resource.primaryIdentifier?.includes(name));
1111
}
1212

13-
export function findArnProperty(resource: Resource, filter: (name: string) => boolean = () => true): any {
14-
const possibleArnNames = ['Arn', `${resource.name}Arn`];
13+
export function findArnProperty(resource: Resource, filter: (name: string) => boolean = () => true): string | undefined {
14+
const prefixes = ['', resource.name];
15+
const suffixes = ['Arn', 'ARN'];
16+
const primaryIdentifierSuffixes = ['Id', 'ID'];
17+
18+
// if the primary identifier uses a prefix that is different than the resource name, we add that to the list
19+
if (resource.primaryIdentifier?.length === 1) {
20+
for (const suffix of primaryIdentifierSuffixes) {
21+
if (resource.primaryIdentifier[0].endsWith(suffix)) {
22+
const prefix = resource.primaryIdentifier[0].slice(0, -suffix.length);
23+
if (prefix && !prefixes.includes(prefix)) {
24+
prefixes.push(prefix);
25+
}
26+
break;
27+
}
28+
}
29+
}
30+
31+
// this is a combination of all prefixes with all suffixes, order by prefixes as they appear in the list
32+
const possibleArnNames = prefixes.flatMap(prefix => suffixes.map(suffix => prefix + suffix));
33+
1534
for (const name of possibleArnNames) {
1635
const prop = resource.attributes[name];
1736
if (prop && filter(name)) {
@@ -21,3 +40,27 @@ export function findArnProperty(resource: Resource, filter: (name: string) => bo
2140
return undefined;
2241
}
2342

43+
/**
44+
* Extracts all variables from an ARN Format template
45+
*
46+
* @example
47+
* extractVariablesFromArnFormat("arn:${Partition}:sagemaker:${Region}:${Account}:workteam/${WorkteamName}")
48+
* // returns ['Partition', 'Region', 'Account', 'WorkteamName']
49+
*/
50+
export function extractVariablesFromArnFormat(format: string): string[] {
51+
return (format.match(/\${([^{}]+)}/g) || []).map(match => match.slice(2, -1));
52+
}
53+
54+
/**
55+
* Extracts all resource specific variables from an ARN Format template.
56+
*
57+
* To get all variables, use `extractVariablesFromArnFormat(format)`
58+
*
59+
* @example
60+
* extractVariablesFromArnFormat("arn:${Partition}:sagemaker:${Region}:${Account}:workteam/${WorkteamName}")
61+
* // returns ['WorkteamName']
62+
*/
63+
export function extractResourceVariablesFromArnFormat(format: string) {
64+
const [_arn, _partition, _service, _region, _account, ...rest] = format.split(':');
65+
return extractVariablesFromArnFormat(rest.join(':'));
66+
}

0 commit comments

Comments
 (0)