Module: SportNginAwsAuditor::InstanceHelper

Included in:
CacheInstance, EC2Instance, RDSInstance
Defined in:
lib/sport_ngin_aws_auditor/instance_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_instances_with_tag_to_hash(instances_to_add, instance_hash) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/sport_ngin_aws_auditor/instance_helper.rb', line 21

def add_instances_with_tag_to_hash(instances_to_add, instance_hash)
  instances_to_add.each do |instance|
    next if instance.nil?
    key = instance.to_s << " with tag"
    instance_hash[key] = instance_hash.has_key?(key) ? instance_hash[key] + 1 : 1
  end if instances_to_add
  instance_hash
end

#compare(tag_name) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sport_ngin_aws_auditor/instance_helper.rb', line 30

def compare(tag_name)
  differences = Hash.new()
  instances = get_instances(tag_name)
  instances_with_tag = filter_instances_with_tags(instances)
  instances_without_tag = filter_instance_without_tags(instances)
  instance_hash = instance_count_hash(instances_without_tag)
  ris = instance_count_hash(get_reserved_instances)
  instance_hash.keys.concat(ris.keys).uniq.each do |key|
    instance_count = instance_hash.has_key?(key) ? instance_hash[key] : 0
    ris_count = ris.has_key?(key) ? ris[key] : 0
    differences[key] = ris_count - instance_count
  end
  add_instances_with_tag_to_hash(instances_with_tag, differences)
  differences
end

#filter_instance_without_tags(instances) ⇒ Object

assuming the value of the tag is in the form: 01/01/2000 like a date



55
56
57
58
59
60
# File 'lib/sport_ngin_aws_auditor/instance_helper.rb', line 55

def filter_instance_without_tags(instances)
  instances.select do |instance|
    value = gather_instance_tag_date(instance)
    value.nil? || (Date.today.to_s >= value.to_s)
  end
end

#filter_instances_with_tags(instances) ⇒ Object

assuming the value of the tag is in the form: 01/01/2000 like a date



47
48
49
50
51
52
# File 'lib/sport_ngin_aws_auditor/instance_helper.rb', line 47

def filter_instances_with_tags(instances)
  instances.select do |instance|
    value = gather_instance_tag_date(instance)
    value && (Date.today.to_s < value.to_s)
  end
end

#gather_instance_tag_date(instance) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/sport_ngin_aws_auditor/instance_helper.rb', line 62

def gather_instance_tag_date(instance)
  value = instance.no_reserved_instance_tag_value
  unless value.nil?
    date_hash = Date._strptime(value, '%m/%d/%Y')
    value = Date.new(date_hash[:year], date_hash[:mon], date_hash[:mday]) if date_hash
  end
  value
end

#instance_count_hash(instances) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/sport_ngin_aws_auditor/instance_helper.rb', line 12

def instance_count_hash(instances)
  instance_hash = Hash.new()
  instances.each do |instance|
    next if instance.nil?
    instance_hash[instance.to_s] = instance_hash.has_key?(instance.to_s) ? instance_hash[instance.to_s] + instance.count : instance.count
  end if instances
  instance_hash
end

#instance_hashObject



4
5
6
# File 'lib/sport_ngin_aws_auditor/instance_helper.rb', line 4

def instance_hash
  Hash[get_instances.map { |instance| instance.nil? ? next : [instance.id, instance]}.compact]
end

#reserved_instance_hashObject



8
9
10
# File 'lib/sport_ngin_aws_auditor/instance_helper.rb', line 8

def reserved_instance_hash
  Hash[get_reserved_instances.map { |instance| instance.nil? ? next : [instance.id, instance]}.compact]
end