cumulus-aws 0.11.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (173) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +3 -0
  3. data/.travis.yml +12 -0
  4. data/Gemfile +4 -0
  5. data/Gemfile.lock +29 -0
  6. data/LICENSE +202 -0
  7. data/README.md +41 -0
  8. data/autocomplete +137 -0
  9. data/bin/cumulus +658 -0
  10. data/cumulus +2 -0
  11. data/cumulus-aws.gemspec +20 -0
  12. data/lib/autoscaling/AutoScaling.rb +40 -0
  13. data/lib/autoscaling/loader/Loader.rb +56 -0
  14. data/lib/autoscaling/manager/Manager.rb +360 -0
  15. data/lib/autoscaling/models/AlarmConfig.rb +165 -0
  16. data/lib/autoscaling/models/AlarmDiff.rb +172 -0
  17. data/lib/autoscaling/models/AutoScalingDiff.rb +178 -0
  18. data/lib/autoscaling/models/GroupConfig.rb +330 -0
  19. data/lib/autoscaling/models/PolicyConfig.rb +135 -0
  20. data/lib/autoscaling/models/PolicyDiff.rb +73 -0
  21. data/lib/autoscaling/models/ScheduledActionDiff.rb +53 -0
  22. data/lib/autoscaling/models/ScheduledConfig.rb +96 -0
  23. data/lib/aws_extensions/ec2/DhcpOptions.rb +41 -0
  24. data/lib/aws_extensions/ec2/Instance.rb +29 -0
  25. data/lib/aws_extensions/ec2/NetworkAcl.rb +25 -0
  26. data/lib/aws_extensions/ec2/NetworkInterface.rb +14 -0
  27. data/lib/aws_extensions/ec2/RouteTable.rb +26 -0
  28. data/lib/aws_extensions/ec2/SecurityGroup.rb +16 -0
  29. data/lib/aws_extensions/ec2/Subnet.rb +28 -0
  30. data/lib/aws_extensions/ec2/Volume.rb +24 -0
  31. data/lib/aws_extensions/ec2/Vpc.rb +14 -0
  32. data/lib/aws_extensions/ec2/VpcEndpoint.rb +11 -0
  33. data/lib/aws_extensions/elb/BackendServerDescription.rb +12 -0
  34. data/lib/aws_extensions/elb/PolicyDescription.rb +14 -0
  35. data/lib/aws_extensions/kinesis/StreamDescription.rb +12 -0
  36. data/lib/aws_extensions/route53/AliasTarget.rb +21 -0
  37. data/lib/aws_extensions/s3/Bucket.rb +33 -0
  38. data/lib/aws_extensions/s3/BucketAcl.rb +28 -0
  39. data/lib/aws_extensions/s3/BucketCors.rb +17 -0
  40. data/lib/aws_extensions/s3/BucketLifecycle.rb +21 -0
  41. data/lib/aws_extensions/s3/BucketLogging.rb +18 -0
  42. data/lib/aws_extensions/s3/BucketNotification.rb +23 -0
  43. data/lib/aws_extensions/s3/BucketPolicy.rb +18 -0
  44. data/lib/aws_extensions/s3/BucketTagging.rb +15 -0
  45. data/lib/aws_extensions/s3/BucketVersioning.rb +14 -0
  46. data/lib/aws_extensions/s3/BucketWebsite.rb +49 -0
  47. data/lib/aws_extensions/s3/CORSRule.rb +27 -0
  48. data/lib/aws_extensions/s3/ReplicationConfiguration.rb +22 -0
  49. data/lib/cloudfront/CloudFront.rb +83 -0
  50. data/lib/cloudfront/loader/Loader.rb +31 -0
  51. data/lib/cloudfront/manager/Manager.rb +183 -0
  52. data/lib/cloudfront/models/CacheBehaviorConfig.rb +237 -0
  53. data/lib/cloudfront/models/CacheBehaviorDiff.rb +211 -0
  54. data/lib/cloudfront/models/CustomOriginConfig.rb +51 -0
  55. data/lib/cloudfront/models/CustomOriginDiff.rb +74 -0
  56. data/lib/cloudfront/models/DistributionConfig.rb +183 -0
  57. data/lib/cloudfront/models/DistributionDiff.rb +131 -0
  58. data/lib/cloudfront/models/InvalidationConfig.rb +37 -0
  59. data/lib/cloudfront/models/OriginConfig.rb +144 -0
  60. data/lib/cloudfront/models/OriginDiff.rb +86 -0
  61. data/lib/cloudfront/models/OriginSslProtocols.rb +28 -0
  62. data/lib/cloudfront/models/OriginSslProtocolsDiff.rb +39 -0
  63. data/lib/common/BaseLoader.rb +80 -0
  64. data/lib/common/manager/Manager.rb +148 -0
  65. data/lib/common/models/Diff.rb +114 -0
  66. data/lib/common/models/ListChange.rb +21 -0
  67. data/lib/common/models/TagsDiff.rb +55 -0
  68. data/lib/common/models/UTCTimeSource.rb +17 -0
  69. data/lib/conf/Configuration.rb +365 -0
  70. data/lib/ec2/EC2.rb +503 -0
  71. data/lib/ec2/IPProtocolMapping.rb +165 -0
  72. data/lib/ec2/loaders/EbsLoader.rb +19 -0
  73. data/lib/ec2/loaders/InstanceLoader.rb +32 -0
  74. data/lib/ec2/managers/EbsManager.rb +176 -0
  75. data/lib/ec2/managers/InstanceManager.rb +509 -0
  76. data/lib/ec2/models/EbsGroupConfig.rb +133 -0
  77. data/lib/ec2/models/EbsGroupDiff.rb +48 -0
  78. data/lib/ec2/models/InstanceConfig.rb +202 -0
  79. data/lib/ec2/models/InstanceDiff.rb +95 -0
  80. data/lib/elb/ELB.rb +148 -0
  81. data/lib/elb/loader/Loader.rb +65 -0
  82. data/lib/elb/manager/Manager.rb +581 -0
  83. data/lib/elb/models/AccessLogConfig.rb +82 -0
  84. data/lib/elb/models/AccessLogDiff.rb +47 -0
  85. data/lib/elb/models/HealthCheckConfig.rb +91 -0
  86. data/lib/elb/models/HealthCheckDiff.rb +50 -0
  87. data/lib/elb/models/ListenerConfig.rb +99 -0
  88. data/lib/elb/models/ListenerDiff.rb +91 -0
  89. data/lib/elb/models/LoadBalancerConfig.rb +239 -0
  90. data/lib/elb/models/LoadBalancerDiff.rb +265 -0
  91. data/lib/iam/IAM.rb +36 -0
  92. data/lib/iam/loader/Loader.rb +117 -0
  93. data/lib/iam/manager/IamGroups.rb +98 -0
  94. data/lib/iam/manager/IamResource.rb +288 -0
  95. data/lib/iam/manager/IamRoles.rb +112 -0
  96. data/lib/iam/manager/IamUsers.rb +54 -0
  97. data/lib/iam/manager/Manager.rb +29 -0
  98. data/lib/iam/migration/AssumeRoleUnifier.rb +34 -0
  99. data/lib/iam/migration/PolicyUnifier.rb +90 -0
  100. data/lib/iam/models/GroupConfig.rb +40 -0
  101. data/lib/iam/models/IamDiff.rb +132 -0
  102. data/lib/iam/models/PolicyConfig.rb +67 -0
  103. data/lib/iam/models/ResourceWithPolicy.rb +208 -0
  104. data/lib/iam/models/RoleConfig.rb +53 -0
  105. data/lib/iam/models/StatementConfig.rb +35 -0
  106. data/lib/iam/models/UserConfig.rb +21 -0
  107. data/lib/kinesis/Kinesis.rb +94 -0
  108. data/lib/kinesis/loader/Loader.rb +19 -0
  109. data/lib/kinesis/manager/Manager.rb +206 -0
  110. data/lib/kinesis/models/StreamConfig.rb +75 -0
  111. data/lib/kinesis/models/StreamDiff.rb +58 -0
  112. data/lib/lambda/Lambda.rb +41 -0
  113. data/lib/route53/loader/Loader.rb +32 -0
  114. data/lib/route53/manager/Manager.rb +241 -0
  115. data/lib/route53/models/AliasTarget.rb +86 -0
  116. data/lib/route53/models/RecordConfig.rb +178 -0
  117. data/lib/route53/models/RecordDiff.rb +140 -0
  118. data/lib/route53/models/Vpc.rb +24 -0
  119. data/lib/route53/models/ZoneConfig.rb +156 -0
  120. data/lib/route53/models/ZoneDiff.rb +118 -0
  121. data/lib/s3/S3.rb +89 -0
  122. data/lib/s3/loader/Loader.rb +66 -0
  123. data/lib/s3/manager/Manager.rb +296 -0
  124. data/lib/s3/models/BucketConfig.rb +321 -0
  125. data/lib/s3/models/BucketDiff.rb +167 -0
  126. data/lib/s3/models/GrantConfig.rb +189 -0
  127. data/lib/s3/models/GrantDiff.rb +50 -0
  128. data/lib/s3/models/LifecycleConfig.rb +142 -0
  129. data/lib/s3/models/LifecycleDiff.rb +46 -0
  130. data/lib/s3/models/LoggingConfig.rb +81 -0
  131. data/lib/s3/models/NotificationConfig.rb +157 -0
  132. data/lib/s3/models/NotificationDiff.rb +62 -0
  133. data/lib/s3/models/ReplicationConfig.rb +133 -0
  134. data/lib/s3/models/ReplicationDiff.rb +60 -0
  135. data/lib/s3/models/WebsiteConfig.rb +107 -0
  136. data/lib/security/SecurityGroups.rb +39 -0
  137. data/lib/security/loader/Loader.rb +94 -0
  138. data/lib/security/manager/Manager.rb +246 -0
  139. data/lib/security/models/RuleConfig.rb +161 -0
  140. data/lib/security/models/RuleDiff.rb +72 -0
  141. data/lib/security/models/RuleMigration.rb +127 -0
  142. data/lib/security/models/SecurityGroupConfig.rb +172 -0
  143. data/lib/security/models/SecurityGroupDiff.rb +112 -0
  144. data/lib/sns/SNS.rb +40 -0
  145. data/lib/sqs/SQS.rb +62 -0
  146. data/lib/sqs/loader/Loader.rb +34 -0
  147. data/lib/sqs/manager/Manager.rb +128 -0
  148. data/lib/sqs/models/DeadLetterConfig.rb +70 -0
  149. data/lib/sqs/models/DeadLetterDiff.rb +35 -0
  150. data/lib/sqs/models/QueueConfig.rb +115 -0
  151. data/lib/sqs/models/QueueDiff.rb +89 -0
  152. data/lib/util/Colors.rb +111 -0
  153. data/lib/util/StatusCodes.rb +51 -0
  154. data/lib/vpc/loader/Loader.rb +73 -0
  155. data/lib/vpc/manager/Manager.rb +954 -0
  156. data/lib/vpc/models/AclEntryConfig.rb +150 -0
  157. data/lib/vpc/models/AclEntryDiff.rb +54 -0
  158. data/lib/vpc/models/DhcpConfig.rb +100 -0
  159. data/lib/vpc/models/DhcpDiff.rb +90 -0
  160. data/lib/vpc/models/EndpointConfig.rb +76 -0
  161. data/lib/vpc/models/EndpointDiff.rb +69 -0
  162. data/lib/vpc/models/NetworkAclConfig.rb +87 -0
  163. data/lib/vpc/models/NetworkAclDiff.rb +116 -0
  164. data/lib/vpc/models/RouteConfig.rb +82 -0
  165. data/lib/vpc/models/RouteDiff.rb +50 -0
  166. data/lib/vpc/models/RouteTableConfig.rb +92 -0
  167. data/lib/vpc/models/RouteTableDiff.rb +101 -0
  168. data/lib/vpc/models/SubnetConfig.rb +113 -0
  169. data/lib/vpc/models/SubnetDiff.rb +78 -0
  170. data/lib/vpc/models/VpcConfig.rb +173 -0
  171. data/lib/vpc/models/VpcDiff.rb +315 -0
  172. data/rakefile.rb +8 -0
  173. metadata +245 -0
