Class: LambdaWrap::API

Inherits:
Object
  • Object
show all
Defined in:
lib/lambda_wrap/api_manager.rb

Overview

Top level class that manages the Serverless Microservice API deployment.

Since:

  • 1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ API

TODO:

Allow clients to pass in a YAML file for all construction.

Constructor for the high level API Manager class.

Parameters:

  • options (Hash) (defaults to: {})

    The Options to configure the API.

Options Hash (options):

  • :access_key_id (String)

    The AWS Access Key Id to communicate with AWS. Will also check the environment variables for this value.

  • :secret_access_key (String)

    The AWS Secret Access Key to communicate with AWS. Also checks environment variables for this value.

  • :region (String)

    The AWS Region to deploy API to. Also checks environment variables for this value.

Since:

  • 1.0



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
# File 'lib/lambda_wrap/api_manager.rb', line 34

def initialize(options = {})
  unless options[:lambda_client] && options[:dynamo_client] && options[:api_gateway_client]
    access_key_id = options[:access_key_id] || ENV['AWS_ACCESS_KEY_ID'] || ENV['ACCESS_KEY'] ||
                    raise(ArgumentError, 'Cannot find AWS Access Key ID.')

    secret_access_key = options[:secret_access_key] || ENV['AWS_SECRET_ACCESS_KEY'] || ENV['SECRET_KEY'] ||
                        raise(ArgumentError, 'Cannot find AWS Secret Key.')

    credentials = Aws::Credentials.new(access_key_id, secret_access_key)
  end

  region = options[:region] || ENV['AWS_REGION'] || ENV['AMAZON_REGION'] || ENV['AWS_DEFAULT_REGION'] ||
           raise(ArgumentError, 'Cannot find AWS Region.')

  @lambdas = []
  @dynamo_tables = []
  @api_gateways = []

  @region = region
  @lambda_client = options[:lambda_client] ||
                   Aws::Lambda::Client.new(credentials: credentials, region: region)
  @dynamo_client = options[:dynamo_client] ||
                   Aws::DynamoDB::Client.new(credentials: credentials, region: region)
  @api_gateway_client = options[:api_gateway_client] ||
                        Aws::APIGateway::Client.new(credentials: credentials, region: region)
end

Instance Attribute Details

#api_gatewaysArray<LambdaWrap::ApiGateway> (readonly)

Returns The List of API Gateways to be deployed with the API.

Returns:

Since:

  • 1.0



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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/lambda_wrap/api_manager.rb', line 17

