awspec 0.33.0 → 0.34.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +5 -1
  3. data/lib/awspec.rb +1 -0
  4. data/lib/awspec/command/generate.rb +3 -4
  5. data/lib/awspec/error.rb +4 -0
  6. data/lib/awspec/ext.rb +1 -0
  7. data/lib/awspec/ext/array.rb +8 -0
  8. data/lib/awspec/generator.rb +1 -0
  9. data/lib/awspec/generator/spec/lambda.rb +29 -0
  10. data/lib/awspec/helper/finder.rb +2 -0
  11. data/lib/awspec/helper/finder/ami.rb +5 -3
  12. data/lib/awspec/helper/finder/autoscaling.rb +2 -2
  13. data/lib/awspec/helper/finder/cloudwatch.rb +4 -3
  14. data/lib/awspec/helper/finder/directconnect.rb +1 -1
  15. data/lib/awspec/helper/finder/ebs.rb +4 -3
  16. data/lib/awspec/helper/finder/ec2.rb +15 -51
  17. data/lib/awspec/helper/finder/elasticache.rb +2 -2
  18. data/lib/awspec/helper/finder/elb.rb +2 -2
  19. data/lib/awspec/helper/finder/iam.rb +1 -1
  20. data/lib/awspec/helper/finder/lambda.rb +8 -2
  21. data/lib/awspec/helper/finder/rds.rb +2 -2
  22. data/lib/awspec/helper/finder/route53.rb +3 -1
  23. data/lib/awspec/helper/finder/security_group.rb +10 -11
  24. data/lib/awspec/helper/finder/ses.rb +2 -2
  25. data/lib/awspec/helper/finder/subnet.rb +24 -0
  26. data/lib/awspec/helper/finder/vpc.rb +14 -28
  27. data/lib/awspec/helper/type.rb +8 -10
  28. data/lib/awspec/matcher/have_route.rb +1 -1
  29. data/lib/awspec/stub/duplicated_resource_type.rb +26 -0
  30. data/lib/awspec/stub/lambda.rb +5 -2
  31. data/lib/awspec/type/ami.rb +2 -2
  32. data/lib/awspec/type/autoscaling_group.rb +1 -1
  33. data/lib/awspec/type/base.rb +1 -1
  34. data/lib/awspec/type/cloudwatch_alarm.rb +4 -4
  35. data/lib/awspec/type/directconnect_virtual_interface.rb +2 -2
  36. data/lib/awspec/type/ebs.rb +5 -5
  37. data/lib/awspec/type/ec2.rb +12 -12
  38. data/lib/awspec/type/elasticache.rb +5 -5
  39. data/lib/awspec/type/elb.rb +8 -8
  40. data/lib/awspec/type/iam_group.rb +3 -3
  41. data/lib/awspec/type/iam_policy.rb +4 -4
  42. data/lib/awspec/type/iam_role.rb +2 -2
  43. data/lib/awspec/type/iam_user.rb +2 -2
  44. data/lib/awspec/type/lambda.rb +2 -2
  45. data/lib/awspec/type/launch_configuration.rb +3 -3
  46. data/lib/awspec/type/nat_gateway.rb +2 -2
  47. data/lib/awspec/type/network_acl.rb +5 -5
  48. data/lib/awspec/type/rds.rb +17 -17
  49. data/lib/awspec/type/route53_hosted_zone.rb +2 -2
  50. data/lib/awspec/type/route_table.rb +2 -2
  51. data/lib/awspec/type/security_group.rb +39 -39
  52. data/lib/awspec/type/ses_identity.rb +5 -5
  53. data/lib/awspec/type/subnet.rb +2 -2
  54. data/lib/awspec/type/vpc.rb +4 -4
  55. data/lib/awspec/version.rb +1 -1
  56. metadata +7 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 93670776bfee37d66642109ff0753d21ec787a45
4
- data.tar.gz: ac0cd0cd42c5389fb20b033779523f5453875810
3
+ metadata.gz: f1909c7febd232e63f4f01933058887c972a174e
4
+ data.tar.gz: cb3d860018b22a635823a714d05b86ae23db32b6
5
5
  SHA512:
