All files / src/aws role.ts

89.47% Statements 17/19
100% Branches 0/0
66.67% Functions 2/3
89.47% Lines 17/19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75  2x 2x     2x 2x 2x 2x 2x 2x 2x 2x                                     2x 2x 2x 2x     2x     2x                                                         2x          
import * as names from './names'
 
export default class Role {
  private dependencies: string[] = []
  private type: string = 'AWS::IAM::Role'
  private version: string = '2012-10-17'
  private actions = {
    CloudWatch: [
      'cloudwatch:PutMetricAlarm',
      'cloudwatch:DescribeAlarms',
      'cloudwatch:DeleteAlarms',
      'cloudwatch:GetMetricStatistics',
      'cloudwatch:SetAlarmState'
    ],
    DynamoDB: [
      'dynamodb:DescribeTable',
      'dynamodb:UpdateTable'
    ]
  }
 
  constructor (
    private service: string,
    private table: string,
    private index: string,
    private stage: string
  ) { }

  public setDependencies(list: string[]): Role {
    this.dependencies = list
 
    return this
  }
 
  public toJSON(): any {
    const nameRole = names.role(this.service, this.table, this.index, this.stage)
    const namePolicyRole = names.policyRole(this.service, this.table, this.index, this.stage)
 
    const dependencies = [ this.table ].concat(this.dependencies)
    const principal = {
      Service: 'application-autoscaling.amazonaws.com'
    }
    const resource = {
      'Fn::Join': [ '', [ 'arn:aws:dynamodb:*:', { Ref: 'AWS::AccountId' }, ':table/', { Ref: this.table } ] ]
    }
 
    return {
      [nameRole]: {
        DependsOn: dependencies,
        Properties: {
          AssumeRolePolicyDocument: {
            Statement: [
              { Action: 'sts:AssumeRole', Effect: 'Allow', Principal: principal }
            ],
            Version: this.version
          },
          Policies: [
            {
              PolicyDocument: {
                Statement: [
                  { Action: this.actions.CloudWatch, Effect: 'Allow', Resource: '*' },
                  { Action: this.actions.DynamoDB, Effect: 'Allow', Resource: resource }
                ],
                Version: this.version
              },
              PolicyName: namePolicyRole
            }
          ],
          RoleName: nameRole
        },
        Type: this.type
      }
    }
  }
}