40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/chef/knife/cosmic_volume_attach.rb', line 40
def run
validate_base_options
volumename = locate_config_value(:volume) || @name_args[0]
exit_with_error 'Invalid volume name.' unless valid_cosmic_name? volumename
vmname = locate_config_value(:vm) || @name_args[1]
exit_with_error 'Invalid virtual machine.' unless valid_cosmic_name? vmname
volume = connection.get_volume(volumename)
exit_with_error "Volume #{volumename} not found." unless volume && volume['id']
exit_with_error "Volume #{volumename} is currently attached." if volume['vmname']
vm = connection.get_server(vmname)
exit_with_error "Virtual machine #{vmname} not found." unless vm && vm['id']
puts ui.color("Attaching volume #{volumename} to #{vmname}", :magenta)
params = {
'command' => 'attachVolume',
'id' => volume['id'],
'virtualmachineid' => vm['id']
}
json = connection.send_async_request(params)
exit_with_error 'Unable to attach volume' unless json
puts "Volume #{volumename} is now attached to #{json['volume']['vmname']}"
end
|