6
- metadata.gz: b4ebfe837caefcd8556221f041a151a9a7f3856da19ae114b87631235d65ceb29002edc04b7a1b61f3ca2603d0573043375c401c7715ab3f9ed4cffa04b750a3
7
- data.tar.gz: 9584928a9101a96c6f688f6e26fafde38eef905ec972cf5a9047178d1dbbe22b88f90c0a611ff3008896cba1f712e2851e1116494a3a58d4d3e3524b8331a8a1
6
+ metadata.gz: 9f71aa2801687ba6a5d85329fd7613e4d64ac27477daa109df26fd604b18def3103129f5f2576188665a0db7dfba1dc6412e905b58b3fd33deb89e233474959e
7
+ data.tar.gz: d16c5744e51f88335da74c058c90b4fb6f55563f3766d5b27d6b1ec8f6138222875f496336dea431a53d10facca6f91b8b790a1298061a8f5120370174c2e100
data/Rakefile CHANGED
@@ -15,7 +15,7 @@ end
15
15
  if defined?(RSpec)
16
16
  task spec: 'spec:all'
17
17
  namespace :spec do
18
- task all: ['spec:type', 'spec:generator_spec', 'spec:generator_doc']
18
+ task all: ['spec:type', 'spec:core', 'spec:generator_spec', 'spec:generator_doc']
19
19
 
20
20
  task type: types
21
21
 
@@ -25,6 +25,10 @@ if defined?(RSpec)
25
25
  end
26
26
  end
27
27
 
28
+ RSpec::Core::RakeTask.new(:core) do |t|
29
+ t.pattern = 'spec/core/*_spec.rb'
30
+ end
31
+
28
32
  RSpec::Core::RakeTask.new(:generator_spec) do |t|
29
33
  t.pattern = 'spec/generator/spec/*_spec.rb'
30
34
  end
@@ -5,6 +5,7 @@ require 'time'
5
5
  require 'active_support/inflector'
6
6
  require 'awsecrets'
7
7
  require 'awspec/version'
8
+ require 'awspec/error'
8
9
  require 'awspec/cli'
9
10
  require 'awspec/stub'
10
11
  require 'awspec/matcher'
@@ -11,10 +11,9 @@ module Awspec
11
11
 
12
12
  types.each do |type|
13
13
  desc type + ' [vpc_id]', "Generate #{type} spec from VPC ID (or VPC \"Name\" tag)"
14
- define_method type do |*args|
14
+ define_method type do |_vpc_id|
15
15
  Awsecrets.load(profile: options[:profile])
16
- vpc_id = args.first
17
- eval "puts Awspec::Generator::Spec::#{type.camelize}.new.generate_by_vpc_id(vpc_id)"
16
+ eval "puts Awspec::Generator::Spec::#{type.camelize}.new.generate_by_vpc_id(_vpc_id)"
18
17
  end
19
18
  end
20
19
 
@@ -35,7 +34,7 @@ module Awspec
35
34
  end
36
35
 
37
36
  types_for_generate_all = %w(
38
- iam_policy cloudwatch_alarm directconnect ebs
37
+ iam_policy cloudwatch_alarm directconnect ebs lambda
39
38
  )
40
39
 
41
40
  types_for_generate_all.each do |type|
@@ -0,0 +1,4 @@
1
+ module Awspec
2
+ class DuplicatedResourceTypeError < StandardError
3
+ end
4
+ end
@@ -1 +1,2 @@
1
1
  require 'awspec/ext/struct'
2
+ require 'awspec/ext/array'
@@ -0,0 +1,8 @@
1
+ class Array
2
+ def single_resource(id = nil)
3
+ if self.count > 1
4
+ raise Awspec::DuplicatedResourceTypeError, "Duplicated resource type #{id}"
5
+ end
6
+ self.first if self.count == 1
7
+ end
8
+ end
@@ -14,6 +14,7 @@ require 'awspec/generator/spec/directconnect'
14
14
  require 'awspec/generator/spec/ebs'
15
15
  require 'awspec/generator/spec/s3_bucket'
16
16
  require 'awspec/generator/spec/nat_gateway'
17
+ require 'awspec/generator/spec/lambda'
17
18
 
18
19
  # Doc
19
20
  require 'awspec/generator/doc/type'
@@ -0,0 +1,29 @@
1
+ module Awspec::Generator
2
+ module Spec
3
+ class Lambda
4
+ include Awspec::Helper::Finder
5
+ def generate_all
6
+ lambda_functions = select_all_lambda_functions
7
+ raise 'Not Found lambda' if lambda_functions.empty?
8
+ ERB.new(lambda_spec_template, nil, '-').result(binding).chomp
9
+ end
10
+
11
+ def lambda_spec_template
12
+ template = <<-'EOF'
13
+ <% lambda_functions.each do |function| %>
14
+ describe lambda('<%= function.function_name %>') do
15
+ it { should exist }
16
+ its(:description) { should eq '<%= function.description %>' }
17
+ its(:runtime) { should eq '<%= function.runtime %>' }
18
+ its(:handler) { should eq '<%= function.handler %>' }
19
+ its(:code_size) { should eq <%= function.code_size %> }
20
+ its(:timeout) { should eq <%= function.timeout %> }
21
+ its(:memory_size) { should eq <%= function.memory_size %> }
22
+ end
23
+ <% end %>
24
+ EOF
25
+ template
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,5 +1,6 @@
1
1
  require 'aws-sdk'
