diff --git a/API.md b/API.md index 8897b96..5b322ed 100644 --- a/API.md +++ b/API.md @@ -214,6 +214,7 @@ Grant the given identity permissions to use this code star connetion. | isConstruct | Checks if `x` is a construct. | | isOwnedResource | Returns true if the construct was created by CDK, and false otherwise. | | isResource | Check whether the given construct is a Resource. | +| fromCodeStarConnectionArn | Import an externally defined Code Star Connection using its ARN. | --- @@ -281,6 +282,40 @@ Check whether the given construct is a Resource. --- +##### `fromCodeStarConnectionArn` + +```typescript +import { CodeStarConnection } from '@jttc/aws-codestarconnection' + +CodeStarConnection.fromCodeStarConnectionArn(scope: Construct, id: string, codestarConnectionArn: string) +``` + +Import an externally defined Code Star Connection using its ARN. + +###### `scope`Required + +- *Type:* constructs.Construct + +the construct that will "own" the imported key. + +--- + +###### `id`Required + +- *Type:* string + +the id of the imported code star conection in the construct tree. + +--- + +###### `codestarConnectionArn`Required + +- *Type:* string + +the ARN of an existing Code Star Connection. + +--- + #### Properties | **Name** | **Type** | **Description** | diff --git a/src/code-star-connection.ts b/src/code-star-connection.ts index ac8d391..c78da3a 100644 --- a/src/code-star-connection.ts +++ b/src/code-star-connection.ts @@ -426,6 +426,28 @@ export interface CodeStarConnectionProps { * } */ export class CodeStarConnection extends CodeStarConnectionBase { + /** + * Import an externally defined Code Star Connection using its ARN. + * + * @param scope the construct that will "own" the imported key. + * @param id the id of the imported code star conection in the construct tree. + * @param codestarConnectionArn the ARN of an existing Code Star Connection. + */ + public static fromCodeStarConnectionArn( + scope: Construct, + id: string, + codestarConnectionArn: string + ): ICodeStarConnection { + class Import extends CodeStarConnectionBase { + public connectionName = ''; + public connectionArn = codestarConnectionArn; + } + + return new Import(scope, id, { + environmentFromArn: codestarConnectionArn, + }); + } + public readonly connectionName: string; public readonly connectionArn: string; diff --git a/test/code-star-connection.test.ts b/test/code-star-connection.test.ts index 2139316..11cf985 100644 --- a/test/code-star-connection.test.ts +++ b/test/code-star-connection.test.ts @@ -225,3 +225,36 @@ describe('Grants permisions', () => { }); }); }); + +describe('Static methods', () => { + it('should return a instance of code star connection and use the grantUse method', () => { + // GIVEN + const app = new App(); + const stack = new Stack(app, 'TestStack'); + + // WHEN + const codestarConnection = CodeStarConnection.fromCodeStarConnectionArn( + stack, + 'CodeStarConnectionFromArn', + 'arn:aws:codestar-connections:eu-west-1:123456789012:connection/8c86942e-a7ca-4a4a-8b63-e2f7f5efaeee' + ); + + const role = new Role(stack, 'Role', { + assumedBy: new AnyPrincipal(), + }); + + codestarConnection.grantUse(role); + + Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', { + PolicyDocument: { + Statement: [ + { + Action: CodeStarConnectionPolicyActions.USE_CONNECTION, + Effect: 'Allow', + Resource: codestarConnection.connectionArn, + }, + ], + }, + }); + }); +});