lib/kitchen/driver/ec2.rb in kitchen-ec2-3.11.1 vs lib/kitchen/driver/ec2.rb in kitchen-ec2-3.12.0
- old
+ new
@@ -263,13 +263,12 @@
instance.transport.connection(state).wait_until_ready
attach_network_interface(state) unless config[:elastic_network_interface_id].nil?
create_ec2_json(state) if /chef/i.match?(instance.provisioner.name)
debug("ec2:create '#{state[:hostname]}'")
rescue Exception => e
- # Clean up any auto-created security groups or keys on the way out.
- delete_security_group(state)
- delete_key(state)
+ # Clean up the instance and any auto-created security groups or keys on the way out.
+ destroy(state)
raise "#{e.message} in the specified region #{config[:region]}. Please check this AMI is available in this region."
end
def destroy(state)
if state[:server_id]
@@ -431,20 +430,26 @@
# => Use explicitly specified subnets
keys << :subnet_id
else
# => Enable cascading through matching subnets
client = ::Aws::EC2::Client.new(region: config[:region])
- subnets = client.describe_subnets(
- filters: [
+
+ filters = [config[:subnet_filter]].flatten
+
+ r = { filters: [] }
+ filters.each do |subnet_filter|
+ r[:filters] <<
{
- name: "tag:#{config[:subnet_filter][:tag]}",
- values: [config[:subnet_filter][:value]],
- },
- ]
- ).subnets
- raise "A subnet matching '#{config[:subnet_filter][:tag]}:#{config[:subnet_filter][:value]}' does not exist!" unless subnets.any?
+ name: "tag:#{subnet_filter[:tag]}",
+ values: [subnet_filter[:value]],
+ }
+ end
+ subnets = client.describe_subnets(r).subnets
+
+ raise "Subnets with tags '#{filters}' not found!" if subnets.empty?
+
configs = subnets.map do |subnet|
new_config = config.clone
new_config[:subnet_id] = subnet.subnet_id
new_config[:subnet_filter] = nil
new_config
@@ -749,11 +754,22 @@
subnets = ec2.client.describe_subnets(filters: [{ name: "subnet-id", values: [config[:subnet_id]] }]).subnets
raise "Subnet #{config[:subnet_id]} not found during security group creation" if subnets.empty?
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
- raise "Subnets with tag '#{config[:subnet_filter][:tag]}=#{config[:subnet_filter][:value]}' not found during security group creation" if subnets.empty?
+ filters = [config[:subnet_filter]].flatten
+
+ r = { filters: [] }
+ filters.each do |subnet_filter|
+ r[:filters] << {
+ name: "tag:#{subnet_filter[:tag]}",
+ values: [subnet_filter[:value]],
+ }
+ end
+
+ subnets = ec2.client.describe_subnets(r).subnets
+
+ raise "Subnets with tags '#{filters}' not found during security group creation" if subnets.empty?
subnets.first.vpc_id
else
# Try to check for a default VPC.
vpcs = ec2.client.describe_vpcs(filters: [{ name: "isDefault", values: ["true"] }]).vpcs