Class: BuildCloud::EBSVolume

Inherits:
Object
  • Object
show all
Includes:
Component
Defined in:
lib/build-cloud/ebsvolume.rb

Constant Summary collapse

@@objects =
[]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Component

included

Constructor Details

#initialize(fog_interfaces, log, options = {}) ⇒ EBSVolume

Returns a new instance of EBSVolume.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/build-cloud/ebsvolume.rb', line 25

def initialize ( fog_interfaces, log, options = {} )

    @compute = fog_interfaces[:compute]
    @log     = log
    @options = options

    @log.debug( options.inspect )

    required_options(:name, :availability_zone, :size)

end

Class Method Details

.get_id_by_name(name) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/build-cloud/ebsvolume.rb', line 7

def self.get_id_by_name( name )

    volume = self.search( :name => name ).first

    unless volume
        raise "Couldn't get an EBSVolume object for #{name} - is it defined?"
    end

    volume_fog = volume.read

    unless volume_fog
        raise "Couldn't get an EBSVolume fog object for #{name} - is it created?"
    end

    volume_fog.id

end

Instance Method Details

#createObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/build-cloud/ebsvolume.rb', line 37

def create
    
    return if exists?

    @log.info( "Creating volume #{@options[:name]}" )

    options = @options.dup

    volume = @compute.volumes.new(options)
    volume.save

    attributes = {}
    attributes[:resource_id] = volume.id.to_s
    attributes[:key] = 'Name'
    attributes[:value] = @options[:name]
    volume_tag = @compute.tags.new( attributes )
    volume_tag.save

    @log.debug( volume.inspect )

    if @options[:instance_name]
        instance_id = BuildCloud::Instance.get_id_by_name( options[:instance_name] )
        attach_response = @compute.attach_volume(instance_id, volume.id, options[:device])
        @log.debug( attach_response.inspect )
    end

end

#deleteObject



71
72
73
74
75
76
77
78
79
# File 'lib/build-cloud/ebsvolume.rb', line 71

def delete

    return unless exists?

    @log.info( "Deleting volume #{@options[:name]}" )

    fog_object.destroy

end

#readObject Also known as: fog_object



65
66
67
# File 'lib/build-cloud/ebsvolume.rb', line 65

def read
    @compute.volumes.select { |v| v.tags['Name'] == @options[:name]}.first
end