Update POST response code and add location header

This commit updates POST responses based on API-WG guidelines [1] and
RFC 2616 [2]. Specifically:
- POST requests have been updated to return '201 (Created)' on success
  (previously returned 200)
- a Location header field has been added to responses (previously not
  included in response)

In addition:
- The unit tests have been updated to deal with 201 return codes and
  to check for the presense of the Location header
- The generate_fake_data script has been updated to deal with 201
  return codes
- POST requests in the documentation have been updated to the
  new status code

[1] https://specs.openstack.org/openstack/api-wg/guidelines/http.html
[2] https://tools.ietf.org/html/rfc2616#section-9.5

Change-Id: I2246ff5edfa6fccd9711b67efb73504fb1eb2653
Closes-bug: 1658800
This commit is contained in:
Mark Harrington
2017-02-02 20:15:03 +00:00
parent 8b581d157d
commit 5411d8fc84
20 changed files with 288 additions and 106 deletions

View File

@@ -13,7 +13,7 @@ Create Cell
Create a new Cell
Normal response codes: OK(200)
Normal response codes: OK(201)
Error response codes: invalid request(400), validation exception(405)

View File

@@ -13,7 +13,7 @@ Create Host
Create a new host
Normal response codes: OK(200)
Normal response codes: OK(201)
Error response codes: invalid request(400), validation exception(405)

View File

@@ -13,7 +13,7 @@ Create Region
Creates a new Region
Normal response codes: OK(200)
Normal response codes: OK(201)
Error response codes: invalid request(400), validation exception(405)

View File

@@ -1,6 +1,7 @@
from oslo_serialization import jsonutils
from oslo_log import log
from craton.api import v1
from craton.api.v1 import base
from craton import db as dbapi
from craton import util
@@ -30,7 +31,13 @@ class Cells(base.Resource):
cell["variables"] = jsonutils.to_primitive(cell_obj.variables)
else:
cell["variables"] = {}
return cell, 200, None
location = v1.api.url_for(
CellById, id=cell_obj.id, _external=True
)
headers = {'Location': location}
return cell, 201, headers
class CellById(base.Resource):

View File

@@ -1,6 +1,7 @@
from oslo_serialization import jsonutils
from oslo_log import log
from craton.api import v1
from craton.api.v1 import base
from craton import db as dbapi
from craton import util
@@ -30,7 +31,13 @@ class Hosts(base.Resource):
host["variables"] = jsonutils.to_primitive(host_obj.variables)
else:
host["variables"] = {}
return host, 200, None
location = v1.api.url_for(
HostById, id=host_obj.id, _external=True
)
headers = {'Location': location}
return host, 201, headers
def format_variables(args, obj):

View File

@@ -1,6 +1,7 @@
from oslo_serialization import jsonutils
from oslo_log import log
from craton.api import v1
from craton.api.v1 import base
from craton import db as dbapi
from craton import util
@@ -26,7 +27,13 @@ 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)
return jsonutils.to_primitive(network_obj), 200, None
location = v1.api.url_for(
NetworkById, id=network_obj.id, _external=True
)
headers = {'Location': location}
return jsonutils.to_primitive(network_obj), 201, headers
class NetworkById(base.Resource):
@@ -94,7 +101,13 @@ class NetworkDevices(base.Resource):
json = util.copy_project_id_into_json(context, request_data)
obj = dbapi.network_devices_create(context, json)
device = jsonutils.to_primitive(obj)
return device, 200, None
location = v1.api.url_for(
NetworkDeviceById, id=obj.id, _external=True
)
headers = {'Location': location}
return device, 201, headers
class NetworkDeviceById(base.Resource):
@@ -191,7 +204,13 @@ class NetworkInterfaces(base.Resource):
json = util.copy_project_id_into_json(context, request_data)
obj = dbapi.network_interfaces_create(context, json)
interface = jsonutils.to_primitive(obj)
return interface, 200, None
location = v1.api.url_for(
NetworkInterfaceById, id=obj.id, _external=True
)
headers = {'Location': location}
return interface, 201, headers
class NetworkInterfaceById(base.Resource):

