swaggerless 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1512b9d9d029261974bcaf339643955248d045ac
4
- data.tar.gz: 5fbfa3e56463e5f30c4fa2ff56e247b2069b44f2
3
+ metadata.gz: b470ab830a269ae86c58935b56f8f77b69dc533e
4
+ data.tar.gz: 92a6d081e5ae87547a0e2e5ed2a7a4e1a71bf078
5
5
  SHA512:
6
- metadata.gz: bc62dd7eaa7fdc62ea7daacf11f2187364a5ba5a4b8c56ed0ad3f5459857c904f65e614f166a8c25ecafeee3bdf314154fa0552ccc6f492df8647cc24f057049
7
- data.tar.gz: b19139e3cfa3e88e79e77aae15c59cf53eb0cccfb8da9d026a7371b3dc64a40563d1ca163a1967ff59c53412f5c4e437b3de87de9a4b7466fc03eb3900a5509a
6
+ metadata.gz: 5a27cfff8adfccbeef49741e172aed6e61bd5649e0f088d044488b97eab4e0ea5e79fb9bd015afe42881d42762b139c96f7b5a3a37428487b55679ea846deaa4
7
+ data.tar.gz: 9e8b10444659019a60131bafbaf8eb1f8a4b49359a98ee0e0a862227de982189c95e3ddf8dda77938e924cc9d04ca9b7960d309f56e6f65c9be0e4831b0ea0ba
@@ -12,6 +12,12 @@ module Swaggerless
12
12
  @swaggerExtractor = Swaggerless::SwaggerExtractor.new();
13
13
  end
14
14
 
15
+ def remove_stage(swagger, stage_to_remove)
16
+ apis = @api_gateway_client.get_rest_apis(limit: 500).data
17
+ api = apis.items.select { |a| a.name == swagger['info']['title'] }.first
18
+ @api_gateway_client.delete_stage({rest_api_id: api.id, stage_name: stage_to_remove})
19
+ end
20
+
15
21
  def clean_unused_deployments(swagger)
16
22
  apis = @api_gateway_client.get_rest_apis(limit: 500).data
17
23
  api = apis.items.select { |a| a.name == swagger['info']['title'] }.first
@@ -44,9 +50,11 @@ module Swaggerless
44
50
  end
45
51
 
46
52
  configured_lambdas = @swaggerExtractor.get_lambda_map(swagger)
47
- configured_lambdas.each do |functionName, value|
53
+ configured_lambdas.each do |functionName, config|
48
54
  lambda_versions_used = Hash.new
49
-
55
+ if config[:handler] == nil
56
+ next
57
+ end
50
58
  begin
51
59
  resp = @lambda_client.list_aliases({function_name: functionName, max_items: 500 })
52
60
  resp.aliases.each do |funcAlias|
@@ -1,4 +1,5 @@
1
1
  require "aws-sdk"
2
+ require 'digest/sha1'
2
3
 
3
4
  module Swaggerless
4
5
 
@@ -13,6 +14,7 @@ module Swaggerless
13
14
  @verbose = false;
14
15
  @function_alias = get_current_package_alias
15
16
  @swaggerExtractor = Swaggerless::SwaggerExtractor.new();
17
+ @cloudwatch_client = Aws::CloudWatchLogs::Client.new();
16
18
  @lambda_client = Aws::Lambda::Client.new(region: @region)
17
19
  end
18
20
 
@@ -25,14 +27,15 @@ module Swaggerless
25
27
  end
26
28
 
27
29
  def deploy_authoirizers_and_update_authorizers_uri(lambda_role_arn, swagger)
30
+ lambdas_configs = @swaggerExtractor.get_lambda_map(swagger)
28
31
  swagger["securityDefinitions"].each do |securityDefinitionName, securityDefinition|
29
32
  if securityDefinition[AMZ_APIGATEWAY_AUTHORIZER] != nil then
30
33
  authorizer = securityDefinition[EXT_LAMBDA_NAME]
