awspec 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,23 @@
1
+ module Awspec::Generator
2
+ module Spec
3
+ class RdsDbParameterGroup
4
+ include Awspec::Helper::Finder
5
+ def generate_by_paramater_group(paramater_group)
6
+ @paramater_group = paramater_group
7
+ res = select_all_rds_db_parameters(@paramater_group)
8
+ ERB.new(db_paramater_group_template, nil, '-').result(binding).gsub(/^\n/, '')
9
+ end
10
+
11
+ def db_paramater_group_template
12
+ template = <<-'EOF'
13
+ describe rds_db_parameter_group('<%= @paramater_group %>') do
14
+ <% res.each do |key, value| %>
15
+ its('<%= key %>') { should eq '<%= value %>' }
16
+ <% end %>
17
+ end
18
+ EOF
19
+ template
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,4 +1,5 @@
1
1
  require 'aws-sdk'
2
+ require 'awspec/helper/finder/nlb'
2
3
  require 'awspec/helper/finder/alb'
3
4
  require 'awspec/helper/finder/vpc'
4
5
  require 'awspec/helper/finder/subnet'
@@ -39,6 +40,7 @@ require 'awspec/helper/client_wrap'
39
40
 
40
41
  module Awspec::Helper
41
42
  module Finder
43
+ include Awspec::Helper::Finder::Nlb
42
44
  include Awspec::Helper::Finder::Alb
43
45
  include Awspec::Helper::Finder::Vpc
44
46
  include Awspec::Helper::Finder::Subnet
@@ -22,6 +22,13 @@ module Awspec::Helper
22
22
  res.load_balancer_target_groups
23
23
  end
24
24
 
25
+ def select_lb_target_group_by_autoscaling_group_name(name)
26
+ res = autoscaling_client.describe_load_balancer_target_groups({
27
+ auto_scaling_group_name: name
28
+ })
29
+ res.load_balancer_target_groups
30
+ end
31
+
25
32
  def find_block_device_mapping(id, device_id)
26
33
  ret = find_launch_configuration(id).block_device_mappings.select do |device|
27
34
  next true if device.device_name == device_id
@@ -0,0 +1,57 @@
1
+ module Awspec::Helper
2
+ module Finder
3
+ module Nlb
4
+ def find_nlb(id)
5
+ res = elbv2_client.describe_load_balancers({ names: [id] })
6
+ res.load_balancers.select do |lb|
7
+ lb.type == 'network'
8
+ end.single_resource(id)
9
+ rescue
10
+ return nil
11
+ end
12
+
13
+ def select_nlb_by_vpc_id(vpc_id)
14
+ res = elbv2_client.describe_load_balancers
15
+ res.load_balancers.select do |lb|
16
+ lb.vpc_id == vpc_id && lb.type == 'network'
17
+ end
18
+ end
19
+
20
+ def find_nlb_listener(arn)
21
+ res = elbv2_client.describe_listeners({ listener_arns: [arn] })
22
+ res.listeners.single_resource(arn)
23
+ rescue
24
+ return nil
25
+ end
26
+
27
+ def find_nlb_target_group(id)
28
+ res = elbv2_client.describe_target_groups({ names: [id] })
29
+ httpx_res = res.target_groups.select do |tg|
30
+ %w(HTTP HTTPS).include?(tg.protocol)
31
+ end
32
+ if !httpx_res || httpx_res.empty?
33
+ raise "ERROR: Found no HTTP nor HTTPS -protocol target group named '#{id}'."
34
+ end
35
+ httpx_res.single_resource(id)
36
+ rescue
37
+ # Prefer the HTTP/HTTPS protocol target group, but survive without it:
38
+ begin
39
+ res.target_groups.single_resource(id)
40
+ rescue
41
+ return nil
42
+ end
43
+ end
44
+
45
+ def select_rule_by_nlb_listener_id(id)
46
+ selected = []
47
+ next_marker = nil
48
+ loop do
49
+ res = elbv2_client.describe_rules(marker: next_marker, listener_arn: id)
50
+ selected += res.rules unless res.nil?
51
+ (res.nil? && next_marker = res.next_marker) || break
52
+ end
53
+ selected
54
+ end
55
+ end
56
+ end
57
+ end
@@ -20,6 +20,34 @@ module Awspec::Helper
20
20
  db_instance.db_subnet_group.vpc_id == vpc_id
21
21
  end
22
22
  end