class API
  attr_reader :lambdas
  attr_reader :dynamo_tables
  attr_reader :api_gateways
  attr_reader :region

  # Constructor for the high level API Manager class.
  #
  # @param [Hash] options The Options to configure the API.
  # @option options [String] :access_key_id The AWS Access Key Id to communicate with AWS. Will also check the
  #   environment variables for this value.
  # @option options [String] :secret_access_key The AWS Secret Access Key to communicate with AWS. Also checks
  #   environment variables for this value.
  # @option options [String] :region The AWS Region to deploy API to. Also checks environment variables for this
  #   value.
  #
  # @todo Allow clients to pass in a YAML file for all construction.
  def initialize(options = {})
    unless options[:lambda_client] && options[:dynamo_client] && options[:api_gateway_client]
      access_key_id = options[:access_key_id] || ENV['AWS_ACCESS_KEY_ID'] || ENV['ACCESS_KEY'] ||
                      raise(ArgumentError, 'Cannot find AWS Access Key ID.')

      secret_access_key = options[:secret_access_key] || ENV['AWS_SECRET_ACCESS_KEY'] || ENV['SECRET_KEY'] ||
                          raise(ArgumentError, 'Cannot find AWS Secret Key.')

      credentials = Aws::Credentials.new(access_key_id, secret_access_key)
    end

    region = options[:region] || ENV['AWS_REGION'] || ENV['AMAZON_REGION'] || ENV['AWS_DEFAULT_REGION'] ||
             raise(ArgumentError, 'Cannot find AWS Region.')

    @lambdas = []
    @dynamo_tables = []
    @api_gateways = []

    @region = region
    @lambda_client = options[:lambda_client] ||
                     Aws::Lambda::Client.new(credentials: credentials, region: region)
    @dynamo_client = options[:dynamo_client] ||
                     Aws::DynamoDB::Client.new(credentials: credentials, region: region)
    @api_gateway_client = options[:api_gateway_client] ||
                          Aws::APIGateway::Client.new(credentials: credentials, region: region)
  end

  # Add Lambda Object(s) to the API.
  #
  # @param [LambdaWrap::Lambda Array<LambdaWrap::Lambda>] new_lambda Splat of LambdaWrap Lambda
  #  objects to add to the API. Overloaded as:
  #  add_lambda(lambda1) OR  add_lambda([lambda1, lambda2]) OR add_lambda(lambda1, lambda2)
  def add_lambda(*new_lambda)
    flattened_lambdas = new_lambda.flatten
    flattened_lambdas.each { |lambda| parameter_guard(lambda, LambdaWrap::Lambda, 'LambdaWrap::Lambda') }
    lambdas.concat(flattened_lambdas)
  end

  # Add Dynamo Table Object(s) to the API.
  #
  # @param [LambdaWrap::DynamoTable, Array<LambdaWrap::DynamoTable>] new_table Splat of LambdaWrap DynamoTable
  #  objects to add to the API. Overloaded as:
  #  add_dynamo_table(table1) OR  add_dynamo_table([table1, table2]) OR add_dynamo_table(table1, table2)
  def add_dynamo_table(*new_table)
    flattened_tables = new_table.flatten
    flattened_tables.each { |table| parameter_guard(table, LambdaWrap::DynamoTable, 'LambdaWrap::DynamoTable') }
    dynamo_tables.concat(flattened_tables)
  end

  # Add API Gateway Object(s) to the API.
  #
  # @param [LambdaWrap::ApiGateway, Array<LambdaWrap::ApiGateway>] new_api_gateway Splat of LambdaWrap API Gateway
  #  objects to add to the API. Overloaded as:
  #  add_api_gateway(apig1) OR  add_api_gateway([apig1, apig2]) OR add_api_gateway(apig1, apig2)
  def add_api_gateway(*new_api_gateway)
    flattened_api_gateways = new_api_gateway.flatten
    flattened_api_gateways.each { |apig| parameter_guard(apig, LambdaWrap::ApiGateway, 'LambdaWrap::ApiGateway') }
    api_gateways.concat(flattened_api_gateways)
  end

  # Deploys all services to the specified environment.
  #
  # @param [LambdaWrap::Environment] environment_options the Environment to deploy
  def deploy(environment_options)
    environment_parameter_guard(environment_options)
    if no_op?
      puts 'Nothing to deploy.'
      return
    end

    deployment_start_message = 'Deploying '
    deployment_start_message += "#{dynamo_tables.length} Dynamo Tables, " unless dynamo_tables.empty?
    deployment_start_message += "#{lambdas.length} Lambdas, " unless lambdas.empty?
    deployment_start_message += "#{api_gateways.length} API Gateways " unless api_gateways.empty?
    deployment_start_message += "to Environment: #{environment_options.name}"
    puts deployment_start_message

    total_time_start = Time.now

    services_time_start = total_time_start
    dynamo_tables.each { |table| table.deploy(environment_options, @dynamo_client, @region) }
    services_time_end = Time.now

    unless dynamo_tables.empty?
      puts "Deploying #{dynamo_tables.length} Table(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    services_time_start = Time.now
    lambdas.each { |lambda| lambda.deploy(environment_options, @lambda_client, @region) }
    services_time_end = Time.now

    unless lambdas.empty?
      puts "Deploying #{lambdas.length} Lambda(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    services_time_start = Time.now
    api_gateways.each { |apig| apig.deploy(environment_options, @api_gateway_client, @region) }
    services_time_end = Time.now

    unless api_gateways.empty?
      puts "Deploying #{api_gateways.length} API Gateway(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    total_time_end = Time.now

    puts "Total API Deployment took: \
    #{Time.at(total_time_end - total_time_start).utc.strftime('%H:%M:%S')}"
    puts "Successfully deployed API to #{environment_options.name}"

    true
  end

  # Tearsdown Environment for all services.
  #
  # @param [LambdaWrap::Environment] environment_options the Environment to teardown
  def teardown(environment_options)
    environment_parameter_guard(environment_options)
    if no_op?
      puts 'Nothing to teardown.'
      return
    end

    deployment_start_message = 'Tearing-down '
    deployment_start_message += "#{dynamo_tables.length} Dynamo Tables, " unless dynamo_tables.empty?
    deployment_start_message += "#{lambdas.length} Lambdas, " unless lambdas.empty?
    deployment_start_message += "#{api_gateways.length} API Gateways " unless api_gateways.empty?
    deployment_start_message += " Environment: #{environment_options.name}"
    puts deployment_start_message

    total_time_start = Time.now

    services_time_start = total_time_start
    dynamo_tables.each { |table| table.teardown(environment_options, @dynamo_client, @region) }
    services_time_end = Time.now

    unless dynamo_tables.empty?
      puts "Tearing-down #{dynamo_tables.length} Table(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    services_time_start = Time.now
    lambdas.each { |lambda| lambda.teardown(environment_options, @lambda_client, @region) }
    services_time_end = Time.now

    unless lambdas.empty?
      puts "Tearing-down #{lambdas.length} Lambda(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    services_time_start = Time.now
    api_gateways.each { |apig| apig.teardown(environment_options, @api_gateway_client, @region) }
    services_time_end = Time.now

    unless api_gateways.empty?
      puts "Tearing-down #{api_gateways.length} API Gateway(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    total_time_end = Time.now

    puts "Total API Tear-down took: \
    #{Time.at(total_time_end - total_time_start).utc.strftime('%H:%M:%S')}"
    puts "Successful Teardown API to #{environment_options.name}"

    true
  end

  # Deletes all services from the cloud.
  def delete
    if dynamo_tables.empty? && lambdas.empty? && api_gateways.empty?
      puts 'Nothing to Deleting.'
      return
    end

    deployment_start_message = 'Deleting '
    deployment_start_message += "#{dynamo_tables.length} Dynamo Tables, " unless dynamo_tables.empty?
    deployment_start_message += "#{lambdas.length} Lambdas, " unless lambdas.empty?
    deployment_start_message += "#{api_gateways.length} API Gateways " unless api_gateways.empty?
    puts deployment_start_message

    total_time_start = Time.now

    services_time_start = total_time_start
    dynamo_tables.each { |table| table.delete(@dynamo_client, @region) }
    services_time_end = Time.now

    unless dynamo_tables.empty?
      puts "Deleting #{dynamo_tables.length} Table(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    services_time_start = Time.now
    lambdas.each { |lambda| lambda.delete(@lambda_client, @region) }
    services_time_end = Time.now

    unless lambdas.empty?
      puts "Deleting #{lambdas.length} Lambda(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    services_time_start = Time.now
    api_gateways.each { |apig| apig.delete(@api_gateway_client, @region) }
    services_time_end = Time.now

    unless api_gateways.empty?
      puts "Deleting #{api_gateways.length} API Gateway(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    total_time_end = Time.now

    puts "Total API Deletion took: \
    #{Time.at(total_time_end - total_time_start).utc.strftime('%H:%M:%S')}"
    puts 'Successful Deletion of API'

    true
  end

  private

  def environment_parameter_guard(parameter)
    parameter_guard(parameter, LambdaWrap::Environment, 'LambdaWrap::Environment')
  end

  def parameter_guard(parameter, type, type_name)
    return if parameter.is_a?(type)
    raise ArgumentError, "Must pass a #{type_name} to the API Manager. Got: #{parameter}"
  end

  def no_op?
    dynamo_tables.empty? && lambdas.empty? && api_gateways.empty?
  end
end

#dynamo_tablesArray<LambdaWrap::DynamoTables> (readonly)

Returns The List of DynamoTables to be deployed with the API.

Returns:

  • (Array<LambdaWrap::DynamoTables>)

    The List of DynamoTables to be deployed with the API.

Since:

  • 1.0



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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/lambda_wrap/api_manager.rb', line 17

