Merge "Use openstack cli to manage neutron subnets"

This commit is contained in:
Zuul 2021-11-29 16:54:17 +00:00 committed by Gerrit Code Review
commit 1b87565f2a
5 changed files with 608 additions and 515 deletions

View File

@ -170,6 +170,16 @@ class Puppet::Provider::Neutron < Puppet::Provider::Openstack
@neutron_credentials = nil
end
def self.get_network_name(id)
network = self.request('network', 'show', [id])
return network[:name]
end
def self.get_subnet_name(id)
subnet = self.request('subnet', 'show', [id])
return subnet[:name]
end
def self.list_neutron_resources(type)
ids = []
list = cleanup_csv_with_id(auth_neutron("#{type}-list", '--format=csv',

View File

@ -1,280 +0,0 @@
require 'json'
require File.join(File.dirname(__FILE__), '..','..','..',
'puppet/provider/neutron')
Puppet::Type.type(:neutron_subnet).provide(
:neutron,
:parent => Puppet::Provider::Neutron
) do
desc <<-EOT
Neutron provider to manage neutron_subnet type.
Assumes that the neutron service is configured on the same host.
EOT
mk_resource_methods
def self.neutron_type
'subnet'
end
def self.do_not_manage
@do_not_manage
end
def self.do_not_manage=(value)
@do_not_manage = value
end
def self.instances
self.do_not_manage = true
list = list_neutron_resources(neutron_type).collect do |id|
attrs = get_neutron_resource_attrs(neutron_type, id)
new(
:ensure => :present,
:name => attrs['name'],
:id => attrs['id'],
:cidr => attrs['cidr'],
:ip_version => attrs['ip_version'],
:ipv6_ra_mode => attrs['ipv6_ra_mode'],
:ipv6_address_mode => attrs['ipv6_address_mode'],
:gateway_ip => parse_gateway_ip(attrs['gateway_ip']),
:allocation_pools => parse_allocation_pool(attrs['allocation_pools']),
:host_routes => parse_host_routes(attrs['host_routes']),
:dns_nameservers => parse_dns_nameservers(attrs['dns_nameservers']),
:enable_dhcp => attrs['enable_dhcp'],
:network_id => attrs['network_id'],
:tenant_id => attrs['tenant_id']
)
end
self.do_not_manage = false
list
end
def self.prefetch(resources)
subnets = instances
resources.keys.each do |name|
if provider = subnets.find{ |subnet| subnet.name == name }
resources[name].provider = provider
end
end
end
def self.parse_gateway_ip(value)
return '' if value.nil?
return value
end
def self.parse_allocation_pool(values)
allocation_pools = []
return [] if values.empty? or values == '[]'
for value in Array(values)
allocation_pool = JSON.parse(value.gsub(/\\"/,'"').gsub('u\'', '"')
.gsub('\'','"').gsub('[', '')
.gsub(']', ''))
start_ip = allocation_pool['start']
end_ip = allocation_pool['end']
allocation_pools << "start=#{start_ip},end=#{end_ip}"
end
return allocation_pools
end
def self.parse_host_routes(values)
host_routes = []
return [] if values.empty? or values == '[]'
# Strip brackets from output, needed after neutronclient >= 6.1.0
values = values.gsub('[', '').gsub(']', '')
for value in Array(values)
host_route = JSON.parse(value.gsub(/\\"/,'"').gsub('u\'', '"')
.gsub('\'','"'))
nexthop = host_route['nexthop']
destination = host_route['destination']
host_routes << "destination=#{destination},nexthop=#{nexthop}"
end
return host_routes
end
def self.parse_dns_nameservers(values)
# With neutronclient >= 6.1.0 we need to parse the string a bit
if values.is_a? String
values = values.gsub('u\'', '').gsub('\'','').gsub('[', '')
.gsub(']', '').gsub(',', '').split(' ')
end
# just enforce that this is actually an array
return Array(values)
end
def exists?
@property_hash[:ensure] == :present
end
def create
if self.class.do_not_manage
fail("Not managing Neutron_subnet[#{@resource[:name]}] due to earlier Neutron API failures.")
end
opts = ["--name=#{@resource[:name]}"]
if @resource[:ip_version]
opts << "--ip-version=#{@resource[:ip_version]}"
end
if @resource[:ipv6_ra_mode]
opts << "--ipv6-ra-mode=#{@resource[:ipv6_ra_mode]}"
end
if @resource[:ipv6_address_mode]
opts << "--ipv6-address-mode=#{@resource[:ipv6_address_mode]}"
end
if @resource[:gateway_ip]
if @resource[:gateway_ip] == ''
opts << '--no-gateway'
else
opts << "--gateway-ip=#{@resource[:gateway_ip]}"
end
end
if @resource[:enable_dhcp] == 'False'
opts << "--disable-dhcp"
else
opts << "--enable-dhcp"
end
if @resource[:allocation_pools]
Array(@resource[:allocation_pools]).each do |allocation_pool|
opts << "--allocation-pool=#{allocation_pool}"
end
end
if @resource[:dns_nameservers]
Array(@resource[:dns_nameservers]).each do |nameserver|
opts << "--dns-nameserver=#{nameserver}"
end
end
if @resource[:host_routes]
Array(@resource[:host_routes]).each do |host_route|
opts << "--host-route=#{host_route}"
end
end
if @resource[:tenant_name]
tenant_id = self.class.get_tenant_id(@resource.catalog,
@resource[:tenant_name])
opts << "--tenant_id=#{tenant_id}"
elsif @resource[:tenant_id]
opts << "--tenant_id=#{@resource[:tenant_id]}"
end
if @resource[:network_name]
opts << resource[:network_name]
elsif @resource[:network_id]
opts << resource[:network_id]
end
results = auth_neutron('subnet-create', '--format=shell',
opts, resource[:cidr])
attrs = self.class.parse_creation_output(results)
@property_hash = {
:ensure => :present,
:name => resource[:name],
:id => attrs['id'],
:cidr => attrs['cidr'],
:ip_version => attrs['ip_version'],
:ipv6_ra_mode => attrs['ipv6_ra_mode'],
:ipv6_address_mode => attrs['ipv6_address_mode'],
:gateway_ip => self.class.parse_gateway_ip(attrs['gateway_ip']),
:allocation_pools => self.class.parse_allocation_pool(attrs['allocation_pools']),
:host_routes => self.class.parse_host_routes(attrs['host_routes']),
:dns_nameservers => self.class.parse_dns_nameservers(attrs['dns_nameservers']),
:enable_dhcp => attrs['enable_dhcp'],
:network_id => attrs['network_id'],
:tenant_id => attrs['tenant_id'],
}
end
def destroy
if self.class.do_not_manage
fail("Not managing Neutron_subnet[#{@resource[:name]}] due to earlier Neutron API failures.")
end
auth_neutron('subnet-delete', name)
@property_hash[:ensure] = :absent
end
def gateway_ip=(value)
if self.class.do_not_manage
fail("Not managing Neutron_subnet[#{@resource[:name]}] due to earlier Neutron API failures.")
end
if value == ''
auth_neutron('subnet-update', '--no-gateway', name)
else
auth_neutron('subnet-update', "--gateway-ip=#{value}", name)
end
end
def enable_dhcp=(value)
if self.class.do_not_manage
fail("Not managing Neutron_subnet[#{@resource[:name]}] due to earlier Neutron API failures.")
end
if value == 'False'
auth_neutron('subnet-update', "--disable-dhcp", name)
else
auth_neutron('subnet-update', "--enable-dhcp", name)
end
end
def allocation_pools=(values)
if self.class.do_not_manage
fail("Not managing Neutron_subnet[#{@resource[:name]}] due to earlier Neutron API failures.")
end
unless values.empty?
opts = ["#{name}", "--allocation-pool"]
for value in values
opts << value
end
auth_neutron('subnet-update', opts)
end
end
def dns_nameservers=(values)
if self.class.do_not_manage
fail("Not managing Neutron_subnet[#{@resource[:name]}] due to earlier Neutron API failures.")
end
unless values.empty?
opts = ["#{name}", "--dns-nameservers", "list=true"]
for value in values
opts << value
end
auth_neutron('subnet-update', opts)
end
end
def host_routes=(values)
if self.class.do_not_manage
fail("Not managing Neutron_subnet[#{@resource[:name]}] due to earlier Neutron API failures.")
end
unless values.empty?
opts = ["#{name}", "--host-routes", "type=dict", "list=true"]
for value in values
opts << value
end
auth_neutron('subnet-update', opts)
end
end
[
:cidr,
:ip_version,
:ipv6_ra_mode,
:ipv6_address_mode,
:network_id,
:tenant_id,
].each do |attr|
define_method(attr.to_s + "=") do |value|
fail("Property #{attr.to_s} does not support being updated")
end
end
end

View File

@ -0,0 +1,286 @@
require File.join(File.dirname(__FILE__), '..','..','..',
'puppet/provider/neutron')
Puppet::Type.type(:neutron_subnet).provide(
:openstack,
:parent => Puppet::Provider::Neutron
) do
desc <<-EOT
Neutron provider to manage neutron_subnet type.
Assumes that the neutron service is configured on the same host.
EOT
@credentials = Puppet::Provider::Openstack::CredentialsV3.new
mk_resource_methods
def initialize(value={})
super(value)
@property_flush = {}
end
def self.do_not_manage
@do_not_manage
end
def self.do_not_manage=(value)
@do_not_manage = value
end
def self.instances
self.do_not_manage = true
list = request('subnet', 'list').collect do |attrs|
subnet = request('subnet', 'show', attrs[:id])
new(
:ensure => :present,
:name => attrs[:name],
:id => attrs[:id],
:cidr => subnet[:cidr],
:ip_version => subnet[:ip_version],
:ipv6_ra_mode => subnet[:ipv6_ra_mode],
:ipv6_address_mode => subnet[:ipv6_address_mode],
:gateway_ip => parse_gateway_ip(subnet[:gateway_ip]),
:allocation_pools => parse_allocation_pool(subnet[:allocation_pools]),
:host_routes => parse_host_routes(subnet[:host_routes]),
:dns_nameservers => parse_dns_nameservers(subnet[:dns_nameservers]),
:enable_dhcp => subnet[:enable_dhcp],
:network_id => subnet[:network_id],
:network_name => get_network_name(subnet[:network_id]),
:tenant_id => subnet[:project_id],
)
end
self.do_not_manage = false
list
end
def self.prefetch(resources)
subnets = instances
resources.keys.each do |name|
if provider = subnets.find{ |subnet| subnet.name == name }
resources[name].provider = provider
end
end
end
def self.parse_gateway_ip(value)
return '' if value.nil?
return value
end
def self.parse_allocation_pool(values)
allocation_pools = []
return [] if values.empty? or values == '[]'
values = values.gsub('[', '').gsub(']', '')
for value in Array(values)
allocation_pool = JSON.parse(value.gsub(/\\"/,'"').gsub('u\'', '"')
.gsub('\'','"'))
start_ip = allocation_pool['start']
end_ip = allocation_pool['end']
allocation_pools << "start=#{start_ip},end=#{end_ip}"
end
return allocation_pools
end
def self.parse_host_routes(values)
host_routes = []
return [] if values.empty? or values == '[]'
values = values.gsub('[', '').gsub(']', '')
for value in Array(values)
host_route = JSON.parse(value.gsub(/\\"/,'"').gsub('u\'', '"')
.gsub('\'','"'))
nexthop = host_route['nexthop']
destination = host_route['destination']
host_routes << "destination=#{destination},nexthop=#{nexthop}"
end
return host_routes
end
def self.parse_dns_nameservers(values)
if values.is_a? String
values = values.gsub('u\'', '').gsub('\'','').gsub('[', '')
.gsub(']', '').gsub(',', '').split(' ')
end
# just enforce that this is actually an array
return Array(values)
end
def exists?
@property_hash[:ensure] == :present
end
def create
if self.class.do_not_manage
fail("Not managing Neutron_subnet[#{@resource[:name]}] due to earlier Neutron API failures.")
end
opts = [@resource[:name]]
if @resource[:ip_version]
opts << "--ip-version=#{@resource[:ip_version]}"
end
if @resource[:ipv6_ra_mode]
opts << "--ipv6-ra-mode=#{@resource[:ipv6_ra_mode]}"
end
if @resource[:ipv6_address_mode]
opts << "--ipv6-address-mode=#{@resource[:ipv6_address_mode]}"
end
if @resource[:gateway_ip]
if @resource[:gateway_ip] == ''
opts << '--gateway=none'
else
opts << "--gateway=#{@resource[:gateway_ip]}"
end
end
if @resource[:enable_dhcp] == 'False'
opts << "--no-dhcp"
else
opts << "--dhcp"
end
if @resource[:allocation_pools]
Array(@resource[:allocation_pools]).each do |allocation_pool|
opts << "--allocation-pool=#{allocation_pool}"
end
end
if @resource[:dns_nameservers]
Array(@resource[:dns_nameservers]).each do |nameserver|
opts << "--dns-nameserver=#{nameserver}"
end
end
if @resource[:host_routes]
Array(@resource[:host_routes]).each do |host_route|
opts << "--host-route=#{host_route}"
end
end
if @resource[:tenant_name]
opts << "--project=#{@resource[:tenant_name]}"
elsif @resource[:tenant_id]
opts << "--project=#{@resource[:tenant_id]}"
end
if @resource[:network_name]
opts << "--network=#{@resource[:network_name]}"
elsif @resource[:network_id]
opts << "--network=#{@resource[:network_id]}"
end
opts << "--subnet-range=#{@resource[:cidr]}"
subnet = self.class.request('subnet', 'create', opts)
@property_hash = {
:ensure => :present,
:name => subnet[:name],
:id => subnet[:id],
:cidr => subnet[:cidr],
:ip_version => subnet[:ip_version],
:ipv6_ra_mode => subnet[:ipv6_ra_mode],
:ipv6_address_mode => subnet[:ipv6_address_mode],
:gateway_ip => self.class.parse_gateway_ip(subnet[:gateway_ip]),
:allocation_pools => self.class.parse_allocation_pool(subnet[:allocation_pools]),
:host_routes => self.class.parse_host_routes(subnet[:host_routes]),
:dns_nameservers => self.class.parse_dns_nameservers(subnet[:dns_nameservers]),
:enable_dhcp => subnet[:enable_dhcp],
:network_id => subnet[:network_id],
:network_name => self.class.get_network_name(subnet[:network_id]),
:tenant_id => subnet[:project_id],
}
end
def flush
if !@property_flush.empty?
opts = [@resource[:name]]
clear_opts = [@resource[:name]]
if @property_flush.has_key?(:gateway_ip)
if @property_flush[:gateway_ip] == ''
opts << '--gateway=none'
else
opts << "--gateway=#{@property_flush[:gateway_ip]}"
end
end
if @property_flush.has_key?(:enable_dhcp)
if @property_flush[:enable_dhcp] == 'False'
opts << '--no-dhcp'
else
opts << '--dhcp'
end
end
if @property_flush.has_key?(:allocation_pools)
clear_opts << '--no-allocation-pool'
Array(@property_flush[:allocation_pools]).each do |allocation_pool|
opts << "--allocation-pool=#{allocation_pool}"
end
end
if @property_flush.has_key?(:dns_nameservers)
clear_opts << '--no-dns-nameservers'
Array(@property_flush[:dns_nameservers]).each do |nameserver|
opts << "--dns-nameserver=#{nameserver}"
end
end
if @property_flush.has_key?(:host_routes)
clear_opts << '--no-host-route'
Array(@property_flush[:host_routes]).each do |host_route|
opts << "--host-route=#{host_route}"
end
end
if clear_opts.length > 1
self.class.request('subnet', 'set', clear_opts)
end
if opts.length > 1
self.class.request('subnet', 'set', opts)
end
@property_flush.clear
end
end
def destroy
if self.class.do_not_manage
fail("Not managing Neutron_subnet[#{@resource[:name]}] due to earlier Neutron API failures.")
end
self.class.request('subnet', 'delete', @resource[:name])
@property_hash.clear
@property_hash[:ensure] = :absent
end
[
:gateway_ip,
:enable_dhcp,
:allocation_pools,
:dns_nameservers,
:host_routes,
].each do |attr|
define_method(attr.to_s + "=") do |value|
if self.class.do_not_manage
fail("Not managing Neutron_network[#{@resource[:name]}] due to earlier Neutron API failures.")
end
@property_flush[attr] = value
end
end
[
:cidr,
:ip_version,
:ipv6_ra_mode,
:ipv6_address_mode,
:network_id,
:tenant_id,
].each do |attr|
define_method(attr.to_s + "=") do |value|
fail("Property #{attr.to_s} does not support being updated")
end
end
end

View File

@ -1,235 +0,0 @@
require 'puppet'
require 'spec_helper'
require 'puppet/provider/neutron_subnet/neutron'
provider_class = Puppet::Type.type(:neutron_subnet).provider(:neutron)
describe provider_class do
let :subnet_name do
'net1'
end
let :subnet_v6_name do
'net2'
end
let :subnet_attrs do
{
:name => subnet_name,
:ensure => 'present',
:cidr => '10.0.0.0/24',
:ip_version => '4',
:gateway_ip => '10.0.0.1',
:enable_dhcp => 'False',
:network_name => 'net1',
:tenant_id => '60f9544eb94c42a6b7e8e98c2be981b1',
:allocation_pools => 'start=10.0.0.2,end=10.0.0.10',
:dns_nameservers => '8.8.8.8',
:host_routes => 'destination=12.0.0.0/24,nexthop=10.0.0.1',
}
end
let :subnet_v6_attrs do
{
:name => subnet_v6_name,
:ensure => 'present',
:cidr => '2001:abcd::/64',
:ip_version => '6',
:gateway_ip => '2001:abcd::1',
:enable_dhcp => 'False',
:network_name => 'net2',
:tenant_id => '60f9544eb94c42a6b7e8e98c2be981b1',
:allocation_pools => 'start=2001:abcd::2,end=2001:abcd::ffff:ffff:ffff:fffe',
:dns_nameservers => '2001:4860:4860::8888',
:host_routes => 'destination=2001:abcd:0:1::/64,nexthop=2001:abcd::1',
}
end
let :resource do
Puppet::Type::Neutron_subnet.new(subnet_attrs)
end
let :provider do
provider_class.new(resource)
end
describe 'when creating a subnet' do
it 'should call subnet-create with appropriate command line options' do
provider.class.stubs(:get_tenant_id).returns(subnet_attrs[:tenant_id])
output = 'Created a new subnet:
allocation_pools="{\"start\": \"10.0.0.2\", \"end\": \"10.0.0.10\"}"
cidr="10.0.0.0/24"
dns_nameservers="8.8.8.8"
enable_dhcp="False"
gateway_ip="10.0.0.1"
host_routes="{\"nexthop\": \"10.0.0.1\", \"destination\": \"12.0.0.0/24\"}"
id="dd5e0ef1-2c88-4b0b-ba08-7df65be87963"
ip_version="4"
name="net1"
network_id="98873773-aa34-4b87-af05-70903659246f"
tenant_id="60f9544eb94c42a6b7e8e98c2be981b1"'
provider.expects(:auth_neutron).with('subnet-create', '--format=shell',
["--name=#{subnet_attrs[:name]}",
"--ip-version=#{subnet_attrs[:ip_version]}",
"--gateway-ip=#{subnet_attrs[:gateway_ip]}",
"--disable-dhcp",
"--allocation-pool=#{subnet_attrs[:allocation_pools]}",
"--dns-nameserver=#{subnet_attrs[:dns_nameservers]}",
"--host-route=#{subnet_attrs[:host_routes]}",
"--tenant_id=#{subnet_attrs[:tenant_id]}",
subnet_name],
subnet_attrs[:cidr]).returns(output)
provider.create
end
end
describe 'when creating a subnet with neutronclient >= 6.1.0' do
it 'should call subnet-create with appropriate command line options' do
provider.class.stubs(:get_tenant_id).returns(subnet_attrs[:tenant_id])
output = 'Created a new subnet:
allocation_pools="[{u\'start\': u\'10.0.0.2\', u\'end\': u\'10.0.0.10\'}]"
cidr="10.0.0.0/24"
dns_nameservers="[u\'8.8.8.8\']"
enable_dhcp="False"
gateway_ip="10.0.0.1"
host_routes="[{u\'destination\': u\'12.0.0.0/24\', u\'nexthop\': u\'10.0.0.1\'}]"
id="dd5e0ef1-2c88-4b0b-ba08-7df65be87963"
ip_version="4"
name="net1"
network_id="98873773-aa34-4b87-af05-70903659246f"
tenant_id="60f9544eb94c42a6b7e8e98c2be981b1"'
provider.expects(:auth_neutron).with('subnet-create', '--format=shell',
["--name=#{subnet_attrs[:name]}",
"--ip-version=#{subnet_attrs[:ip_version]}",
"--gateway-ip=#{subnet_attrs[:gateway_ip]}",
"--disable-dhcp",
"--allocation-pool=#{subnet_attrs[:allocation_pools]}",
"--dns-nameserver=#{subnet_attrs[:dns_nameservers]}",
"--host-route=#{subnet_attrs[:host_routes]}",
"--tenant_id=#{subnet_attrs[:tenant_id]}",
subnet_name],
subnet_attrs[:cidr]).returns(output)
provider.create
end
end
describe 'when creating a ipv6 subnet' do
let :resource do
Puppet::Type::Neutron_subnet.new(subnet_v6_attrs)
end
let :provider do
provider_class.new(resource)
end
it 'should call subnet-create with appropriate command line options' do
provider.class.stubs(:get_tenant_id).returns(subnet_v6_attrs[:tenant_id])
output = 'Created a new subnet:
allocation_pools="{\"start\": \"2001:abcd::2\", \"end\": \"2001:abcd::ffff:ffff:ffff:fffe\"}"
cidr="2001:abcd::/64"
dns_nameservers="2001:4860:4860::8888"
enable_dhcp="False"
gateway_ip="2001:abcd::1"
host_routes="{\"nexthop\": \"2001:abcd::1\", \"destination\": \"2001:abcd:0:1::/64\"}"
id="dd5e0ef1-2c88-4b0b-ba08-7df65be87963"
ip_version="6"
name="net1"
network_id="98873773-aa34-4b87-af05-70903659246f"
tenant_id="60f9544eb94c42a6b7e8e98c2be981b1"'
provider.expects(:auth_neutron).with('subnet-create', '--format=shell',
["--name=#{subnet_v6_attrs[:name]}",
"--ip-version=#{subnet_v6_attrs[:ip_version]}",
"--gateway-ip=#{subnet_v6_attrs[:gateway_ip]}",
"--disable-dhcp",
"--allocation-pool=#{subnet_v6_attrs[:allocation_pools]}",
"--dns-nameserver=#{subnet_v6_attrs[:dns_nameservers]}",
"--host-route=#{subnet_v6_attrs[:host_routes]}",
"--tenant_id=#{subnet_v6_attrs[:tenant_id]}",
subnet_v6_name],
subnet_v6_attrs[:cidr]).returns(output)
provider.create
end
end
describe 'when updating a subnet' do
it 'should call subnet-update to change gateway_ip' do
provider.expects(:auth_neutron).with('subnet-update',
'--gateway-ip=10.0.0.2',
subnet_name)
provider.gateway_ip=('10.0.0.2')
end
it 'should call subnet-update to remove gateway_ip with empty string' do
provider.expects(:auth_neutron).with('subnet-update',
'--no-gateway',
subnet_name)
provider.gateway_ip=('')
end
it 'should call subnet-update to change enable_dhcp' do
provider.expects(:auth_neutron).with('subnet-update',
'--enable-dhcp',
subnet_name)
provider.enable_dhcp=('True')
end
it 'should call subnet-update to change dns_nameservers' do
provider.expects(:auth_neutron).with('subnet-update',
[subnet_name,
'--dns-nameservers',
'list=true',
'9.9.9.9'])
provider.dns_nameservers=(['9.9.9.9'])
end
it 'should call subnet-update to change host_routes' do
provider.expects(:auth_neutron).with('subnet-update',
[subnet_name,
'--host-routes',
'type=dict',
'list=true',
'destination=12.0.0.0/24,nexthop=10.0.0.2'])
provider.host_routes=(['destination=12.0.0.0/24,nexthop=10.0.0.2'])
end
it 'should not update if dns_nameservers are empty' do
provider.dns_nameservers=('')
end
it 'should not update if host_routes are empty' do
provider.host_routes=('')
end
end
describe 'when updating a subnet (reverse)' do
let :subnet_attrs_mod do
subnet_attrs.merge!({:enable_dhcp => 'True'})
end
let :resource do
Puppet::Type::Neutron_subnet.new(subnet_attrs_mod)
end
let :provider do
provider_class.new(resource)
end
it 'should call subnet-update to change enable_dhcp' do
provider.expects(:auth_neutron).with('subnet-update',
'--disable-dhcp',
subnet_name)
provider.enable_dhcp=('False')
end
end
end

View File

@ -0,0 +1,312 @@
require 'puppet'
require 'spec_helper'
require 'puppet/provider/neutron_subnet/openstack'
provider_class = Puppet::Type.type(:neutron_subnet).provider(:openstack)
describe provider_class do
let(:set_env) do
ENV['OS_USERNAME'] = 'test'
ENV['OS_PASSWORD'] = 'abc123'
ENV['OS_PROJECT_NAME'] = 'test'
ENV['OS_AUTH_URL'] = 'http://127.0.0.1:5000'
end
describe 'manage subnets' do
let :subnet_name do
'subnet1'
end
let :subnet_attrs do
{
:ensure => 'present',
:name => subnet_name,
:network_name => 'net1',
:cidr => '10.0.0.0/24',
}
end
let :resource do
Puppet::Type::Neutron_subnet.new(subnet_attrs)
end
let :provider do
provider_class.new(resource)
end
before :each do
set_env
end
describe '#create' do
context 'with defaults' do
it 'creates subnet' do
provider_class.expects(:openstack)
.with('subnet', 'create', '--format', 'shell',
['subnet1', '--dhcp', '--network=net1',
'--subnet-range=10.0.0.0/24'])
.returns('allocation_pools="[{\'start\': \'10.0.0.2\', \'end\': \'10.0.0.254\'}]"
cidr="10.0.0.0/24"
description=""
dns_nameservers="[]"
enable_dhcp="True"
host_routes="[]"
id="dd5e0ef1-2c88-4b0b-ba08-7df65be87963"
ip_version="4"
ipv6_address_mode="None"
ipv6_ra_mode="None"
name="subnet1"
network_id="076520cc-b783-4cf5-a4a9-4cb5a5e93a9b"
project_id="60f9544eb94c42a6b7e8e98c2be981b1"')
provider_class.expects(:openstack)
.with('network', 'show', '--format', 'shell',
['076520cc-b783-4cf5-a4a9-4cb5a5e93a9b'])
.returns('id="076520cc-b783-4cf5-a4a9-4cb5a5e93a9b"
name="net1"')
provider.create
expect(provider.exists?).to be_truthy
expect(provider.allocation_pools).to eq(['start=10.0.0.2,end=10.0.0.254'])
expect(provider.host_routes).to eq([])
expect(provider.network_name).to eq('net1')
end
end
context 'with params' do
let :subnet_attrs do
{
:ensure => 'present',
:name => subnet_name,
:network_name => 'net1',
:cidr => '10.0.0.0/24',
:ip_version => '4',
:gateway_ip => '10.0.0.1',
:enable_dhcp => 'False',
:allocation_pools => 'start=10.0.0.2,end=10.0.0.10',
:dns_nameservers => '8.8.8.8',
:host_routes => 'destination=10.0.1.0/24,nexthop=10.0.0.1',
}
end
it 'creates subnet' do
provider_class.expects(:openstack)
.with('subnet', 'create', '--format', 'shell',
['subnet1', '--ip-version=4',
'--gateway=10.0.0.1', '--no-dhcp',
'--allocation-pool=start=10.0.0.2,end=10.0.0.10',
'--dns-nameserver=8.8.8.8',
'--host-route=destination=10.0.1.0/24,nexthop=10.0.0.1',
'--network=net1',
'--subnet-range=10.0.0.0/24'])
.returns('allocation_pools="[{\'start\': \'10.0.0.2\', \'end\': \'10.0.0.10\'}]"
cidr="10.0.0.0/24"
description=""
dns_nameservers="[\'8.8.8.8\']"
enable_dhcp="False"
host_routes="[{\'destination\': \'10.0.1.0/24\', \'nexthop\': \'10.0.0.1\'}]"
id="dd5e0ef1-2c88-4b0b-ba08-7df65be87963"
ip_version="4"
ipv6_address_mode="None"
ipv6_ra_mode="None"
name="subnet1"
network_id="076520cc-b783-4cf5-a4a9-4cb5a5e93a9b"
project_id="60f9544eb94c42a6b7e8e98c2be981b1"')
provider_class.expects(:openstack)
.with('network', 'show', '--format', 'shell',
['076520cc-b783-4cf5-a4a9-4cb5a5e93a9b'])
.returns('id="076520cc-b783-4cf5-a4a9-4cb5a5e93a9b"
name="net1"')
provider.create
expect(provider.exists?).to be_truthy
expect(provider.allocation_pools).to eq(['start=10.0.0.2,end=10.0.0.10'])
expect(provider.host_routes).to eq(['destination=10.0.1.0/24,nexthop=10.0.0.1'])
expect(provider.network_name).to eq('net1')
end
end
context 'with ipv6' do
let :subnet_attrs do
{
:ensure => 'present',
:name => subnet_name,
:network_name => 'net1',
:cidr => '2001:abcd::/64',
:ip_version => '6',
:gateway_ip => '2001:abcd::1',
:allocation_pools => 'start=2001:abcd::2,end=2001:abcd::ffff:ffff:ffff:fffe',
:dns_nameservers => '2001:4860:4860::8888',
:host_routes => 'destination=2001:abcd:0:1::/64,nexthop=2001:abcd::1',
}
end
it 'creates ipv6 subnet' do
provider_class.expects(:openstack)
.with('subnet', 'create', '--format', 'shell',
['subnet1', '--ip-version=6',
'--gateway=2001:abcd::1', '--dhcp',
'--allocation-pool=start=2001:abcd::2,end=2001:abcd::ffff:ffff:ffff:fffe',
'--dns-nameserver=2001:4860:4860::8888',
'--host-route=destination=2001:abcd:0:1::/64,nexthop=2001:abcd::1',
'--network=net1',
'--subnet-range=2001:abcd::/64'])
.returns('allocation_pools="[{\'start\': \'2001:abcd::2\', \'end\': \'2001:abcd::ffff:ffff:ffff:fffe\'}]"
cird="2001:abcd::/64"
description=""
dns_nameservers="[\'2001:4860:4860::8888\']"
enable_dhcp="True"
host_routes="[{\'destination\': \'2001:abcd:0:1::/64\', \'nexthop\': \'2001:abcd::1\'}]"
id="dd5e0ef1-2c88-4b0b-ba08-7df65be87963"
ip_version="6"
ipv6_address_mode="None"
ipv6_ra_mode="None"
name="subnet1"
network_id="076520cc-b783-4cf5-a4a9-4cb5a5e93a9b"
project_id="60f9544eb94c42a6b7e8e98c2be981b1"')
provider_class.expects(:openstack)
.with('network', 'show', '--format', 'shell',
['076520cc-b783-4cf5-a4a9-4cb5a5e93a9b'])
.returns('id="076520cc-b783-4cf5-a4a9-4cb5a5e93a9b"
name="net1"')
provider.create
expect(provider.exists?).to be_truthy
expect(provider.allocation_pools).to eq(['start=2001:abcd::2,end=2001:abcd::ffff:ffff:ffff:fffe'])
expect(provider.host_routes).to eq(['destination=2001:abcd:0:1::/64,nexthop=2001:abcd::1'])
expect(provider.network_name).to eq('net1')
end
end
end
describe '#destroy' do
it 'removes subnet' do
provider_class.expects(:openstack)
.with('subnet', 'delete', 'subnet1')
provider.destroy
expect(provider.exists?).to be_falsey
end
end
describe '#flush' do
context 'gateway_ip' do
it 'updates subnet' do
provider_class.expects(:openstack)
.with('subnet', 'set', ['subnet1', '--gateway=10.0.0.1'])
provider.gateway_ip = '10.0.0.1'
provider.flush
provider_class.expects(:openstack)
.with('subnet', 'set', ['subnet1', '--gateway=none'])
provider.gateway_ip = ''
provider.flush
end
end
context '.enable_dhcp' do
it 'updates subnet' do
provider_class.expects(:openstack)
.with('subnet', 'set', ['subnet1', '--no-dhcp'])
provider.enable_dhcp = 'False'
provider.flush
provider_class.expects(:openstack)
.with('subnet', 'set', ['subnet1', '--dhcp'])
provider.enable_dhcp = 'True'
provider.flush
end
end
context '.allocation_pools' do
it 'updates subnet' do
provider_class.expects(:openstack)
.with('subnet', 'set', ['subnet1', '--no-allocation-pool'])
provider_class.expects(:openstack)
.with('subnet', 'set', ['subnet1', '--allocation-pool=start=10.0.0.2,end=10.0.0.10'])
provider.allocation_pools = 'start=10.0.0.2,end=10.0.0.10'
provider.flush
end
end
context '.dns_nameservers' do
it 'updates subnet' do
provider_class.expects(:openstack)
.with('subnet', 'set', ['subnet1', '--no-dns-nameservers'])
provider_class.expects(:openstack)
.with('subnet', 'set', ['subnet1', '--dns-nameserver=8.8.8.8'])
provider.dns_nameservers = '8.8.8.8'
provider.flush
end
end
end
describe '#instances' do
it 'lists subnets' do
provider_class.expects(:openstack)
.with('subnet', 'list', '--quiet', '--format', 'csv', [])
.returns('"ID","Name","Network","Subnet"
"dd5e0ef1-2c88-4b0b-ba08-7df65be87963","subnet1","076520cc-b783-4cf5-a4a9-4cb5a5e93a9b","10.0.0.0/24",
"0da7a631-0f8f-4e51-8b1c-7a29d0d4f7b5","subnet2","34e8f42b-89db-4a5b-92db-76ca7073414d","10.0.1.0/24",
')
provider_class.expects(:openstack)
.with('subnet', 'show', '--format', 'shell', 'dd5e0ef1-2c88-4b0b-ba08-7df65be87963')
.returns('allocation_pools="[{\'start\': \'10.0.0.2\', \'end\': \'10.0.0.254\'}]"
cidr="10.0.0.0/24"
description=""
dns_nameservers="[]"
enable_dhcp="True"
gateway_ip="10.0.0.1"
host_routes="[]"
id="dd5e0ef1-2c88-4b0b-ba08-7df65be87963"
ip_version="4"
ipv6_address_mode="None"
ipv6_ra_mode="None"
name="subnet1"
network_id="076520cc-b783-4cf5-a4a9-4cb5a5e93a9b"
project_id="60f9544eb94c42a6b7e8e98c2be981b1"')
provider_class.expects(:openstack)
.with('network', 'show', '--format', 'shell', ['076520cc-b783-4cf5-a4a9-4cb5a5e93a9b'])
.returns('name="net1"')
provider_class.expects(:openstack)
.with('subnet', 'show', '--format', 'shell', '0da7a631-0f8f-4e51-8b1c-7a29d0d4f7b5')
.returns('allocation_pools="[{\'start\': \'10.0.1.2\', \'end\': \'10.0.1.254\'}]"
cidr="10.0.1.0/24"
description=""
dns_nameservers="[]"
enable_dhcp="False"
gateway_ip="10.0.1.1"
host_routes="[]"
id="0da7a631-0f8f-4e51-8b1c-7a29d0d4f7b5"
ip_version="4"
ipv6_address_mode="None"
ipv6_ra_mode="None"
name="subnet2"
network_id="34e8f42b-89db-4a5b-92db-76ca7073414d"
project_id="60f9544eb94c42a6b7e8e98c2be981b1"')
provider_class.expects(:openstack)
.with('network', 'show', '--format', 'shell', ['34e8f42b-89db-4a5b-92db-76ca7073414d'])
.returns('name="net2"')
instances = provider_class.instances
expect(instances.length).to eq(2)
expect(instances[0].id).to eq('dd5e0ef1-2c88-4b0b-ba08-7df65be87963')
expect(instances[0].name).to eq('subnet1')
expect(instances[0].ip_version).to eq('4')
expect(instances[0].network_id).to eq('076520cc-b783-4cf5-a4a9-4cb5a5e93a9b')
expect(instances[0].network_name).to eq('net1')
expect(instances[0].cidr).to eq('10.0.0.0/24')
expect(instances[0].gateway_ip).to eq('10.0.0.1')
expect(instances[0].allocation_pools).to eq(['start=10.0.0.2,end=10.0.0.254'])
expect(instances[0].enable_dhcp).to eq('True')
expect(instances[0].tenant_id).to eq('60f9544eb94c42a6b7e8e98c2be981b1')
expect(instances[1].id).to eq('0da7a631-0f8f-4e51-8b1c-7a29d0d4f7b5')
expect(instances[1].name).to eq('subnet2')
expect(instances[1].ip_version).to eq('4')
expect(instances[1].network_id).to eq('34e8f42b-89db-4a5b-92db-76ca7073414d')
expect(instances[1].network_name).to eq('net2')
expect(instances[1].gateway_ip).to eq('10.0.1.1')
expect(instances[1].cidr).to eq('10.0.1.0/24')
expect(instances[1].allocation_pools).to eq(['start=10.0.1.2,end=10.0.1.254'])
expect(instances[1].enable_dhcp).to eq('False')
expect(instances[1].tenant_id).to eq('60f9544eb94c42a6b7e8e98c2be981b1')
end
end
end
end