|
| 1 | +import * as iam from '@aws-cdk/aws-iam'; |
| 2 | +import * as s3 from '@aws-cdk/aws-s3'; |
| 3 | +import * as cdk from '@aws-cdk/core'; |
| 4 | +import { CfnReportGroup } from './codebuild.generated'; |
| 5 | +import { renderReportGroupArn, reportGroupArnComponents } from './report-group-utils'; |
| 6 | + |
| 7 | +/** |
| 8 | + * The interface representing the ReportGroup resource - |
| 9 | + * either an existing one, imported using the |
| 10 | + * {@link ReportGroup.fromReportGroupName} method, |
| 11 | + * or a new one, created with the {@link ReportGroup} class. |
| 12 | + */ |
| 13 | +export interface IReportGroup extends cdk.IResource { |
| 14 | + /** |
| 15 | + * The ARN of the ReportGroup. |
| 16 | + * |
| 17 | + * @attribute |
| 18 | + */ |
| 19 | + readonly reportGroupArn: string; |
| 20 | + |
| 21 | + /** |
| 22 | + * The name of the ReportGroup. |
| 23 | + * |
| 24 | + * @attribute |
| 25 | + */ |
| 26 | + readonly reportGroupName: string; |
| 27 | + |
| 28 | + /** |
| 29 | + * Grants the given entity permissions to write |
| 30 | + * (that is, upload reports to) |
| 31 | + * this report group. |
| 32 | + */ |
| 33 | + grantWrite(identity: iam.IGrantable): iam.Grant; |
| 34 | +} |
| 35 | + |
| 36 | +abstract class ReportGroupBase extends cdk.Resource implements IReportGroup { |
| 37 | + public abstract readonly reportGroupArn: string; |
| 38 | + public abstract readonly reportGroupName: string; |
| 39 | + protected abstract readonly exportBucket?: s3.IBucket; |
| 40 | + |
| 41 | + public grantWrite(identity: iam.IGrantable): iam.Grant { |
| 42 | + const ret = iam.Grant.addToPrincipal({ |
| 43 | + grantee: identity, |
| 44 | + actions: [ |
| 45 | + 'codebuild:CreateReport', |
| 46 | + 'codebuild:UpdateReport', |
| 47 | + 'codebuild:BatchPutTestCases', |
| 48 | + ], |
| 49 | + resourceArns: [this.reportGroupArn], |
| 50 | + }); |
| 51 | + |
| 52 | + if (this.exportBucket) { |
| 53 | + this.exportBucket.grantWrite(identity); |
| 54 | + } |
| 55 | + |
| 56 | + return ret; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Construction properties for {@link ReportGroup}. |
| 62 | + */ |
| 63 | +export interface ReportGroupProps { |
| 64 | + /** |
| 65 | + * The physical name of the report group. |
| 66 | + * |
| 67 | + * @default - CloudFormation-generated name |
| 68 | + */ |
| 69 | + readonly reportGroupName?: string; |
| 70 | + |
| 71 | + /** |
| 72 | + * An optional S3 bucket to export the reports to. |
| 73 | + * |
| 74 | + * @default - the reports will not be exported |
| 75 | + */ |
| 76 | + readonly exportBucket?: s3.IBucket; |
| 77 | + |
| 78 | + /** |
| 79 | + * Whether to output the report files into the export bucket as-is, |
| 80 | + * or create a ZIP from them before doing the export. |
| 81 | + * Ignored if {@link exportBucket} has not been provided. |
| 82 | + * |
| 83 | + * @default - false (the files will not be ZIPped) |
| 84 | + */ |
| 85 | + readonly zipExport?: boolean; |
| 86 | + |
| 87 | + /** |
| 88 | + * What to do when this resource is deleted from a stack. |
| 89 | + * As CodeBuild does not allow deleting a ResourceGroup that has reports inside of it, |
| 90 | + * this is set to retain the resource by default. |
| 91 | + * |
| 92 | + * @default RemovalPolicy.RETAIN |
| 93 | + */ |
| 94 | + readonly removalPolicy?: cdk.RemovalPolicy; |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * The ReportGroup resource class. |
| 99 | + */ |
| 100 | +export class ReportGroup extends ReportGroupBase { |
| 101 | + |
| 102 | + /** |
| 103 | + * Reference an existing ReportGroup, |
| 104 | + * defined outside of the CDK code, |
| 105 | + * by name. |
| 106 | + */ |
| 107 | + public static fromReportGroupName(scope: cdk.Construct, id: string, reportGroupName: string): IReportGroup { |
| 108 | + class Import extends ReportGroupBase { |
| 109 | + public readonly reportGroupName = reportGroupName; |
| 110 | + public readonly reportGroupArn = renderReportGroupArn(scope, reportGroupName); |
| 111 | + protected readonly exportBucket = undefined; |
| 112 | + } |
| 113 | + |
| 114 | + return new Import(scope, id); |
| 115 | + } |
| 116 | + |
| 117 | + public readonly reportGroupArn: string; |
| 118 | + public readonly reportGroupName: string; |
| 119 | + protected readonly exportBucket?: s3.IBucket; |
| 120 | + |
| 121 | + constructor(scope: cdk.Construct, id: string, props: ReportGroupProps = {}) { |
| 122 | + super(scope, id, { |
| 123 | + physicalName: props.reportGroupName, |
| 124 | + }); |
| 125 | + |
| 126 | + const resource = new CfnReportGroup(this, 'Resource', { |
| 127 | + type: 'TEST', |
| 128 | + exportConfig: { |
| 129 | + exportConfigType: props.exportBucket ? 'S3' : 'NO_EXPORT', |
| 130 | + s3Destination: props.exportBucket |
| 131 | + ? { |
| 132 | + bucket: props.exportBucket.bucketName, |
| 133 | + encryptionDisabled: props.exportBucket.encryptionKey ? false : undefined, |
| 134 | + encryptionKey: props.exportBucket.encryptionKey?.keyArn, |
| 135 | + packaging: props.zipExport ? 'ZIP' : undefined, |
| 136 | + } |
| 137 | + : undefined, |
| 138 | + }, |
| 139 | + }); |
| 140 | + resource.applyRemovalPolicy(props.removalPolicy, { |
| 141 | + default: cdk.RemovalPolicy.RETAIN, |
| 142 | + }); |
| 143 | + this.reportGroupArn = this.getResourceArnAttribute(resource.attrArn, |
| 144 | + reportGroupArnComponents(this.physicalName)); |
| 145 | + this.reportGroupName = this.getResourceNameAttribute( |
| 146 | + // there is no separate name attribute, |
| 147 | + // so use Fn::Select + Fn::Split to make one |
| 148 | + cdk.Fn.select(1, cdk.Fn.split('/', resource.ref)), |
| 149 | + ); |
| 150 | + this.exportBucket = props.exportBucket; |
| 151 | + } |
| 152 | +} |
0 commit comments