class API
  attr_reader :lambdas
  attr_reader :dynamo_tables
  attr_reader :api_gateways
  attr_reader :region

  # Constructor for the high level API Manager class.
  #
  # @param [Hash] options The Options to configure the API.
  # @option options [String] :access_key_id The AWS Access Key Id to communicate with AWS. Will also check the
  #   environment variables for this value.
  # @option options [String] :secret_access_key The AWS Secret Access Key to communicate with AWS. Also checks
  #   environment variables for this value.
  # @option options [String] :region The AWS Region to deploy API to. Also checks environment variables for this
  #   value.
  #
  # @todo Allow clients to pass in a YAML file for all construction.
  def initialize(options = {})
    unless options[:lambda_client] && options[:dynamo_client] && options[:api_gateway_client]
      access_key_id = options[:access_key_id] || ENV['AWS_ACCESS_KEY_ID'] || ENV['ACCESS_KEY'] ||
                      raise(ArgumentError, 'Cannot find AWS Access Key ID.')

      secret_access_key = options[:secret_access_key] || ENV['AWS_SECRET_ACCESS_KEY'] || ENV['SECRET_KEY'] ||
                          raise(ArgumentError, 'Cannot find AWS Secret Key.')

      credentials = Aws::Credentials.new(access_key_id, secret_access_key)
    end

    region = options[:region] || ENV['AWS_REGION'] || ENV['AMAZON_REGION'] || ENV['AWS_DEFAULT_REGION'] ||
             raise(ArgumentError, 'Cannot find AWS Region.')

    @lambdas = []
    @dynamo_tables = []
    @api_gateways = []

    @region = region
    @lambda_client = options[:lambda_client] ||
                     Aws::Lambda::Client.new(credentials: credentials, region: region)
    @dynamo_client = options[:dynamo_client] ||
                     Aws::DynamoDB::Client.new(credentials: credentials, region: region)
    @api_gateway_client = options[:api_gateway_client] ||
                          Aws::APIGateway::Client.new(credentials: credentials, region: region)
  end

  # Add Lambda Object(s) to the API.
  #
  # @param [LambdaWrap::Lambda Array<LambdaWrap::Lambda>] new_lambda Splat of LambdaWrap Lambda
  #  objects to add to the API. Overloaded as:
  #  add_lambda(lambda1) OR  add_lambda([lambda1, lambda2]) OR add_lambda(lambda1, lambda2)
  def add_lambda(*new_lambda)
    flattened_lambdas = new_lambda.flatten
    flattened_lambdas.each { |lambda| parameter_guard(lambda, LambdaWrap::Lambda, 'LambdaWrap::Lambda') }
    lambdas.concat(flattened_lambdas)
  end

  # Add Dynamo Table Object(s) to the API.
  #
  # @param [LambdaWrap::DynamoTable, Array<LambdaWrap::DynamoTable>] new_table Splat of LambdaWrap DynamoTable
  #  objects to add to the API. Overloaded as:
  #  add_dynamo_table(table1) OR  add_dynamo_table([table1, table2]) OR add_dynamo_table(table1, table2)
  def add_dynamo_table(*new_table)
    flattened_tables = new_table.flatten
    flattened_tables.each { |table| parameter_guard(table, LambdaWrap::DynamoTable, 'LambdaWrap::DynamoTable') }
    dynamo_tables.concat(flattened_tables)
  end

  # Add API Gateway Object(s) to the API.
  #
  # @param [LambdaWrap::ApiGateway, Array<LambdaWrap::ApiGateway>] new_api_gateway Splat of LambdaWrap API Gateway
  #  objects to add to the API. Overloaded as:
  #  add_api_gateway(apig1) OR  add_api_gateway([apig1, apig2]) OR add_api_gateway(apig1, apig2)
  def add_api_gateway(*new_api_gateway)
    flattened_api_gateways = new_api_gateway.flatten
    flattened_api_gateways.each { |apig| parameter_guard(apig, LambdaWrap::ApiGateway, 'LambdaWrap::ApiGateway') }
    api_gateways.concat(flattened_api_gateways)
  end

  # Deploys all services to the specified environment.
  #
  # @param [LambdaWrap::Environment] environment_options the Environment to deploy
  def deploy(environment_options)
    environment_parameter_guard(environment_options)
    if no_op?
      puts 'Nothing to deploy.'
      return
    end

    deployment_start_message = 'Deploying '
    deployment_start_message += "#{dynamo_tables.length} Dynamo Tables, " unless dynamo_tables.empty?
    deployment_start_message += "#{lambdas.length} Lambdas, " unless lambdas.empty?
    deployment_start_message += "#{api_gateways.length} API Gateways " unless api_gateways.empty?
    deployment_start_message += "to Environment: #{environment_options.name}"
    puts deployment_start_message

    total_time_start = Time.now

    services_time_start = total_time_start
    dynamo_tables.each { |table| table.deploy(environment_options, @dynamo_client, @region) }
    services_time_end = Time.now

    unless dynamo_tables.empty?
      puts "Deploying #{dynamo_tables.length} Table(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    services_time_start = Time.now
    lambdas.each { |lambda| lambda.deploy(environment_options, @lambda_client, @region) }
    services_time_end = Time.now

    unless lambdas.empty?
      puts "Deploying #{lambdas.length} Lambda(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    services_time_start = Time.now
    api_gateways.each { |apig| apig.deploy(environment_options, @api_gateway_client, @region) }
    services_time_end = Time.now

    unless api_gateways.empty?
      puts "Deploying #{api_gateways.length} API Gateway(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    total_time_end = Time.now

    puts "Total API Deployment took: \
    #{Time.at(total_time_end - total_time_start).utc.strftime('%H:%M:%S')}"
    puts "Successfully deployed API to #{environment_options.name}"

    true
  end

  # Tearsdown Environment for all services.
  #
  # @param [LambdaWrap::Environment] environment_options the Environment to teardown
  def teardown(environment_options)
    environment_parameter_guard(environment_options)
    if no_op?
      puts 'Nothing to teardown.'
      return
    end

    deployment_start_message = 'Tearing-down '
    deployment_start_message += "#{dynamo_tables.length} Dynamo Tables, " unless dynamo_tables.empty?
    deployment_start_message += "#{lambdas.length} Lambdas, " unless lambdas.empty?
    deployment_start_message += "#{api_gateways.length} API Gateways " unless api_gateways.empty?
    deployment_start_message += " Environment: #{environment_options.name}"
    puts deployment_start_message

    total_time_start = Time.now

    services_time_start = total_time_start
    dynamo_tables.each { |table| table.teardown(environment_options, @dynamo_client, @region) }
    services_time_end = Time.now

    unless dynamo_tables.empty?
      puts "Tearing-down #{dynamo_tables.length} Table(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    services_time_start = Time.now
    lambdas.each { |lambda| lambda.teardown(environment_options, @lambda_client, @region) }
    services_time_end = Time.now

    unless lambdas.empty?
      puts "Tearing-down #{lambdas.length} Lambda(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    services_time_start = Time.now
    api_gateways.each { |apig| apig.teardown(environment_options, @api_gateway_client, @region) }
    services_time_end = Time.now

    unless api_gateways.empty?
      puts "Tearing-down #{api_gateways.length} API Gateway(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    total_time_end = Time.now

    puts "Total API Tear-down took: \
    #{Time.at(total_time_end - total_time_start).utc.strftime('%H:%M:%S')}"
    puts "Successful Teardown API to #{environment_options.name}"

    true
  end

  # Deletes all services from the cloud.
  def delete
    if dynamo_tables.empty? && lambdas.empty? && api_gateways.empty?
      puts 'Nothing to Deleting.'
      return
    end

    deployment_start_message = 'Deleting '
    deployment_start_message += "#{dynamo_tables.length} Dynamo Tables, " unless dynamo_tables.empty?
    deployment_start_message += "#{lambdas.length} Lambdas, " unless lambdas.empty?
    deployment_start_message += "#{api_gateways.length} API Gateways " unless api_gateways.empty?
    puts deployment_start_message

    total_time_start = Time.now

    services_time_start = total_time_start
    dynamo_tables.each { |table| table.delete(@dynamo_client, @region) }
    services_time_end = Time.now

    unless dynamo_tables.empty?
      puts "Deleting #{dynamo_tables.length} Table(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    services_time_start = Time.now
    lambdas.each { |lambda| lambda.delete(@lambda_client, @region) }
    services_time_end = Time.now

    unless lambdas.empty?
      puts "Deleting #{lambdas.length} Lambda(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    services_time_start = Time.now
    api_gateways.each { |apig| apig.delete(@api_gateway_client, @region) }
    services_time_end = Time.now

    unless api_gateways.empty?
      puts "Deleting #{api_gateways.length} API Gateway(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    total_time_end = Time.now

    puts "Total API Deletion took: \
    #{Time.at(total_time_end - total_time_start).utc.strftime('%H:%M:%S')}"
    puts 'Successful Deletion of API'

    true
  end

  private

  def environment_parameter_guard(parameter)
    parameter_guard(parameter, LambdaWrap::Environment, 'LambdaWrap::Environment')
  end

  def parameter_guard(parameter, type, type_name)
    return if parameter.is_a?(type)
    raise ArgumentError, "Must pass a #{type_name} to the API Manager. Got: #{parameter}"
  end

  def no_op?
    dynamo_tables.empty? && lambdas.empty? && api_gateways.empty?
  end