@@ -0,0 +1,503 @@
1
+ require "conf/Configuration"
2
+ require "ec2/models/EbsGroupConfig"
3
+
4
+ require "aws-sdk"
5
+
6
+ module Cumulus
7
+ module EC2
8
+ class << self
9
+ @@client = Aws::EC2::Client.new(Configuration.instance.client)
10
+
11
+ require "aws_extensions/ec2/Subnet"
12
+ Aws::EC2::Types::Subnet.send(:include, AwsExtensions::EC2::Subnet)
13
+
14
+ require "aws_extensions/ec2/Vpc"
15
+ Aws::EC2::Types::Vpc.send(:include, AwsExtensions::EC2::Vpc)
16
+
17
+ require "aws_extensions/ec2/RouteTable"
18
+ Aws::EC2::Types::RouteTable.send(:include, AwsExtensions::EC2::RouteTable)
19
+
20
+ require "aws_extensions/ec2/NetworkAcl"
21
+ Aws::EC2::Types::NetworkAcl.send(:include, AwsExtensions::EC2::NetworkAcl)
22
+
23
+ require "aws_extensions/ec2/NetworkInterface"
24
+ Aws::EC2::Types::NetworkInterface.send(:include, AwsExtensions::EC2::NetworkInterface)
25
+
26
+ require "aws_extensions/ec2/VpcEndpoint"
27
+ Aws::EC2::Types::VpcEndpoint.send(:include, AwsExtensions::EC2::VpcEndpoint)
28
+
29
+ require "aws_extensions/ec2/Volume"
30
+ Aws::EC2::Types::Volume.send(:include, AwsExtensions::EC2::Volume)
31
+
32
+ require "aws_extensions/ec2/Instance"
33
+ Aws::EC2::Types::Instance.send(:include, AwsExtensions::EC2::Instance)
34
+
35
+ # Easily load instance attribute data individually and lazy
36
+ InstanceAttributes = Struct.new(:instance_id) do
37
+
38
+ def user_data
39
+ @user_data ||= EC2::init_instance_attribute(self.instance_id, "userData").user_data.value
40
+ end
41
+
42
+ def tags
43
+ @tags ||= EC2::init_tags(self.instance_id)
44
+ end
45
+
46
+ end
47
+
48
+ # Public
49
+ #
50
+ # Returns a Hash of subnet id to Aws::EC2::Types::Subnet
51
+ def id_subnets
52
+ @id_subnets ||= Hash[subnets.map { |subnet| [subnet.subnet_id, subnet] }]
53
+ end
54
+
55
+ # Public
56
+ #
57
+ # Returns a Hash of subnet name or id to Aws::EC2::Types::Subnet
58
+ def named_subnets
59
+ @named_subnets ||= Hash[subnets.map { |subnet| [subnet.name || subnet.subnet_id, subnet] }]
60
+ .reject { |k, v| !k or !v }
61
+ end
62
+
63
+ # Public
64
+ #
65
+ # Returns a Hash of VPC id to array of Aws::EC2::Types::Subnet for the VPC
66
+ def vpc_subnets
67
+ @vpc_subnets ||= Hash[id_vpcs.map do |vpc_id, _|
68
+ [vpc_id, subnets.select { |subnet| subnet.vpc_id == vpc_id }]
69
+ end]
70
+ end
71
+
72
+ # Public: Lazily load the subnets
73
+ def subnets
74
+ @subnets ||= init_subnets
75
+ end
76
+
77
+ # Public
78
+ #
79
+ # Returns a Hash of VPC name or id to Aws::EC2::Types::Vpc
80
+ def named_vpcs
81
+ @named_vpcs ||= Hash[vpcs.map { |vpc| [vpc.name || vpc.vpc_id, vpc] }]
82
+ .reject { |k, v| !k or !v }
83
+ end
84
+
85
+ # Public
86
+ #
87
+ # Returns a Hash of VPC id to Aws::EC2::Types::Vpc
88
+ def id_vpcs
89
+ @vpc_ids ||= Hash[vpcs.map { |vpc| [vpc.vpc_id, vpc] }]
90
+ end
91
+
92
+ # Public refreshes the list of vpcs
93
+ def refresh_vpcs!
94
+ @vpcs = init_vpcs
95
+ end
96
+
97
+ # Public: Lazily load the vpcs
98
+ def vpcs
99
+ @vpcs ||= init_vpcs
100
+ end
101
+
102
+ # Public
103
+ #
104
+ # Returns a Hash of route tables name or id to Aws::EC2::Types::RouteTable
105
+ def named_route_tables
106
+ @named_route_tables ||= Hash[route_tables.map { |rt| [rt.name || rt.route_table_id, rt] }]
107
+ .reject { |k, v| !k or !v }
108
+ end
109
+
110
+ # Public
111
+ #
112
+ # Returns a Hash of subnet id to Aws::EC2::Types::RouteTable
113
+ def subnet_route_tables
114
+ @subnet_route_tables ||= Hash[route_tables.flat_map do |rt|
115
+ rt.subnet_ids.map { |subnet_id| [subnet_id, rt] }
116
+ end]
117
+ end
118
+
119
+ # Public
120
+ #
121
+ # Returns a Hash of vpc id to array of Aws::EC2::Types::RouteTable
122
+ def vpc_route_tables
123
+ @vpc_route_tables ||= Hash[id_vpcs.map do |vpc_id, _|
124
+ [vpc_id, route_tables.select { |rt| rt.vpc_id == vpc_id }]
125
+ end]
126
+ end
127
+
128
+ # Public
129
+ #
130
+ # Returns a Hash of route table id Aws::EC2::Types::RouteTable
131
+ def id_route_tables
132
+ @id_route_tables ||= Hash[@route_tables.map { |rt| [rt.route_table_id, rt] }]
133
+ end
134
+
135
+ # Public: Lazily load route tables
136
+ def route_tables
137
+ @route_tables ||= init_route_tables
138
+ end
139
+
140
+ # Public refreshes the list of route tables
141
+ def refresh_route_tables!
142
+ @route_tables = init_route_tables
143
+ end
144
+
145
+ # Public
146
+ #
147
+ # Returns a Hash of subnet id to Aws::EC2::Types::NetworkAcl associated with the subnet
148
+ def subnet_network_acls
149
+ @subnet_network_acls =
150
+ Hash[network_acls.flat_map do |acl|
151
+ acl.subnet_ids.map { |subnet_id| [subnet_id, acl] }
152
+ end]
153
+ end
154
+
155
+ # Public
156
+ #
157
+ # Returns a Hash of vpc id to array of Aws::EC2::Types::NetworkAcl
158
+ def vpc_network_acls
159
+ @vpc_network_acls = Hash[id_vpcs.map do |vpc_id, _|
160
+ [vpc_id, network_acls.select { |acl| acl.vpc_id == vpc_id }]
161
+ end]
162
+ end
163
+
164
+ # Public
165
+ #
166
+ # Returns a Hash of network acl name or id to Aws::EC2::Types::NetworkAcl
167
+ def named_network_acls
168
+ @named_network_acls = Hash[network_acls.map do |acl|
169
+ [acl.name || acl.network_acl_id, acl]
170
+ end]
171
+ end
172
+
173
+ # Public: Lazily load the network acls
174
+ def network_acls
175
+ @network_acls ||= init_network_acls
176
+ end
177
+
178
+ # Public: Refresh the list of Network ACLs
179
+ def refresh_network_acls!
180
+ @network_acls = init_network_acls
181
+ end
182
+
183
+ # Public
184
+ #
185
+ # Returns a Hash of dhcp options id to Aws::EC2::Types::DhcpOptions
186
+ def id_dhcp_options
187
+ @id_dhcp_options ||= Hash[dhcp_options.map { |dhcp| [dhcp.dhcp_options_id, dhcp] }]
188
+ end
189
+
190
+ # Public: Lazily load the dhcp options
191
+ def dhcp_options
192
+ @dhcp_options ||= init_dhcp_options
193
+ end
194
+
195
+ # Public: Lazily load the vpc endpoints
196
+ #
197
+ # Returns a Hash of vpc id to array of Aws::EC2::Types::VpcEndpoint
198
+ def vpc_endpoints
199
+ @vpc_endpoints ||= Hash[id_vpcs.map do |vpc_id, _|
200
+ [vpc_id, endpoints.select { |e| e.vpc_id == vpc_id } ]
201
+ end]
202
+ end
203
+
204
+ # Public: Lazily load the endpoints
205
+ def endpoints
206
+ @endpoints ||= init_endpoints
207
+ end
208
+
209
+ # Public
210
+ #
211
+ # Returns a Hash of public ip to Aws::EC2::Types::Address
212
+ def public_addresses
213
+ @public_addresses ||= Hash[addresses.map { |addr| [addr.public_ip, addr] }]
214
+ end
215
+
216
+ # Public
217
+ #
218
+ # Returns a Hash of vpc id to array of Aws::EC2::Types::Address that has a network interface in the vpc
219
+ def vpc_addresses
220
+ @vpc_addresses ||= Hash[id_vpcs.map do |vpc_id, _|
221
+ interface_ids = vpc_network_interfaces[vpc_id].map { |interface| interface.network_interface_id }
222
+ [vpc_id, addresses.select { |addr| interface_ids.include? addr.network_interface_id }]
223
+ end]
224
+ end
225
+
226
+ # Public: Lazily load the addresses
227
+ def addresses
228
+ @addresses ||= init_addresses
229
+ end
230
+
231
+ # Public
232
+ #
233
+ # Returns a Hash of interface name to Aws::EC2::Types::NetworkInterface
234
+ def named_network_interfaces
235
+ @named_network_interfaces ||= Hash[network_interfaces.map { |net| [net.name || net.network_interface_id, net] }]
236
+ .reject { |k, v| !k or !v }
237
+ end
238
+
239
+ # Public
240
+ #
241
+ # Returns a Hash of interface id to Aws::EC2::Types::NetworkInterface
242
+ def id_network_interfaces
243
+ @id_network_interfaces ||= Hash[network_interfaces.map { |net| [net.network_interface_id, net] }]
244
+ end
245
+
246
+ # Public
247
+ #
248
+ # Returns a Hash of vpc id to array of Aws::EC2::Types::NetworkInterface
249
+ def vpc_network_interfaces
250
+ @vpc_network_interfaces ||= Hash[id_vpcs.map do |vpc_id, _|
251
+ [vpc_id, network_interfaces.select { |net| net.vpc_id == vpc_id}]
252
+ end]
253
+ end
254
+
255
+ # Public: Lazily load network interfaces
256
+ def network_interfaces
257
+ @network_interfaces ||= init_network_interfaces
258
+ end
259
+
260
+ # Public
261
+ #
262
+ # Returns a Hash of ebs volume group name to EbsGroupConfig
263
+ def group_ebs_volumes
264
+ @group_ebs_volumes ||= Hash[ebs_groups.map do |group_name|
265
+ vols = ebs_volumes.select { |vol| vol.group == group_name}
266
+ [group_name, EbsGroupConfig.new(group_name).populate!(vols)]
267
+ end]
268
+ end
269
+
270
+ # Public
271
+ #
272
+ # Returns a Hash of ebs volume group name to Aws::EC2::Types::Volume
273
+ def group_ebs_volumes_aws
274
+ @group_ebs_volumes_aws ||= Hash[ebs_groups.map do |group_name|
275
+ vols = ebs_volumes.select { |vol| vol.group == group_name}
276
+ [group_name, vols]
277
+ end]
278
+ end
279
+
280
+ # Public
281
+ #
282
+ # Returns an array of the group names used by all ebs volumes
283
+ def ebs_groups
284
+ @ebs_groups ||= ebs_volumes.map(&:group).reject(&:nil?).uniq
285
+ end
286
+
287
+ # Public
288
+ #
289
+ # Returns a Hash of ebs volume id to volume
290
+ def id_ebs_volumes
291
+ @id_ebs_volumes ||= Hash[ebs_volumes.map { |vol| [vol.volume_id, vol] }]
292
+ end
293
+
294
+ # Public: Lazily loads EBS volumes, rejecting all root-mounted volumes
295
+ def ebs_volumes
296
+ @ebs_volumes ||= init_ebs_volumes.reject do |vol|
297
+ vol.attachments.any? do |att|
298
+ attached_instance = id_instances[att.instance_id]
299
+ attached_instance.root_device_name == att.device
300
+ end
301
+ end
302
+ end
303
+
304
+ # Public
305
+ #
306
+ # Returns a Hash of instance id to Aws::EC2::Types::Instance
307
+ def id_instances
308
+ @id_instances ||= Hash[instances.map { |i| [i.instance_id, i] }]
309
+ end
310
+
311
+ # Public
312
+ #
313
+ # Returns a Hash of instance name or id to Aws::EC2::Types::Instance
314
+ def named_instances
315
+ @named_instances ||= Hash[instances.map { |i| [i.name || i.instance_id, i] }]
316
+ end
317
+
318
+ # Public
319
+ #
320
+ # Returns a Hash of instance id to attributes, lazily loading each attribute
321
+ def id_instance_attributes(instance_id)
322
+ @id_instance_attributes ||= {}
323
+ @id_instance_attributes[instance_id] ||= InstanceAttributes.new(instance_id)
324
+ end
325
+
326
+ # Public: Load instance attributes
327
+ #
328
+ # Returns the instance attribute as whatever type that attribute is
329
+ def init_instance_attribute(instance_id, attribute)
330
+ @@client.describe_instance_attribute({
331
+ instance_id: instance_id,
332
+ attribute: attribute
333
+ })
334
+ end
335
+
336
+ # Public: Lazily loads instances
337
+ def instances
338
+ @instances ||= init_instances.reject(&:terminated?)
339
+ end
340
+
341
+ # Public
342
+ #
343
+ # Returns a Hash of placement group name to Aws::EC2::Types::PlacementGroup
344
+ def named_placement_groups
345
+ @named_placement_groups ||= Hash[placement_groups.map { |pg| [pg.group_name, pg] }]
346
+ end
347
+
348
+ # Public: Lazily load placement groups
349
+ def placement_groups
350
+ @placement_groups ||= init_placement_groups
351
+ end
352
+
353
+ # Public: Load tags for an ec2 object
354
+ #
355
+ # Returns the tags as Hash
356
+ def init_tags(object_id)
357
+ next_token = nil
358
+ tags_loaded = false
359
+
360
+ tags = []
361
+
362
+ while !tags_loaded do
363
+ resp = @@client.describe_tags({
364
+ filters: [
365
+ {
366
+ name: "resource-id",
367
+ values: [object_id]
368
+ }
369
+ ]
370
+ })
371
+
372
+ tags.concat(resp.tags)
373
+ tags_loaded = resp.next_token.nil? || resp.next_token.empty?
374
+ next_token = resp.next_token
375
+
376
+ end
377
+
378
+ Hash[tags.map { |tag| [tag.key, tag.value] }]
379
+
380
+ end
381
+
382
+ def supports_vpc
383
+ @supports_vpc ||= supported_platforms.include?("VPC")
384
+ end
385
+
386
+ def supports_ec2_classic
387
+ @supports_ec2_classic ||= supported_platforms.include?("EC2")
388
+ end
389
+
390
+ def supported_platforms
391
+ @supported_platforms ||= @@client.describe_account_attributes({
392
+ attribute_names: ["supported-platforms"]
393
+ }).account_attributes.first.attribute_values.map(&:attribute_value)
394
+ end
395
+
396
+ private
397
+
398
+ # Internal: Load all subnets
399
+ #
400
+ # Returns an array of Aws::EC2::Types::Subnet
401
+ def init_subnets
402
+ @@client.describe_subnets.subnets
403
+ end
404
+
405
+ # Internal: Load VPCs
406
+ #
407
+ # Returns the VPCs as Aws::EC2::Types::Vpc
408
+ def init_vpcs
409
+ @@client.describe_vpcs.vpcs
410
+ end
411
+
412
+ # Internal: Load route tables
413
+ #
414
+ # Returns the route tables as Aws::EC2::Types::RouteTable
415
+ def init_route_tables
416
+ @@client.describe_route_tables.route_tables
417
+ end
418
+
419
+ # Internal: Load network acls
420
+ #
421
+ # Returns the network acls as Aws::EC2::Types::NetworkAcl
422
+ def init_network_acls
423
+ @@client.describe_network_acls.network_acls
424
+ end
425
+
426
+ # Internal: Load DHCP Options
427
+ #
428
+ # Returns the dhcp options as Aws::EC2::Types::DhcpOptions
429
+ def init_dhcp_options
430
+ @@client.describe_dhcp_options.dhcp_options
431
+ end
432
+
433
+ # Internal: Load VPC Endpoints
434
+ #
435
+ # Returns the vpc endpoints as Aws::EC2::Types::VpcEndpoint
436
+ def init_endpoints
437
+ endpoints = []
438
+ next_token = nil
439
+ all_records_retrieved = false
440
+
441
+ until all_records_retrieved
442
+ response = @@client.describe_vpc_endpoints({
443
+ next_token: next_token
444
+ })
445
+ next_token = response.next_token
446
+ all_records_retrieved = next_token.nil? || next_token.empty?
447
+ endpoints << response.vpc_endpoints
448
+ end
449
+
450
+ endpoints.flatten
451
+ end
452
+
453
+ # Internal: Load allocated addresses
454
+ #
455
+ # Returns the address as Aws::EC2::Types::Address
456
+ def init_addresses
457
+ @@client.describe_addresses.addresses
458
+ end
459
+
460
+ # Internal: Load network interfaces
461
+ #
462
+ # Returns the network interface as Aws::EC2::Types::NetworkInterface
463
+ def init_network_interfaces
464
+ @@client.describe_network_interfaces.network_interfaces
465
+ end
466
+
467
+ # Internal: Load EBS Volumes
468
+ #
469
+ # Returns the volumes as Aws::EC2::Types::Volume
470
+ def init_ebs_volumes
471
+ @@client.describe_volumes.volumes
472
+ end
473
+
474
+ # Internal: Load instances
475
+ #
476
+ # Returns the instances as Aws::EC2::Types::Instance
477
+ def init_instances
478
+ instances = []
479
+ next_token = nil
480
+ all_records_retrieved = false
481
+
482
+ until all_records_retrieved
483
+ response = @@client.describe_instances({
484
+ next_token: next_token
485
+ })
486
+ next_token = response.next_token
487
+ all_records_retrieved = next_token.nil? || next_token.empty?
488
+ instances << response.reservations.map { |r| r.instances }
489
+ end
490
+
491
+ instances.flatten
492
+ end
493
+
494
+ # Internal: Load placement groups
495
+ #
496
+ # Returns the placement groups as Aws::EC2::Types::PlacementGroup
497
+ def init_placement_groups
498
+ @@client.describe_placement_groups.placement_groups
499
+ end
500
+
501
+ end
502
+ end
503
+ end