1 require File.join(File.dirname(__FILE__), '..','..','..',
2 'puppet/provider/neutron')
4 Puppet::Type.type(:neutron_subnet).provide(
6 :parent => Puppet::Provider::Neutron
9 Neutron provider to manage neutron_subnet type.
11 Assumes that the neutron service is configured on the same host.
14 commands :neutron => 'neutron'
23 list_neutron_resources(neutron_type).collect do |id|
24 attrs = get_neutron_resource_attrs(neutron_type, id)
27 :name => attrs['name'],
29 :cidr => attrs['cidr'],
30 :ip_version => attrs['ip_version'],
31 :gateway_ip => parse_gateway_ip(attrs['gateway_ip']),
32 :allocation_pools => parse_allocation_pool(attrs['allocation_pools']),
33 :host_routes => parse_host_routes(attrs['host_routes']),
34 :dns_nameservers => parse_dns_nameservers(attrs['dns_nameservers']),
35 :enable_dhcp => attrs['enable_dhcp'],
36 :network_id => attrs['network_id'],
37 :tenant_id => attrs['tenant_id']
42 def self.prefetch(resources)
44 resources.keys.each do |name|
45 if provider = subnets.find{ |subnet| subnet.name == name }
46 resources[name].provider = provider
51 def self.parse_gateway_ip(value)
52 return '' if value.nil?
56 def self.parse_allocation_pool(values)
58 return [] if values.empty?
59 for value in Array(values)
60 matchdata = /\{\s*"start"\s*:\s*"(.*)"\s*,\s*"end"\s*:\s*"(.*)"\s*\}/.match(value.gsub(/\\"/,'"'))
61 start_ip = matchdata[1]
63 allocation_pools << "start=#{start_ip},end=#{end_ip}"
65 return allocation_pools
68 def self.parse_host_routes(values)
70 return [] if values.empty?
71 for value in Array(values)
72 matchdata = /\{\s*"destination"\s*:\s*"(.*)"\s*,\s*"nexthop"\s*:\s*"(.*)"\s*\}/.match(value.gsub(/\\"/,'"'))
73 destination = matchdata[1]
74 nexthop = matchdata[2]
75 host_routes << "destination=#{destination},nexthop=#{nexthop}"
80 def self.parse_dns_nameservers(values)
81 # just enforce that this is actually an array
86 @property_hash[:ensure] == :present
90 opts = ["--name=#{@resource[:name]}"]
92 if @resource[:ip_version]
93 opts << "--ip-version=#{@resource[:ip_version]}"
96 if @resource[:gateway_ip]
97 if @resource[:gateway_ip] == ''
98 opts << '--no-gateway'
100 opts << "--gateway-ip=#{@resource[:gateway_ip]}"
104 if @resource[:enable_dhcp] == 'False'
105 opts << "--disable-dhcp"
107 opts << "--enable-dhcp"
110 if @resource[:allocation_pools]
111 Array(@resource[:allocation_pools]).each do |allocation_pool|
112 opts << "--allocation-pool=#{allocation_pool}"
116 if @resource[:dns_nameservers]
117 Array(@resource[:dns_nameservers]).each do |nameserver|
118 opts << "--dns-nameserver=#{nameserver}"
122 if @resource[:host_routes]
123 Array(@resource[:host_routes]).each do |host_route|
124 opts << "--host-route=#{host_route}"
128 if @resource[:tenant_name]
129 tenant_id = self.class.get_tenant_id(model.catalog,
130 @resource[:tenant_name])
131 opts << "--tenant_id=#{tenant_id}"
132 elsif @resource[:tenant_id]
133 opts << "--tenant_id=#{@resource[:tenant_id]}"
136 if @resource[:network_name]
137 opts << resource[:network_name]
138 elsif @resource[:network_id]
139 opts << resource[:network_id]
142 results = auth_neutron('subnet-create', '--format=shell',
143 opts, resource[:cidr])
145 if results =~ /Created a new subnet:/
146 attrs = self.class.parse_creation_output(results)
149 :name => resource[:name],
151 :cidr => attrs['cidr'],
152 :ip_version => attrs['ip_version'],
153 :gateway_ip => self.class.parse_gateway_ip(attrs['gateway_ip']),
154 :allocation_pools => self.class.parse_allocation_pool(attrs['allocation_pools']),
155 :host_routes => self.class.parse_host_routes(attrs['host_routes']),
156 :dns_nameservers => self.class.parse_dns_nameservers(attrs['dns_nameservers']),
157 :enable_dhcp => attrs['enable_dhcp'],
158 :network_id => attrs['network_id'],
159 :tenant_id => attrs['tenant_id'],
162 fail("did not get expected message on subnet creation, got #{results}")
167 auth_neutron('subnet-delete', name)
168 @property_hash[:ensure] = :absent
171 def gateway_ip=(value)
173 auth_neutron('subnet-update', '--no-gateway', name)
175 auth_neutron('subnet-update', "--gateway-ip=#{value}", name)
179 def enable_dhcp=(value)
181 auth_neutron('subnet-update', "--disable-dhcp", name)
183 auth_neutron('subnet-update', "--enable-dhcp", name)
187 def dns_nameservers=(values)
189 opts = ["#{name}", "--dns-nameservers", "list=true"]
193 auth_neutron('subnet-update', opts)
197 def host_routes=(values)
199 opts = ["#{name}", "--host-routes", "type=dict", "list=true"]
203 auth_neutron('subnet-update', opts)
214 define_method(attr.to_s + "=") do |value|
215 fail("Property #{attr.to_s} does not support being updated")