end

#lambdasArray<LambdaWrap::Lambda> (readonly)

Returns The List of Lambdas to be deployed with the API.

Returns:

Since:

  • 1.0



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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/lambda_wrap/api_manager.rb', line 17

class API
  attr_reader :lambdas
  attr_reader :dynamo_tables
  attr_reader :api_gateways
  attr_reader :region

  # Constructor for the high level API Manager class.
  #
  # @param [Hash] options The Options to configure the API.
  # @option options [String] :access_key_id The AWS Access Key Id to communicate with AWS. Will also check the
  #   environment variables for this value.
  # @option options [String] :secret_access_key The AWS Secret Access Key to communicate with AWS. Also checks
  #   environment variables for this value.
  # @option options [String] :region The AWS Region to deploy API to. Also checks environment variables for this
  #   value.
  #
  # @todo Allow clients to pass in a YAML file for all construction.
  def initialize(options = {})
    unless options[:lambda_client] && options[:dynamo_client] && options[:api_gateway_client]
      access_key_id = options[:access_key_id] || ENV['AWS_ACCESS_KEY_ID'] || ENV['ACCESS_KEY'] ||
                      raise(ArgumentError, 'Cannot find AWS Access Key ID.')

      secret_access_key = options[:secret_access_key] || ENV['AWS_SECRET_ACCESS_KEY'] || ENV['SECRET_KEY'] ||
                          raise(ArgumentError, 'Cannot find AWS Secret Key.')

      credentials = Aws::Credentials.new(access_key_id, secret_access_key)
    end

    region = options[:region] || ENV['AWS_REGION'] || ENV['AMAZON_REGION'] || ENV['AWS_DEFAULT_REGION'] ||
             raise(ArgumentError, 'Cannot find AWS Region.')

    @lambdas = []
    @dynamo_tables = []
    @api_gateways = []

    @region = region
    @lambda_client = options[:lambda_client] ||
                     Aws::Lambda::Client.new(credentials: credentials, region: region)
    @dynamo_client = options[:dynamo_client] ||
                     Aws::DynamoDB::Client.new(credentials: credentials, region: region)
    @api_gateway_client = options[:api_gateway_client] ||
                          Aws::APIGateway::Client.new(credentials: credentials, region: region)
  end

  # Add Lambda Object(s) to the API.
  #
  # @param [LambdaWrap::Lambda Array<LambdaWrap::Lambda>] new_lambda Splat of LambdaWrap Lambda
  #  objects to add to the API. Overloaded as:
  #  add_lambda(lambda1) OR  add_lambda([lambda1, lambda2]) OR add_lambda(lambda1, lambda2)
  def add_lambda(*new_lambda)
    flattened_lambdas = new_lambda.flatten
    flattened_lambdas.each { |lambda| parameter_guard(lambda, LambdaWrap::Lambda, 'LambdaWrap::Lambda') }
    lambdas.concat(flattened_lambdas)
  end

  # Add Dynamo Table Object(s) to the API.
  #
  # @param [LambdaWrap::DynamoTable, Array<LambdaWrap::DynamoTable>] new_table Splat of LambdaWrap DynamoTable
  #  objects to add to the API. Overloaded as:
  #  add_dynamo_table(table1) OR  add_dynamo_table([table1, table2]) OR add_dynamo_table(table1, table2)
  def add_dynamo_table(*new_table)
    flattened_tables = new_table.flatten
    flattened_tables.each { |table| parameter_guard(table, LambdaWrap::DynamoTable, 'LambdaWrap::DynamoTable') }
    dynamo_tables.concat(flattened_tables)
  end

  # Add API Gateway Object(s) to the API.
  #
  # @param [LambdaWrap::ApiGateway, Array<LambdaWrap::ApiGateway>] new_api_gateway Splat of LambdaWrap API Gateway
  #  objects to add to the API. Overloaded as:
  #  add_api_gateway(apig1) OR  add_api_gateway([apig1, apig2]) OR add_api_gateway(apig1, apig2)
  def add_api_gateway(*new_api_gateway)
    flattened_api_gateways = new_api_gateway.flatten
    flattened_api_gateways.each { |apig| parameter_guard(apig, LambdaWrap::ApiGateway, 'LambdaWrap::ApiGateway') }
    api_gateways.concat(flattened_api_gateways)
  end

  # Deploys all services to the specified environment.
  #
  # @param [LambdaWrap::Environment] environment_options the Environment to deploy
  def deploy(environment_options)
    environment_parameter_guard(environment_options)
    if no_op?
      puts 'Nothing to deploy.'
      return
    end

    deployment_start_message = 'Deploying '
    deployment_start_message += "#{dynamo_tables.length} Dynamo Tables, " unless dynamo_tables.empty?
    deployment_start_message += "#{lambdas.length} Lambdas, " unless lambdas.empty?
    deployment_start_message += "#{api_gateways.length} API Gateways " unless api_gateways.empty?
    deployment_start_message += "to Environment: #{environment_options.name}"
    puts deployment_start_message

    total_time_start = Time.now

    services_time_start = total_time_start
    dynamo_tables.each { |table| table.deploy(environment_options, @dynamo_client, @region) }
    services_time_end = Time.now

    unless dynamo_tables.empty?
      puts "Deploying #{dynamo_tables.length} Table(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    services_time_start = Time.now
    lambdas.each { |lambda| lambda.deploy(environment_options, @lambda_client, @region) }
    services_time_end = Time.now

    unless lambdas.empty?
      puts "Deploying #{lambdas.length} Lambda(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    services_time_start = Time.now
    api_gateways.each { |apig| apig.deploy(environment_options, @api_gateway_client, @region) }
    services_time_end = Time.now

    unless api_gateways.empty?
      puts "Deploying #{api_gateways.length} API Gateway(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    total_time_end = Time.now

    puts "Total API Deployment took: \
    #{Time.at(total_time_end - total_time_start).utc.strftime('%H:%M:%S')}"
    puts "Successfully deployed API to #{environment_options.name}"

    true
  end

  # Tearsdown Environment for all services.
  #
  # @param [LambdaWrap::Environment] environment_options the Environment to teardown
  def teardown(environment_options)
    environment_parameter_guard(environment_options)
    if no_op?
      puts 'Nothing to teardown.'
      return
    end

    deployment_start_message = 'Tearing-down '
    deployment_start_message += "#{dynamo_tables.length} Dynamo Tables, " unless dynamo_tables.empty?
    deployment_start_message += "#{lambdas.length} Lambdas, " unless lambdas.empty?
    deployment_start_message += "#{api_gateways.length} API Gateways " unless api_gateways.empty?
    deployment_start_message += " Environment: #{environment_options.name}"
    puts deployment_start_message

    total_time_start = Time.now

    services_time_start = total_time_start
    dynamo_tables.each { |table| table.teardown(environment_options, @dynamo_client, @region) }
    services_time_end = Time.now

    unless dynamo_tables.empty?
      puts "Tearing-down #{dynamo_tables.length} Table(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    services_time_start = Time.now
    lambdas.each { |lambda| lambda.teardown(environment_options, @lambda_client, @region) }
    services_time_end = Time.now

    unless lambdas.empty?
      puts "Tearing-down #{lambdas.length} Lambda(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    services_time_start = Time.now
    api_gateways.each { |apig| apig.teardown(environment_options, @api_gateway_client, @region) }
    services_time_end = Time.now

    unless api_gateways.empty?
      puts "Tearing-down #{api_gateways.length} API Gateway(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    total_time_end = Time.now

    puts "Total API Tear-down took: \
    #{Time.at(total_time_end - total_time_start).utc.strftime('%H:%M:%S')}"
    puts "Successful Teardown API to #{environment_options.name}"

    true
  end

  # Deletes all services from the cloud.
  def delete
    if dynamo_tables.empty? && lambdas.empty? && api_gateways.empty?
      puts 'Nothing to Deleting.'
      return
    end

    deployment_start_message = 'Deleting '
    deployment_start_message += "#{dynamo_tables.length} Dynamo Tables, " unless dynamo_tables.empty?
    deployment_start_message += "#{lambdas.length} Lambdas, " unless lambdas.empty?
    deployment_start_message += "#{api_gateways.length} API Gateways " unless api_gateways.empty?
    puts deployment_start_message

    total_time_start = Time.now

    services_time_start = total_time_start
    dynamo_tables.each { |table| table.delete(@dynamo_client, @region) }
    services_time_end = Time.now

    unless dynamo_tables.empty?
      puts "Deleting #{dynamo_tables.length} Table(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    services_time_start = Time.now
    lambdas.each { |lambda| lambda.delete(@lambda_client, @region) }
    services_time_end = Time.now

    unless lambdas.empty?
      puts "Deleting #{lambdas.length} Lambda(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    services_time_start = Time.now
    api_gateways.each { |apig| apig.delete(@api_gateway_client, @region) }
    services_time_end = Time.now

    unless api_gateways.empty?
      puts "Deleting #{api_gateways.length} API Gateway(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    total_time_end = Time.now

    puts "Total API Deletion took: \
    #{Time.at(total_time_end - total_time_start).utc.strftime('%H:%M:%S')}"
    puts 'Successful Deletion of API'

    true
  end

  private

  def environment_parameter_guard(parameter)
    parameter_guard(parameter, LambdaWrap::Environment, 'LambdaWrap::Environment')
  end

  def parameter_guard(parameter, type, type_name)
    return if parameter.is_a?(type)
    raise ArgumentError, "Must pass a #{type_name} to the API Manager. Got: #{parameter}"
  end

  def no_op?
    dynamo_tables.empty? && lambdas.empty? && api_gateways.empty?
  end
