Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make transit_gateway have_attachment work with names #586

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions doc/_resource_types/transit_gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,25 @@ describe transit_gateway('my-tgw') do
its(:transit_gateway_id) { should eq 'tgw-1234567890abcdefg' }
end
```
### have_attachment

#### using attachment id
```ruby
describe transit_gateway('tgw-1234567890abcdefg') do
it { should have_attachment('tgw-attach-1234567890abcdefg') }
end
```

#### using attachment name
```ruby
describe transit_gateway('tgw-1234567890abcdefg') do
it { should have_attachment('my-prod-tgw-attachment') }
end
```

#### using regular expression attachment name
```ruby
describe transit_gateway('tgw-1234567890abcdefg') do
it { should have_attachment(/^my-\w+-tgw-attachment$/) }
end
```
21 changes: 21 additions & 0 deletions doc/resource_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -3884,6 +3884,27 @@ end

### have_attachment

#### using attachment id
```ruby
describe transit_gateway('tgw-1234567890abcdefg') do
it { should have_attachment('tgw-attach-1234567890abcdefg') }
end
```

#### using attachment name
```ruby
describe transit_gateway('tgw-1234567890abcdefg') do
it { should have_attachment('my-prod-tgw-attachment') }
end
```

#### using regular expression attachment name
```ruby
describe transit_gateway('tgw-1234567890abcdefg') do
it { should have_attachment(/^my-\w+-tgw-attachment$/) }
end
```

### have_tag

### its(:transit_gateway_id), its(:transit_gateway_arn), its(:state), its(:owner_id), its(:description), its(:creation_time)
Expand Down
18 changes: 15 additions & 3 deletions lib/awspec/type/transit_gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,21 @@ def options
end

def has_attachment?(att_id)
atts = find_tgw_attachments_by_tgw_id(@id)
ret = atts.find_all { |att| att.transit_gateway_attachment_id == att_id }
ret.any?
atts = find_tgw_attachments_by_tgw_id(id)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specific bugfix on this line changing @id to id as @id wasn't initialised if should exist hadn't been called first.


atts.any? do |att|
att.transit_gateway_attachment_id == att_id || attachment_has_name?(att, att_id)
end
end

private

def attachment_has_name?(attachment, name)
if name.is_a?(Regexp)
attachment.tags.any? { |tag| tag.key == 'Name' && (name =~ tag.value) }
else
attachment.tags.any? { |tag| tag.key == 'Name' && tag.value == name }
end
end
end
end
20 changes: 20 additions & 0 deletions spec/type/transit_gateway_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,23 @@
it { should exist }
its(:transit_gateway_id) { should eq 'tgw-1234567890abcdefg' }
end

describe transit_gateway('my-transit-gateway') do
it { should exist }
it 'supports `have_attachment` by attachment name' do
should have_attachment('my-transit-gateway-attachment')
end
end

describe transit_gateway('my-transit-gateway') do
it { should exist }
it 'supports `have_attachment` by attachment regular expression' do
should have_attachment(/my-transit-gateway-attachment/)
end
end

describe transit_gateway('my-transit-gateway') do
it 'supports have_attachment when `should exist` hasn\'t been called first' do
should have_attachment('tgw-attach-1234567890abcdefg')
end
end