Class: Awshark::Rds::CheckReservations

Inherits:
Object
  • Object
show all
Defined in:
lib/awshark/rds/check_reservations.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instances:, reservations:) ⇒ CheckReservations

Returns a new instance of CheckReservations.



8
9
10
11
# File 'lib/awshark/rds/check_reservations.rb', line 8

def initialize(instances:, reservations:)
  @instances = instances
  @reservations = reservations
end

Instance Attribute Details

#instancesObject (readonly)

Returns the value of attribute instances.



6
7
8
# File 'lib/awshark/rds/check_reservations.rb', line 6

def instances
  @instances
end

#reservationsObject (readonly)

Returns the value of attribute reservations.



6
7
8
# File 'lib/awshark/rds/check_reservations.rb', line 6

def reservations
  @reservations
end

Instance Method Details

#checkObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/awshark/rds/check_reservations.rb', line 27

def check
  type_permutations.map do |permutation|
    type = permutation[:type]
    multi_az = permutation[:multi_az]

    instance_count = count_instances(type, multi_az)
    reserved_count = count_reservations(type, multi_az)

    OpenStruct.new(
      type: type,
      multi_az: multi_az,
      instance_count: instance_count,
      reserved_count: reserved_count
    )
  end
end

#count_instances(type, multi_az) ⇒ Object



44
45
46
47
48
# File 'lib/awshark/rds/check_reservations.rb', line 44

def count_instances(type, multi_az)
  instances.select do |i|
    i.type == type && i.multi_az == multi_az
  end.size
end

#count_reservations(type, multi_az) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/awshark/rds/check_reservations.rb', line 50

def count_reservations(type, multi_az)
  reservation = reservations.detect do |r|
    r.type == type && r.multi_az == multi_az
  end

  reservation ? reservation.count : 0
end

#type_permutationsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/awshark/rds/check_reservations.rb', line 13

def type_permutations
  type_permutations_hash = {}

  (reservations + instances).each do |instance|
    key = "#{instance.type}+#{instance.multi_az}"
    type_permutations_hash[key] = {
      type: instance.type,
      multi_az: instance.multi_az
    }
  end

  type_permutations_hash.values
end