Skip to content

Commit

Permalink
Make transit_gateway have_attachment work with names
Browse files Browse the repository at this point in the history
With this change, it is now possible to check transit gateways have
attachments based on the attachment name instead of just id. The name
can either be a string or a regex.

`have_attachment` now also works if `should exist` hasn't been called
first (where `@id` wasn't initialised).
  • Loading branch information
alexjfisher committed Apr 5, 2024
1 parent ea64e0d commit 2b5f1b7
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
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)

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

0 comments on commit 2b5f1b7

Please sign in to comment.