Class: Vcloud::Core::IndependentDisk

Inherits:
Object
  • Object
show all
Defined in:
lib/vcloud/core/independent_disk.rb

Defined Under Namespace

Classes: DiskAlreadyExistsException, DiskNotFoundException, MultipleDisksFoundException, QueryExecutionError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ IndependentDisk

Returns a new instance of IndependentDisk.



12
13
14
15
16
17
# File 'lib/vcloud/core/independent_disk.rb', line 12

def initialize(id)
  unless id =~ /^[-0-9a-f]+$/
    raise "IndependentDisk id : #{id} is not in correct format"
  end
  @id = id
end

Instance Attribute Details

#idObject (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) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/vcloud/core/independent_disk.rb', line 77

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) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vcloud/core/independent_disk.rb', line 36

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) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vcloud/core/independent_disk.rb', line 19

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_vmsObject



67
68
69
70
71
72
73
74
75
# File 'lib/vcloud/core/independent_disk.rb', line 67

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

#hrefObject



63
64
65
# File 'lib/vcloud/core/independent_disk.rb', line 63

def href
  vcloud_attributes[:href]
end

#nameObject



59
60
61
# File 'lib/vcloud/core/independent_disk.rb', line 59

def name
  vcloud_attributes[:name]
end

#vcloud_attributesObject



55
56
57
# File 'lib/vcloud/core/independent_disk.rb', line 55

def vcloud_attributes
  Vcloud::Core::Fog::ServiceInterface.new.get_disk(id)
end