Class: HP::Cloud::VolumeHelper

Inherits:
BaseHelper show all
Defined in:
lib/hpcloud/volume_helper.rb

Constant Summary collapse

@@serverous =
nil

Instance Attribute Summary collapse

Attributes inherited from BaseHelper

#connection, #cstatus, #fog

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseHelper

#destroy, #is_valid?, #set_error, #to_hash

Constructor Details

#initialize(connection, foggy = nil) ⇒ VolumeHelper

Returns a new instance of VolumeHelper.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/hpcloud/volume_helper.rb', line 33

def initialize(connection, foggy = nil)
  super(connection, foggy)
  if foggy.nil?
    return
  end
  @id = foggy.id
  @name = foggy.name
  @size = foggy.size
  @type = foggy.type
  @created = foggy.created_at
  @status = foggy.status
  @description = foggy.description
  @availability_zone = foggy.availability_zone
  @servers = ''
  @@serverous = Servers.new if @@serverous.nil?
  foggy.attachments.each { |x|
    srv = @@serverous.get(x["server_id"].to_s)
    if srv.is_valid? == false
      next
    end
    if @servers.length > 0
      @servers += ','
    end
    @servers += srv.name
  }
end

Instance Attribute Details

#availability_zoneObject

Returns the value of attribute availability_zone.



26
27
28
# File 'lib/hpcloud/volume_helper.rb', line 26

def availability_zone
  @availability_zone
end

#createdObject

Returns the value of attribute created.



26
27
28
# File 'lib/hpcloud/volume_helper.rb', line 26

def created
  @created
end

#descriptionObject

Returns the value of attribute description.



26
27
28
# File 'lib/hpcloud/volume_helper.rb', line 26

def description
  @description
end

#idObject

Returns the value of attribute id.



26
27
28
# File 'lib/hpcloud/volume_helper.rb', line 26

def id
  @id
end

#imagerefObject

Returns the value of attribute imageref.



26
27
28
# File 'lib/hpcloud/volume_helper.rb', line 26

def imageref
  @imageref
end

#metaObject

Returns the value of attribute meta.



27
28
29
# File 'lib/hpcloud/volume_helper.rb', line 27

def meta
  @meta
end

#nameObject

Returns the value of attribute name.



26
27
28
# File 'lib/hpcloud/volume_helper.rb', line 26

def name
  @name
end

#serversObject

Returns the value of attribute servers.



26
27
28
# File 'lib/hpcloud/volume_helper.rb', line 26

def servers
  @servers
end

#sizeObject

Returns the value of attribute size.



26
27
28
# File 'lib/hpcloud/volume_helper.rb', line 26

def size
  @size
end

#snapshot_idObject

Returns the value of attribute snapshot_id.



26
27
28
# File 'lib/hpcloud/volume_helper.rb', line 26

def snapshot_id
  @snapshot_id
end

#statusObject

Returns the value of attribute status.



26
27
28
# File 'lib/hpcloud/volume_helper.rb', line 26

def status
  @status
end

#typeObject

Returns the value of attribute type.



26
27
28
# File 'lib/hpcloud/volume_helper.rb', line 26

def type
  @type
end

Class Method Details

.clear_cacheObject



102
103
104
# File 'lib/hpcloud/volume_helper.rb', line 102

def self.clear_cache
  @@serverous = nil
end

.get_keysObject



29
30
31
# File 'lib/hpcloud/volume_helper.rb', line 29

def self.get_keys()
  return [ "id", "name", "size", "type", "created", "status", "description", "servers", "availability_zone" ]
end

Instance Method Details

#attach(server, device) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/hpcloud/volume_helper.rb', line 82

def attach(server, device)
  begin
    @fog.attach(server.id, device)
  rescue Exception => e
    set_error("Error attaching '#{name}' on server '#{server.name}' to device '#{device}'.")
    return false
  end
  return true
end

#detachObject



92
93
94
95
96
97
98
99
100
# File 'lib/hpcloud/volume_helper.rb', line 92

def detach()
  begin
    @fog.detach
  rescue Exception => e
    set_error("Error detaching '#{name}' from '#{@servers}'.")
    return false
  end
  return true
end

#map_device(device) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/hpcloud/volume_helper.rb', line 106

def map_device(device)
  begin
    return "/dev/vda" if device == "0"
    i = device.to_i
    return device if i < 1 || i > 26
    i = i +  96
    return "/dev/vd" + i.chr
  rescue Exception => e
  end
  return device
end

#saveObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/hpcloud/volume_helper.rb', line 60

def save
  return false if is_valid? == false
  if @fog.nil?
    hsh = {:name => @name,
       :description => @description,
       :size => @size}
    hsh[:snapshot_id] = @snapshot_id unless @snapshot_id.nil?
    hsh[:image_id] = @imageref unless @imageref.nil?
    hsh[:availability_zone] = @availability_zone unless @availability_zone.nil?
    volume = @connection.block.volumes.create(hsh)
    if volume.nil?
      set_error("Error creating volume '#{@name}'")
      return false
    end
    @id = volume.id
    @fog = volume
    return true
  else
    raise "Update not implemented"
  end
end