Module: Awspec::Helper::Finder::Ec2

Included in:
Awspec::Helper::Finder
Defined in:
lib/awspec/helper/finder/ec2.rb

Instance Method Summary collapse

Instance Method Details

#find_ec2(id) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/awspec/helper/finder/ec2.rb', line 4

def find_ec2(id)
  # instance_id or tag:Name
  begin
    res = ec2_client.describe_instances({
                                          instance_ids: [id]
                                        })
  rescue
    # Aws::EC2::Errors::InvalidInstanceIDMalformed
    # Aws::EC2::Errors::InvalidInstanceIDNotFound
    res = ec2_client.describe_instances({
                                          filters: [{ name: 'tag:Name', values: [id] }]
                                        })
  end
  # rubocop:enable Style/GuardClause
  if res.reservations.count == 1
    res.reservations.first.instances.single_resource(id)
  elsif res.reservations.count > 1
    raise Awspec::DuplicatedResourceTypeError, "Duplicate instances matching id or tag #{id}"
  end
end

#find_ec2_attribute(id, attribute) ⇒ Object



25
26
27
28
29
# File 'lib/awspec/helper/finder/ec2.rb', line 25

def find_ec2_attribute(id, attribute)
  res = ec2_client.describe_instance_attribute({
                                                 instance_id: id, attribute: attribute
                                               })
end

#find_ec2_credit_specifications(id) ⇒ Object



31
32
33
34
35
36
# File 'lib/awspec/helper/finder/ec2.rb', line 31

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



38
39
40
41
42
43
# File 'lib/awspec/helper/finder/ec2.rb', line 38

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



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/awspec/helper/finder/ec2.rb', line 177

def find_launch_template(id)
  # launch_template_id or launch_template_name
  begin
    res = ec2_client.describe_launch_templates({
                                                 launch_template_ids: [id]
                                               })
  rescue
    res = ec2_client.describe_launch_templates({
                                                 launch_template_names: [id]
                                               })
  end
  res.launch_templates.single_resource(id)
end

#find_launch_template_versions(id) ⇒ Object



191
192
193
194
195
196
197
198
199
200
# File 'lib/awspec/helper/finder/ec2.rb', line 191

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
  res = ec2_client.describe_launch_template_versions({
                                                       launch_template_name: id
                                                     })
end

#find_nat_gateway(gateway_id) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/awspec/helper/finder/ec2.rb', line 87

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
    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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/awspec/helper/finder/ec2.rb', line 105

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_vpn_connection(vpn_connection_id) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/awspec/helper/finder/ec2.rb', line 65

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



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/awspec/helper/finder/ec2.rb', line 122

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_eip_by_instance_id(id) ⇒ Object



135
136
137
138
139
140
# File 'lib/awspec/helper/finder/ec2.rb', line 135

def select_eip_by_instance_id(id)
  res = ec2_client.describe_addresses({
                                        filters: [{ name: 'instance-id', values: [id] }]
                                      })
  res.addresses
end

#select_eip_by_public_ip(id) ⇒ Object



142
143
144
145
146
147
# File 'lib/awspec/helper/finder/ec2.rb', line 142

def select_eip_by_public_ip(id)
  res = ec2_client.describe_addresses({
                                        filters: [{ name: 'public-ip', values: [id] }]
                                      })
  res.addresses
end

#select_internet_gateway_by_vpc_id(vpc_id) ⇒ Object



163
164
165
166
167
168
# File 'lib/awspec/helper/finder/ec2.rb', line 163

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



156
157
158
159
160
161
# File 'lib/awspec/helper/finder/ec2.rb', line 156

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



149
150
151
152
153
154
# File 'lib/awspec/helper/finder/ec2.rb', line 149

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



170
171
172
173
174
175
# File 'lib/awspec/helper/finder/ec2.rb', line 170

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