Merge "Add quantum_subnet custom type."

This commit is contained in:
Jenkins 2013-06-25 07:16:20 +00:00 committed by Gerrit Code Review
commit 7413198d3d
4 changed files with 319 additions and 0 deletions

View File

@ -14,6 +14,13 @@ quantum_network { 'public':
tenant_name => 'admin',
}
quantum_subnet { 'public_subnet':
ensure => 'present',
cidr => '172.24.4.224/28',
network_name => 'public',
tenant_name => 'admin',
}
keystone_tenant { 'demo':
ensure => present,
}
@ -22,3 +29,10 @@ quantum_network { 'private':
ensure => present,
tenant_name => 'demo',
}
quantum_subnet { 'private_subnet':
ensure => present,
cidr => '10.0.0.0/24',
network_name => 'private',
tenant_name => 'demo',
}

View File

@ -0,0 +1,140 @@
require File.join(File.dirname(__FILE__), '..','..','..',
'puppet/provider/quantum')
Puppet::Type.type(:quantum_subnet).provide(
:quantum,
:parent => Puppet::Provider::Quantum
) do
desc <<-EOT
Quantum provider to manage quantum_subnet type.
Assumes that the quantum service is configured on the same host.
EOT
commands :quantum => 'quantum'
mk_resource_methods
def self.quantum_type
'subnet'
end
def self.instances
list_quantum_resources(quantum_type).collect do |id|
attrs = get_quantum_resource_attrs(quantum_type, id)
new(
:ensure => :present,
:name => attrs['name'],
:id => attrs['id'],
:cidr => attrs['cidr'],
:ip_version => attrs['ip_version'],
:gateway_ip => attrs['gateway_ip'],
:allocation_pools => attrs['allocation_pools'],
:host_routes => attrs['host_routes'],
:dns_nameservers => attrs['dns_nameservers'],
:enable_dhcp => attrs['enable_dhcp'],
:network_id => attrs['network_id'],
:tenant_id => attrs['tenant_id']
)
end
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 exists?
@property_hash[:ensure] == :present
end
def create
opts = ["--name=#{@resource[:name]}"]
if @resource[:ip_version]
opts << "--ip-version=#{@resource[:ip_version]}"
end
if @resource[:gateway_ip]
opts << "--gateway-ip=#{@resource[:gateway_ip]}"
end
if @resource[:enable_dhcp]
opts << "--enable-dhcp=#{@resource[:enable_dhcp]}"
end
if @resource[:tenant_name]
opts << "--tenant_id=#{get_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_quantum('subnet-create', '--format=shell',
opts, resource[:cidr])
if results =~ /Created a new subnet:/
@subnet = Hash.new
results.split("\n").compact do |line|
@subnet[line.split('=').first] = \
line.split('=', 2)[1].gsub(/\A"|"\Z/, '')
end
@property_hash = {
:ensure => :present,
:name => resource[:name],
:id => @subnet[:id],
:cidr => @subnet[:cidr],
:ip_version => @subnet[:ip_version],
:gateway_ip => @subnet[:gateway_ip],
:allocation_pools => @subnet[:allocation_pools],
:host_routes => @subnet[:host_routes],
:dns_nameservers => @subnet[:dns_nameservers],
:enable_dhcp => @subnet[:enable_dhcp],
:network_id => @subnet[:network_id],
:tenant_id => @subnet[:tenant_id],
}
else
fail("did not get expected message on subnet creation, got #{results}")
end
end
def get_tenant_id
@tenant_id ||= model.catalog.resource( \
"Keystone_tenant[#{resource[:tenant_name]}]").provider.id
end
def destroy
auth_quantum('subnet-delete', name)
@property_hash[:ensure] = :absent
end
def gateway_ip=(value)
auth_quantum('subnet-update', "--gateway-ip=#{value}", name)
end
def enable_dhcp=(value)
auth_quantum('subnet-update', "--enable-dhcp=#{value}", name)
end
[
:cidr,
:ip_version,
: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,114 @@
Puppet::Type.newtype(:quantum_subnet) do
ensurable
newparam(:name, :namevar => true) do
desc 'Symbolic name for the subnet'
newvalues(/.*/)
end
newproperty(:id) do
desc 'The unique id of the subnet'
validate do |v|
raise(Puppet::Error, 'This is a read only property')
end
end
newproperty(:cidr) do
desc 'CIDR representing IP range for this subnet, based on IP version'
end
newproperty(:ip_version) do
desc 'The IP version of the CIDR'
newvalues('4', '6')
end
newproperty(:allocation_pools) do
desc 'Sub-ranges of cidr available for dynamic allocation to ports'
validate do |v|
raise(Puppet::Error, 'This is a read only property')
end
end
newproperty(:gateway_ip) do
desc 'The default gateway used by devices in this subnet'
end
newproperty(:enable_dhcp) do
desc 'Whether DHCP is enabled for this subnet or not.'
newvalues(/(t|T)rue/, /(f|F)alse/)
munge do |v|
v.to_s.capitalize
end
end
newproperty(:host_routes) do
desc <<-EOT
Routes that should be used by devices with IPs from this subnet
(not including local subnet route).
EOT
validate do |v|
raise(Puppet::Error, 'This is a read only property')
end
end
newproperty(:dns_nameservers) do
desc 'DNS name servers used by hosts in this subnet.'
validate do |v|
raise(Puppet::Error, 'This is a read only property')
end
end
newproperty(:network_id) do
desc 'A uuid identifying the network this subnet is associated with.'
end
newparam(:network_name) do
desc 'The name of the network this subnet is associated with.'
end
newparam(:tenant_name) do
desc 'The name of the tenant which will own the subnet.'
end
newproperty(:tenant_id) do
desc 'A uuid identifying the tenant which will own the subnet.'
end
autorequire(:service) do
['quantum-server']
end
autorequire(:keystone_tenant) do
[self[:tenant_name]] if self[:tenant_name]
end
autorequire(:quantum_network) do
[self[:network_name]] if self[:network_name]
end
validate do
if self[:ensure] != :present
return
end
if ! self[:cidr]
raise(Puppet::Error, 'Please provide a valid CIDR')
elsif ! (self[:network_id] || self[:network_name])
raise(Puppet::Error, <<-EOT
A value for one of network_name or network_id must be provided.
EOT
)
elsif self[:network_id] && self[:network_name]
raise(Puppet::Error, <<-EOT
Please provide a value for only one of network_name and network_id.
EOT
)
elsif self[:tenant_id] && self[:tenant_name]
raise(Puppet::Error, <<-EOT
Please provide a value for only one of tenant_name and tenant_id.
EOT
)
end
end
end

View File

@ -0,0 +1,51 @@
require 'puppet'
require 'spec_helper'
require 'puppet/provider/quantum_subnet/quantum'
provider_class = Puppet::Type.type(:quantum_subnet).provider(:quantum)
describe provider_class do
let :subnet_name do
'net1'
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 => '',
}
end
describe 'when updating a subnet' do
let :resource do
Puppet::Type::Quantum_subnet.new(subnet_attrs)
end
let :provider do
provider_class.new(resource)
end
it 'should call subnet-update to change gateway_ip' do
provider.expects(:auth_quantum).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 change enable_dhcp' do
provider.expects(:auth_quantum).with('subnet-update',
'--enable-dhcp=True',
subnet_name)
provider.enable_dhcp=('True')
end
end
end