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
This commit is contained in:
Thomas Maddox 2017-03-21 21:21:13 +00:00
parent 12b31e49ac
commit 5852565dcd
2 changed files with 26 additions and 1 deletions

View File

@ -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):

View File

@ -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