View File

@@ -1,6 +1,7 @@
from oslo_serialization import jsonutils
from oslo_log import log
from craton.api import v1
from craton.api.v1 import base
from craton import db as dbapi
from craton import util
@@ -47,7 +48,13 @@ class Regions(base.Resource):
region["variables"] = jsonutils.to_primitive(region_obj.variables)
else:
region["variables"] = {}
return region, 200, None
location = v1.api.url_for(
RegionsById, id=region_obj.id, _external=True
)
headers = {'Location': location}
return region, 201, headers
class RegionsById(base.Resource):

View File

@@ -1,6 +1,7 @@
from oslo_serialization import jsonutils
from oslo_log import log
from craton.api import v1
from craton.api.v1 import base
from craton import db as dbapi
@@ -35,7 +36,13 @@ class Projects(base.Resource):
def post(self, context, request_data):
"""Create a new project. Requires super admin privileges."""
project_obj = dbapi.projects_create(context, request_data)
return jsonutils.to_primitive(project_obj), 200, None
location = v1.api.url_for(
ProjectById, id=project_obj.id, _external=True
)
headers = {'Location': location}
return jsonutils.to_primitive(project_obj), 201, headers
class ProjectById(base.Resource):

View File

@@ -2,6 +2,7 @@ from oslo_serialization import jsonutils
from oslo_log import log
from oslo_utils import uuidutils
from craton.api import v1
from craton.api.v1 import base
from craton import db as dbapi
from craton import util
@@ -43,7 +44,13 @@ class Users(base.Resource):
api_key = uuidutils.generate_uuid()
request_data["api_key"] = api_key
user_obj = dbapi.users_create(context, json)
return jsonutils.to_primitive(user_obj), 200, None
location = v1.api.url_for(
UserById, id=user_obj.id, _external=True
)
headers = {'Location': location}
return jsonutils.to_primitive(user_obj), 201, headers
class UserById(base.Resource):

View File

