Skip to content

Commit

Permalink
Add sub-command volume-list
Browse files Browse the repository at this point in the history
issue #24
  • Loading branch information
ggiamarchi committed Sep 13, 2014
1 parent ea1767b commit 833278a
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ Available subcommands:
flavor-list List available flavors
network-list List private networks in project
floatingip-list List floating IP and floating IP pools
volume-list List existing volumes
```

For instance `vagrant openstack image-list` lists images available in Glance.
Expand Down
18 changes: 14 additions & 4 deletions source/lib/vagrant-openstack-provider/client/cinder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,20 @@ def initialize

def get_all_volumes(env)
volumes_json = get(env, "#{@session.endpoints[:volume]}/volumes/detail")
JSON.parse(volumes_json)['volumes'].map do |v|
name = v['display_name']
name = v['name'] if name.nil? # To be compatible with cinder api v1 and v2
Volume.new(v['id'], name, v['size'], v['status'], v['bootable'], v['instance_id'], v['device'])
JSON.parse(volumes_json)['volumes'].map do |volume|
name = volume['display_name']
name = volume['name'] if name.nil? # To be compatible with cinder api v1 and v2
case volume['attachments'].size
when 0
@logger.debug "No attachment found for volume #{volume['id']}"
else
attachment = volume['attachments'][0]
server_id = attachment['server_id']
device = attachment['device']
@logger.warn "Found #{attachment.size} attachments for volume #{volume['id']} : " if attachment.size > 1
@logger.debug "Attachment found for volume #{volume['id']} : #{attachment.to_json}"
end
Volume.new(volume['id'], name, volume['size'], volume['status'], volume['bootable'], server_id, device)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion source/lib/vagrant-openstack-provider/client/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def to_s
bootable: @bootable,
instance_id: @instance_id,
device: @device
}
}.to_json
end

protected
Expand Down
3 changes: 2 additions & 1 deletion source/lib/vagrant-openstack-provider/command/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module Command
{ name: :'image-list', file: 'image_list' , clazz: 'ImageList' },
{ name: :'flavor-list', file: 'flavor_list', clazz: 'FlavorList' },
{ name: :'network-list', file: 'network_list', clazz: 'NetworkList' },
{ name: :'floatingip-list', file: 'floatingip_list', clazz: 'FloatingIpList' }
{ name: :'floatingip-list', file: 'floatingip_list', clazz: 'FloatingIpList' },
{ name: :'volume-list', file: 'volume_list', clazz: 'VolumeList' }
]

class Main < Vagrant.plugin('2', :command)
Expand Down
27 changes: 27 additions & 0 deletions source/lib/vagrant-openstack-provider/command/volume_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'vagrant-openstack-provider/command/utils'
require 'vagrant-openstack-provider/command/abstract_command'

module VagrantPlugins
module Openstack
module Command
class VolumeList < AbstractCommand
include VagrantPlugins::Openstack::Command::Utils

def self.synopsis
I18n.t('vagrant_openstack.command.volume_list_synopsis')
end
def cmd(name, argv, env)
fail Errors::NoArgRequiredForCommand, cmd: name unless argv.size == 0 || argv == ['--']
volumes = env[:openstack_client].cinder.get_all_volumes(env)

rows = []
volumes.each do |v|
attachment = "#{v.instance_id} (#{v.device})" unless v.instance_id.nil?
rows << [v.id, v.name, v.size, v.status, attachment]
end
display_table(env, ['Id', 'Name', 'Size (Go)', 'Status', 'Attachment (instance id and device)'], rows)
end
end
end
end
end
2 changes: 2 additions & 0 deletions source/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,5 @@ en:
List private networks in project
flaotingip_list_synopsis : |-
List floating IP and floating IP pools
volume_list_synopsis : |-
List existing volumes
29 changes: 17 additions & 12 deletions source/spec/vagrant-openstack-provider/client/cinder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,28 @@
"size": "2",
"status": "available",
"bootable": "true",
"instance_id": null,
"device": "/dev/vdb"
"attachments": []
},
{
"id": "654",
"display_name": "vol-02",
"size": "4",
"status": "in-use",
"bootable": "false",
"instance_id": "inst-01",
"device": "/dev/vdc"
"attachments": [
{
"server_id": "inst-01",
"device": "/dev/vdc"
}
]
}
]
}
')

volumes = @cinder_client.get_all_volumes(env)

expect(volumes).to eq [Volume.new('987', 'vol-01', '2', 'available', 'true', nil, '/dev/vdb'),
expect(volumes).to eq [Volume.new('987', 'vol-01', '2', 'available', 'true', nil, nil),
Volume.new('654', 'vol-02', '4', 'in-use', 'false', 'inst-01', '/dev/vdc')]
end
end
Expand All @@ -83,25 +86,27 @@
"size": "2",
"status": "available",
"bootable": "true",
"instance_id": null,
"device": "/dev/vdb"
"attachments": []
},
{
"id": "654",
"name": "vol-02",
"size": "4",
"status": "in-use",
"bootable": "false",
"instance_id": "inst-01",
"device": "/dev/vdc"
"attachments": [
{
"server_id": "inst-01",
"device": "/dev/vdc"
}
]
}
]
}
')
}')

volumes = @cinder_client.get_all_volumes(env)

expect(volumes).to eq [Volume.new('987', 'vol-01', '2', 'available', 'true', nil, '/dev/vdb'),
expect(volumes).to eq [Volume.new('987', 'vol-01', '2', 'available', 'true', nil, nil),
Volume.new('654', 'vol-02', '4', 'in-use', 'false', 'inst-01', '/dev/vdc')]
end
end
Expand Down
33 changes: 33 additions & 0 deletions source/spec/vagrant-openstack-provider/command/volume_list_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'vagrant-openstack-provider/spec_helper'

describe VagrantPlugins::Openstack::Command::FloatingIpList do
describe 'cmd' do

let(:cinder) do
double('cinder').tap do |cinder|
cinder.stub(:get_all_volumes) do
[Volume.new('987', 'vol-01', '2', 'available', 'true', nil, nil),
Volume.new('654', 'vol-02', '4', 'in-use', 'false', 'inst-01', '/dev/vdc')]
end
end
end

let(:env) do
Hash.new.tap do |env|
env[:ui] = double('ui')
env[:ui].stub(:info).with(anything)
env[:openstack_client] = double
env[:openstack_client].stub(:cinder) { cinder }
end
end

before :each do
@volume_list_cmd = VagrantPlugins::Openstack::Command::VolumeList.new(nil, env)
end

it 'should get volumes list from server' do
cinder.should_receive(:get_all_volumes).with(env)
@volume_list_cmd.cmd('volume-list', [], env)
end
end
end

0 comments on commit 833278a

Please sign in to comment.