end

#regionString (readonly)

Returns The AWS Region to deploy the API.

Returns:

  • (String)

    The AWS Region to deploy the API.

Since:

  • 1.0



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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/lambda_wrap/api_manager.rb', line 17

class API
  attr_reader :lambdas
  attr_reader :dynamo_tables
  attr_reader :api_gateways
  attr_reader :region

  # Constructor for the high level API Manager class.
  #
  # @param [Hash] options The Options to configure the API.
  # @option options [String] :access_key_id The AWS Access Key Id to communicate with AWS. Will also check the
  #   environment variables for this value.
  # @option options [String] :secret_access_key The AWS Secret Access Key to communicate with AWS. Also checks
  #   environment variables for this value.
  # @option options [String] :region The AWS Region to deploy API to. Also checks environment variables for this
  #   value.
  #
  # @todo Allow clients to pass in a YAML file for all construction.
  def initialize(options = {})
    unless options[:lambda_client] && options[:dynamo_client] && options[:api_gateway_client]
      access_key_id = options[:access_key_id] || ENV['AWS_ACCESS_KEY_ID'] || ENV['ACCESS_KEY'] ||
                      raise(ArgumentError, 'Cannot find AWS Access Key ID.')

      secret_access_key = options[:secret_access_key] || ENV['AWS_SECRET_ACCESS_KEY'] || ENV['SECRET_KEY'] ||
                          raise(ArgumentError, 'Cannot find AWS Secret Key.')

      credentials = Aws::Credentials.new(access_key_id, secret_access_key)
    end

    region = options[:region] || ENV['AWS_REGION'] || ENV['AMAZON_REGION'] || ENV['AWS_DEFAULT_REGION'] ||
             raise(ArgumentError, 'Cannot find AWS Region.')

    @lambdas = []
    @dynamo_tables = []
    @api_gateways = []

    @region = region
    @lambda_client = options[:lambda_client] ||
                     Aws::Lambda::Client.new(credentials: credentials, region: region)
    @dynamo_client = options[:dynamo_client] ||
                     Aws::DynamoDB::Client.new(credentials: credentials, region: region)
    @api_gateway_client = options[:api_gateway_client] ||
                          Aws::APIGateway::Client.new(credentials: credentials, region: region)
  end

  # Add Lambda Object(s) to the API.
  #
  # @param [LambdaWrap::Lambda Array<LambdaWrap::Lambda>] new_lambda Splat of LambdaWrap Lambda
  #  objects to add to the API. Overloaded as:
  #  add_lambda(lambda1) OR  add_lambda([lambda1, lambda2]) OR add_lambda(lambda1, lambda2)
  def add_lambda(*new_lambda)
    flattened_lambdas = new_lambda.flatten
    flattened_lambdas.each { |lambda| parameter_guard(lambda, LambdaWrap::Lambda, 'LambdaWrap::Lambda') }
    lambdas.concat(flattened_lambdas)
  end

  # Add Dynamo Table Object(s) to the API.
  #
  # @param [LambdaWrap::DynamoTable, Array<LambdaWrap::DynamoTable>] new_table Splat of LambdaWrap DynamoTable
  #  objects to add to the API. Overloaded as:
  #  add_dynamo_table(table1) OR  add_dynamo_table([table1, table2]) OR add_dynamo_table(table1, table2)
  def add_dynamo_table(*new_table)
    flattened_tables = new_table.flatten
    flattened_tables.each { |table| parameter_guard(table, LambdaWrap::DynamoTable, 'LambdaWrap::DynamoTable') }
    dynamo_tables.concat(flattened_tables)
  end

  # Add API Gateway Object(s) to the API.
  #
  # @param [LambdaWrap::ApiGateway, Array<LambdaWrap::ApiGateway>] new_api_gateway Splat of LambdaWrap API Gateway
  #  objects to add to the API. Overloaded as:
  #  add_api_gateway(apig1) OR  add_api_gateway([apig1, apig2]) OR add_api_gateway(apig1, apig2)
  def add_api_gateway(*new_api_gateway)
    flattened_api_gateways = new_api_gateway.flatten
    flattened_api_gateways.each { |apig| parameter_guard(apig, LambdaWrap::ApiGateway, 'LambdaWrap::ApiGateway') }
    api_gateways.concat(flattened_api_gateways)
  end

  # Deploys all services to the specified environment.
  #
  # @param [LambdaWrap::Environment] environment_options the Environment to deploy
  def deploy(environment_options)
    environment_parameter_guard(environment_options)
    if no_op?
      puts 'Nothing to deploy.'
      return
    end

    deployment_start_message = 'Deploying '
    deployment_start_message += "#{dynamo_tables.length} Dynamo Tables, " unless dynamo_tables.empty?
    deployment_start_message += "#{lambdas.length} Lambdas, " unless lambdas.empty?
    deployment_start_message += "#{api_gateways.length} API Gateways " unless api_gateways.empty?
    deployment_start_message += "to Environment: #{environment_options.name}"
    puts deployment_start_message

    total_time_start = Time.now

    services_time_start = total_time_start
    dynamo_tables.each { |table| table.deploy(environment_options, @dynamo_client, @region) }
    services_time_end = Time.now

    unless dynamo_tables.empty?
      puts "Deploying #{dynamo_tables.length} Table(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    services_time_start = Time.now
    lambdas.each { |lambda| lambda.deploy(environment_options, @lambda_client, @region) }
    services_time_end = Time.now

    unless lambdas.empty?
      puts "Deploying #{lambdas.length} Lambda(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    services_time_start = Time.now
    api_gateways.each { |apig| apig.deploy(environment_options, @api_gateway_client, @region) }
    services_time_end = Time.now

    unless api_gateways.empty?
      puts "Deploying #{api_gateways.length} API Gateway(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    total_time_end = Time.now

    puts "Total API Deployment took: \
    #{Time.at(total_time_end - total_time_start).utc.strftime('%H:%M:%S')}"
    puts "Successfully deployed API to #{environment_options.name}"

    true
  end

  # Tearsdown Environment for all services.
  #
  # @param [LambdaWrap::Environment] environment_options the Environment to teardown
  def teardown(environment_options)
    environment_parameter_guard(environment_options)
    if no_op?
      puts 'Nothing to teardown.'
      return
    end

    deployment_start_message = 'Tearing-down '
    deployment_start_message += "#{dynamo_tables.length} Dynamo Tables, " unless dynamo_tables.empty?
    deployment_start_message += "#{lambdas.length} Lambdas, " unless lambdas.empty?
    deployment_start_message += "#{api_gateways.length} API Gateways " unless api_gateways.empty?
    deployment_start_message += " Environment: #{environment_options.name}"
    puts deployment_start_message

    total_time_start = Time.now

    services_time_start = total_time_start
    dynamo_tables.each { |table| table.teardown(environment_options, @dynamo_client, @region) }
    services_time_end = Time.now

    unless dynamo_tables.empty?
      puts "Tearing-down #{dynamo_tables.length} Table(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    services_time_start = Time.now
    lambdas.each { |lambda| lambda.teardown(environment_options, @lambda_client, @region) }
    services_time_end = Time.now

    unless lambdas.empty?
      puts "Tearing-down #{lambdas.length} Lambda(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    services_time_start = Time.now
    api_gateways.each { |apig| apig.teardown(environment_options, @api_gateway_client, @region) }
    services_time_end = Time.now

    unless api_gateways.empty?
      puts "Tearing-down #{api_gateways.length} API Gateway(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    total_time_end = Time.now

    puts "Total API Tear-down took: \
    #{Time.at(total_time_end - total_time_start).utc.strftime('%H:%M:%S')}"
    puts "Successful Teardown API to #{environment_options.name}"

    true
  end

  # Deletes all services from the cloud.
  def delete
    if dynamo_tables.empty? && lambdas.empty? && api_gateways.empty?
      puts 'Nothing to Deleting.'
      return
    end

    deployment_start_message = 'Deleting '
    deployment_start_message += "#{dynamo_tables.length} Dynamo Tables, " unless dynamo_tables.empty?
    deployment_start_message += "#{lambdas.length} Lambdas, " unless lambdas.empty?
    deployment_start_message += "#{api_gateways.length} API Gateways " unless api_gateways.empty?
    puts deployment_start_message

    total_time_start = Time.now

    services_time_start = total_time_start
    dynamo_tables.each { |table| table.delete(@dynamo_client, @region) }
    services_time_end = Time.now

    unless dynamo_tables.empty?
      puts "Deleting #{dynamo_tables.length} Table(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    services_time_start = Time.now
    lambdas.each { |lambda| lambda.delete(@lambda_client, @region) }
    services_time_end = Time.now

    unless lambdas.empty?
      puts "Deleting #{lambdas.length} Lambda(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    services_time_start = Time.now
    api_gateways.each { |apig| apig.delete(@api_gateway_client, @region) }
    services_time_end = Time.now

    unless api_gateways.empty?
      puts "Deleting #{api_gateways.length} API Gateway(s) took: \
      #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
    end

    total_time_end = Time.now

    puts "Total API Deletion took: \
    #{Time.at(total_time_end - total_time_start).utc.strftime('%H:%M:%S')}"
    puts 'Successful Deletion of API'

    true
  end

  private

  def environment_parameter_guard(parameter)
    parameter_guard(parameter, LambdaWrap::Environment, 'LambdaWrap::Environment')
  end

  def parameter_guard(parameter, type, type_name)
    return if parameter.is_a?(type)
    raise ArgumentError, "Must pass a #{type_name} to the API Manager. Got: #{parameter}"
  end

  def no_op?
    dynamo_tables.empty? && lambdas.empty? && api_gateways.empty?
  end
