All files / src/aws policy.ts

90% Statements 18/20
100% Branches 0/0
66.67% Functions 2/3
90% Lines 18/20
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  2x 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 Policy {
  private dependencies: string[] = []
  private type: string = 'AWS::ApplicationAutoScaling::ScalingPolicy'
 
  constructor (
    private service: string,
    private table: string,
    private value: number,
    private read: boolean,
    private scaleIn: number,
    private scaleOut: number,
    private index: string,
    private stage: string
  ) { }

  public setDependencies(list: string[]): Policy {
    this.dependencies = list
 
    return this
  }
 
  public toJSON(): any {
    const nameMetric = names.metric(this.read)
    const nameScalePolicy = names.policyScale(this.service, this.table, this.read, this.index, this.stage)
    const nameTarget = names.target(this.service, this.table, this.read, this.index, this.stage)
 
    const dependencies = [this.table, nameTarget ].concat(this.dependencies)
 
    return {
      [nameScalePolicy]: {
        DependsOn: dependencies,
        Properties: {
          PolicyName: nameScalePolicy,
          PolicyType: 'TargetTrackingScaling',
          ScalingTargetId: { Ref: nameTarget },
          TargetTrackingScalingPolicyConfiguration: {
            PredefinedMetricSpecification: {
              PredefinedMetricType: nameMetric
            },
            ScaleInCooldown: this.scaleIn,
            ScaleOutCooldown: this.scaleOut,
            TargetValue: this.value
          }
        },
        Type: this.type
      }
    }
  }
}