Add network index support in network modules

In the module extracting network data from an existing
heat stack, use the 'tripleo_net_idx' tag to write out
network data file in v2 format using identical network
ordering with the network data used to deploy the stack.

In the module used to create composable networks pass
in the loop index_var in the playbook and set the
tripleo_net_idx tag when creating and updating neutron
network resources.

Partial-Implements: blueprint network-data-v2-ports
Change-Id: Ib96e17987ae1dca629e275b0f219ef435af63bb0
changes/18/772818/1
Harald Jensås 2 years ago
parent 5b6456792f
commit e65363f263
  1. 17
      tripleo_ansible/ansible_plugins/modules/tripleo_composable_network.py
  2. 13
      tripleo_ansible/ansible_plugins/modules/tripleo_overcloud_network_extract.py
  3. 5
      tripleo_ansible/playbooks/cli-overcloud-network-provision.yaml
  4. 16
      tripleo_ansible/tests/modules/test_tripleo_composable_network.py
  5. 19
      tripleo_ansible/tests/modules/test_tripleo_overcloud_network_extract.py

@ -53,6 +53,10 @@ options:
description:
- Structure describing a TripleO composable network
type: dict
idx:
description:
- TripleO network index number
type: int
author:
- Harald Jensås <hjensas@redhat.com>
'''
@ -96,6 +100,7 @@ EXAMPLES = '''
nexthop: 172.18.1.254
vip: false
vlan: 21
idx: 1
'''
DEFAULT_NETWORK = 'ctlplane'
@ -115,8 +120,9 @@ def get_overcloud_domain_name(conn, default_network):
return DEFAULT_DOMAIN
def build_network_tag_field(net_data):
tags = ['='.join(['tripleo_network_name', net_data['name']])]
def build_network_tag_field(net_data, idx):
tags = ['='.join(['tripleo_network_name', net_data['name']]),
'='.join(['tripleo_net_idx', str(idx)])]
service_net_map_replace = net_data.get('service_net_map_replace')
vip = net_data.get('vip')
if service_net_map_replace:
@ -137,7 +143,7 @@ def build_subnet_tag_field(subnet_data):
return tags
def create_net_spec(net_data, overcloud_domain_name):
def create_net_spec(net_data, overcloud_domain_name, idx):
name_lower = net_data.get('name_lower', net_data['name'].lower())
net_spec = {
'admin_state_up': net_data.get('admin_state_up', DEFAULT_ADMIN_STATE),
@ -152,7 +158,7 @@ def create_net_spec(net_data, overcloud_domain_name):
'provider:network_type': DEFAULT_NETWORK_TYPE,
}
net_spec.update({'tags': build_network_tag_field(net_data)})
net_spec.update({'tags': build_network_tag_field(net_data, idx)})
return net_spec
@ -418,6 +424,7 @@ def run_module():
default_network = module.params.get('default_network', DEFAULT_NETWORK)
net_data = module.params['net_data']
idx = module.params['idx']
error_messages = network_data_v2.validate_json_schema(net_data)
if error_messages:
module.fail_json(msg='\n\n'.join(error_messages))
@ -427,7 +434,7 @@ def run_module():
# Create or update the network
net_spec = create_net_spec(
net_data, get_overcloud_domain_name(conn, default_network))
net_data, get_overcloud_domain_name(conn, default_network), idx)
changed, network = create_or_update_network(conn, module, net_spec)
result['changed'] = changed if changed else result['changed']

@ -160,6 +160,9 @@ def tripleo_resource_tags_to_dict(resource_tags):
except ValueError:
continue
if key == 'tripleo_net_idx':
value = int(value)
tag_dict.update({key: value})
return tag_dict
@ -211,7 +214,7 @@ def get_network_info(conn, network_id):
pop_defaults(net_dict)
return net_dict
return tag_dict['tripleo_net_idx'], net_dict
def get_subnet_info(conn, subnet_id):
@ -284,21 +287,23 @@ def get_subnet_info(conn, subnet_id):
def parse_net_resources(conn, net_resources):
network_data = list()
indexed_networks = dict()
for net in net_resources:
name = net.rpartition('Network')[0]
net_entry = {'name': name, 'subnets': dict()}
for res in net_resources[net]:
res_dict = net_resources[net][res]
if res_dict['resource_type'] == TYPE_NET:
net_dict = get_network_info(conn, res_dict[RES_ID])
idx, net_dict = get_network_info(conn, res_dict[RES_ID])
net_entry.update(net_dict)
if res_dict['resource_type'] == TYPE_SUBNET:
subnet_name, subnet_dict = get_subnet_info(conn,
res_dict[RES_ID])
net_entry['subnets'].update({subnet_name: subnet_dict})
network_data.append(net_entry)
indexed_networks[idx] = net_entry
network_data = [indexed_networks[i] for i in sorted(indexed_networks)]
return network_data

@ -49,10 +49,13 @@
when:
- network_data is undefined
- name: Create composable networks
- name: Create/Update composable networks
tripleo_composable_network:
net_data: "{{ item }}"
idx: "{{ idx }}"
loop: "{{ network_data }}"
loop_control:
index_var: idx
- name: Populate environment
tripleo_network_populate_environment:

@ -25,18 +25,22 @@ from tripleo_ansible.tests import stubs
class TestTripleoComposableNetwork(tests_base.TestCase):
def test_build_network_tag_field(self):
idx = 3
net_data = {'name': 'foo',
'service_net_map_replace': 'replacement',
'vip': True}
expected = ['tripleo_network_name=foo',
'tripleo_net_idx=3',
'tripleo_service_net_map_replace=replacement',
'tripleo_vip=true']
result = plugin.build_network_tag_field(net_data)
result = plugin.build_network_tag_field(net_data, idx)
self.assertEqual(expected, result)
idx = 1
net_data = {'name': 'foo'}
expected = ['tripleo_network_name=foo']
result = plugin.build_network_tag_field(net_data)
expected = ['tripleo_network_name=foo',
'tripleo_net_idx=1']
result = plugin.build_network_tag_field(net_data, idx)
self.assertEqual(expected, result)
def test_build_subnet_tag_field(self):
@ -52,6 +56,7 @@ class TestTripleoComposableNetwork(tests_base.TestCase):
self.assertEqual(expected, result)
def test_create_net_spec(self):
idx = 3
net_data = {'name': 'NetName'}
overcloud_domain_name = 'example.com.'
expected = {
@ -62,10 +67,11 @@ class TestTripleoComposableNetwork(tests_base.TestCase):
'shared': plugin.DEFAULT_SHARED,
'provider:physical_network': 'netname',
'provider:network_type': plugin.DEFAULT_NETWORK_TYPE,
'tags': ['tripleo_network_name=NetName'],
'tags': ['tripleo_network_name=NetName',
'tripleo_net_idx=3'],
}
result = plugin.create_net_spec(net_data, overcloud_domain_name)
result = plugin.create_net_spec(net_data, overcloud_domain_name, idx)
self.assertEqual(expected, result)
def test_validate_network_update(self):

@ -25,8 +25,10 @@ from tripleo_ansible.tests import stubs
class TestTripleoOvercloudNetworkExtract(tests_base.TestCase):
def test_tripleo_resource_tags_to_dict(self):
tags = ['foo=bar', 'baz=qux', 'tripleo_foo=bar', 'tripleo_baz=qux']
expected = {'tripleo_foo': 'bar', 'tripleo_baz': 'qux'}
tags = ['foo=bar', 'baz=qux', 'tripleo_foo=bar', 'tripleo_baz=qux',
'tripleo_net_idx=3']
expected = {'tripleo_foo': 'bar', 'tripleo_baz': 'qux',
'tripleo_net_idx': 3}
result = plugin.tripleo_resource_tags_to_dict(tags)
self.assertEqual(expected, result)
@ -69,14 +71,13 @@ class TestTripleoOvercloudNetworkExtract(tests_base.TestCase):
mtu=1500,
is_shared=False,
is_admin_state_up=False,
tags=['tripleo_service_net_map_replace=external']
tags=['tripleo_net_idx=3',
'tripleo_service_net_map_replace=external']
)
conn_mock.network.get_network.return_value = fake_network
expected = {
'name_lower': 'public',
'dns_domain': 'public.localdomain.',
'service_net_map_replace': 'external',
}
expected = (3, {'name_lower': 'public',
'dns_domain': 'public.localdomain.',
'service_net_map_replace': 'external'})
result = plugin.get_network_info(
conn_mock, '132f871f-eaec-4fed-9475-0d54465e0f00')
self.assertEqual(expected, result)
@ -264,7 +265,7 @@ class TestTripleoOvercloudNetworkExtract(tests_base.TestCase):
'physical_network': 'leaf1',
}
mock_get_network.return_value = fake_network
mock_get_network.return_value = (0, fake_network)
mock_get_subnet.side_effect = [
('storage', fake_subnet_storage),
('leaf1', fake_subnet_storage_leaf1)]

Loading…
Cancel
Save