Class: Vcloud::Core::IndependentDisk
- Inherits:
-
Object
- Object
- Vcloud::Core::IndependentDisk
- Defined in:
- lib/vcloud/core/independent_disk.rb
Defined Under Namespace
Classes: DiskAlreadyExistsException, DiskNotFoundException, MultipleDisksFoundException, QueryExecutionError
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
-
.convert_size_to_bytes(size) ⇒ Integer
Convert an integer and units suffix (e.g. 10mb) into an integer of bytes Allowed suffixes are: mb, gb, mib, gib.
-
.create(vdc, name, size) ⇒ Vcloud::Core::IndependentDisk
Create a named, sized IndependentDisk in a particular named vDC.
-
.get_by_name_and_vdc_name(name, vdc_name) ⇒ Vcloud::Core::IndependentDisk
Return the ID of an IndependentDisk referred to by name and vDC.
Instance Method Summary collapse
-
#attached_vms ⇒ Array
Return an array of Vcloud::Core::Vm objects which are attached to independent disk.
-
#destroy ⇒ Boolean
Delete the IndependentDisk entity referred to by this object.
-
#href ⇒ String
Return the href of IndependentDisk.
-
#initialize(id) ⇒ Vcloud::Core::IndependentDisk
constructor
Return an object referring to a particular IndependentDisk.
-
#name ⇒ String
Return the name of IndependentDisk.
-
#vcloud_attributes ⇒ Hash
Return all the vcloud attributes of IndependentDisk.
Constructor Details
#initialize(id) ⇒ Vcloud::Core::IndependentDisk
Return an object referring to a particular IndependentDisk
16 17 18 19 20 21 |
# File 'lib/vcloud/core/independent_disk.rb', line 16 def initialize(id) unless id =~ /^[-0-9a-f]+$/ raise "IndependentDisk id : #{id} is not in correct format" end @id = id end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
10 11 12 |
# File 'lib/vcloud/core/independent_disk.rb', line 10 def id @id end |
Class Method Details
.convert_size_to_bytes(size) ⇒ Integer
Convert an integer and units suffix (e.g. 10mb) into an integer of bytes Allowed suffixes are: mb, gb, mib, gib
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/vcloud/core/independent_disk.rb', line 120 def self.convert_size_to_bytes(size) if size.to_s =~ /^(\d+)mb$/i Integer($1) * (10**6) elsif size.to_s =~ /^(\d+)gb$/i Integer($1) * (10**9) elsif size.to_s =~ /^(\d+)mib$/i Integer($1) * (2**20) elsif size.to_s =~ /^(\d+)gib$/i Integer($1) * (2**30) elsif size.to_s =~ /^(\d+)$/i Integer($1) else raise ArgumentError, "Cannot convert size string '#{size}' into a number of bytes" end end |
.create(vdc, name, size) ⇒ Vcloud::Core::IndependentDisk
Create a named, sized IndependentDisk in a particular named vDC
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/vcloud/core/independent_disk.rb', line 54 def self.create(vdc, name, size) vdc_name = vdc.name begin self.get_by_name_and_vdc_name(name, vdc_name) rescue DiskNotFoundException ok_to_create = true end unless ok_to_create raise DiskAlreadyExistsException, "Cannot create Independent Disk '#{name}' in vDC '#{vdc_name}' - a disk with " + "that name is already present" end size_in_bytes = convert_size_to_bytes(size) body = Vcloud::Core::Fog::ServiceInterface.new.post_create_disk(vdc.id, name, size_in_bytes) return self.new(body[:href].split('/').last) end |
.get_by_name_and_vdc_name(name, vdc_name) ⇒ Vcloud::Core::IndependentDisk
Return the ID of an IndependentDisk referred to by name and vDC
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/vcloud/core/independent_disk.rb', line 28 def self.get_by_name_and_vdc_name(name, vdc_name) q = Vcloud::Core::QueryRunner.new query_results = q.run('disk', :filter => "name==#{name};vdcName==#{vdc_name}") unless query_results raise QueryExecutionError, "Error finding IndependentDisk by name '#{name}' & vdc '#{vdc_name}'" end raise DiskNotFoundException, "IndependentDisk '#{name}' not found in vDC '#{vdc_name}'" if query_results.size == 0 if query_results.size > 1 raise MultipleDisksFoundException, "Multiple IndependentDisks matching '#{name}' found in vDC '#{vdc_name}. " + "Create disks via IndependentDisk.new(disk_id) instead." end return self.new(query_results.first[:href].split('/').last) end |
Instance Method Details
#attached_vms ⇒ Array
Return an array of Vcloud::Core::Vm objects which are attached to independent disk
98 99 100 101 102 103 104 105 106 |
# File 'lib/vcloud/core/independent_disk.rb', line 98 def attached_vms body = Vcloud::Core::Fog::ServiceInterface.new.get_vms_disk_attached_to(id) vms = body.fetch(:VmReference) vms.map do |vm| id = vm.fetch(:href).split('/').last parent_vapp = Vcloud::Core::Vapp.get_by_child_vm_id(id) Vcloud::Core::Vm.new(id, parent_vapp) end end |
#destroy ⇒ Boolean
Delete the IndependentDisk entity referred to by this object.
111 112 113 |
# File 'lib/vcloud/core/independent_disk.rb', line 111 def destroy Vcloud::Core::Fog::ServiceInterface.new.delete_disk(id) end |
#href ⇒ String
Return the href of IndependentDisk
90 91 92 |
# File 'lib/vcloud/core/independent_disk.rb', line 90 def href vcloud_attributes[:href] end |
#name ⇒ String
Return the name of IndependentDisk
83 84 85 |
# File 'lib/vcloud/core/independent_disk.rb', line 83 def name vcloud_attributes[:name] end |
#vcloud_attributes ⇒ Hash
Return all the vcloud attributes of IndependentDisk
76 77 78 |
# File 'lib/vcloud/core/independent_disk.rb', line 76 def vcloud_attributes Vcloud::Core::Fog::ServiceInterface.new.get_disk(id) end |