end

Instance Method Details

#add_api_gateway(*new_api_gateway) ⇒ Object

Add API Gateway Object(s) to the API.

Parameters:

  • new_api_gateway (LambdaWrap::ApiGateway, Array<LambdaWrap::ApiGateway>)

    Splat of LambdaWrap API Gateway objects to add to the API. Overloaded as: add_api_gateway(apig1) OR add_api_gateway([apig1, apig2]) OR add_api_gateway(apig1, apig2)

Since:

  • 1.0



88
89
90
91
92
# File 'lib/lambda_wrap/api_manager.rb', line 88

def add_api_gateway(*new_api_gateway)
  flattened_api_gateways = new_api_gateway.flatten
  flattened_api_gateways.each { |apig| parameter_guard(apig, LambdaWrap::ApiGateway, 'LambdaWrap::ApiGateway') }
  api_gateways.concat(flattened_api_gateways)
end

#add_dynamo_table(*new_table) ⇒ Object

Add Dynamo Table Object(s) to the API.

Parameters:

  • new_table (LambdaWrap::DynamoTable, Array<LambdaWrap::DynamoTable>)

    Splat of LambdaWrap DynamoTable objects to add to the API. Overloaded as: add_dynamo_table(table1) OR add_dynamo_table([table1, table2]) OR add_dynamo_table(table1, table2)

