Skip to content
This repository has been archived by the owner on Sep 14, 2022. It is now read-only.

Give greater flexibility on images and subnets filtering #1

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 23 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,26 @@ platforms:
- name: ubuntu-14.04
driver:
image_search:
owner-id: "099720109477"
name: ubuntu/images/*/ubuntu-*-14.04*
filters:
owner-id: "099720109477"
name: ubuntu/images/*/ubuntu-*-14.04*
```

More specific filters can also be used:

```yaml
platforms:
- name: ubuntu-14.04
driver:
image_search:

filters: # describe-images "filters" API
name: "debian-10-amd64-*"
tag:Name: "foobar"

owners: # describe-images "owners" API
- self
- "136693071363"
```

In the event that there are multiple matches (as sometimes happens), we sort to
Expand Down Expand Up @@ -266,8 +284,9 @@ The default is unset, or `nil`.
An example of usage:
```yaml
subnet_filter:
tag: 'Name'
value: 'example-subnet-name'
filters:
tag:Name: 'example-subnet-name'
mapPublicIpOnLaunch: 'true'
```

#### `tags`
Expand Down
10 changes: 5 additions & 5 deletions lib/kitchen/driver/aws/instance_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ def ec2_instance_data # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
if config[:subnet_id].nil? && config[:subnet_filter]
config[:subnet_id] = ::Aws::EC2::Client
.new(region: config[:region]).describe_subnets(
filters: [
filters: config[:subnet_filter].fetch(:filters, {}).map do |key, value|
{
name: "tag:#{config[:subnet_filter][:tag]}",
values: [config[:subnet_filter][:value]],
},
]
name: key,
values: [value],
}
end
)[0][0].subnet_id

if config[:subnet_id].nil?
Expand Down
7 changes: 4 additions & 3 deletions lib/kitchen/driver/aws/standard_platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,13 @@ def initialize(driver, name, version, architecture)
def find_image(image_search)
driver.debug("Searching for images matching #{image_search} ...")
# Convert to ec2 search format (pairs of name+values)
filters = image_search.map do |key, value|
{ name: key.to_s, values: Array(value).map(&:to_s) }
filters = image_search
filters[:filters] = image_search.fetch(:filters, {}).map do |key, value|
{name: key, values: [value]}
end

# We prefer most recent first
images = driver.ec2.resource.images(filters: filters)
images = driver.ec2.resource.images(filters)
images = sort_images(images)
show_returned_images(images)

Expand Down
5 changes: 4 additions & 1 deletion lib/kitchen/driver/ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,10 @@ def create_security_group(state)

subnets.first.vpc_id
elsif config[:subnet_filter]
subnets = ec2.client.describe_subnets(filters: [{ name: "tag:#{config[:subnet_filter][:tag]}", values: [config[:subnet_filter][:value]] }]).subnets
filters = config[:subnet_filter].fetch(:filters, {}).map do |key, value|
{name: key, values: [value]}
end
subnets = ec2.client.describe_subnets(filters: filters).subnets
raise "Subnets with tag '#{config[:subnet_filter][:tag]}=#{config[:subnet_filter][:value]}' not found during security group creation" if subnets.empty?
multani marked this conversation as resolved.
Show resolved Hide resolved
subnets.first.vpc_id
else
Expand Down