diff --git a/README.md b/README.md index ea05f7a..c59434a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/source/lib/vagrant-openstack-provider/client/domain.rb b/source/lib/vagrant-openstack-provider/client/domain.rb index 77ba616..189f105 100644 --- a/source/lib/vagrant-openstack-provider/client/domain.rb +++ b/source/lib/vagrant-openstack-provider/client/domain.rb @@ -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 diff --git a/source/lib/vagrant-openstack-provider/client/neutron.rb b/source/lib/vagrant-openstack-provider/client/neutron.rb index da66894..1abdb39 100644 --- a/source/lib/vagrant-openstack-provider/client/neutron.rb +++ b/source/lib/vagrant-openstack-provider/client/neutron.rb @@ -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) diff --git a/source/lib/vagrant-openstack-provider/command/main.rb b/source/lib/vagrant-openstack-provider/command/main.rb index a0391d4..4c650d6 100644 --- a/source/lib/vagrant-openstack-provider/command/main.rb +++ b/source/lib/vagrant-openstack-provider/command/main.rb @@ -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' } diff --git a/source/lib/vagrant-openstack-provider/command/subnet_list.rb b/source/lib/vagrant-openstack-provider/command/subnet_list.rb new file mode 100644 index 0000000..7caa053 --- /dev/null +++ b/source/lib/vagrant-openstack-provider/command/subnet_list.rb @@ -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 diff --git a/source/locales/en.yml b/source/locales/en.yml index 2dc4ce7..5fbcc5d 100644 --- a/source/locales/en.yml +++ b/source/locales/en.yml @@ -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 : |- diff --git a/source/spec/vagrant-openstack-provider/client/neutron_spec.rb b/source/spec/vagrant-openstack-provider/client/neutron_spec.rb index eb03ed6..0b41a83 100644 --- a/source/spec/vagrant-openstack-provider/client/neutron_spec.rb +++ b/source/spec/vagrant-openstack-provider/client/neutron_spec.rb @@ -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 diff --git a/source/spec/vagrant-openstack-provider/command/subnet_list_spec.rb b/source/spec/vagrant-openstack-provider/command/subnet_list_spec.rb new file mode 100644 index 0000000..b32864e --- /dev/null +++ b/source/spec/vagrant-openstack-provider/command/subnet_list_spec.rb @@ -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