23
+
24
+ def select_all_rds_db_parameters(paramater_group)
25
+ parameters = {}
26
+ res = rds_client.describe_db_parameters({
27
+ db_parameter_group_name: paramater_group
28
+ })
29
+ loop do
30
+ res.parameters.each do |param|
31
+ parameters[param.parameter_name] = param.parameter_value
32
+ end
33
+ (res.next_page? && res = res.next_page) || break
34
+ end
35
+ parameters
36
+ end
37
+
38
+ def select_all_rds_db_cluster_parameters(paramater_group)
39
+ parameters = {}
40
+ res = rds_client.describe_db_cluster_parameters({
41
+ db_cluster_parameter_group_name: paramater_group
42
+ })
43
+ loop do
44
+ res.parameters.each do |param|
45
+ parameters[param.parameter_name] = param.parameter_value
46
+ end
47
+ (res.respond_to?(:next_page?) && res.next_page? && res = res.next_page) || break
48
+ end
49
+ parameters
50
+ end
23
51
  end
24
52
  end
25
53
  end
@@ -8,14 +8,16 @@ module Awspec
8
8
  require 'awspec/type/account_attribute'
9
9
 
10
10
  TYPES = %w(
11
- alb ami autoscaling_group cloudtrail cloudwatch_alarm cloudwatch_event directconnect_virtual_interface
11
+ alb alb_listener alb_target_group ami autoscaling_group cloudtrail
12
+ cloudwatch_alarm cloudwatch_event directconnect_virtual_interface
12
13
  ebs ec2 ecr_repository ecs_cluster ecs_container_instance ecs_service ecs_task_definition
13
14
  efs elasticache elasticache_cache_parameter_group elasticsearch elb iam_group
14
15
  iam_policy iam_role iam_user kms lambda launch_configuration nat_gateway
15
- network_acl network_interface rds rds_db_cluster_parameter_group rds_db_parameter_group route53_hosted_zone
16
+ network_acl network_interface nlb nlb_listener nlb_target_group
17
+ rds rds_db_cluster_parameter_group rds_db_parameter_group route53_hosted_zone
16
18
  route_table s3_bucket security_group ses_identity subnet vpc cloudfront_distribution
17
19
  elastictranscoder_pipeline waf_web_acl customer_gateway vpn_gateway vpn_connection internet_gateway acm
18
- cloudwatch_logs dynamodb_table eip sqs alb_listener alb_target_group cloudformation_stack
20
+ cloudwatch_logs dynamodb_table eip sqs cloudformation_stack
19
21
  )
20
22
 
