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_compute_offering, #resolve_disk_offering, #resolve_domain, #resolve_host, #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, #get_server_fqdn, #get_server_public_ip, #get_ssh_port_forwarding_rule, #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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/cloudstack-cli/commands/volume.rb', line 104

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cloudstack-cli/commands/volume.rb', line 68

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



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/cloudstack-cli/commands/volume.rb', line 156

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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/cloudstack-cli/commands/volume.rb', line 132

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

  say "Detach volume #{name} from VM #{volume["vmname"]} "
  client.detach_volume id: volume['id']
  say " OK.", :green
end

#listObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cloudstack-cli/commands/volume.rb', line 10

def list
  resolve_project
  
  resolve_zone
  volumes = client.list_volumes(options)
  if volumes.size < 1
    say "No volumes found."
  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

#show(name) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cloudstack-cli/commands/volume.rb', line 39

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