2
2
  require 'awspec/helper/finder/vpc'
3
+ require 'awspec/helper/finder/subnet'
3
4
  require 'awspec/helper/finder/ec2'
4
5
  require 'awspec/helper/finder/security_group'
5
6
  require 'awspec/helper/finder/rds'
@@ -19,6 +20,7 @@ require 'awspec/helper/finder/ami'
19
20
  module Awspec::Helper
20
21
  module Finder
21
22
  include Awspec::Helper::Finder::Vpc
23
+ include Awspec::Helper::Finder::Subnet
22
24
  include Awspec::Helper::Finder::Ec2
23
25
  include Awspec::Helper::Finder::SecurityGroup
24
26
  include Awspec::Helper::Finder::Rds
@@ -5,15 +5,17 @@ module Awspec::Helper
5
5
  res = ec2_client.describe_images({
6
6
  filters: [{ name: 'image-id', values: [image_id] }]
7
7
  })
8
- return res[:images].first if res[:images].count == 1
8
+ resource = res.images.single_resource(image_id)
9
+ return resource if resource
9
10
  res = ec2_client.describe_images({
10
11
  filters: [{ name: 'name', values: [image_id] }]
11
12
  })
12
- return res[:images].first if res[:images].count == 1
13
+ resource = res.images.single_resource(image_id)
14
+ return resource if resource
13
15
  res = ec2_client.describe_images({
14
16
  filters: [{ name: 'tag:Name', values: [image_id] }]
15
17
  })
16
- return res[:images].first if res[:images].count == 1
18
+ res.images.single_resource(image_id)
17
19
  end
18
20
  end
19
21
  end
@@ -5,14 +5,14 @@ module Awspec::Helper
5
5
  res = autoscaling_client.describe_auto_scaling_groups({
6
6
  auto_scaling_group_names: [id]
7
7
  })
8
- res[:auto_scaling_groups].first if res[:auto_scaling_groups].count == 1
8
+ res.auto_scaling_groups.single_resource(id)
9
9
  end
10
10
 
11
11
  def find_launch_configuration(id)
12
12
  res = autoscaling_client.describe_launch_configurations({
13
13
  launch_configuration_names: [id]
14
14
  })
15
- res[:launch_configurations].first if res[:launch_configurations].count == 1
15
+ res.launch_configurations.single_resource(id)
16
16
  end
17
17
  end
18
18
  end
@@ -5,12 +5,13 @@ module Awspec::Helper
5
5
  res = cloudwatch_client.describe_alarms({
6
6
  alarm_names: [id]
7
7
  })
8
- return res[:metric_alarms].first if res[:metric_alarms].count == 1
8
+ return res.metric_alarms.first if res.metric_alarms.count == 1
9
9
 
10
10
  res = cloudwatch_client.describe_alarms
11
- res[:metric_alarms].find do |alarm|
12
- alarm[:alarm_arn] == id
11
+ alarms = res.metric_alarms.select do |alarm|
12
+ alarm.alarm_arn == id
13
13
  end
14
+ alarms.single_resource(id)
14
15
  end
15
16
 
16
17
  def select_all_cloudwatch_alarms
@@ -7,7 +7,7 @@ module Awspec::Helper
7
7
  next true if virtual_interface.virtual_interface_id == virtual_interface_id
8
8
  next true if virtual_interface.virtual_interface_name == virtual_interface_id
9
9
  end
10
- return ret.first if ret.count == 1
10
+ ret.single_resource(virtual_interface_id)
11
11
  end
12
12
 
13
13
  def select_virtual_interfaces
@@ -5,18 +5,19 @@ module Awspec::Helper
5
5
  res = ec2_client.describe_volumes({
6
6
  filters: [{ name: 'volume-id', values: [volume_id] }]
7
7
  })
8
- return res[:volumes].first if res[:volumes].count == 1
8
+ resource = res.volumes.single_resource(volume_id)
9
+ return resource if resource
9
10
  res = ec2_client.describe_volumes({
10
11
  filters: [{ name: 'tag:Name', values: [volume_id] }]
11
12
  })
12
- return res[:volumes].first if res[:volumes].count == 1
13
+ res.volumes.single_resource(volume_id)
13
14
  end
14
15
 
15
16
  def select_ebs_by_instance_id(id)
