UNPKG

@aws-amplify/cli-internal

Version:
122 lines • 7.29 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GeoResourceRenderer = void 0; const typescript_1 = __importDefault(require("typescript")); const ts_1 = require("../../ts"); const factory = typescript_1.default.factory; const GEO_ARN_RESOURCE_TYPE = { Map: 'map', PlaceIndex: 'place-index', GeofenceCollection: 'geofence-collection', }; class GeoResourceRenderer { render(params) { const needsIamImport = params.gen1Actions.length > 0; return factory.createNodeArray([ this.renderConstructImport(params), ...(needsIamImport ? [ts_1.TS.namedImport('aws-cdk-lib/aws-iam', 'Policy', 'PolicyStatement')] : []), this.renderBackendTypeImport(), ts_1.newLineIdentifier, ts_1.TS.createBranchNameDeclaration(), ts_1.newLineIdentifier, this.renderDefineResource(params), ]); } renderConstructImport(params) { return ts_1.TS.namedImport(`./${params.constructFileName}`, params.constructClassName); } renderBackendTypeImport() { return ts_1.TS.typeImport('../../backend', 'Backend'); } renderDefineResource(params) { const { constructClassName, resourceName } = params; const functionName = `define${resourceName.charAt(0).toUpperCase()}${resourceName.slice(1)}`; const createStackCall = ts_1.TS.declareConst(`${resourceName}Stack`, factory.createCallExpression(ts_1.TS.propAccess('backend', 'createStack'), undefined, [ factory.createStringLiteral(`geo${resourceName}`), ])); const constructProps = this.buildConstructProps(params); const constructInstantiation = ts_1.TS.declareConst(resourceName, factory.createNewExpression(factory.createIdentifier(constructClassName), undefined, [ factory.createIdentifier(`${resourceName}Stack`), factory.createStringLiteral(resourceName), factory.createObjectLiteralExpression(constructProps, true), ])); const body = [createStackCall, constructInstantiation]; if (params.gen1Actions.length > 0) { body.push(...this.renderIamPolicy(params)); } body.push(factory.createReturnStatement(factory.createIdentifier(resourceName))); return ts_1.TS.exportedFunction(functionName, body); } renderIamPolicy(params) { const { resourceName, gen1Actions, gen1ResourceName, serviceName, groupRoles, needsAuthAndUnauthRoles } = params; const arnResourceType = GEO_ARN_RESOURCE_TYPE[serviceName]; const stackVar = `${resourceName}Stack`; const arnExpr = factory.createTemplateExpression(factory.createTemplateHead('arn:aws:geo:'), [ factory.createTemplateSpan(ts_1.TS.propAccess(stackVar, 'region'), factory.createTemplateMiddle(':')), factory.createTemplateSpan(ts_1.TS.propAccess(stackVar, 'account'), factory.createTemplateTail(`:${arnResourceType}/${gen1ResourceName}`)), ]); const policyDecl = ts_1.TS.declareConst('policy', factory.createNewExpression(factory.createIdentifier('Policy'), undefined, [ factory.createIdentifier(resourceName), factory.createStringLiteral('gen1AuthPolicy'), factory.createObjectLiteralExpression([ factory.createPropertyAssignment('statements', factory.createArrayLiteralExpression([ factory.createNewExpression(factory.createIdentifier('PolicyStatement'), undefined, [ factory.createObjectLiteralExpression([ factory.createPropertyAssignment('actions', factory.createArrayLiteralExpression(gen1Actions.map((a) => factory.createStringLiteral(a)))), factory.createPropertyAssignment('resources', factory.createArrayLiteralExpression([arnExpr])), ], true), ]), ])), ], true), ])); const statements = [policyDecl]; if (needsAuthAndUnauthRoles) { statements.push(createAttachInlinePolicy(ts_1.TS.propAccess('backend', 'auth', 'resources', 'authenticatedUserIamRole'))); statements.push(createAttachInlinePolicy(ts_1.TS.propAccess('backend', 'auth', 'resources', 'unauthenticatedUserIamRole'))); } for (const groupRole of groupRoles) { const groupAccess = factory.createPropertyAccessExpression(factory.createElementAccessExpression(ts_1.TS.propAccess('backend', 'auth', 'resources', 'groups'), factory.createStringLiteral(groupRole.groupName)), factory.createIdentifier('role')); statements.push(createAttachInlinePolicy(groupAccess)); } return statements; } buildConstructProps(params) { const props = []; if (params.needsAuthAndUnauthRoles) { props.push(factory.createPropertyAssignment(factory.createIdentifier('authRoleName'), createAuthRoleAccess())); props.push(factory.createPropertyAssignment(factory.createIdentifier('unauthRoleName'), createUnauthRoleAccess())); } if (params.userPoolIdParamName) { props.push(factory.createPropertyAssignment(factory.createIdentifier(params.userPoolIdParamName), createUserPoolIdAccess())); } for (const groupRole of params.groupRoles) { props.push(factory.createPropertyAssignment(factory.createIdentifier(groupRole.paramName), createGroupRoleAccess(groupRole.groupName))); } for (const prop of params.serviceSpecificProps) { props.push(factory.createPropertyAssignment(factory.createIdentifier(prop.key), factory.createStringLiteral(prop.value))); } props.push(factory.createShorthandPropertyAssignment(factory.createIdentifier('branchName'))); props.push(factory.createPropertyAssignment(factory.createIdentifier('isDefault'), factory.createStringLiteral(params.isDefault))); return props; } } exports.GeoResourceRenderer = GeoResourceRenderer; function createAuthRoleAccess() { return ts_1.TS.propAccess('backend', 'auth', 'resources', 'authenticatedUserIamRole', 'roleName'); } function createUnauthRoleAccess() { return ts_1.TS.propAccess('backend', 'auth', 'resources', 'unauthenticatedUserIamRole', 'roleName'); } function createUserPoolIdAccess() { return ts_1.TS.propAccess('backend', 'auth', 'resources', 'userPool', 'userPoolId'); } function createGroupRoleAccess(groupName) { return factory.createPropertyAccessExpression(factory.createPropertyAccessExpression(factory.createElementAccessExpression(ts_1.TS.propAccess('backend', 'auth', 'resources', 'groups'), factory.createStringLiteral(groupName)), factory.createIdentifier('role')), factory.createIdentifier('roleName')); } function createAttachInlinePolicy(roleExpr) { return factory.createExpressionStatement(factory.createCallExpression(factory.createPropertyAccessExpression(roleExpr, factory.createIdentifier('attachInlinePolicy')), undefined, [factory.createIdentifier('policy')])); } //# sourceMappingURL=geo-resource.renderer.js.map