Class: Volume

Inherits:
CloudstackCli::Base show all
Defined in:
lib/cloudstack-cli/commands/volume.rb

Constant Summary

Constants included from CloudstackCli::Helper

CloudstackCli::Helper::ASYNC_STATES

Instance Attribute Summary

Attributes inherited from CloudstackCli::Base

#config

Instance Method Summary collapse

Methods inherited from CloudstackCli::Base

exit_on_failure?, start

Methods included from CloudstackCli::OptionResolver

#resolve_account, #resolve_cluster, #resolve_compute_offering, #resolve_disk_offering, #resolve_domain, #resolve_host, #resolve_ip_network_list, #resolve_iso, #resolve_iso_for_vm_deployment, #resolve_networks, #resolve_project, #resolve_snapshot, #resolve_template, #resolve_virtual_machine, #resolve_zone, #vm_options_to_params

Methods included from CloudstackCli::Helper

#ask_number, #bootstrap_server, #bootstrap_server_interactive, #create_port_rules, #create_server, #get_server_default_nic, #pf_rule_to_object, #print_job_status, #print_options, #run_background_jobs, #update_job_status, #update_jobs, #watch_jobs

Methods inherited from Thor

banner, basename2, old_subcommand, subcommand

Instance Method Details

#attach(name) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/cloudstack-cli/commands/volume.rb', line 117

def attach(name)
  resolve_project
  resolve_virtual_machine

  volume = client.list_volumes(
    name: name,
    listall: true,
    project_id: options[:project_id]
  ).first

  if !volume
    say "Error: Volume #{name} not found.", :red
    exit 1
  elsif volume.has_key?("virtualmachineid")
    say "Error: Volume #{name} already attached to VM #{volume["vmname"]}.", :red
    exit 1
  end

  say "Attach volume #{name} to VM #{options[:virtual_machine]} "
  client.attach_volume(
    id: volume['id'],
    virtualmachineid: options[:virtual_machine_id]
  )
  say " OK.", :green
end

#create(name) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/cloudstack-cli/commands/volume.rb', line 81

def create(name)
  options[:name] = name
  resolve_project
  resolve_zone
  resolve_disk_offering
  resolve_snapshot
  resolve_virtual_machine

  if !options[:disk_offering_id] && !options[:snapshot_id]
    say "Either disk_offering or snapshot must be passed in.", :yellow
    exit 1
  elsif options[:disk_offering_id] && !options[:zone_id]
    say "Zone is required when deploying with disk-offering.", :yellow
    exit 1
  end

  say "Creating volume #{name} "
  job = client.create_volume(options).merge(sync: true)
  say " OK.", :green

  # attach the new volume if a vm is profided and not a sapshot
  if options[:virtual_machine] && options[:snapshot] == nil
    sleep 2
    say "Attach volume #{name} to VM #{options[:virtual_machine]} "
    client.attach_volume(
      id: job['volume']['id'],
      virtualmachineid: options[:virtual_machine_id],
      sync: true
    )
    say " OK.", :green
  end
end

#delete(name) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/cloudstack-cli/commands/volume.rb', line 171

def delete(name)
  resolve_project

  volume = client.list_volumes(
    name: name,
    listall: true,
    project_id: options[:project_id]
  ).first

  if !volume
    say "Error: Volume #{name} not found.", :red
    exit 1
  elsif volume.has_key?("virtualmachineid")
    say "Error: Volume #{name} must be detached before deletion.", :red
    exit 1
  end

  say "Delete volume #{name} "
  client.delete_volume id: volume['id']
  say " OK.", :green
end

#detach(name) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/cloudstack-cli/commands/volume.rb', line 146

def detach(name)
  resolve_project

  volume = client.list_volumes(
    name: name,
    listall: true,
    project_id: options[:project_id]
  ).first

  if !volume
    say "Error: Volume #{name} not found.", :red
    exit 1
  elsif !volume.has_key?("virtualmachineid")
    say "Error: Volume #{name} currently not attached to any VM.", :red
    exit 1
  end
  exit unless options[:force] ||
    yes?("Detach volume #{name} from virtual_machine #{volume["vmname"]}? [y/N]:", :magenta)
  say "Detach volume #{name} from VM #{volume["vmname"]} "
  client.detach_volume id: volume['id']
  say " OK.", :green
end

#listObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cloudstack-cli/commands/volume.rb', line 14

def list
  resolve_project
  
  resolve_zone
  add_filters_to_options("listVolumes") if options[:filter]
  volumes = client.list_volumes(options)
  volumes = filter_objects(volumes) if options[:filter]
  if volumes.size < 1
    say "No volumes found."
  else
    case options[:format].to_sym
    when :yaml
      puts({volumes: volumes}.to_yaml)
    when :json
      puts JSON.pretty_generate(volumes: volumes)
    else
      table = [%w(Name Type Size VM Storage Offeringname Zone Status)]
      table.first << 'Project' if options[:project]
      volumes.each do |volume|
        table << [
          volume['name'], volume['type'],
          (volume['size'] / 1024**3).to_s + 'GB',
          volume['vmname'],
          volume['storage'],
          volume['diskofferingname'],
          volume['zonename'],
          volume['state']
        ]
        table.last << volume['project'] if options[:project]
      end
      print_table(table)
      say "Total number of volumes: #{volumes.size}"
    end
  end
end

#show(name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cloudstack-cli/commands/volume.rb', line 52

def show(name)
  resolve_project
  options[:listall] = true
  options[:name] = name
  volumes = client.list_volumes(options)
  if volumes.size < 1
    say "No volume with name \"#{name}\" found."
  else
    volume = volumes.first
    table = volume.map do |key, value|
      [ set_color("#{key}:", :yellow), "#{value}" ]
    end
    print_table table
  end
end