From 4ab0167b912d886dc40afaf9bb0d661dbc4548fb Mon Sep 17 00:00:00 2001 From: alpineriveredge Date: Wed, 19 Oct 2022 17:04:43 +0900 Subject: [PATCH] Make vpn_gateway searchable by name --- doc/_resource_types/vpn_gateway.md | 9 ++++++++- doc/resource_types.md | 11 ++++++++++- lib/awspec/generator/doc/type/vpn_gateway.rb | 3 ++- lib/awspec/helper/finder/ec2.rb | 16 ++++++++++------ lib/awspec/stub/vpn_gateway.rb | 15 ++++++++++++++- lib/awspec/type/vpn_gateway.rb | 12 ++---------- spec/type/vpn_gateway_spec.rb | 10 ++++++++++ 7 files changed, 56 insertions(+), 20 deletions(-) diff --git a/doc/_resource_types/vpn_gateway.md b/doc/_resource_types/vpn_gateway.md index 7549e7768..ed37014cd 100644 --- a/doc/_resource_types/vpn_gateway.md +++ b/doc/_resource_types/vpn_gateway.md @@ -22,4 +22,11 @@ describe vpn_gateway('my-vpn-gateway') do end ``` -### its(:vpn_gateway_id), its(:state), its(:type), its(:availability_zone) +### belong_to_vpc + +```ruby +describe vpn_gateway('my-vpn-gateway') do + it { should belong_to_vpc('vpc-ab123cde') } + it { should belong_to_vpc('my-vpc') } +end +``` diff --git a/doc/resource_types.md b/doc/resource_types.md index 239f1eeed..f34c9c106 100644 --- a/doc/resource_types.md +++ b/doc/resource_types.md @@ -4100,7 +4100,16 @@ end ``` -### its(:availability_zone), its(:state), its(:type), its(:vpc_attachments), its(:vpn_gateway_id), its(:amazon_side_asn), its(:tags) +### belong_to_vpc + +```ruby +describe vpn_gateway('my-vpn-gateway') do + it { should belong_to_vpc('vpc-ab123cde') } + it { should belong_to_vpc('my-vpc') } +end +``` + +### its(:availability_zone), its(:state), its(:type), its(:vpn_gateway_id), its(:amazon_side_asn) ## waf_web_acl WafWebAcl resource type. diff --git a/lib/awspec/generator/doc/type/vpn_gateway.rb b/lib/awspec/generator/doc/type/vpn_gateway.rb index 55722f648..fdedc0766 100644 --- a/lib/awspec/generator/doc/type/vpn_gateway.rb +++ b/lib/awspec/generator/doc/type/vpn_gateway.rb @@ -10,7 +10,8 @@ def initialize @type = Awspec::Type::VpnGateway.new('my-vpn-gateway') @ret = @type.resource_via_client @matchers = [ - Awspec::Type::VpnGateway::STATES.map { |state| "be_#{state.tr('-', '_')}" }.join(', ') + Awspec::Type::VpnGateway::STATES.map { |state| "be_#{state.tr('-', '_')}" }.join(', '), + 'belong_to_vpc' ] @ignore_matchers = Awspec::Type::VpnGateway::STATES.map { |state| "be_#{state.tr('-', '_')}" } @describes = [] diff --git a/lib/awspec/helper/finder/ec2.rb b/lib/awspec/helper/finder/ec2.rb index 95bc6a1d0..7fdd8a3fe 100644 --- a/lib/awspec/helper/finder/ec2.rb +++ b/lib/awspec/helper/finder/ec2.rb @@ -76,12 +76,16 @@ def find_ec2_status(id) define_method "find_#{type}_gateway" do |*args| gateway_id = args.first method_name = "describe_#{type}_gateways" - res = ec2_client.send( - method_name, - { filters: [{ name: "#{type}-gateway-id", values: [gateway_id] }] } - ) - resource = res["#{type}_gateways"].single_resource(gateway_id) - return resource if resource + begin + res = ec2_client.send( + method_name, + { filters: [{ name: "#{type}-gateway-id", values: [gateway_id] }] } + ) + resource = res["#{type}_gateways"].single_resource(gateway_id) + return resource if resource + rescue StandardError + resource = nil + end res = ec2_client.send( method_name, diff --git a/lib/awspec/stub/vpn_gateway.rb b/lib/awspec/stub/vpn_gateway.rb index 9e41acc42..c3b4d7545 100644 --- a/lib/awspec/stub/vpn_gateway.rb +++ b/lib/awspec/stub/vpn_gateway.rb @@ -7,7 +7,20 @@ vpn_gateway_id: 'vgw-cg5692g4', availability_zone: 'us-east-1a', state: 'available', - type: "ipsec.1" + type: "ipsec.1", + vpc_attachments: [ + { + state: 'attached', + vpc_id: 'vpc-ab123cde' + } + ], + amazon_side_asn: 64512, + tags: [ + { + key: 'Name', + value: 'my-vpn-gateway' + } + ] } ] } diff --git a/lib/awspec/type/vpn_gateway.rb b/lib/awspec/type/vpn_gateway.rb index a854ba81a..de49c613e 100644 --- a/lib/awspec/type/vpn_gateway.rb +++ b/lib/awspec/type/vpn_gateway.rb @@ -27,16 +27,8 @@ def id end end - def availability_zone - resource_via_client.availability_zone - end - - def vpc_attachments - resource_via_client.vpc_attachments - end - - def type - resource_via_client.type + def vpc_id + resource_via_client.vpc_attachments.first.vpc_id end end end diff --git a/spec/type/vpn_gateway_spec.rb b/spec/type/vpn_gateway_spec.rb index 4161eb85e..59c61a454 100644 --- a/spec/type/vpn_gateway_spec.rb +++ b/spec/type/vpn_gateway_spec.rb @@ -9,3 +9,13 @@ its(:availability_zone) { should eq 'us-east-1a' } its(:type) { should eq 'ipsec.1' } end + +describe vpn_gateway('my-vpn-gateway') do + it { should exist } + it { should be_available } + its(:availability_zone) { should eq 'us-east-1a' } + its(:type) { should eq 'ipsec.1' } + it { should belong_to_vpc('vpc-ab123cde') } + its(:amazon_side_asn) { should eq 64_512 } + it { should have_tag('Name').value('my-vpn-gateway') } +end