31
- if securityDefinition[EXT_LAMBDA_NAME] then
32
- securityDefinition[AMZ_APIGATEWAY_AUTHORIZER]["authorizerUri"] = deploy_lambda(lambda_role_arn, securityDefinition[EXT_LAMBDA_NAME],
33
- "Authorizer for #{swagger["info"]["title"]}", securityDefinition[EXT_LAMBDA_RUNTIME], securityDefinition[EXT_LAMBDA_HANDLER],
34
- securityDefinition[EXT_LAMBDA_TIMEOUT])
35
- else
34
+ if securityDefinition[EXT_LAMBDA_NAME] and securityDefinition[EXT_LAMBDA_HANDLER] then
35
+ config = lambdas_configs[securityDefinition[EXT_LAMBDA_NAME]]
36
+ securityDefinition[AMZ_APIGATEWAY_AUTHORIZER]["authorizerUri"] =
37
+ deploy_lambda_and_attach_log_forwarder(lambda_role_arn, securityDefinition, config)
38
+ elsif securityDefinition[EXT_LAMBDA_NAME]
36
39
  securityDefinition[AMZ_APIGATEWAY_AUTHORIZER]["authorizerUri"] = "arn:aws:apigateway:#{@region}:lambda:path/2015-03-31/functions/arn:aws:lambda:#{@region}:#{@account}:function:#{authorizer}/invocations"
37
40
  end
38
41
  policy_exists = false
@@ -59,8 +62,8 @@ module Swaggerless
59
62
  if method_config[EXT_LAMBDA_HANDLER] then
60
63
  if deployed_operations[method_config[EXT_LAMBDA_NAME]] == nil
61
64
  config = lambdas_configs[method_config[EXT_LAMBDA_NAME]]
62
- deployed_operations[method_config[EXT_LAMBDA_NAME]] = deploy_lambda(lambda_role_arn, method_config[EXT_LAMBDA_NAME], config[:description],
63
- config[:runtime], config[:handler], config[:timeout])
65
+ deployed_operations[method_config[EXT_LAMBDA_NAME]] =
66
+ deploy_lambda_and_attach_log_forwarder(lambda_role_arn, method_config, config)
64
67
  end
65
68
  puts "Updating swagger with integration uri for #{method} #{path}: #{deployed_operations[method_config[EXT_LAMBDA_NAME]]}" unless not @verbose
66
69
  method_config['x-amazon-apigateway-integration']['uri'] = deployed_operations[method_config[EXT_LAMBDA_NAME]]
@@ -77,6 +80,45 @@ module Swaggerless
77
80
  end
78
81
  end
79
82
 
83
+ def deploy_lambda_and_attach_log_forwarder(lambda_role_arn, lambda_obj, config)
84
+ lambda_arn = deploy_lambda(lambda_role_arn, lambda_obj[EXT_LAMBDA_NAME], config[:description], config[:runtime], config[:handler], config[:timeout])
85
+ if config[:log_forwarder]
86
+ attach_log_forwarder(lambda_obj[EXT_LAMBDA_NAME], config[:log_forwarder])
87
+ end
88
+ return lambda_arn
89
+ end
90
+
91
+ def attach_log_forwarder(lambda_name, log_forwarder)
92
+ permissionStatementId = Digest::SHA1.hexdigest "logs_#{lambda_name}_to_#{log_forwarder}"
93
+ begin
94
+ @lambda_client.remove_permission({function_name: "arn:aws:lambda:#{@region}:#{@account}:function:#{log_forwarder}",
95
+ statement_id: permissionStatementId})
96
+ rescue Aws::Lambda::Errors::ResourceNotFoundException => e
97
+ end
98
+
99
+ @lambda_client.add_permission({function_name: "arn:aws:lambda:#{@region}:#{@account}:function:#{log_forwarder}",
100
+ statement_id: permissionStatementId,
101
+ action: "lambda:InvokeFunction",
102
+ principal: "logs.#{@region}.amazonaws.com", source_arn: "arn:aws:logs:#{@region}:#{@account}:log-group:/aws/lambda/#{lambda_name}:*",
103
+ source_account: "#{@account}" })
104
+
105
+ begin
106
+ resp = @cloudwatch_client.describe_log_groups({ log_group_name_prefix: "/aws/lambda/#{lambda_name}", limit: 1 })
107
+ if resp.log_groups.length == 0
108
+ @cloudwatch_client.create_log_group({log_group_name: "/aws/lambda/#{lambda_name}"})
109
+ end
110
+ resp = @cloudwatch_client.describe_subscription_filters({log_group_name: "/aws/lambda/#{lambda_name}"})
111
+ if resp.subscription_filters.length > 0
112
+ resp.subscription_filters.each do |filter|
113
+ @cloudwatch_client.delete_subscription_filter({log_group_name: "/aws/lambda/#{lambda_name}", filter_name: filter.filter_name})
114
+ end
115
+ end
116
+ @cloudwatch_client.put_subscription_filter({ log_group_name: "/aws/lambda/#{lambda_name}",
117
+ filter_name: log_forwarder, filter_pattern: '',
118
+ destination_arn: "arn:aws:lambda:#{@region}:#{@account}:function:#{log_forwarder}"})
119
+ end
120
+ end
121
+
80
122
  def deploy_lambda(lambda_role_arn, function_name, summary, runtime, handler, timeout)
