From 4120615999e332f522b8f96ade7818b58ad85159 Mon Sep 17 00:00:00 2001 From: inokappa Date: Tue, 6 Feb 2018 08:09:55 +0900 Subject: [PATCH 1/3] fix document --- doc/_resource_types/cloudfront_distribution.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/_resource_types/cloudfront_distribution.md b/doc/_resource_types/cloudfront_distribution.md index b03d43c63..1dbf8401b 100644 --- a/doc/_resource_types/cloudfront_distribution.md +++ b/doc/_resource_types/cloudfront_distribution.md @@ -17,6 +17,7 @@ end ### have_custom_response_error_code ```ruby +describe cloudfront_distribution('123456789zyxw.cloudfront.net') do it do should have_custom_response_error_code(400) .error_caching_min_ttl(60) @@ -33,6 +34,7 @@ end should have_custom_response_error_code(500) .error_caching_min_ttl(60) end +end ``` ### have_origin From 1b478f4f7f72159188dc3ae930021a8398a95926 Mon Sep 17 00:00:00 2001 From: inokappa Date: Tue, 6 Feb 2018 08:11:40 +0900 Subject: [PATCH 2/3] add genetater --- lib/awspec/generator/spec/elb.rb | 3 +++ spec/generator/spec/elb_spec.rb | 2 ++ 2 files changed, 5 insertions(+) diff --git a/lib/awspec/generator/spec/elb.rb b/lib/awspec/generator/spec/elb.rb index 6d9af1520..829efc6af 100644 --- a/lib/awspec/generator/spec/elb.rb +++ b/lib/awspec/generator/spec/elb.rb @@ -47,6 +47,9 @@ def elb_spec_template <% lb[:listener_descriptions].each do |desc| %> it { should have_listener(protocol: '<%= desc.listener.protocol %>', port: <%= desc.listener.load_balancer_port %>, instance_protocol: '<%= desc.listener.instance_protocol %>', instance_port: <%= desc.listener.instance_port %>) } <% end %> +<% select_all_elb_tags(lb.load_balancer_name).each do |tag| %> + it { should have_tag('<%= tag.key %>').value('<%= tag.value %>') } +<% end %> end EOF template diff --git a/spec/generator/spec/elb_spec.rb b/spec/generator/spec/elb_spec.rb index 5b23cf404..8a5b0f639 100644 --- a/spec/generator/spec/elb_spec.rb +++ b/spec/generator/spec/elb_spec.rb @@ -16,6 +16,8 @@ its(:health_check_unhealthy_threshold) { should eq 10 } its(:health_check_healthy_threshold) { should eq 2 } it { should have_listener(protocol: 'HTTPS', port: 443, instance_protocol: 'HTTP', instance_port: 80) } + it { should have_tag('Name').value('my-elb') } + it { should have_tag('my-tag-key').value('my-tag-value') } end EOF expect(elb.generate_by_vpc_id('my-vpc').to_s.gsub(/\n/, "\n")).to eq spec From eec65aa7a72a068e089a38e13033eb61e9e25e6c Mon Sep 17 00:00:00 2001 From: inokappa Date: Tue, 6 Feb 2018 08:13:57 +0900 Subject: [PATCH 3/3] add have_tag? to elb resouce type --- doc/_resource_types/elb.md | 9 +++++++++ doc/resource_types.md | 12 ++++++++++++ lib/awspec/helper/finder/elb.rb | 7 +++++++ lib/awspec/stub/elb.rb | 17 +++++++++++++++++ lib/awspec/type/elb.rb | 7 +++++++ spec/type/elb_spec.rb | 2 ++ 6 files changed, 54 insertions(+) diff --git a/doc/_resource_types/elb.md b/doc/_resource_types/elb.md index 05d5b160a..2831ac413 100644 --- a/doc/_resource_types/elb.md +++ b/doc/_resource_types/elb.md @@ -47,3 +47,12 @@ describe elb('my-elb') do it { should belong_to_vpc('my-vpc') } end ``` + +### have_tag + +```ruby +describe elb('my-elb') do + it { should have_tag('Name').value('my-elb') } + it { should have_tag('my-tag-key').value('my-tag-value') } +end +``` diff --git a/doc/resource_types.md b/doc/resource_types.md index 8e20b8cda..914174cba 100644 --- a/doc/resource_types.md +++ b/doc/resource_types.md @@ -334,6 +334,7 @@ end ### have_custom_response_error_code ```ruby +describe cloudfront_distribution('123456789zyxw.cloudfront.net') do it do should have_custom_response_error_code(400) .error_caching_min_ttl(60) @@ -350,6 +351,7 @@ end should have_custom_response_error_code(500) .error_caching_min_ttl(60) end +end ``` @@ -1274,6 +1276,15 @@ end ``` +### have_tag + +```ruby +describe elb('my-elb') do + it { should have_tag('Name').value('my-elb') } + it { should have_tag('my-tag-key').value('my-tag-value') } +end +``` + ### belong_to_vpc ```ruby @@ -1282,6 +1293,7 @@ describe elb('my-elb') do end ``` + ### its(:health_check_target), its(:health_check_interval), its(:health_check_timeout), its(:health_check_unhealthy_threshold), its(:health_check_healthy_threshold), its(:load_balancer_name), its(:dns_name), its(:canonical_hosted_zone_name), its(:canonical_hosted_zone_name_id), its(:backend_server_descriptions), its(:availability_zones), its(:subnets), its(:vpc_id), its(:security_groups), its(:created_time), its(:scheme) ## iam_group diff --git a/lib/awspec/helper/finder/elb.rb b/lib/awspec/helper/finder/elb.rb index aeb7a72a0..4c6d7ac5a 100644 --- a/lib/awspec/helper/finder/elb.rb +++ b/lib/awspec/helper/finder/elb.rb @@ -16,6 +16,13 @@ def select_elb_by_vpc_id(vpc_id) lb.vpc_id == vpc_id end end + + def select_all_elb_tags(id) + res = elb_client.describe_tags({ + load_balancer_names: [id] + }) + res.tag_descriptions.single_resource(id).tags + end end end end diff --git a/lib/awspec/stub/elb.rb b/lib/awspec/stub/elb.rb index 79345eccd..69907d5b0 100644 --- a/lib/awspec/stub/elb.rb +++ b/lib/awspec/stub/elb.rb @@ -37,6 +37,23 @@ vpc_id: 'vpc-ab123cde' } ] + }, + describe_tags: { + tag_descriptions: [ + { + load_balancer_name: 'my-elb', + tags: [ + { + key: 'Name', + value: 'my-elb' + }, + { + key: 'my-tag-key', + value: 'my-tag-value' + } + ] + } + ] } } } diff --git a/lib/awspec/type/elb.rb b/lib/awspec/type/elb.rb index a26f8b923..9c9b8e77e 100644 --- a/lib/awspec/type/elb.rb +++ b/lib/awspec/type/elb.rb @@ -57,5 +57,12 @@ def has_listener?(protocol:, port:, instance_protocol:, instance_port:) listener.instance_protocol == instance_protocol && listener.instance_port == instance_port end end + + def has_tag?(tag_key, tag_value) + tag_set = select_all_elb_tags(@id) + tag_set.find do |tag| + tag.key == tag_key && tag.value == tag_value + end + end end end diff --git a/spec/type/elb_spec.rb b/spec/type/elb_spec.rb index 335563af2..92fff911e 100644 --- a/spec/type/elb_spec.rb +++ b/spec/type/elb_spec.rb @@ -8,4 +8,6 @@ it { should have_subnet('my-subnet') } it { should have_listener(protocol: 'HTTPS', port: 443, instance_protocol: 'HTTP', instance_port: 80) } it { should belong_to_vpc('my-vpc') } + it { should have_tag('Name').value('my-elb') } + it { should have_tag('my-tag-key').value('my-tag-value') } end