Class: AwsAuditor::EC2Instance

Inherits:
Object
  • Object
show all
Extended by:
EC2Wrapper, InstanceHelper
Defined in:
lib/aws_auditor/ec2_instance.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from EC2Wrapper

#ec2

Class Method Summary collapse

Instance Method Summary collapse

Methods included from InstanceHelper

compare, instance_count_hash, instance_hash, reserved_instance_hash

Constructor Details

#initialize(ec2_instance, count = 1) ⇒ EC2Instance

Returns a new instance of EC2Instance.



13
14
15
16
17
18
19
20
21
# File 'lib/aws_auditor/ec2_instance.rb', line 13

def initialize(ec2_instance, count=1)
  @id = ec2_instance.id
  @name = nil
  @platform = platform_helper(ec2_instance)
  @availability_zone = ec2_instance.availability_zone
  @instance_type = ec2_instance.instance_type
  @count = count
  @stack_name = nil
end

Class Attribute Details

.instancesObject

Returns the value of attribute instances.



9
10
11
# File 'lib/aws_auditor/ec2_instance.rb', line 9

def instances
  @instances
end

.reserved_instancesObject

Returns the value of attribute reserved_instances.



9
10
11
# File 'lib/aws_auditor/ec2_instance.rb', line 9

def reserved_instances
  @reserved_instances
end

Instance Attribute Details

#availability_zoneObject

Returns the value of attribute availability_zone.



12
13
14
# File 'lib/aws_auditor/ec2_instance.rb', line 12

def availability_zone
  @availability_zone
end

#countObject

Returns the value of attribute count.



12
13
14
# File 'lib/aws_auditor/ec2_instance.rb', line 12

def count
  @count
end

#idObject

Returns the value of attribute id.



12
13
14
# File 'lib/aws_auditor/ec2_instance.rb', line 12

def id
  @id
end

#instance_typeObject

Returns the value of attribute instance_type.



12
13
14
# File 'lib/aws_auditor/ec2_instance.rb', line 12

def instance_type
  @instance_type
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/aws_auditor/ec2_instance.rb', line 12

def name
  @name
end

#platformObject

Returns the value of attribute platform.



12
13
14
# File 'lib/aws_auditor/ec2_instance.rb', line 12

def platform
  @platform
end

#stack_nameObject

Returns the value of attribute stack_name.



12
13
14
# File 'lib/aws_auditor/ec2_instance.rb', line 12

def stack_name
  @stack_name
end

Class Method Details

.bucketizeObject



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/aws_auditor/ec2_instance.rb', line 79

def self.bucketize
  buckets = {}
  get_instances.map do |instance|
    name = instance.stack_name || instance.name
    if name
      buckets[name] = [] unless buckets.has_key? name
      buckets[name] << instance
    else
      puts "Could not sort #{instance.id}, as it has no stack_name or name"
    end
  end
  buckets.sort_by{|k,v| k }
end

.get_instancesObject



27
28
29
30
31
32
33
34
# File 'lib/aws_auditor/ec2_instance.rb', line 27

def self.get_instances
  return @instances if @instances
  @instances = ec2.instances.map do |instance|
    next unless instance.status.to_s == 'running'
    new(instance)
  end.compact
  get_more_info
end

.get_reserved_instancesObject



36
37
38
39
40
41
42
# File 'lib/aws_auditor/ec2_instance.rb', line 36

def self.get_reserved_instances
  return @reserved_instances if @reserved_instances
  @reserved_instances = ec2.reserved_instances.map do |ri|
    next unless ri.state == 'active'
    new(ri, ri.instance_count)
  end.compact
end

Instance Method Details

#to_sObject



23
24
25
# File 'lib/aws_auditor/ec2_instance.rb', line 23

def to_s
  "#{@platform} #{@availability_zone} #{@instance_type}"
end