Since:

  • 1.0



77
78
79
80
81
# File 'lib/lambda_wrap/api_manager.rb', line 77

def add_dynamo_table(*new_table)
  flattened_tables = new_table.flatten
  flattened_tables.each { |table| parameter_guard(table, LambdaWrap::DynamoTable, 'LambdaWrap::DynamoTable') }
  dynamo_tables.concat(flattened_tables)
end

#add_lambda(*new_lambda) ⇒ Object

Add Lambda Object(s) to the API.

Parameters:

  • new_lambda (LambdaWrap::Lambda Array<LambdaWrap::Lambda>)

    Splat of LambdaWrap Lambda objects to add to the API. Overloaded as: add_lambda(lambda1) OR add_lambda([lambda1, lambda2]) OR add_lambda(lambda1, lambda2)

Since:

  • 1.0



66
67
68
69
70
# File 'lib/lambda_wrap/api_manager.rb', line 66

def add_lambda(*new_lambda)
  flattened_lambdas = new_lambda.flatten
  flattened_lambdas.each { |lambda| parameter_guard(lambda, LambdaWrap::Lambda, 'LambdaWrap::Lambda') }
  lambdas.concat(flattened_lambdas)
end

#deleteObject

Deletes all services from the cloud.

Since:

  • 1.0



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/lambda_wrap/api_manager.rb', line 205

