Module: Awspec::Helper::Finder::Ec2
- Included in:
- Awspec::Helper::Finder
- Defined in:
- lib/awspec/helper/finder/ec2.rb
Instance Method Summary collapse
- #dup_ec2_instance(id) ⇒ Object
- #find_ec2(id) ⇒ Object
- #find_ec2_attribute(id, attribute) ⇒ Object
- #find_ec2_credit_specifications(id) ⇒ Object
- #find_ec2_status(id) ⇒ Object
- #find_launch_template(id) ⇒ Object
- #find_launch_template_versions(id) ⇒ Object
- #find_nat_gateway(gateway_id) ⇒ Object
- #find_network_interface(interface_id) ⇒ Object
- #find_tgw_attachments_by_tgw_id(tgw_id) ⇒ Object
- #find_vpn_connection(vpn_connection_id) ⇒ Object
- #select_ec2_by_vpc_id(vpc_id) ⇒ Object
- #select_internet_gateway_by_vpc_id(vpc_id) ⇒ Object
- #select_nat_gateway_by_vpc_id(vpc_id) ⇒ Object
- #select_network_interface_by_instance_id(id) ⇒ Object
- #select_network_interface_by_vpc_id(vpc_id) ⇒ Object
Instance Method Details
#dup_ec2_instance(id) ⇒ Object
49 50 51 |
# File 'lib/awspec/helper/finder/ec2.rb', line 49 def dup_ec2_instance(id) "Duplicate instances matching id or tag #{id}" end |
#find_ec2(id) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/awspec/helper/finder/ec2.rb', line 6 def find_ec2(id) # instance_id or tag:Name # First tries to search by using an educated guess, based on the # references below: # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/resource-ids.html # https://docs.chef.io/inspec/resources/aws_ec2_instance/ # This should be faster then just first trying ID when the parameter is # clearly not one # https://medium.com/@Bakku1505/ruby-start-with-end-with-vs-regular-expressions-59728be0859e if id.start_with?('i-') && id.length == 19 && id =~ /^i-[0-9a-f]/ begin res = ec2_client.describe_instances({ instance_ids: [id] }) rescue Aws::EC2::Errors::InvalidInstanceIDNotFound, Aws::EC2::Errors::InvalidInstanceIDMalformed => e res = ec2_client.describe_instances({ filters: [{ name: 'tag:Name', values: [id] }] }) end else begin res = ec2_client.describe_instances({ filters: [{ name: 'tag:Name', values: [id] }] }) rescue Aws::EC2::Errors::InvalidInstanceIDNotFound, Aws::EC2::Errors::InvalidInstanceIDMalformed => e res = ec2_client.describe_instances({ instance_ids: [id] }) if res.reservations.count > 1 warn "Warning: '#{id}' unexpectedly identified as a valid instance ID during fallback search" end end end return nil if res.reservations.count == 0 return res.reservations.first.instances.single_resource(id) if res.reservations.count == 1 raise Awspec::DuplicatedResourceTypeError, dup_ec2_instance(id) if res.reservations.count > 1 raise "Unexpected condition of having reservations = #{res.reservations.count}" end |
#find_ec2_attribute(id, attribute) ⇒ Object
53 54 55 56 57 |
# File 'lib/awspec/helper/finder/ec2.rb', line 53 def find_ec2_attribute(id, attribute) res = ec2_client.describe_instance_attribute({ instance_id: id, attribute: attribute }) end |
#find_ec2_credit_specifications(id) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/awspec/helper/finder/ec2.rb', line 59 def find_ec2_credit_specifications(id) res = ec2_client.describe_instance_credit_specifications({ instance_ids: [id] }) res.instance_credit_specifications.first if res.instance_credit_specifications.count == 1 end |
#find_ec2_status(id) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/awspec/helper/finder/ec2.rb', line 66 def find_ec2_status(id) res = ec2_client.describe_instance_status({ instance_ids: [id] }) res.instance_statuses.first if res.instance_statuses.count == 1 end |
#find_launch_template(id) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/awspec/helper/finder/ec2.rb', line 198 def find_launch_template(id) # launch_template_id or launch_template_name begin res = ec2_client.describe_launch_templates({ launch_template_ids: [id] }) rescue StandardError res = ec2_client.describe_launch_templates({ launch_template_names: [id] }) end res.launch_templates.single_resource(id) end |
#find_launch_template_versions(id) ⇒ Object
212 213 214 215 216 217 218 219 220 221 |
# File 'lib/awspec/helper/finder/ec2.rb', line 212 def find_launch_template_versions(id) # launch_template_id or launch_template_name res = ec2_client.describe_launch_template_versions({ launch_template_id: id }) rescue StandardError res = ec2_client.describe_launch_template_versions({ launch_template_name: id }) end |
#find_nat_gateway(gateway_id) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/awspec/helper/finder/ec2.rb', line 121 def find_nat_gateway(gateway_id) # nat_gateway_id or tag:Name begin res = ec2_client.describe_nat_gateways({ nat_gateway_ids: [gateway_id] }) rescue StandardError res = ec2_client.describe_nat_gateways({ filter: [{ name: 'tag:Name', values: [gateway_id] }] }) end if res.nat_gateways.count == 1 res.nat_gateways.single_resource(gateway_id) elsif res.nat_gateways.count > 1 raise Awspec::DuplicatedResourceTypeError, "Duplicate nat_gateways matching id or tag #{gateway_id}" end end |
#find_network_interface(interface_id) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/awspec/helper/finder/ec2.rb', line 139 def find_network_interface(interface_id) res = ec2_client.describe_network_interfaces({ filters: [ { name: 'network-interface-id', values: [interface_id] } ] }) resource = res.network_interfaces.single_resource(interface_id) return resource if resource res = ec2_client.describe_network_interfaces({ filters: [{ name: 'tag:Name', values: [interface_id] }] }) res.network_interfaces.single_resource(interface_id) end |
#find_tgw_attachments_by_tgw_id(tgw_id) ⇒ Object
223 224 225 226 227 228 229 230 |
# File 'lib/awspec/helper/finder/ec2.rb', line 223 def (tgw_id) res = ec2_client.({ filters: [ { name: 'transit-gateway-id', values: [tgw_id] } ] }) res. end |
#find_vpn_connection(vpn_connection_id) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/awspec/helper/finder/ec2.rb', line 98 def find_vpn_connection(vpn_connection_id) res = ec2_client.describe_vpn_connections({ filters: [ { name: 'vpn-connection-id', values: [vpn_connection_id] } ] }) resource = res.vpn_connections.single_resource(vpn_connection_id) return resource if resource res = ec2_client.describe_vpn_connections({ filters: [ { name: 'tag:Name', values: [vpn_connection_id] } ] }) res.vpn_connections.single_resource(vpn_connection_id) end |
#select_ec2_by_vpc_id(vpc_id) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/awspec/helper/finder/ec2.rb', line 157 def select_ec2_by_vpc_id(vpc_id) res = ec2_client.describe_instances({ filters: [{ name: 'vpc-id', values: [vpc_id] }] }) instances = [] res.reservations.each do |reservation| reservation.instances.each do |instance| instances.push(instance) end end instances end |
#select_internet_gateway_by_vpc_id(vpc_id) ⇒ Object
184 185 186 187 188 189 |
# File 'lib/awspec/helper/finder/ec2.rb', line 184 def select_internet_gateway_by_vpc_id(vpc_id) res = ec2_client.describe_internet_gateways({ filters: [{ name: 'attachment.vpc-id', values: [vpc_id] }] }) res.internet_gateways end |
#select_nat_gateway_by_vpc_id(vpc_id) ⇒ Object
177 178 179 180 181 182 |
# File 'lib/awspec/helper/finder/ec2.rb', line 177 def select_nat_gateway_by_vpc_id(vpc_id) res = ec2_client.describe_nat_gateways({ filter: [{ name: 'vpc-id', values: [vpc_id] }] }) res.nat_gateways end |
#select_network_interface_by_instance_id(id) ⇒ Object
170 171 172 173 174 175 |
# File 'lib/awspec/helper/finder/ec2.rb', line 170 def select_network_interface_by_instance_id(id) res = ec2_client.describe_network_interfaces({ filters: [{ name: 'attachment.instance-id', values: [id] }] }) res.network_interfaces end |
#select_network_interface_by_vpc_id(vpc_id) ⇒ Object
191 192 193 194 195 196 |
# File 'lib/awspec/helper/finder/ec2.rb', line 191 def select_network_interface_by_vpc_id(vpc_id) res = ec2_client.describe_network_interfaces({ filters: [{ name: 'vpc-id', values: [vpc_id] }] }) res.network_interfaces end |