-
-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #587 from alpineriveredge/add-managed-prefix-list
Add managed_prefix_list resource
- Loading branch information
Showing
14 changed files
with
292 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
### exist | ||
|
||
```ruby | ||
describe managed_prefix_list('my-managed-prefix-list') do | ||
it { should exist } | ||
end | ||
``` | ||
|
||
### have_cidr | ||
|
||
```ruby | ||
describe managed_prefix_list('my-managed-prefix-list') do | ||
it { should have_cidr('10.0.0.0/16') } | ||
it { should have_cidr('192.168.0.0/24').desc('dev') } | ||
end | ||
``` | ||
|
||
### its(:entries_count) | ||
|
||
```ruby | ||
describe managed_prefix_list('my-managed-prefix-list') do | ||
its(:entries_count) { should eq 2 } | ||
end | ||
``` | ||
|
||
### have_tag | ||
|
||
```ruby | ||
describe managed_prefix_list('my-managed-prefix-list') do | ||
it { should have_tag('env').value('dev') } | ||
end | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module Awspec::Generator | ||
module Doc | ||
module Type | ||
class ManagedPrefixList < Base | ||
def initialize | ||
super | ||
@type_name = 'ManagedPrefixList' | ||
@type = Awspec::Type::ManagedPrefixList.new('my-managed-prefix-list') | ||
@ret = @type.resource_via_client | ||
@matchers = ['its(:entries_count)'] | ||
@ignore_matchers = [] | ||
@describes = [] | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
module Awspec::Generator | ||
module Spec | ||
class ManagedPrefixList | ||
include Awspec::Helper::Finder | ||
def select_all_managed_prefix_lists | ||
res = ec2_client.describe_managed_prefix_lists | ||
res.prefix_lists | ||
end | ||
|
||
def generate_all | ||
prefix_lists = select_all_managed_prefix_lists | ||
raise 'Not Found Managed Prefix List.' if prefix_lists.empty? | ||
|
||
specs = prefix_lists.map do |prefix_list| | ||
entries = select_managed_prefix_list_entries(prefix_list.prefix_list_id) | ||
content = ERB.new(managed_prefix_list_spec_template, nil, '-').result(binding).gsub(/^\n/, '') | ||
end | ||
specs.join("\n") | ||
end | ||
|
||
def managed_prefix_list_spec_template | ||
<<-'EOF' | ||
describe managed_prefix_list('<%= prefix_list.prefix_list_name %>') do | ||
it { should exist } | ||
<% entries.each do |entry| %> | ||
<% if entry.description.nil? %> | ||
it { should have_cidr('<%= entry.cidr %>') } | ||
<% else %> | ||
it { should have_cidr('<%= entry.cidr %>').desc('<%= entry.description %>') } | ||
<% end %> | ||
<% end %> | ||
its(:entries_count) { should eq <%= entries.length %> } | ||
its(:prefix_list_id) { should eq '<%= prefix_list.prefix_list_id %>' } | ||
its(:address_family) { should eq '<%= prefix_list.address_family %>' } | ||
its(:state) { should eq '<%= prefix_list.state %>' } | ||
its(:prefix_list_arn) { should eq '<%= prefix_list.prefix_list_arn %>' } | ||
<% if prefix_list.max_entries %> | ||
its(:max_entries) { should eq <%= prefix_list.max_entries %> } | ||
<% end %> | ||
<% if prefix_list.version %> | ||
its(:version) { should eq <%= prefix_list.version %> } | ||
<% end %> | ||
its(:owner_id) { should eq '<%= prefix_list.owner_id %>' } | ||
<% prefix_list.tags.each do |tag| %> | ||
it { should have_tag('<%= tag.key %>').value('<%= tag.value %>') } | ||
<% end %> | ||
end | ||
EOF | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec::Matchers.define :have_cidr do |cidr| | ||
match do |type| | ||
type.has_cidr?(cidr, @desc) | ||
end | ||
|
||
chain :desc do |desc| | ||
@desc = desc | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
Aws.config[:ec2] = { | ||
stub_responses: { | ||
describe_managed_prefix_lists: { | ||
next_token: nil, | ||
prefix_lists: [ | ||
{ | ||
prefix_list_id: 'pl-12345678', | ||
address_family: 'IPv4', | ||
state: 'create-complete', | ||
state_message: nil, | ||
prefix_list_arn: 'arn:aws:ec2:ap-northeast-1:aws:prefix-list/pl-12345678', | ||
prefix_list_name: 'my-managed-prefix-list', | ||
max_entries: 2, | ||
version: 1, | ||
tags: [ | ||
{ | ||
key: 'env', | ||
value: 'dev' | ||
} | ||
], | ||
owner_id: '123456789012' | ||
} | ||
] | ||
}, | ||
get_managed_prefix_list_entries: { | ||
next_toke: nil, | ||
entries: [ | ||
{ | ||
cidr: '10.0.0.0/16', | ||
description: 'test' | ||
}, | ||
{ | ||
cidr: '192.168.0.0/24', | ||
description: 'dev' | ||
} | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
module Awspec::Type | ||
class ManagedPrefixList < ResourceBase | ||
aws_resource Aws::EC2::Types::ManagedPrefixList | ||
tags_allowed | ||
|
||
def resource_via_client | ||
@resource_via_client ||= find_managed_prefix_list(@display_name) | ||
end | ||
|
||
def id | ||
@id ||= resource_via_client.prefix_list_id if resource_via_client | ||
end | ||
|
||
def entries | ||
@entries ||= select_managed_prefix_list_entries(id) | ||
end | ||
|
||
def has_cidr?(cidr, description = nil) | ||
entries.find do |entry| | ||
if description.nil? | ||
entry.cidr == cidr | ||
else | ||
entry.cidr == cidr && entry.description == description | ||
end | ||
end | ||
end | ||
|
||
def entries_count | ||
entries.length | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe 'Awspec::Generator::Spec::ManagedPrefixList' do | ||
before do | ||
Awspec::Stub.load 'managed_prefix_list' | ||
end | ||
let(:managed_prefix_list) { Awspec::Generator::Spec::ManagedPrefixList.new } | ||
it 'generate spec' do | ||
spec = <<-'EOF' | ||
describe managed_prefix_list('my-managed-prefix-list') do | ||
it { should exist } | ||
it { should have_cidr('10.0.0.0/16').desc('test') } | ||
it { should have_cidr('192.168.0.0/24').desc('dev') } | ||
its(:entries_count) { should eq 2 } | ||
its(:prefix_list_id) { should eq 'pl-12345678' } | ||
its(:address_family) { should eq 'IPv4' } | ||
its(:state) { should eq 'create-complete' } | ||
its(:prefix_list_arn) { should eq 'arn:aws:ec2:ap-northeast-1:aws:prefix-list/pl-12345678' } | ||
its(:max_entries) { should eq 2 } | ||
its(:version) { should eq 1 } | ||
its(:owner_id) { should eq '123456789012' } | ||
it { should have_tag('env').value('dev') } | ||
end | ||
EOF | ||
expect(managed_prefix_list.generate_all.to_s.gsub(/\n/, "\n")).to eq spec | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
Awspec::Stub.load 'managed_prefix_list' | ||
|
||
describe managed_prefix_list('my-managed-prefix-list') do | ||
it { should exist } | ||
it { should have_cidr('10.0.0.0/16') } | ||
it { should have_cidr('192.168.0.0/24').desc('dev') } | ||
its(:entries_count) { should eq 2 } | ||
it { should have_tag('env').value('dev') } | ||
end |