21
23
  ACCOUNT_ATTRIBUTES = %w(
@@ -61,6 +61,9 @@ require 'awspec/matcher/belong_to_domain'
61
61
  # Alb Target Group
62
62
  require 'awspec/matcher/belong_to_alb'
63
63
 
64
+ # Nlb Target Group
65
+ require 'awspec/matcher/belong_to_nlb'
66
+
64
67
  # VPC
65
68
  require 'awspec/matcher/be_connected_to_vpc'
66
69
  require 'awspec/matcher/have_vpc_peering_connection'
@@ -0,0 +1,8 @@
1
+ RSpec::Matchers.define :belong_to_nlb do |nlb_arn|
2
+ match do |type|
3
+ return true if type.load_balancer_arns.include?(nlb_arn)
4
+ ret = type.find_nlb(nlb_arn)
5
+ return false unless ret
6
+ type.load_balancer_arns.include?(ret.load_balancer_arn)
7
+ end
8
+ end
@@ -1,7 +1,8 @@
1
1
  RSpec::Matchers.define :have_rule do |rule_id|
2
2
  match do |type|
3
3
  return type.has_rule?(rule_id, @priority, @action) if type.instance_of?(Awspec::Type::WafWebAcl)
4
- type.has_rule?(rule_id, @priority, @conditions, @actions) if type.instance_of?(Awspec::Type::AlbListener)
4
+ return type.has_rule?(rule_id, @priority, @conditions, @actions) if type.instance_of?(Awspec::Type::AlbListener)
5
+ type.has_rule?(rule_id, @priority, @conditions, @actions) if type.instance_of?(Awspec::Type::NlbListener)
5
6
  end
6
7
 
7
8
  chain :priority do |priority|
@@ -0,0 +1,119 @@
1
+ # rubocop:disable Metrics/LineLength
2
+ Aws.config[:elasticloadbalancingv2] = {
3
+ stub_responses: {
4
+ describe_load_balancers: {
5
+ load_balancers: [
6
+ {
7
+ load_balancer_arn:
8
+ 'arn:aws:elasticloadbalancing:ap-northeast-1:1234567890:loadbalancer/app/my-nlb/1aa1bb1cc1ddee11',
9
+ dns_name:
10
+ 'internal-my-nlb-1551266724.ap-northeast-1.elb.amazonaws.com',
11
+ canonical_hosted_zone_id: 'A12BCDEDCBA34BC',
12
+ created_time: Time.new(2017, 4, 4, 9, 00, 00, '+00:00'),
13
+ load_balancer_name: 'my-nlb',
14
+ # scheme: 'internal',
15
+ scheme: 'internet-facing',
16
+ vpc_id: 'vpc-ab123cde',
17
+ state:
18
+ {
19
+ code: 'active',
20
+ reason: nil
21
+ },
22
+ type: 'network',
23
+ availability_zones:
24
+ [
25
+ {
26
+ zone_name: 'ap-northeast-1a',
27
+ subnet_id: 'subnet-1234a567'
28
+ },
29
+ {
30
+ zone_name: 'ap-northeast-1c',
31
+ subnet_id: 'subnet-abcd7890'
32
+ }
33
+ ],
34
+ ip_address_type: 'ipv4'
35
+ },
36
+ {
37
+ load_balancer_arn:
38
+ 'arn:aws:elasticloadbalancing:ap-northeast-1:1234567890:loadbalancer/net/my-alb/2aa2bb2cc2ddee22',
39
+ dns_name:
40
+ 'my-alb-2aa2bb2cc2ddee22.elb.ap-northeast-1.amazonaws.com',
41
+ canonical_hosted_zone_id: 'N12BCDEDCBN34BC',
42
+ created_time: Time.new(2017, 4, 4, 9, 00, 00, '+00:00'),
43
+ load_balancer_name: 'my-alb',
44
+ scheme: 'internal',
45
+ vpc_id: 'vpc-ab123cde',
46
+ state:
47
+ {
48
+ code: 'active',
49
+ reason: nil
50
+ },
51
+ type: 'application',
52
+ availability_zones:
53
+ [
54
+ {
55
+ zone_name: 'ap-northeast-1a',
56
+ subnet_id: 'subnet-7890ne12'
57
+ },
58
+ {
59
+ zone_name: 'ap-northeast-1c',
60
+ subnet_id: 'subnet-nnnd1234'
61
+ }
62
+ ],
63
+ ip_address_type: 'ipv4'
64
+ }
65
+ ],
66
+ next_marker: nil
67
+ },
68
+ describe_listeners: {
69
+ listeners: [
70
+ {
71
+ default_actions: [
72
+ {
73
+ target_group_arn: 'arn:aws:elasticloadbalancing:ap-northeast-1:1234567890:targetgroup/my-targets/73e2d6bc24d8a067',
74
+ type: 'forward'
75
+ }
76
+ ],
77
+ listener_arn: 'arn:aws:elasticloadbalancing:ap-northeast-1:1234567890:listener/app/my-nlb/1aa1bb1cc1ddee11/f2f7dc8efc522ab2',
78
+ load_balancer_arn: 'arn:aws:elasticloadbalancing:ap-northeast-1:1234567890:loadbalancer/app/my-nlb/1aa1bb1cc1ddee11',
79
+ port: 80,
80
+ protocol: 'HTTP'
81
+ }
82
+ ]
83
+ }
84
+ }
85
+ }
86
+
87
+ Aws.config[:ec2] = {
88
+ stub_responses: {
89
+ describe_subnets: {
90
+ subnets: [
91
+ {
92
+ state: 'available',
93
+ vpc_id: 'vpc-ab123cde',
94
+ subnet_id: 'subnet-1234a567',
95
+ cidr_block: '10.0.1.0/24',
96
+ tags: [
97
+ {
98
+ key: 'Name',
99
+ value: 'my-subnet'
100
+ }
101
+ ]
102
+ }
103
+ ]
104
+ },
105
+ describe_vpcs: {
106
+ vpcs: [
107
+ {
108
+ vpc_id: 'vpc-ab123cde',
109
+ tags: [
110
+ {
111
+ key: 'Name',
112
+ value: 'my-vpc'
113
+ }
114
+ ]
115
+ }
116
+ ]
117
+ }
118
+ }
119
+ }
@@ -0,0 +1,131 @@
1
+ # rubocop:disable Metrics/LineLength
2
+ Aws.config[:elasticloadbalancingv2] = {
3
+ stub_responses: {
4
+ describe_load_balancers: {
5
+ load_balancers: [
6
+ {
7
+ load_balancer_arn:
8
+ 'arn:aws:elasticloadbalancing:ap-northeast-1:1234567890:loadbalancer/app/my-nlb/1aa1bb1cc1ddee11',
9
+ dns_name:
10
+ 'internal-my-nlb-1551266724.ap-northeast-1.elb.amazonaws.com',
11
+ canonical_hosted_zone_id: 'A12BCDEDCBA34BC',
12
+ created_time: Time.new(2017, 4, 4, 9, 00, 00, '+00:00'),
13
+ load_balancer_name: 'my-nlb',
14
+ # scheme: 'internal',
15
+ scheme: 'internet-facing',
16
+ vpc_id: 'vpc-ab123cde',
17
+ state:
18
+ {
19
+ code: 'active',
20
+ reason: nil
21
+ },
22
+ type: 'network',
23
+ availability_zones:
24
+ [
25
+ {
26
+ zone_name: 'ap-northeast-1a',
27
+ subnet_id: 'subnet-1234a567'
28
+ },
29
+ {
30
+ zone_name: 'ap-northeast-1c',
31
+ subnet_id: 'subnet-abcd7890'
32
+ }
33
+ ],
34
+ ip_address_type: 'ipv4'
35
+ }
36
+ ],
37
+ next_marker: nil
38
+ },
39
+ describe_listeners: {
40
+ listeners: [
41
+ {
42
+ default_actions: [
43
+ {
44
+ target_group_arn: 'arn:aws:elasticloadbalancing:ap-northeast-1:1234567890:targetgroup/my-targets/73e2d6bc24d8a067',
45
+ type: 'forward'
46
+ }
47
+ ],
48
+ listener_arn: 'arn:aws:elasticloadbalancing:ap-northeast-1:1234567890:listener/app/my-nlb/1aa1bb1cc1ddee11/f2f7dc8efc522ab2',
49
+ load_balancer_arn: 'arn:aws:elasticloadbalancing:ap-northeast-1:1234567890:loadbalancer/app/my-nlb/1aa1bb1cc1ddee11',
50
+ port: 80,
51
+ protocol: 'HTTP'
52
+ }
53
+ ]
54
+ },
55
+ describe_target_groups: {
56
+ target_groups: [
57
+ {
58
+ health_check_interval_seconds: 30,
59
+ health_check_path: '/',
60
+ health_check_port: 'traffic-port',
61
+ health_check_protocol: 'HTTP',
62
+ health_check_timeout_seconds: 5,
63
+ healthy_threshold_count: 5,
64
+ load_balancer_arns: [
65
+ 'arn:aws:elasticloadbalancing:ap-northeast-1:1234567890:loadbalancer/app/my-nlb/1aa1bb1cc1ddee11'
66
+ ],
67
+ matcher: {
68
+ http_code: '200'
69
+ },
70
+ port: 80,
71
+ protocol: 'HTTP',
72
+ target_group_arn: 'arn:aws:elasticloadbalancing:ap-northeast-1:1234567890:targetgroup/my-nlb-target-group/73e2d6bc24d8a067',
73
+ target_group_name: 'my-nlb-target-group',
74
+ unhealthy_threshold_count: 2,
75
+ vpc_id: 'vpc-ab123cde'
76
+ }
77
+ ]
78
+ },
79
+ describe_rules: {
80
+ rules: [
81
+ {
82
+ actions: [
83
+ {
84
+ target_group_arn: 'arn:aws:elasticloadbalancing:ap-northeast-1:1234567890:targetgroup/my-nlb-target-group/73e2d6bc24d8a067',
85
+ type: 'forward'
86
+ }
87
+ ],
88
+ conditions: [
89
+ ],
90
+ is_default: true,
91
+ priority: 'default',
92
+ rule_arn: 'arn:aws:elasticloadbalancing:ap-northeast-1:1234567890:listener-rule/app/my-nlb/1aa1bb1cc1ddee11/f2f7dc8efc522ab2/defaaaaaaaultbbbb'
93
+ }
94
+ ]
95
+ }
96
+ }
97
+ }
98
+
99
+ Aws.config[:ec2] = {
100
+ stub_responses: {
101
+ describe_subnets: {
102
+ subnets: [
103
+ {
104
+ state: 'available',
105
+ vpc_id: 'vpc-ab123cde',
106
+ subnet_id: 'subnet-1234a567',
107
+ cidr_block: '10.0.1.0/24',
108
+ tags: [
109
+ {
110
+ key: 'Name',
111
+ value: 'my-subnet'
112
+ }
113
+ ]
114
+ }
115
+ ]
116
+ },
117
+ describe_vpcs: {
118
+ vpcs: [
119
+ {
120
+ vpc_id: 'vpc-ab123cde',
121
+ tags: [
122
+ {
123
+ key: 'Name',
124
+ value: 'my-vpc'
125
+ }
126
+ ]
127
+ }
128
+ ]
129
+ }
130
+ }
131
+ }