Skip to content

Commit 1b42e58

Browse files
committed
Merge pull request #107 from muga/add_support_for_delete_of_volumes
Add support for delete of volumes
2 parents 89a5753 + a5d2991 commit 1b42e58

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

lib/chef/knife/cs_volume_delete.rb

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#
2+
# Author:: Muga Nishizawa (<[email protected]>)
3+
# Copyright:: Copyright (c) 2014 Muga Nishizawa.
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
require 'chef/knife/cs_base'
20+
21+
module KnifeCloudstack
22+
class CsVolumeDelete < Chef::Knife
23+
24+
include Chef::Knife::KnifeCloudstackBase
25+
26+
deps do
27+
require 'knife-cloudstack/connection'
28+
require 'chef/api_client'
29+
require 'chef/knife'
30+
Chef::Knife.load_deps
31+
end
32+
33+
banner "knife cs volume delete VOLUME_NAME [VOLUME_NAME ...] (options)"
34+
35+
def run
36+
validate_base_options
37+
38+
@name_args.each do |volume_name|
39+
volume = connection.get_volume(volume_name)
40+
41+
if !volume then
42+
ui.error("Volume '#{volume_name}' not found")
43+
next
44+
end
45+
46+
if vmn = volume['vmname']
47+
ui.error("Volume '#{volume_name}' attached to VM '#{vmn}'")
48+
ui.error("You should detach it from VM to delete the volume.")
49+
next
50+
end
51+
52+
show_object_details(volume)
53+
54+
result = confirm_action("Do you really want to delete this volume")
55+
if result
56+
print "#{ui.color("Waiting for deletion", :magenta)}"
57+
connection.delete_volume(volume_name)
58+
puts "\n"
59+
ui.msg("Deleted volume #{volume_name}")
60+
end
61+
end
62+
end
63+
64+
def show_object_details(v)
65+
return if locate_config_value(:yes)
66+
67+
object_fields = []
68+
object_fields << ui.color("Name:", :cyan)
69+
object_fields << v['name'].to_s
70+
object_fields << ui.color("Account:", :cyan)
71+
object_fields << v['account']
72+
object_fields << ui.color("Domain:", :cyan)
73+
object_fields << v['domain']
74+
object_fields << ui.color("State:", :cyan)
75+
object_fields << v['state']
76+
object_fields << ui.color("VMName:", :cyan)
77+
object_fields << v['vmname']
78+
object_fields << ui.color("VMState:", :cyan)
79+
object_fields << v['vmstate']
80+
81+
puts "\n"
82+
puts ui.list(object_fields, :uneven_columns_across, 2)
83+
puts "\n"
84+
end
85+
86+
def confirm_action(question)
87+
return true if locate_config_value(:yes)
88+
result = ui.ask_question(question, :default => "Y" )
89+
if result == "Y" || result == "y" then
90+
return true
91+
else
92+
return false
93+
end
94+
end
95+
96+
end
97+
end

lib/knife-cloudstack/connection.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,45 @@ def get_disk_offering(name)
468468
nil
469469
end
470470

471+
##
472+
# Finds the volume with the specified name.
473+
#
474+
475+
def get_volume(name)
476+
params = {
477+
'command' => 'listVolumes',
478+
'name' => name
479+
}
480+
json = send_request(params)
481+
volumes = json['volume']
482+
483+
if !volumes || volumes.empty? then
484+
return nil
485+
end
486+
volume = volumes.select { |item| name == item['name'] }
487+
volume.first
488+
end
489+
490+
##
491+
# Deletes the volume with the specified name.
492+
#
493+
494+
def delete_volume(name)
495+
volume = get_volume(name)
496+
if !volume || !volume['id'] then
497+
puts "\nError: Volume '#{name}' does not exist"
498+
exit 1
499+
end
500+
501+
params = {
502+
'command' => 'deleteVolume',
503+
'id' => volume['id']
504+
}
505+
506+
json = send_request(params)
507+
json['success']
508+
end
509+
471510
##
472511
# Lists all templates that match the specified filter.
473512
#

0 commit comments

Comments
 (0)