@@ -1529,7 +1529,7 @@ filters = {
},
},
("hosts", "POST"): {
200: {
201: {
"headers": None,
"schema": DefinitionsHost,
},
@@ -1684,7 +1684,7 @@ filters = {
},
},
("cells", "POST"): {
200: {
201: {
"headers": None,
"schema": DefinitionsCell,
},
@@ -1719,7 +1719,7 @@ filters = {
},
},
("regions", "POST"): {
200: {
201: {
"headers": None,
"schema": DefinitionsRegion,
},
@@ -1895,7 +1895,7 @@ filters = {
},
},
("projects", "POST"): {
200: {
201: {
"headers": None,
"schema": DefinitionProject,
},
@@ -1934,7 +1934,7 @@ filters = {
},
},
("users", "POST"): {
200: {
201: {
"headers": None,
"schema": DefinitionUser,
},
@@ -2045,7 +2045,7 @@ filters = {
},
},
("network_devices", "POST"): {
200: {
201: {
"headers": None,
"schema": DefinitionNetworkDeviceId,
},
@@ -2258,7 +2258,7 @@ filters = {
},
},
("networks", "POST"): {
200: {
201: {
"headers": None,
"schema": DefinitionNetwork,
},
@@ -2417,7 +2417,7 @@ filters = {
},
},
("network_interfaces", "POST"): {
200: {
201: {
"headers": None,
"schema": DefinitionNetworkInterface,
},

View File

@@ -172,7 +172,7 @@ class TestCase(testtools.TestCase):
if not self.container_setup_error:
data = _container.container_data
self.service_ip = data['NetworkSettings']['IPAddress']
self.url = 'http://{}:8080/'.format(self.service_ip)
self.url = 'http://{}:8080'.format(self.service_ip)
self.session.headers['X-Auth-Project'] = FAKE_DATA_GEN_PROJECT_ID
self.session.headers['X-Auth-Token'] = FAKE_DATA_GEN_TOKEN
self.session.headers['X-Auth-User'] = FAKE_DATA_GEN_USERNAME

View File

@@ -14,7 +14,12 @@ class APIV1CellTest(TestCase):
url = self.url + '/v1/regions'
payload = {'name': 'region-1'}
region = self.post(url, data=payload)
self.assertEqual(200, region.status_code)
self.assertEqual(201, region.status_code)
self.assertIn('Location', region.headers)
self.assertEqual(
region.headers['Location'],
"{}/{}".format(url, region.json()['id'])
)
return region.json()
def create_cell(self, name, variables=None):
@@ -23,7 +28,12 @@ class APIV1CellTest(TestCase):
if variables:
payload['variables'] = variables
cell = self.post(url, data=payload)
self.assertEqual(200, cell.status_code)
self.assertEqual(201, cell.status_code)
self.assertIn('Location', cell.headers)
self.assertEqual(
cell.headers['Location'],
"{}/{}".format(url, cell.json()['id'])
)
return cell.json()
def test_cell_create_with_variables(self):

View File

@@ -14,7 +14,12 @@ class APIV1HostTest(TestCase):
url = self.url + '/v1/regions'
payload = {'name': 'region-1'}
region = self.post(url, data=payload)
self.assertEqual(200, region.status_code)
self.assertEqual(201, region.status_code)
self.assertIn('Location', region.headers)
self.assertEqual(
region.headers['Location'],
"{}/{}".format(url, region.json()['id'])
)
return region.json()
def create_host(self, name, hosttype, ip_address, **variables):
@@ -26,7 +31,12 @@ class APIV1HostTest(TestCase):
payload['variables'] = variables
host = self.post(url, data=payload)
self.assertEqual(200, host.status_code)
self.assertEqual(201, host.status_code)
self.assertIn('Location', host.headers)
self.assertEqual(
host.headers['Location'],
"{}/{}".format(url, host.json()['id'])
)
return host.json()
def test_create_host(self):

View File

@@ -17,7 +17,12 @@ class APIV1RegionTest(TestCase):
if variables:
values['variables'] = variables
resp = self.post(url, data=values)
self.assertEqual(200, resp.status_code)
self.assertEqual(201, resp.status_code)
self.assertIn('Location', resp.headers)
self.assertEqual(
resp.headers['Location'],
"{}/{}".format(url, resp.json()['id'])
)
return resp.json()
def test_create_region_full_data(self):
@@ -27,7 +32,12 @@ class APIV1RegionTest(TestCase):
"variables": {"a": "b"}}
url = self.url + '/v1/regions'
resp = self.post(url, data=values)
self.assertEqual(200, resp.status_code)
self.assertEqual(201, resp.status_code)
self.assertIn('Location', resp.headers)
self.assertEqual(
resp.headers['Location'],
"{}/{}".format(url, resp.json()['id'])
)
self.assertEqual(values['name'], resp.json()['name'])
def test_create_region_without_variables(self):
@@ -35,7 +45,12 @@ class APIV1RegionTest(TestCase):
"note": "This is region-two"}
url = self.url + '/v1/regions'
resp = self.post(url, data=values)
self.assertEqual(200, resp.status_code)
self.assertEqual(201, resp.status_code)
self.assertIn('Location', resp.headers)
self.assertEqual(
resp.headers['Location'],
"{}/{}".format(url, resp.json()['id'])
)
self.assertEqual("region-two", resp.json()['name'])
def test_create_region_with_no_name_fails(self):

View File

@@ -8,20 +8,22 @@ objects for test.
class Project(object):
def __init__(self, name):
def __init__(self, id, name):
self.id = id
self.name = name
def items(self):
return iter(self.__dict__.items())
PROJECT1 = Project("project1")
PROJECT2 = Project("project2")
PROJECT1 = Project("4534dcb4-dacd-474f-8afc-8bd5ab2d26e8", "project1")
PROJECT2 = Project("77c527cb-837d-4fcb-bafb-af37ba3d13a4", "project2")
class User(object):
def __init__(self, username, project_id, is_admin, is_root,
def __init__(self, id, username, project_id, is_admin, is_root,
api_key, roles=None):
self.id = id
self.username = username
self.project_id = project_id
self.is_admin = is_admin
@@ -33,15 +35,16 @@ class User(object):
return iter(self.__dict__.items())
USER1 = User('user1', "2757a1b4-cd90-4891-886c-a246fd4e7064", True, False,
USER1 = User(1, 'user1', "2757a1b4-cd90-4891-886c-a246fd4e7064", True, False,
'xx-yy-zz')
USER2 = User('user2', "05d081ca-dcf5-4e96-b132-23b94d665799", False, False,
USER2 = User(2, 'user2', "05d081ca-dcf5-4e96-b132-23b94d665799", False, False,
'aa-bb-cc')
class Cell(object):
def __init__(self, name, status, region_id, project_id, variables,
def __init__(self, id, name, status, region_id, project_id, variables,
labels=None):
self.id = id
self.name = name
self.status = status
self.region_id = region_id
@@ -53,19 +56,20 @@ class Cell(object):
return iter(self.__dict__.items())
CELL1 = Cell("cell1", "active", 1, 1, {"key1": "value1",
"key2": "value2"})
CELL2 = Cell("cell2", "active", "2", "abcd", {"key3": "value3",
"key4": "value4"})
CELL3 = Cell("cell1", "active", 2, 1, {"key1": "value1",
"key2": "value2"})
CELL1 = Cell(1, "cell1", "active", 1, 1, {"key1": "value1",
"key2": "value2"})
CELL2 = Cell(2, "cell2", "active", "2", "abcd", {"key3": "value3",
"key4": "value4"})
CELL3 = Cell(3, "cell1", "active", 2, 1, {"key1": "value1",
"key2": "value2"})
CELL_LIST = [CELL1, CELL2]
CELL_LIST2 = [CELL1, CELL3]
class Region(object):
def __init__(self, name, project_id, variables, labels=None):
def __init__(self, id, name, project_id, variables, labels=None):
self.id = id
self.name = name
self.project_id = project_id
self.variables = variables
@@ -75,14 +79,15 @@ class Region(object):
return iter(self.__dict__.items())
REGION1 = Region("region1", "abcd", {"key1": "value1", "key2": "value2"})
REGION2 = Region("region2", "abcd", {"key3": "value3", "key4": "value4"})
REGION1 = Region(1, "region1", "abcd", {"key1": "value1", "key2": "value2"})
REGION2 = Region(2, "region2", "abcd", {"key3": "value3", "key4": "value4"})
REGIONS_LIST = [REGION1, REGION2]
class Host(object):
def __init__(self, name, project_id, region_id, ip_address,
def __init__(self, id, name, project_id, region_id, ip_address,
device_type, variables, labels=None):
self.id = id
self.name = name
self.project_id = project_id
self.region_id = region_id
@@ -96,13 +101,13 @@ class Host(object):
return iter(self.__dict__.items())
HOST1 = Host("www.craton.com", 1, 1, "192.168.1.1", "server",
HOST1 = Host(1, "www.craton.com", 1, 1, "192.168.1.1", "server",
{"key1": "value1", "key2": "value2"})
HOST2 = Host("www.example.com", "1", "1", "192.168.1.2", "server",
HOST2 = Host(2, "www.example.com", "1", "1", "192.168.1.2", "server",
{"key1": "value1", "key2": "value2"})
HOST3 = Host("www.example.net", "1", "2", "10.10.0.1", "server",
HOST3 = Host(3, "www.example.net", "1", "2", "10.10.0.1", "server",
{"key1": "value1", "key2": "value2"})
HOST4 = Host("www.example.net", "1", "2", "10.10.0.1", "server",
HOST4 = Host(4, "www.example.net", "1", "2", "10.10.0.1", "server",
{"key1": "value1", "key2": "value2"}, labels=["a", "b"])
HOSTS_LIST_R1 = [HOST1, HOST2]
HOSTS_LIST_R2 = [HOST3]
@@ -110,8 +115,9 @@ HOSTS_LIST_R3 = [HOST1, HOST2, HOST3]
class Networks(object):
def __init__(self, name, project_id, cidr, gateway, netmask,
def __init__(self, id, name, project_id, cidr, gateway, netmask,
variables, region_id, labels=None):
self.id = id
self.name = name
self.project_id = project_id
self.cidr = cidr
@@ -125,20 +131,21 @@ class Networks(object):
return iter(self.__dict__.items())
NETWORK1 = Networks("PrivateNetwork", 1, "192.168.1.0/24", "192.168.1.1",
NETWORK1 = Networks(1, "PrivateNetwork", 1, "192.168.1.0/24", "192.168.1.1",
"255.255.255.0", {"key1": "value1"}, 1)
NETWORK2 = Networks("PublicNetwork", 1, "10.10.1.0/24", "10.10.1.1",
NETWORK2 = Networks(2, "PublicNetwork", 1, "10.10.1.0/24", "10.10.1.1",
"255.255.255.0", {"pkey1": "pvalue1"}, 1)
NETWORK3 = Networks("OtherNetwork", 1, "10.10.1.0/24", "10.10.1.2",
NETWORK3 = Networks(3, "OtherNetwork", 1, "10.10.1.0/24", "10.10.1.2",
"255.255.255.0", {"okey1": "ovalue1"}, 2)
NETWORKS_LIST = [NETWORK1, NETWORK2]
NETWORKS_LIST2 = [NETWORK1, NETWORK2, NETWORK3]
class NetworkDevice():
def __init__(self, name, project_id, region_id,
def __init__(self, id, name, project_id, region_id,
device_type, ip_address, variables, labels=None):
self.name = name
self.id = id
self.project_id = project_id
self.region_id = region_id
self.device_type = device_type
@@ -151,10 +158,10 @@ class NetworkDevice():
return iter(self.__dict__.items())
NETWORK_DEVICE1 = NetworkDevice("NetDevices1", 1, 1, "Server", "10.10.0.1",
NETWORK_DEVICE1 = NetworkDevice(1, "NetDevices1", 1, 1, "Server", "10.10.0.1",
{"key1": "value1", "key2": "value2"},
labels=["a", "b"])
NETWORK_DEVICE2 = NetworkDevice("NetDevices2", 1, 2, "Server", "10.10.0.2",
NETWORK_DEVICE2 = NetworkDevice(2, "NetDevices2", 1, 2, "Server", "10.10.0.2",
{"key1": "value1", "key2": "value2"},
labels=["a", "b"])
@@ -163,8 +170,9 @@ NETWORK_DEVICE_LIST2 = [NETWORK_DEVICE1, NETWORK_DEVICE2]
class NetworkInterface():
def __init__(self, name, device_id, project_id, interface_type,
def __init__(self, id, name, device_id, project_id, interface_type,
ip_address, variables):
self.id = id
self.name = name
self.device_id = device_id
self.project_id = project_id
@@ -176,11 +184,11 @@ class NetworkInterface():
return iter(self.__dict__.items())
NETWORK_INTERFACE1 = NetworkInterface("NetInterface", 1, 1, "interface_type1",
"10.10.0.1",
NETWORK_INTERFACE1 = NetworkInterface(1, "NetInterface", 1, 1,
"interface_type1", "10.10.0.1",
{"key1": "value1", "key2": "value2"})
NETWORK_INTERFACE2 = NetworkInterface("NetInterface", 2, 1, "interface_type2",
"10.10.0.2",
NETWORK_INTERFACE2 = NetworkInterface(2, "NetInterface", 2, 1,
"interface_type2", "10.10.0.2",
{"key1": "value1", "key2": "value2"})
NETWORK_INTERFACE_LIST1 = [NETWORK_INTERFACE1]

View File

@@ -203,17 +203,37 @@ class APIV1CellsTest(APIV1Test):
mock_cell.return_value = fake_resources.CELL1
data = {'name': 'cell1', 'region_id': 1}
resp = self.post('v1/cells', data=data)
self.assertEqual(200, resp.status_code)
self.assertEqual(201, resp.status_code)
self.assertIn('Location', resp.headers)
self.assertEqual(
resp.headers['Location'],
"http://localhost/v1/cells/1"
)
@mock.patch.object(dbapi, 'cells_create')
def test_create_cell_returns_cell_obj(self, mock_cell):
return_value = {'name': 'cell1', 'region_id': 1, 'id': 1,
'variables': {}}
mock_cell.return_value = return_value
data = {'name': 'cell1', 'region_id': 1}
mock_cell.return_value = fake_resources.CELL1
data = {
'name': "cell1",
'region_id': 1,
'variables': {'key1': 'value1', 'key2': 'value2'},
}
resp = self.post('v1/cells', data=data)
self.assertEqual(200, resp.status_code)
self.assertEqual(return_value, resp.json)
expected_result = {
'id': 1,
'name': 'cell1',
'region_id': 1,
'project_id': 1,
'variables': {'key1': 'value1', 'key2': 'value2'},
}
self.assertEqual(201, resp.status_code)
self.assertEqual(expected_result, resp.json)
self.assertIn('Location', resp.headers)
self.assertEqual(
resp.headers['Location'],
"http://localhost/v1/cells/1"
)
@mock.patch.object(dbapi, 'cells_create')
def test_create_cell_fails_with_invalid_data(self, mock_cell):
@@ -367,7 +387,12 @@ class APIV1RegionsTest(APIV1Test):
mock_region.return_value = fake_resources.REGION1
data = {'name': 'region1'}
resp = self.post('v1/regions', data=data)
self.assertEqual(200, resp.status_code)
self.assertEqual(201, resp.status_code)
self.assertIn('Location', resp.headers)
self.assertEqual(
resp.headers['Location'],
"http://localhost/v1/regions/1"
)
@mock.patch.object(dbapi, 'regions_create')
def test_post_region_with_invalid_property_name(self, mock_region):
@@ -378,7 +403,9 @@ class APIV1RegionsTest(APIV1Test):
@mock.patch.object(dbapi, 'regions_create')
def test_create_region_returns_region_obj(self, mock_region):
return_value = {'name': 'region1', 'project_id': 'abcd',
return_value = {'name': 'region1',
'project_id': 'abcd',
'id': 1,
'variables': {"key1": "value1", "key2": "value2"}}
fake_region = fake_resources.REGION1
fake_region.variables = {"key1": "value1", "key2": "value2"}
@@ -386,8 +413,13 @@ class APIV1RegionsTest(APIV1Test):
data = {'name': 'region1',
'variables': {"key1": "value1", "key2": "value2"}}
resp = self.post('v1/regions', data=data)
self.assertEqual(200, resp.status_code)
self.assertEqual(201, resp.status_code)
self.assertEqual(return_value, resp.json)
self.assertIn('Location', resp.headers)
self.assertEqual(
resp.headers['Location'],
"http://localhost/v1/regions/1"
)
@mock.patch.object(dbapi, 'regions_create')
def test_post_region_with_invalid_data_fails(self, mock_region):
@@ -622,23 +654,46 @@ class APIV1HostsTest(APIV1Test):
@mock.patch.object(dbapi, 'hosts_create')
def test_create_host_with_valid_data(self, mock_host):
mock_host.return_value = fake_resources.HOST1
data = {'name': 'www.host1.com', 'region_id': 1,
'ip_address': '10.0.0.1', 'device_type': 'server',
data = {'name': 'www.craton.com', 'region_id': 1,
'ip_address': '192.168.1.1', 'device_type': 'server',
'active': True}
resp = self.post('/v1/hosts', data=data)
self.assertEqual(200, resp.status_code)
self.assertEqual(201, resp.status_code)
self.assertIn('Location', resp.headers)
self.assertEqual(
resp.headers['Location'],
"http://localhost/v1/hosts/1"
)
@mock.patch.object(dbapi, 'hosts_create')
def test_create_host_returns_host_obj(self, mock_host):
return_value = {'name': 'www.host1.com', 'region_id': 1,
'ip_address': '10.0.0.1', 'id': 1, 'variables': {},
'device_type': 'server', 'active': True}
mock_host.return_value = return_value
data = {'name': 'www.host1.com', 'region_id': 1,
'ip_address': '10.0.0.1', 'device_type': 'server'}
mock_host.return_value = fake_resources.HOST1
data = {
'name': 'www.craton.com',
'region_id': 1,
'ip_address': '192.168.1.1',
'device_type': 'server',
'labels': [],
'variables': {"key1": "value1", "key2": "value2"},
}
resp = self.post('v1/hosts', data=data)
self.assertEqual(200, resp.status_code)
self.assertEqual(return_value, resp.json)
self.assertEqual(201, resp.status_code)
expected_response = {
'id': 1,
'name': 'www.craton.com',
'region_id': 1,
'project_id': 1,
'ip_address': '192.168.1.1',
'device_type': 'server',
'labels': [],
'variables': {"key1": "value1", "key2": "value2"},
}
self.assertEqual(expected_response, resp.json)
self.assertIn('Location', resp.headers)
self.assertEqual(
resp.headers['Location'],
"http://localhost/v1/hosts/1"
)
@mock.patch.object(dbapi, 'hosts_create')
def test_create_host_invalid_property_name(self, mock_host):
@@ -716,12 +771,19 @@ class APIV1HostsVariablesTest(APIV1Test):
class APIV1ProjectsTest(APIV1Test):
@mock.patch.object(dbapi, 'projects_create')
def test_create_project(self, mock_project):
return_value = {'name': 'project1', 'id': 1}
mock_project.return_value = return_value
mock_project.return_value = fake_resources.PROJECT1
data = {'name': 'project1'}
resp = self.post('v1/projects', data=data)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.json['id'], 1)
self.assertEqual(resp.status_code, 201)
self.assertEqual(
resp.json['id'],
"4534dcb4-dacd-474f-8afc-8bd5ab2d26e8"
)
self.assertIn('Location', resp.headers)
self.assertEqual(
resp.headers['Location'],
"http://localhost/v1/projects/4534dcb4-dacd-474f-8afc-8bd5ab2d26e8"
)
@mock.patch.object(dbapi, 'projects_get_all')
def test_project_get_all(self, mock_projects):
@@ -753,13 +815,16 @@ class APIV1UsersTest(APIV1Test):
@mock.patch.object(dbapi, 'projects_get_by_id')
def test_create_users(self, mock_project, mock_user):
mock_project.return_value = {'id': project_id1, 'name': 'project1'}
return_value = {'username': 'user1', 'is_admin': False, 'id': 1,
'api_key': 'xxxx'}
mock_user.return_value = return_value
mock_user.return_value = fake_resources.USER1
data = {'username': 'user1', 'is_admin': False}
resp = self.post('v1/users', data=data)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.status_code, 201)
self.assertEqual(resp.json['id'], 1)
self.assertIn("Location", resp.headers)
self.assertEqual(
resp.headers['Location'],
"http://localhost/v1/users/1"
)
@mock.patch.object(dbapi, 'users_create')
@mock.patch.object(dbapi, 'projects_get_by_id')
@@ -827,12 +892,22 @@ class APIV1NetworksTest(APIV1Test):
@mock.patch.object(dbapi, 'networks_create')
def test_create_networks_with_valid_data(self, mock_network):
mock_network.return_value = None
data = {'name': 'some network', 'region_id': 1,
'cidr': '10.10.1.0/24', 'gateway': '192.168.1.1',
'netmask': '255.255.255.0'}
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,
}
resp = self.post('/v1/networks', data=data)
self.assertEqual(200, resp.status_code)
self.assertEqual(201, resp.status_code)
self.assertIn('Location', resp.headers)
self.assertEqual(
resp.headers['Location'],
"http://localhost/v1/networks/1"
)
@mock.patch.object(dbapi, 'networks_create')
def test_create_networks_with_invalid_data(self, mock_network):
@@ -1034,11 +1109,16 @@ class APIV1NetworkDevicesTest(APIV1Test):
@mock.patch.object(dbapi, 'network_devices_create')
def test_create_network_devices_with_valid_data(self, mock_devices):
mock_devices.return_value = None
mock_devices.return_value = fake_resources.NETWORK_DEVICE1
data = {'name': 'NewNetDevice1', 'region_id': 1,
'device_type': 'Sample', 'ip_address': '0.0.0.0'}
resp = self.post('/v1/network-devices', data=data)
self.assertEqual(200, resp.status_code)
self.assertEqual(201, resp.status_code)
self.assertIn('Location', resp.headers)
self.assertEqual(
resp.headers['Location'],
"http://localhost/v1/network-devices/1"
)
@mock.patch.object(dbapi, 'network_devices_create')
def test_create_netdevices_with_invalid_data(self, mock_devices):
@@ -1176,10 +1256,15 @@ class APIV1NetworkInterfacesTest(APIV1Test):
data = {'name': 'NewNetInterface', 'device_id': 1,
'ip_address': '10.10.0.1', 'interface_type': 'interface_type1'}
resp = self.post('/v1/network-interfaces', data=data)
self.assertEqual(200, resp.status_code)
self.assertEqual(201, resp.status_code)
self.assertEqual(
resp.json['ip_address'], data['ip_address']
)
self.assertIn("Location", resp.headers)
self.assertEqual(
resp.headers['Location'],
"http://localhost/v1/network-interfaces/1"
)
@mock.patch.object(dbapi, 'network_interfaces_create')
def test_network_interfaces_create_invalid_data(self, fake_interfaces):

View File

@@ -12,7 +12,7 @@ Create Cell
Create a new Cell
Normal response codes: OK(200)
Normal response codes: OK(201)
Error response codes: invalid request(400), validation exception(405)

View File

@@ -13,7 +13,7 @@ Create Host
Create a new host
Normal response codes: OK(200)
Normal response codes: OK(201)
Error response codes: invalid request(400), validation exception(405)

View File

@@ -13,7 +13,7 @@ Create Region
Creates a new Region
Normal response codes: OK(200)
Normal response codes: OK(201)
Error response codes: invalid request(400), validation exception(405)

View File

@@ -66,7 +66,7 @@ class Inventory(object):
print("Creating region entry for %s with data %s" % (payload, data))
resp = requests.post(region_url, headers=self.headers,
data=json.dumps(payload), verify=False)
if resp.status_code != 200:
if resp.status_code != 201:
raise Exception(resp.text)
self.region = resp.json()
@@ -85,7 +85,7 @@ class Inventory(object):
print("Creating cell entry %s with data %s" % (payload, data))
resp = requests.post(region_url, headers=self.headers,
data=json.dumps(payload), verify=False)
if resp.status_code != 200:
if resp.status_code != 201:
raise Exception(resp.text)
self.cell = resp.json()
@@ -109,7 +109,7 @@ class Inventory(object):
device_obj = requests.post(region_url, headers=self.headers,
data=json.dumps(payload), verify=False)
if device_obj.status_code != 200:
if device_obj.status_code != 201:
raise Exception(device_obj.text)
if data:
@@ -135,7 +135,7 @@ class Inventory(object):
print("Creating new network: %s" % payload)
resp = requests.post(networks_url, headers=self.headers,
data=json.dumps(payload), verify=False)
if resp.status_code != 200:
if resp.status_code != 201:
raise Exception(resp.text)
return resp.json()
@@ -153,7 +153,7 @@ class Inventory(object):
resp = requests.post(network_devices_url, headers=self.headers,
data=json.dumps(payload), verify=False)
if resp.status_code != 200:
if resp.status_code != 201:
raise Exception(resp.text)
return resp.json()
@@ -177,7 +177,7 @@ class Inventory(object):
% (name, device.get("id"), network.get("id")))
resp = requests.post(netinterfaces_url, headers=self.headers,
data=json.dumps(payload), verify=False)
if resp.status_code != 200:
if resp.status_code != 201:
raise Exception(resp.text)
return resp.json()