neutron_subnet: Fix broken parsing of list values

Closes-Bug: #2072334
Change-Id: I06a12c1ec2769898e8a66ef80b2f4e5017a83e60
This commit is contained in:
Takashi Kajinami 2024-10-08 12:02:13 +09:00
parent 5df5c9404c
commit 3ad715dec4
2 changed files with 12 additions and 21 deletions

View File

@ -70,12 +70,9 @@ Puppet::Type.type(:neutron_subnet).provide(
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('\'','"'))
start_ip = allocation_pool['start']
end_ip = allocation_pool['end']
JSON.parse(values.gsub("'", '"')).each do |pool|
start_ip = pool['start']
end_ip = pool['end']
allocation_pools << "start=#{start_ip},end=#{end_ip}"
end
return allocation_pools
@ -83,24 +80,16 @@ Puppet::Type.type(:neutron_subnet).provide(
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('\'','"'))
nexthop = host_route['nexthop']
destination = host_route['destination']
JSON.parse(values.gsub("'", '"')).each do |route|
nexthop = route['nexthop']
destination = 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('\'','').gsub('[', '').gsub(']', '')
.gsub(',', '').split(' ')
end
# just enforce that this is actually an array
return Array(values)
return JSON.parse(values.gsub("'", '"'))
end
def exists?

View File

@ -244,10 +244,10 @@ name="net1"')
expect(provider_class).to receive(:openstack)
.with('subnet', 'show', '--format', 'shell', 'dd5e0ef1-2c88-4b0b-ba08-7df65be87963')
.and_return('allocation_pools="[{\'start\': \'10.0.0.2\', \'end\': \'10.0.0.254\'}]"
.and_return('allocation_pools="[{\'start\': \'10.0.0.10\', \'end\': \'10.0.0.20\'}, {\'start\': \'10.0.0.30\', \'end\': \'10.0.0.40\'}]"
cidr="10.0.0.0/24"
description=""
dns_nameservers="[]"
dns_nameservers="[\'10.0.0.2\', \'10.0.0.3\']"
enable_dhcp="True"
gateway_ip="10.0.0.1"
host_routes="[]"
@ -292,7 +292,8 @@ project_id="60f9544eb94c42a6b7e8e98c2be981b1"')
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].dns_nameservers).to eq(['10.0.0.2', '10.0.0.3'])
expect(instances[0].allocation_pools).to eq(['start=10.0.0.10,end=10.0.0.20','start=10.0.0.30,end=10.0.0.40'])
expect(instances[0].enable_dhcp).to eq('True')
expect(instances[0].project_id).to eq('60f9544eb94c42a6b7e8e98c2be981b1')
@ -303,6 +304,7 @@ project_id="60f9544eb94c42a6b7e8e98c2be981b1"')
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].dns_nameservers).to eq([])
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].project_id).to eq('60f9544eb94c42a6b7e8e98c2be981b1')