16
17
  res = find_ec2(id)
17
18
  volumes = []
18
19
  return volumes unless res
19
- res[:block_device_mappings].each do |block|
20
+ res.block_device_mappings.each do |block|
20
21
  volume = find_ebs(block.ebs.volume_id)
21
22
  volumes.push(volume) if volume
22
23
  end
@@ -2,39 +2,20 @@ module Awspec::Helper
2
2
  module Finder
3
3
  module Ec2
4
4
  def find_ec2(id)
5
- if id.is_a?(Array)
6
- # Aws::EC2::Client.describe_instances native filters format
5
+ # instance_id or tag:Name
6
+ begin
7
7
  res = ec2_client.describe_instances({
8
- filters: id
8
+ instance_ids: [id]
9
9
  })
10
- elsif id.is_a?(Hash)
11
- # syntax sugar
12
- filters = []
13
- id.each do |k, v|
14
- filters.push({ name: k, values: Array(v) })
15
- end
10
+ rescue
11
+ # Aws::EC2::Errors::InvalidInstanceIDMalformed
12
+ # Aws::EC2::Errors::InvalidInstanceIDNotFound
16
13
  res = ec2_client.describe_instances({
17
- filters: filters
14
+ filters: [{ name: 'tag:Name', values: [id] }]
18
15
  })
19
- elsif id.is_a?(String)
20
- # instance_id or tag:Name
21
- begin
22
- res = ec2_client.describe_instances({
23
- instance_ids: [id]
24
- })
25
- rescue
26
- # Aws::EC2::Errors::InvalidInstanceIDMalformed
27
- # Aws::EC2::Errors::InvalidInstanceIDNotFound
28
- res = ec2_client.describe_instances({
29
- filters: [{ name: 'tag:Name', values: [id] }]
30
- })
31
- end
32
- else
33
- return nil
34
16
  end
35
17
  # rubocop:enable Style/GuardClause
36
- return res[:reservations].first[:instances].first if res[:reservations].count == 1 && \
37
- res[:reservations].first[:instances].count == 1
18
+ res.reservations.first.instances.single_resource(id) if res.reservations.count == 1
38
19
  end
39
20
 
40
21
  def find_ec2_attribute(id, attribute)
@@ -52,12 +33,12 @@ module Awspec::Helper
52
33
  res = ec2_client.method(method_name).call({
53
34
  filters: [{ name: type + '-gateway-id', values: [gateway_id] }]
54
35
  })
55
-
56
- return res[type + '_gateways'].first if res[type + '_gateways'].count == 1
36
+ resource = res[type + '_gateways'].single_resource(gateway_id)
37
+ return resource if resource
57
38
  res = ec2_client.method(method_name).call({
58
39
  filters: [{ name: 'tag:Name', values: [gateway_id] }]
59
40
  })
60
- return res[type + '_gateways'].first if res[type + '_gateways'].count == 1
41
+ res[type + '_gateways'].single_resource(gateway_id)
61
42
  end
62
43
  end
63
44
 
@@ -65,22 +46,7 @@ module Awspec::Helper
65
46
  res = ec2_client.describe_nat_gateways({
66
47
  filter: [{ name: 'nat-gateway-id', values: [gateway_id] }]
67
48
  })
68
- return res['nat_gateways'].first if res['nat_gateways'].count == 1
69
- end
70
-
71
- def find_security_group(sg_id)
72
- res = ec2_client.describe_security_groups({
73
- filters: [{ name: 'group-id', values: [sg_id] }]
74
- })
75
- return res[:security_groups].first if res[:security_groups].count == 1
76
- res = ec2_client.describe_security_groups({
77
- filters: [{ name: 'group-name', values: [sg_id] }]
78
- })
79
- return res[:security_groups].first if res[:security_groups].count == 1
80
- res = ec2_client.describe_security_groups({
81
- filters: [{ name: 'tag:Name', values: [sg_id] }]
82
- })
83
- return res[:security_groups].first if res[:security_groups].count == 1
49
+ res.nat_gateways.single_resource(gateway_id)
84
50
  end
85
51
 
86
52
  def select_ec2_by_vpc_id(vpc_id)
@@ -88,8 +54,7 @@ module Awspec::Helper
88
54
  filters: [{ name: 'vpc-id', values: [vpc_id] }]
89
55
  })
90
56
  instances = []
91
- return instances unless res[:reservations].count > 0
92
- res[:reservations].each do |reservation|
57
+ res.reservations.each do |reservation|
93
58
  reservation.instances.each do |instance|
94
59
  instances.push(instance)
95
60
  end
