Class: SISFC::DataCenter
- Inherits:
-
Object
- Object
- SISFC::DataCenter
- Extended by:
- Forwardable
- Defined in:
- lib/sisfc/data_center.rb
Instance Attribute Summary collapse
-
#dcid ⇒ Object
readonly
Returns the value of attribute dcid.
-
#location_id ⇒ Object
readonly
Returns the value of attribute location_id.
Instance Method Summary collapse
-
#add_vm(vm, component_name) ⇒ Object
returns false in case no more VMs can be allocated.
-
#get_random_vm(component_name) ⇒ Object
returns nil in case no VM for component component_name is running.
-
#initialize(id:, location_id:, name:, type:, **opts) ⇒ DataCenter
constructor
A new instance of DataCenter.
- #private? ⇒ Boolean
- #public? ⇒ Boolean
- #remove_vm(vm, component_name) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(id:, location_id:, name:, type:, **opts) ⇒ DataCenter
Returns a new instance of DataCenter.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/sisfc/data_center.rb', line 14 def initialize(id:, location_id:, name:, type:, **opts) @dcid = id @location_id = location_id @vms = {} @vm_type_count = {} @name = name @type = type raise ArgumentError, "Unsupported type!" unless [ :private, :public ].include?(@type) @availability_check_proc = opts[:maximum_vm_capacity] end |
Instance Attribute Details
#dcid ⇒ Object (readonly)
Returns the value of attribute dcid.
12 13 14 |
# File 'lib/sisfc/data_center.rb', line 12 def dcid @dcid end |
#location_id ⇒ Object (readonly)
Returns the value of attribute location_id.
12 13 14 |
# File 'lib/sisfc/data_center.rb', line 12 def location_id @location_id end |
Instance Method Details
#add_vm(vm, component_name) ⇒ Object
returns false in case no more VMs can be allocated
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/sisfc/data_center.rb', line 26 def add_vm(vm, component_name) @vms[component_name] ||= [] @vm_type_count[vm.size] ||= 0 raise 'Error! VM is already present!' if @vms[component_name].include? vm # defer availablility check to user specified procedure if @availability_check_proc return false unless @availability_check_proc.call(@vm_type_count) end # allocate VM @vms[component_name] << vm @vm_type_count[vm.size] += 1 end |
#get_random_vm(component_name) ⇒ Object
returns nil in case no VM for component component_name is running
51 52 53 54 55 |
# File 'lib/sisfc/data_center.rb', line 51 def get_random_vm(component_name) if @vms.has_key? component_name @vms[component_name].sample end end |
#private? ⇒ Boolean
62 63 64 |
# File 'lib/sisfc/data_center.rb', line 62 def private? @type == :private end |
#public? ⇒ Boolean
66 67 68 |
# File 'lib/sisfc/data_center.rb', line 66 def public? @type == :public end |
#remove_vm(vm, component_name) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/sisfc/data_center.rb', line 42 def remove_vm(vm, component_name) if @vms.has_key? component_name and @vms[component_name].include? vm raise 'Error! Inconsistent number of VMs!' unless @vm_type_count[vm.size] >= 1 @vm_type_count[vm.size] += 1 @vms.delete(vm) end end |
#to_s ⇒ Object
57 58 59 60 |
# File 'lib/sisfc/data_center.rb', line 57 def to_s "Data center #{@dcid}, with VMs:" + @vms.inject("") {|s,(k,v)| s += " (#{k}: #{v.size})" } end |