Skip to content

Commit 95e66c9

Browse files
committed
feat!: resource naming changed
1 parent a18493c commit 95e66c9

9 files changed

+211
-14
lines changed

src/index.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import * as crypto from 'crypto';
1+
//  import * as crypto from 'crypto';
2+
import { ResourceNaming } from '@gammarers/aws-resource-naming';
23
import * as cdk from 'aws-cdk-lib';
34
import * as iam from 'aws-cdk-lib/aws-iam';
45
import * as cr from 'aws-cdk-lib/custom-resources';
@@ -17,26 +18,40 @@ export enum ResponseField {
1718
HOST_ARN = 'HostArn',
1819
}
1920

21+
export interface ResourceNamingOptions {
22+
readonly naming: ResourceNaming.AutoNaming | ResourceNaming.DefaultNaming | {
23+
type: ResourceNaming.NamingType.CUSTOM;
24+
names: {
25+
functionName: string;
26+
functionRoleName: string;
27+
};
28+
};
29+
}
30+
2031
export interface CodeConnectionsHostCustomResourceProps {
2132
readonly name: string;
2233
readonly providerEndpoint: string;
2334
readonly providerType: CodeConnectionsHostProviderType;
35+
readonly resouceNamingOption?: ResourceNamingOptions;
2436
}
2537

2638
export class CodeConnectionsHostCustomResource extends cr.AwsCustomResource {
2739

2840
constructor(scope: Construct, id: string, props: CodeConnectionsHostCustomResourceProps) {
2941

3042
// 👇 Create random 8 length string
31-
const random: string = crypto.createHash('shake256', { outputLength: 4 })
32-
.update(`${cdk.Names.uniqueId(scope)}.${props.name}.${props.providerEndpoint}.${props.providerType}`)
33-
.digest('hex');
43+
const random = ResourceNaming.createRandomString(`${cdk.Names.uniqueId(scope)}.${props.name}.${props.providerEndpoint}.${props.providerType}`);
44+
const autoNaming = {
45+
functionName: `custom-resource-codeconnection-host-${random}-func`,
46+
functionRoleName: `custom-resource-codeconnection-host-${random}-func-exc-role`,
47+
};
48+
const naming = ResourceNaming.naming(autoNaming, props.resouceNamingOption);
3449

3550
const account = cdk.Stack.of(scope).account;
3651
const region = cdk.Stack.of(scope).region;
3752

3853
const functionRole = new iam.Role(scope, 'FunctionRole', {
39-
roleName: `custom-resource-codeconnection-host-${random}-func-exc-role`,
54+
roleName: naming.names.functionRoleName,
4055
description: 'Custom Resource Function Execution Role.',
4156
assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
4257
managedPolicies: [
@@ -62,7 +77,7 @@ export class CodeConnectionsHostCustomResource extends cr.AwsCustomResource {
6277
});
6378

6479
super(scope, id, {
65-
functionName: `custom-resource-codeconnection-host-${random}-func`,
80+
functionName: naming.names.functionName,
6681
role: functionRole,
6782
timeout: cdk.Duration.seconds(15),
6883
installLatestAwsSdk: true,

test/__snapshots__/custom-resource.bitbucket.default.test.ts.snap

Lines changed: 144 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/__snapshots__/custom-resource.github-enterprise.test.ts.snap

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/__snapshots__/custom-resource.github.test.ts.snap

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/__snapshots__/custom-resource.gitlab-self-managed.test.ts.snap

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/__snapshots__/custom-resource.gitlab.test.ts.snap

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { ResourceNaming } from '@gammarers/aws-resource-naming';
2+
import { App, Stack } from 'aws-cdk-lib';
3+
import { Template } from 'aws-cdk-lib/assertions';
4+
import * as codeconnections from 'aws-cdk-lib/aws-codeconnections';
5+
import { AwsCustomResource } from 'aws-cdk-lib/custom-resources';
6+
import { CodeConnectionsHostCustomResource, CodeConnectionsHostProviderType } from '../src';
7+
8+
describe('CustomResource Testing', () => {
9+
10+
const app = new App();
11+
const stack = new Stack(app, 'TestingStack', {
12+
env: {
13+
account: '123456789012',
14+
region: 'us-east-1',
15+
},
16+
});
17+
18+
const bitbucketConnectionHostCustomResource = new CodeConnectionsHostCustomResource(stack, 'BitBucketCodeConnectionsHostCustomResource', {
19+
name: 'bitbucket.example.com',
20+
providerEndpoint: 'https://bitbucket.example.com',
21+
providerType: CodeConnectionsHostProviderType.BIT_BUCKET,
22+
resouceNamingOption: {
23+
naming: {
24+
type: ResourceNaming.NamingType.AUTO,
25+
},
26+
},
27+
});
28+
29+
const hostArn = bitbucketConnectionHostCustomResource.findHostArn();
30+
31+
new codeconnections.CfnConnection(stack, 'Connection', {
32+
connectionName: 'example-bitbucket-connection',
33+
hostArn,
34+
});
35+
36+
const template = Template.fromStack(stack);
37+
38+
it('Is Bitbucket Connection Host CustomResource', () => {
39+
expect(bitbucketConnectionHostCustomResource).toBeInstanceOf(AwsCustomResource);
40+
});
41+
42+
it('Should match snapshot', () => {
43+
expect(template.toJSON()).toMatchSnapshot();
44+
});
45+
46+
});

0 commit comments

Comments
 (0)