81
123
  puts "Deploying #{function_name}"
82
124
  runtime ||= 'nodejs4.3'
@@ -6,6 +6,7 @@ module Swaggerless
6
6
  EXT_LAMBDA_HANDLER = 'x-amazon-lambda-handler'
7
7
  EXT_LAMBDA_TIMEOUT = 'x-amazon-lambda-timeout'
8
8
  EXT_LAMBDA_RUNTIME = 'x-amazon-lambda-runtime'
9
+ EXT_LAMBDA_LOG_FORWARDER = 'x-amazon-lambda-log-forwarder'
9
10
 
10
11
  AMZ_APIGATEWAY_AUTHORIZER = 'x-amazon-apigateway-authorizer'
11
12
 
@@ -50,6 +51,7 @@ module Swaggerless
50
51
  if to[:handler] == nil then to[:handler] = from[:handler] end
51
52
  if to[:timeout] == nil then to[:timeout] = from[:timeout] end
52
53
  if to[:runtime] == nil then to[:runtime] = from[:runtime] end
54
+ if to[:log_forwarder] == nil then to[:log_forwarder] = from[:log_forwarder] end
53
55
  end
54
56
 
55
57
  def is_lambda_config_correct(l1, l2)
@@ -57,14 +59,16 @@ module Swaggerless
57
59
  fill_in_config_gaps(l1,l2)
58
60
  return (l1[:handler] == l2[:handler] and
59
61
  l1[:timeout] == l2[:timeout] and
60
- l1[:runtime] == l2[:runtime])
62
+ l1[:runtime] == l2[:runtime] and
63
+ l1[:log_forwarder] == l2[:log_forwarder])
61
64
  end
62
65
 
63
66
  def build_lambda_config_hash(method_config)
64
67
  { handler: method_config[EXT_LAMBDA_HANDLER],
65
68
  timeout: method_config[EXT_LAMBDA_TIMEOUT],
66
69
  runtime: method_config[EXT_LAMBDA_RUNTIME],
67
- description: method_config[SWGR_SUMMARY] || method_config[SWGR_DESCRIPTION]}
70
+ description: method_config[SWGR_SUMMARY] || method_config[SWGR_DESCRIPTION],
71
+ log_forwarder: method_config[EXT_LAMBDA_LOG_FORWARDER]}
68
72
  end
69
73
 
70
74
  end
@@ -1,3 +1,3 @@
1
1
  module Swaggerless
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
@@ -61,4 +61,15 @@ namespace :swaggerless do
61
61
  Swaggerless::Cleaner.new(@awsAccount, @awsRegion).clean_unused_deployments(swagger)
62
62
  end
63
63
 
64
+ task :delete_stage, [ :environment ] => [ :clean ] do |t, args|
65
+ swagger_content = File.read(@swaggerSpecFile)
66
+ swagger = YAML.load(swagger_content)
67
+ Swaggerless::Cleaner.new(@awsAccount, @awsRegion).remove_stage(swagger, args[:environment].gsub(/[^a-zA-Z0-9_]/, "_"))
68
+ end
69
+
70
+ desc 'Remove stage and cleanup'
71
+ task :delete, [ :environment ] => [ :clean, :delete_stage, :clean_aws_resources ] do |t, args|
72
+ end
73
+
74
+
64
75
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swaggerless
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rafal Nowosielski
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-11-29 00:00:00.000000000 Z
11
+ date: 2016-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler