Class: AwsAuditor::Instance

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

Instance Attribute Summary collapse

Attributes included from EC2Wrapper

#ec2

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aws_instance, count = 1) ⇒ Instance

Returns a new instance of Instance.



6
7
8
9
10
11
12
# File 'lib/aws_auditor/instance.rb', line 6

def initialize(aws_instance, count=1)
  @id = aws_instance.id
  @platform = platform_helper(aws_instance)
  @availability_zone = aws_instance.availability_zone
  @instance_type = aws_instance.instance_type
  @count = count
end

Instance Attribute Details

#availability_zoneObject

Returns the value of attribute availability_zone.



5
6
7
# File 'lib/aws_auditor/instance.rb', line 5

def availability_zone
  @availability_zone
end

#countObject

Returns the value of attribute count.



5
6
7
# File 'lib/aws_auditor/instance.rb', line 5

def count
  @count
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/aws_auditor/instance.rb', line 5

def id
  @id
end

#instance_typeObject

Returns the value of attribute instance_type.



5
6
7
# File 'lib/aws_auditor/instance.rb', line 5

def instance_type
  @instance_type
end

#platformObject

Returns the value of attribute platform.



5
6
7
# File 'lib/aws_auditor/instance.rb', line 5

def platform
  @platform
end

Class Method Details

.get_instancesObject



42
43
44
45
46
47
48
# File 'lib/aws_auditor/instance.rb', line 42

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

.get_reserved_instancesObject



50
51
52
53
54
55
56
# File 'lib/aws_auditor/instance.rb', line 50

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

.instance_hashObject



58
59
60
# File 'lib/aws_auditor/instance.rb', line 58

def self.instance_hash
  Hash[get_instances.map {|instance| [instance.id, instance]}]
end

Instance Method Details

#platform_helper(aws_instance) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/aws_auditor/instance.rb', line 18

def platform_helper(aws_instance)
  if aws_instance.class.to_s == 'AWS::EC2::Instance'
    if aws_instance.vpc? 
      return 'VPC'
    elsif aws_instance.platform
      if aws_instance.platform.downcase.include? 'windows' 
        return 'Windows'
      else
        return 'Linux'
      end
    else
      return 'Linux'
    end
  elsif aws_instance.class.to_s == 'AWS::EC2::ReservedInstances'
    if aws_instance.product_description.downcase.include? 'vpc' 
      return 'VPC'
    elsif aws_instance.product_description.downcase.include? 'windows'
      return 'Windows'
    else
      return 'Linux'
    end
  end
end

#to_sObject



14
15
16
# File 'lib/aws_auditor/instance.rb', line 14

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