"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const codepipeline = require("@aws-cdk/aws-codepipeline-api");
const cdk = require("@aws-cdk/cdk");
/**
 * CodePipeline invoke Action that is provided by an AWS Lambda function.
 *
 * @see https://docs.aws.amazon.com/codepipeline/latest/userguide/actions-invoke-lambda-function.html
 */
class PipelineInvokeAction extends codepipeline.Action {
    constructor(parent, name, props) {
        super(parent, name, {
            stage: props.stage,
            category: codepipeline.ActionCategory.Invoke,
            provider: 'Lambda',
            artifactBounds: codepipeline.defaultBounds(),
            configuration: {
                FunctionName: props.lambda.functionName,
                UserParameters: props.userParameters
            }
        });
        // allow pipeline to list functions
        props.stage.pipelineRole.addToPolicy(new cdk.PolicyStatement()
            .addAction('lambda:ListFunctions')
            .addResource('*'));
        // allow pipeline to invoke this lambda functionn
        props.stage.pipelineRole.addToPolicy(new cdk.PolicyStatement()
            .addAction('lambda:InvokeFunction')
            .addResource(props.lambda.functionArn));
        // allow lambda to put job results for this pipeline.
        const addToPolicy = props.addPutJobResultPolicy !== undefined ? props.addPutJobResultPolicy : true;
        if (addToPolicy) {
            props.lambda.addToRolePolicy(new cdk.PolicyStatement()
                .addResource('*') // to avoid cycles (see docs)
                .addAction('codepipeline:PutJobSuccessResult')
                .addAction('codepipeline:PutJobFailureResult'));
        }
    }
    /**
     * Add an input artifact
     * @param artifact
     */
    addInputArtifact(artifact) {
        super.addInputArtifact(artifact);
        return this;
    }
}
exports.PipelineInvokeAction = PipelineInvokeAction;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicGlwZWxpbmUtYWN0aW9uLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsicGlwZWxpbmUtYWN0aW9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQUEsOERBQStEO0FBQy9ELG9DQUFxQztBQXdDckM7Ozs7R0FJRztBQUNILE1BQWEsb0JBQXFCLFNBQVEsWUFBWSxDQUFDLE1B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