|
| 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 |
0 commit comments