75 lines
1.8 KiB
Ruby
75 lines
1.8 KiB
Ruby
require 'ipaddr'
|
|
|
|
Puppet::Type.type(:nova_network).provide(:nova_manage) do
|
|
|
|
desc "Manage nova network"
|
|
|
|
optional_commands :nova_manage => 'nova-manage'
|
|
|
|
# I need to setup caching and what-not to make this lookup performance not suck
|
|
def self.instances
|
|
begin
|
|
network_list = nova_manage("network", "list")
|
|
rescue Exception => e
|
|
if e.message =~ /No networks defined/
|
|
return []
|
|
else
|
|
raise(e)
|
|
end
|
|
end
|
|
network_list.split("\n")[1..-1].collect do |net|
|
|
if net =~ /^(\S+)\s+(\S+)/
|
|
new(:name => $2 )
|
|
end
|
|
end.compact
|
|
end
|
|
|
|
def create
|
|
optional_opts = []
|
|
{
|
|
# this needs to be converted from a project name to an id
|
|
:project => '--project_id',
|
|
:dns2 => '--dns2',
|
|
:gateway => '--gateway',
|
|
:bridge => '--bridge',
|
|
:vlan_start => '--vlan',
|
|
}.each do |param, opt|
|
|
if resource[param]
|
|
optional_opts.push(opt).push(resource[param])
|
|
end
|
|
end
|
|
if resource[:network_size].to_i <= 2**(32-resource[:name].split('/')[1].to_i)-1
|
|
optional_opts.push('--network_size').push(resource[:network_size])
|
|
end
|
|
|
|
nova_manage('network', 'create',
|
|
resource[:label],
|
|
resource[:name],
|
|
resource[:num_networks],
|
|
optional_opts
|
|
)
|
|
end
|
|
|
|
def exists?
|
|
# notice("exists()")
|
|
net = IPAddr.new(resource[:network])
|
|
|
|
begin
|
|
network_list = nova_manage("network", "list")
|
|
return network_list.split("\n")[1..-1].detect do |n|
|
|
if (ipaddr = n.split[1]) =~ /^(\d+.){3}\d+\/\d+$/
|
|
net.include?(IPAddr.new(ipaddr))
|
|
end
|
|
end
|
|
rescue
|
|
return false
|
|
end
|
|
end
|
|
|
|
|
|
def destroy
|
|
nova_manage("network", "delete", resource[:network])
|
|
end
|
|
|
|
end
|