Skip to content

Commit

Permalink
Add command subnet-list
Browse files Browse the repository at this point in the history
Fix #160
  • Loading branch information
ggiamarchi committed Nov 10, 2014
1 parent 97a9a2c commit f4d87d7
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ Available subcommands:
image-list List available images
flavor-list List available flavors
network-list List private networks in project
subnet-list List subnets for available networks
floatingip-list List floating IP and floating IP pools
volume-list List existing volumes
reset Reset Vagrant OpenStack provider to a clear state
Expand Down
19 changes: 19 additions & 0 deletions source/lib/vagrant-openstack-provider/client/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,25 @@ def state
[@id, @name, @size, @status, @bootable, @instance_id, @device]
end
end

class Subnet < Item
attr_accessor :cidr
attr_accessor :enable_dhcp
attr_accessor :network_id

def initialize(id, name, cidr, enable_dhcp, network_id)
@cidr = cidr
@enable_dhcp = enable_dhcp
@network_id = network_id
super(id, name)
end

protected

def state
[@id, @name, @cidr, @enable_dhcp, @network_id]
end
end
end
end
end
9 changes: 9 additions & 0 deletions source/lib/vagrant-openstack-provider/client/neutron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ def get_all_networks(env)
get_networks(env, true)
end

def get_subnets(env)
subnets_json = get(env, "#{@session.endpoints[:network]}/subnets")
subnets = []
JSON.parse(subnets_json)['subnets'].each do |n|
subnets << Subnet.new(n['id'], n['name'], n['cidr'], n['enable_dhcp'], n['network_id'])
end
subnets
end

private

def get_networks(env, all)
Expand Down
1 change: 1 addition & 0 deletions source/lib/vagrant-openstack-provider/command/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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: :'subnet-list', file: 'subnet_list', clazz: 'SubnetList' },
{ name: :'floatingip-list', file: 'floatingip_list', clazz: 'FloatingIpList' },
{ name: :'volume-list', file: 'volume_list', clazz: 'VolumeList' },
{ name: :'reset', file: 'reset', clazz: 'Reset' }
Expand Down
25 changes: 25 additions & 0 deletions source/lib/vagrant-openstack-provider/command/subnet_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'vagrant-openstack-provider/command/utils'
require 'vagrant-openstack-provider/command/abstract_command'

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

def self.synopsis
I18n.t('vagrant_openstack.command.subnet_list_synopsis')
end

def cmd(name, argv, env)
fail Errors::NoArgRequiredForCommand, cmd: name unless argv.size == 0
rows = []
env[:openstack_client].neutron.get_subnets(env).each do |subnet|
rows << [subnet.id, subnet.name, subnet.cidr, subnet.enable_dhcp, subnet.network_id]
end
display_table(env, ['Id', 'Name', 'CIDR', 'DHCP', 'Network Id'], 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 @@ -284,6 +284,8 @@ en:
List available flavors
network_list_synopsis : |-
List private networks in project
subnet_list_synopsis : |-
List subnets for available networks
flaotingip_list_synopsis : |-
List floating IP and floating IP pools
volume_list_synopsis : |-
Expand Down
32 changes: 32 additions & 0 deletions source/spec/vagrant-openstack-provider/client/neutron_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,38 @@
end
end

describe 'get_subnets' do
context 'with token' do
it 'returns all available subnets', :focus do

stub_request(:get, 'http://neutron/subnets')
.with(
headers:
{
'Accept' => 'application/json',
'X-Auth-Token' => '123456'
})
.to_return(
status: 200,
body: '
{
"subnets": [
{ "id": "subnet-01", "name": "Subnet 1", "cidr": "192.168.1.0/24", "enable_dhcp": true, "network_id": "net-01" },
{ "id": "subnet-02", "name": "Subnet 2", "cidr": "192.168.2.0/24", "enable_dhcp": false, "network_id": "net-01" },
{ "id": "subnet-03", "name": "Subnet 3", "cidr": "192.168.100.0/24", "enable_dhcp": true, "network_id": "net-02" }
]
}
')

networks = @neutron_client.get_subnets(env)

expect(networks).to eq [Subnet.new('subnet-01', 'Subnet 1', '192.168.1.0/24', true, 'net-01'),
Subnet.new('subnet-02', 'Subnet 2', '192.168.2.0/24', false, 'net-01'),
Subnet.new('subnet-03', 'Subnet 3', '192.168.100.0/24', true, 'net-02')]
end
end
end

describe 'get_api_version_list' do
context 'basic' do
it 'returns version list' do
Expand Down
46 changes: 46 additions & 0 deletions source/spec/vagrant-openstack-provider/command/subnet_list_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'vagrant-openstack-provider/spec_helper'

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

let(:neutron) do
double('neutron').tap do |neutron|
neutron.stub(:get_subnets) do
[
Subnet.new('subnet-01', 'Subnet 1', '192.168.1.0/24', true, 'net-01'),
Subnet.new('subnet-02', 'Subnet 2', '192.168.2.0/24', false, 'net-01'),
Subnet.new('subnet-03', 'Subnet 3', '192.168.100.0/24', true, 'net-02')
]
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(:neutron) { neutron }
end
end

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

it 'prints subnet list from server' do
neutron.should_receive(:get_subnets).with(env)

expect(env[:ui]).to receive(:info).with('
+-----------+----------+------------------+-------+------------+
| Id | Name | CIDR | DHCP | Network Id |
+-----------+----------+------------------+-------+------------+
| subnet-01 | Subnet 1 | 192.168.1.0/24 | true | net-01 |
| subnet-02 | Subnet 2 | 192.168.2.0/24 | false | net-01 |
| subnet-03 | Subnet 3 | 192.168.100.0/24 | true | net-02 |
+-----------+----------+------------------+-------+------------+')

@subnet_list_cmd.cmd('subnet-list', [], env)
end
end
end

0 comments on commit f4d87d7

Please sign in to comment.