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) ⇒ Vcloud::Core::IndependentDisk

Return an object referring to a particular IndependentDisk

Parameters:

  • id (String)

    The ID of the independent disk



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

#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) ⇒ Integer

Convert an integer and units suffix (e.g. 10mb) into an integer of bytes Allowed suffixes are: mb, gb, mib, gib

Parameters:

  • size (String)

    the intended size of the disk (optionally with units)

Returns:

  • (Integer)

    the disk size in bytes



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

Parameters:

  • vdc (String)

    The name of the vDC

  • name (String)

    The name of the IndependentDisk

  • size (String, Integer)

    The size as an integer of bytes, or an integer with units (see convert_size_to_bytes)

Returns:



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

Parameters:

  • name (String)

    The name of the disk

  • vdc (String)

    The name of the vDC

Returns:

Raises:



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_vmsArray

Return an array of Vcloud::Core::Vm objects which are attached to independent disk

Returns:

  • (Array)

    an array of Vcloud::Core::Vm



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

#destroyBoolean

Delete the IndependentDisk entity referred to by this object.

Returns:

  • (Boolean)

    Returns true if disk was deleted. Raises an exception otherwise.



111
112
113
# File 'lib/vcloud/core/independent_disk.rb', line 111

def destroy
  Vcloud::Core::Fog::ServiceInterface.new.delete_disk(id)
end

#hrefString

Return the href of IndependentDisk

Returns:

  • (String)

    the href of instance



90
91
92
# File 'lib/vcloud/core/independent_disk.rb', line 90

def href
  vcloud_attributes[:href]
end

#nameString

Return the name of IndependentDisk

Returns:

  • (String)

    the name of instance



83
84
85
# File 'lib/vcloud/core/independent_disk.rb', line 83

def name
  vcloud_attributes[:name]
end

#vcloud_attributesHash

Return all the vcloud attributes of IndependentDisk

Returns:

  • (Hash)

    a hash describing all the attributes of disk



76
77
78
# File 'lib/vcloud/core/independent_disk.rb', line 76

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