From 5852565dcdb7ee86465b9714b1dd706812820130 Mon Sep 17 00:00:00 2001 From: Thomas Maddox Date: Tue, 21 Mar 2017 21:21:13 +0000 Subject: [PATCH] Return variables in response on Network create If a user creates a network and also passes variables during the create process the variables get created but are not present in the response. This patch fixes that by adding variables in response for a create calls on networks when variable was in the create request body. Change-Id: I2320f5b07d3544286234ecc5b1cee4eea999b59e Closes-Bug: 1674815 --- craton/api/v1/resources/inventory/networks.py | 8 +++++++- craton/tests/unit/test_api.py | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/craton/api/v1/resources/inventory/networks.py b/craton/api/v1/resources/inventory/networks.py index 02eaa8d..8b89cf2 100644 --- a/craton/api/v1/resources/inventory/networks.py +++ b/craton/api/v1/resources/inventory/networks.py @@ -35,13 +35,19 @@ class Networks(base.Resource): """Create a new network.""" json = util.copy_project_id_into_json(context, request_data) network_obj = dbapi.networks_create(context, json) + network = jsonutils.to_primitive(network_obj) + if 'variables' in json: + network["variables"] = jsonutils.to_primitive( + network_obj.variables) + else: + network["variables"] = {} location = v1.api.url_for( NetworkById, id=network_obj.id, _external=True ) headers = {'Location': location} - return jsonutils.to_primitive(network_obj), 201, headers + return network, 201, headers class NetworkById(base.Resource): diff --git a/craton/tests/unit/test_api.py b/craton/tests/unit/test_api.py index 03deb14..859c3eb 100644 --- a/craton/tests/unit/test_api.py +++ b/craton/tests/unit/test_api.py @@ -1345,6 +1345,25 @@ class APIV1NetworksTest(APIV1Test): "http://localhost/v1/networks/1" ) + @mock.patch.object(dbapi, 'networks_create') + def test_create_network_returns_network_obj(self, mock_network): + mock_network.return_value = fake_resources.NETWORK1 + data = { + 'name': 'PrivateNetwork', + 'cidr': '192.168.1.0/24', + 'gateway': '192.168.1.1', + 'netmask': '255.255.255.0', + 'variables': {'key1': 'value1'}, + 'region_id': 1, + 'cloud_id': 1, + } + expected_result = copy.deepcopy(data) + expected_result.update({'id': 1, 'project_id': 1}) + + resp = self.post('/v1/networks', data=data) + self.assertEqual(201, resp.status_code) + self.assertEqual(expected_result, resp.json) + @mock.patch.object(dbapi, 'networks_create') def test_create_networks_with_invalid_data(self, mock_network): mock_network.return_value = None