class Volume

Public Instance Methods

attach(name) click to toggle source
    # File lib/cloudstack-cli/commands/volume.rb
117 def attach(name)
118   resolve_project
119   resolve_virtual_machine
120 
121   volume = client.list_volumes(
122     name: name,
123     listall: true,
124     project_id: options[:project_id]
125   ).first
126 
127   if !volume
128     say "Error: Volume #{name} not found.", :red
129     exit 1
130   elsif volume.has_key?("virtualmachineid")
131     say "Error: Volume #{name} already attached to VM #{volume["vmname"]}.", :red
132     exit 1
133   end
134 
135   say "Attach volume #{name} to VM #{options[:virtual_machine]} "
136   client.attach_volume(
137     id: volume['id'],
138     virtualmachineid: options[:virtual_machine_id]
139   )
140   say " OK.", :green
141 end
create(name) click to toggle source
    # File lib/cloudstack-cli/commands/volume.rb
 81 def create(name)
 82   options[:name] = name
 83   resolve_project
 84   resolve_zone
 85   resolve_disk_offering
 86   resolve_snapshot
 87   resolve_virtual_machine
 88 
 89   if !options[:disk_offering_id] && !options[:snapshot_id]
 90     say "Either disk_offering or snapshot must be passed in.", :yellow
 91     exit 1
 92   elsif options[:disk_offering_id] && !options[:zone_id]
 93     say "Zone is required when deploying with disk-offering.", :yellow
 94     exit 1
 95   end
 96 
 97   say "Creating volume #{name} "
 98   job = client.create_volume(options).merge(sync: true)
 99   say " OK.", :green
100 
101   # attach the new volume if a vm is profided and not a sapshot
102   if options[:virtual_machine] && options[:snapshot] == nil
103     sleep 2
104     say "Attach volume #{name} to VM #{options[:virtual_machine]} "
105     client.attach_volume(
106       id: job['volume']['id'],
107       virtualmachineid: options[:virtual_machine_id],
108       sync: true
109     )
110     say " OK.", :green
111   end
112 end
delete(name) click to toggle source
    # File lib/cloudstack-cli/commands/volume.rb
171 def delete(name)
172   resolve_project
173 
174   volume = client.list_volumes(
175     name: name,
176     listall: true,
177     project_id: options[:project_id]
178   ).first
179 
180   if !volume
181     say "Error: Volume #{name} not found.", :red
182     exit 1
183   elsif volume.has_key?("virtualmachineid")
184     say "Error: Volume #{name} must be detached before deletion.", :red
185     exit 1
186   end
187 
188   say "Delete volume #{name} "
189   client.delete_volume id: volume['id']
190   say " OK.", :green
191 end
detach(name) click to toggle source
    # File lib/cloudstack-cli/commands/volume.rb
146 def detach(name)
147   resolve_project
148 
149   volume = client.list_volumes(
150     name: name,
151     listall: true,
152     project_id: options[:project_id]
153   ).first
154 
155   if !volume
156     say "Error: Volume #{name} not found.", :red
157     exit 1
158   elsif !volume.has_key?("virtualmachineid")
159     say "Error: Volume #{name} currently not attached to any VM.", :red
160     exit 1
161   end
162   exit unless options[:force] ||
163     yes?("Detach volume #{name} from virtual_machine #{volume["vmname"]}? [y/N]:", :magenta)
164   say "Detach volume #{name} from VM #{volume["vmname"]} "
165   client.detach_volume id: volume['id']
166   say " OK.", :green
167 end
list() click to toggle source
   # File lib/cloudstack-cli/commands/volume.rb
14 def list
15   resolve_project
16   resolve_account
17   resolve_zone
18   add_filters_to_options("listVolumes") if options[:filter]
19   volumes = client.list_volumes(options)
20   volumes = filter_objects(volumes) if options[:filter]
21   if volumes.size < 1
22     say "No volumes found."
23   else
24     case options[:format].to_sym
25     when :yaml
26       puts({volumes: volumes}.to_yaml)
27     when :json
28       puts JSON.pretty_generate(volumes: volumes)
29     else
30       table = [%w(Name Type Size VM Storage Offeringname Zone Status)]
31       table.first << 'Project' if options[:project]
32       volumes.each do |volume|
33         table << [
34           volume['name'], volume['type'],
35           (volume['size'] / 1024**3).to_s + 'GB',
36           volume['vmname'],
37           volume['storage'],
38           volume['diskofferingname'],
39           volume['zonename'],
40           volume['state']
41         ]
42         table.last << volume['project'] if options[:project]
43       end
44       print_table(table)
45       say "Total number of volumes: #{volumes.size}"
46     end
47   end
48 end
show(name) click to toggle source
   # File lib/cloudstack-cli/commands/volume.rb
52 def show(name)
53   resolve_project
54   options[:listall] = true
55   options[:name] = name
56   volumes = client.list_volumes(options)
57   if volumes.size < 1
58     say "No volume with name \"#{name}\" found."
59   else
60     volume = volumes.first
61     table = volume.map do |key, value|
62       [ set_color("#{key}:", :yellow), "#{value}" ]
63     end
64     print_table table
65   end
66 end