@@ -101,15 +66,14 @@ module Awspec::Helper
101
66
  res = ec2_client.describe_addresses({
102
67
  filters: [{ name: 'instance-id', values: [id] }]
103
68
  })
104
- return [] unless res[:addresses].count > 0
105
- res[:addresses]
69
+ res.addresses
106
70
  end
107
71
 
108
72
  def select_nat_gateway_by_vpc_id(vpc_id)
109
73
  res = ec2_client.describe_nat_gateways({
110
74
  filter: [{ name: 'vpc-id', values: [vpc_id] }]
111
75
  })
112
- res[:nat_gateways]
76
+ res.nat_gateways
113
77
  end
114
78
  end
115
79
  end
@@ -5,7 +5,7 @@ module Awspec::Helper
5
5
  res = elasticache_client.describe_cache_clusters({
6
6
  cache_cluster_id: id
7
7
  })
8
- res[:cache_clusters].first if res[:cache_clusters].count == 1
8
+ res.cache_clusters.single_resource(id)
9
9
  rescue
10
10
  nil
11
11
  end
@@ -14,7 +14,7 @@ module Awspec::Helper
14
14
  res = elasticache_client.describe_cache_subnet_groups({
15
15
  cache_subnet_group_name: group_name
16
16
  })
17
- res[:cache_subnet_groups].first if res[:cache_subnet_groups].count == 1
17
+ res.cache_subnet_groups.single_resource(group_name)
18
18
  end
19
19
  end
20
20
  end
@@ -5,14 +5,14 @@ module Awspec::Helper
5
5
  res = elb_client.describe_load_balancers({
6
6
  load_balancer_names: [id]
7
7
  })
8
- return res[:load_balancer_descriptions].first if res[:load_balancer_descriptions].count == 1
8
+ res.load_balancer_descriptions.single_resource(id)
9
9
  rescue
10
10
  return nil
11
11
  end
12
12
 
13
13
  def select_elb_by_vpc_id(vpc_id)
14
14
  res = elb_client.describe_load_balancers
15
- res[:load_balancer_descriptions].select do |lb|
15
+ res.load_balancer_descriptions.select do |lb|
16
16
  lb.vpc_id == vpc_id
17
17
  end
18
18
  end
@@ -15,7 +15,7 @@ module Awspec::Helper
15
15
  (res.next_page? && res = res.next_page) || break
16
16
  end
17
17
 
18
- selected.first if selected.count == 1
18
+ selected.single_resource(id)
19
19
  end
20
20
  end
21
21
 
@@ -12,14 +12,20 @@ module Awspec::Helper
12
12
  (res.next_page? && res = res.next_page) || break
13
13
  end
14
14
 
15
- selected.first if selected.count == 1
15
+ selected.single_resource(id)
16
16
  end
17
17
 
18
18
  def select_event_source_by_function_arn(function_arn)
19
19
  res = lambda_client.list_event_source_mappings({
20
20
  function_name: function_arn
21
21
  })
22
- res[:event_source_mappings]
22
+ res.event_source_mappings
23
+ end
24
+
25
+ def select_all_lambda_functions
26
+ res = lambda_client.list_functions.map do |responce|
27
+ responce.functions
28
+ end.flatten
23
29
  end
24
30
  end
25
31
  end
@@ -6,12 +6,12 @@ module Awspec::Helper
6
6
  res = rds_client.describe_db_instances({
7
7
  db_instance_identifier: id
8
8
  })
9
- return res[:db_instances].first if res[:db_instances].count == 1
9
+ res.db_instances.single_resource(id)
10
10
  end
11
11
 
12
12
  def select_rds_by_vpc_id(vpc_id)
13
13
  res = rds_client.describe_db_instances
14
- res[:db_instances].select do |db_instance|
14
+ res.db_instances.select do |db_instance|
15
15
  db_instance.db_subnet_group.vpc_id == vpc_id
16
16
  end
17
17
  end
@@ -3,14 +3,16 @@ module Awspec::Helper
3
3
  module Route53
4
4
  def find_hosted_zone(id)
5
5
  res = route53_client.list_hosted_zones
6
+ selected = []
6
7
  loop do
7
8
  res.hosted_zones.each do |hosted_zone|
8
9
  if hosted_zone[:name] == id || hosted_zone[:id] == '/hostedzone/' + id || hosted_zone[:id] == id
9
- return hosted_zone
10
+ selected.push(hosted_zone)
10
11
  end
11
12
  end
12
13
  (res.next_page? && res = res.next_page) || break
13
14
  end
15
+ selected.single_resource(id)
14
16
  end
15
17
 
16
18
  def select_record_sets_by_hosted_zone_id(id)