def delete
  if dynamo_tables.empty? && lambdas.empty? && api_gateways.empty?
    puts 'Nothing to Deleting.'
    return
  end

  deployment_start_message = 'Deleting '
  deployment_start_message += "#{dynamo_tables.length} Dynamo Tables, " unless dynamo_tables.empty?
  deployment_start_message += "#{lambdas.length} Lambdas, " unless lambdas.empty?
  deployment_start_message += "#{api_gateways.length} API Gateways " unless api_gateways.empty?
  puts deployment_start_message

  total_time_start = Time.now

  services_time_start = total_time_start
  dynamo_tables.each { |table| table.delete(@dynamo_client, @region) }
  services_time_end = Time.now

  unless dynamo_tables.empty?
    puts "Deleting #{dynamo_tables.length} Table(s) took: \
    #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
  end

  services_time_start = Time.now
  lambdas.each { |lambda| lambda.delete(@lambda_client, @region) }
  services_time_end = Time.now

  unless lambdas.empty?
    puts "Deleting #{lambdas.length} Lambda(s) took: \
    #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
  end

  services_time_start = Time.now
  api_gateways.each { |apig| apig.delete(@api_gateway_client, @region) }
  services_time_end = Time.now

  unless api_gateways.empty?
    puts "Deleting #{api_gateways.length} API Gateway(s) took: \
    #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
  end

  total_time_end = Time.now

  puts "Total API Deletion took: \
  #{Time.at(total_time_end - total_time_start).utc.strftime('%H:%M:%S')}"
  puts 'Successful Deletion of API'

  true
end

#deploy(environment_options) ⇒ Object

Deploys all services to the specified environment.

Parameters:

Since:

  • 1.0



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
# File 'lib/lambda_wrap/api_manager.rb', line 97

def deploy(environment_options)
  environment_parameter_guard(environment_options)
  if no_op?
    puts 'Nothing to deploy.'
    return
  end

  deployment_start_message = 'Deploying '
  deployment_start_message += "#{dynamo_tables.length} Dynamo Tables, " unless dynamo_tables.empty?
  deployment_start_message += "#{lambdas.length} Lambdas, " unless lambdas.empty?
  deployment_start_message += "#{api_gateways.length} API Gateways " unless api_gateways.empty?
  deployment_start_message += "to Environment: #{environment_options.name}"
  puts deployment_start_message

  total_time_start = Time.now

  services_time_start = total_time_start
  dynamo_tables.each { |table| table.deploy(environment_options, @dynamo_client, @region) }
  services_time_end = Time.now

  unless dynamo_tables.empty?
    puts "Deploying #{dynamo_tables.length} Table(s) took: \
    #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
  end

  services_time_start = Time.now
  lambdas.each { |lambda| lambda.deploy(environment_options, @lambda_client, @region) }
  services_time_end = Time.now

  unless lambdas.empty?
    puts "Deploying #{lambdas.length} Lambda(s) took: \
    #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
  end

  services_time_start = Time.now
  api_gateways.each { |apig| apig.deploy(environment_options, @api_gateway_client, @region) }
  services_time_end = Time.now

  unless api_gateways.empty?
    puts "Deploying #{api_gateways.length} API Gateway(s) took: \
    #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
  end

  total_time_end = Time.now

  puts "Total API Deployment took: \
  #{Time.at(total_time_end - total_time_start).utc.strftime('%H:%M:%S')}"
  puts "Successfully deployed API to #{environment_options.name}"

  true
end

#teardown(environment_options) ⇒ Object

Tearsdown Environment for all services.

Parameters:

Since:

  • 1.0



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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/lambda_wrap/api_manager.rb', line 152

def teardown(environment_options)
  environment_parameter_guard(environment_options)
  if no_op?
    puts 'Nothing to teardown.'
    return
  end

  deployment_start_message = 'Tearing-down '
  deployment_start_message += "#{dynamo_tables.length} Dynamo Tables, " unless dynamo_tables.empty?
  deployment_start_message += "#{lambdas.length} Lambdas, " unless lambdas.empty?
  deployment_start_message += "#{api_gateways.length} API Gateways " unless api_gateways.empty?
  deployment_start_message += " Environment: #{environment_options.name}"
  puts deployment_start_message

  total_time_start = Time.now

  services_time_start = total_time_start
  dynamo_tables.each { |table| table.teardown(environment_options, @dynamo_client, @region) }
  services_time_end = Time.now

  unless dynamo_tables.empty?
    puts "Tearing-down #{dynamo_tables.length} Table(s) took: \
    #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
  end

  services_time_start = Time.now
  lambdas.each { |lambda| lambda.teardown(environment_options, @lambda_client, @region) }
  services_time_end = Time.now

  unless lambdas.empty?
    puts "Tearing-down #{lambdas.length} Lambda(s) took: \
    #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
  end

  services_time_start = Time.now
  api_gateways.each { |apig| apig.teardown(environment_options, @api_gateway_client, @region) }
  services_time_end = Time.now

  unless api_gateways.empty?
    puts "Tearing-down #{api_gateways.length} API Gateway(s) took: \
    #{Time.at(services_time_end - services_time_start).utc.strftime('%H:%M:%S')}"
  end

  total_time_end = Time.now

  puts "Total API Tear-down took: \
  #{Time.at(total_time_end - total_time_start).utc.strftime('%H:%M:%S')}"
  puts "Successful Teardown API to #{environment_options.name}"

  true
end