Use json instead of regexp to parse subnet data

The order of name/value pairs in the json string for allocation pools
and host routes is not guaranteed, and when it comes in a different
order from what the regular expression expects, it doesn't match and
causes the method to throw an unhandled exception when attempting to
extract values from matchdata.

Change-Id: I80dec4019a51943f0e04fcbb3be193b4a8d77745
This commit is contained in:
Dmitry Borodaenko 2015-10-16 17:17:27 -07:00
parent 4b08d26f2f
commit c0481d3020

View File

@ -1,3 +1,4 @@
require 'json'
require File.join(File.dirname(__FILE__), '..','..','..',
'puppet/provider/neutron')
@ -57,9 +58,9 @@ Puppet::Type.type(:neutron_subnet).provide(
allocation_pools = []
return [] if values.empty?
for value in Array(values)
matchdata = /\{\s*"start"\s*:\s*"(.*)"\s*,\s*"end"\s*:\s*"(.*)"\s*\}/.match(value.gsub(/\\"/,'"'))
start_ip = matchdata[1]
end_ip = matchdata[2]
allocation_pool = JSON.parse(value.gsub(/\\"/,'"'))
start_ip = allocation_pool['start']
end_ip = allocation_pool['end']
allocation_pools << "start=#{start_ip},end=#{end_ip}"
end
return allocation_pools
@ -69,9 +70,9 @@ Puppet::Type.type(:neutron_subnet).provide(
host_routes = []
return [] if values.empty?
for value in Array(values)
matchdata = /\{\s*"nexthop"\s*:\s*"(.*)"\s*,\s*"destination"\s*:\s*"(.*)"\s*\}/.match(value.gsub(/\\"/,'"'))
nexthop = matchdata[1]
destination = matchdata[2]
host_route = JSON.parse(value.gsub(/\\"/,'"'))
nexthop = host_route['nexthop']
destination = host_route['destination']
host_routes << "nexthop=#{nexthop},destination=#{destination}"
end
return host_routes