All files / src plugin.ts

25.86% Statements 15/58
60% Branches 24/40
13.64% Functions 3/22
31.91% Lines 15/47
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182  1x 1x 1x 1x 1x 1x 1x 1x                   2x 2x                                                               1x                                                                                                                       5x 1x   4x                       1x                                                                                                              
import * as assert from 'assert'
import * as _ from 'lodash'
import * as util from 'util'
 
import Policy from './aws/policy'
import Role from './aws/role'
import Target from './aws/target'
 
const text = {
  INVALID_CONFIGURATION: 'Invalid serverless configuration',
  NO_AUTOSCALING_CONFIG: 'Not Auto Scaling configuration found',
  ONLY_AWS_SUPPORT: 'Only supported for AWS provicer'
}
 
class Plugin {
  public hooks: {}
 
  /**
   * Constructur
   */
  constructor (private serverless: Serverless) {
    this.hooks = {
      'deploy:compileEvents': this.beforeDeployResources.bind(this)
    }
  }
 
  /**
   * Get the current stage name
   */
  private getStage(): string {
    return this.serverless.getProvider('aws').getStage()
  }

  /**
   * Get the current service name
   */
  private getServiceName(): string {
    return this.serverless.service.getServiceName()
  }

  /**
   * Validate the request and check if configuration is available
   */
  private validate(): void {
    assert(this.serverless, text.INVALID_CONFIGURATION)
    assert(this.serverless.service, text.INVALID_CONFIGURATION)
    assert(this.serverless.service.provider, text.INVALID_CONFIGURATION)
    assert(this.serverless.service.provider.name, text.INVALID_CONFIGURATION)
    assert(this.serverless.service.provider.name === 'aws', text.ONLY_AWS_SUPPORT)
 
    assert(this.serverless.service.custom, text.NO_AUTOSCALING_CONFIG)
    assert(this.serverless.service.custom.capacities, text.NO_AUTOSCALING_CONFIG)
  }

  /**
   * Parse configuration and fill up with default values when needed
   */
  private defaults(config: Capacity): { read: CapacityConfiguration, write: CapacityConfiguration } {
    return {
      read: {
        maximum: config.read && config.read.maximum ? config.read.maximum : 200,
        minimum: config.read && config.read.minimum ? config.read.minimum : 5,
        usage: config.read && config.read.usage ? config.read.usage : 0.75
      },
      write: {
        maximum: config.write && config.write.maximum ? config.write.maximum : 200,
        minimum: config.write && config.write.minimum ? config.write.minimum : 5,
        usage: config.write && config.write.usage ? config.write.usage : 0.75
      }
    }
  }

  /**
   * Create CloudFormation resources for table (and optional index)
   */
  private resources(table: string, index: string, config: Capacity): any[] {
    const resources = []
    const service = this.getServiceName()
    const stage = this.getStage()
    const data = this.defaults(config)
 
    // Start processing configuration
    this.serverless.cli.log(
      util.format(' - Building configuration for resource "table/%s%s"', table, (index ? ('/index/' + index) : ''))
    )
 
    // Add role to manage Auto Scaling policies
    resources.push(new Role(service, table, index, stage))

    // Only add Auto Scaling for read capacity if configuration set is available
    if (config.read) {
      resources.push(
        // ScaleIn/ScaleOut values are fix to 60% usage
        new Policy(service, table, data.read.usage, true, 60, 60, index, stage),
        new Target(service, table, data.read.minimum, data.read.maximum, true, index, stage)
      )
    }

    // Only add Auto Scaling for write capacity if configuration set is available
    if (config.write) {
      resources.push(
        // ScaleIn/ScaleOut values are fix to 60% usage
        new Policy(service, table, data.write.usage, false, 60, 60, index, stage),
        new Target(service, table, data.write.minimum, data.write.maximum, false, index, stage)
      )
    }
 
    return resources
  }
 
  /**
   * Generate CloudFormation resources for DynamoDB table and indexes
   */
  private generate(table: string, config: Capacity) {
    let resources: any[] = []
    let lastRessources: any[] = []
 
    const indexes = this.normalize(config.index)
    if (!config.indexOnly) {
      indexes.unshift('') // Horrible solution
    }
 
    indexes.forEach(
      (index: string) => {
        const current = this.resources(table, index, config).map(
          (resource: any) => resource.setDependencies(lastRessources).toJSON()
        )
 
        resources = resources.concat(current)
        lastRessources = current.map((item: any) => Object.keys(item).pop())
      }
    )
 
    return resources
  }
 
  /**
   * Check if parameter is defined and return as array if only a string is provided
   */
  private normalize(data: string|string[]): string[] {
    if (data && data.constructor !== Array) {
      return [ data as string ]
    }
 
    return (data as string[] || []).slice(0)
  }
 
  /**
   * Process the provided configuration
   */
  private process() {
    this.serverless.service.custom.capacities.filter(
      (config: Capacity) => !!config.read || !!config.write
    ).forEach(
      (config: Capacity) => this.normalize(config.table).forEach(
        (table: string) => this.generate(table, config).forEach(
          (resource: string) => _.merge(
            this.serverless.service.provider.compiledCloudFormationTemplate.Resources,
            resource
          )
        )
      )
    )
  }
 
  private beforeDeployResources(): Promise<any> {
    return Promise.resolve().then(
      () => this.validate()
    ).then(
      () => this.serverless.cli.log(util.format('Configure DynamoDB Auto Scaling …'))
    ).then(
      () => this.process()
    ).then(
      () => this.serverless.cli.log(util.format('Added DynamoDB Auto Scaling to CloudFormation!'))
    ).catch(
      (err: Error) => this.serverless.cli.log(util.format('Skipping DynamoDB Auto Scaling: %s!', err.message))
    )
  }
}
 
module.exports = Plugin