Skip to content
Snippets Groups Projects
Commit 230f3234 authored by Benjamin Bertrand's avatar Benjamin Bertrand
Browse files

Add /network/domains endpoint

parent 23448e02
No related branches found
No related tags found
No related merge requests found
...@@ -164,3 +164,28 @@ def create_macs(): ...@@ -164,3 +164,28 @@ def create_macs():
""" """
return create_generic_model(models.Mac, return create_generic_model(models.Mac,
mandatory_fields=('address',)) mandatory_fields=('address',))
@bp.route('/domains')
@jwt_required
def get_domains():
"""Return domains
.. :quickref: Network; Get domains
"""
return get_generic_model(models.Domain,
order_by=models.Domain.name)
@bp.route('/domains', methods=['POST'])
@jwt_required
@jwt_groups_accepted('admin')
def create_domain():
"""Create a new domain
.. :quickref: Network; Create new domain
:jsonparam name: domain name
"""
return create_generic_model(models.Domain,
mandatory_fields=('name',))
...@@ -27,6 +27,7 @@ ENDPOINT_MODEL = { ...@@ -27,6 +27,7 @@ ENDPOINT_MODEL = {
'network/interfaces': models.Interface, 'network/interfaces': models.Interface,
'network/hosts': models.Host, 'network/hosts': models.Host,
'network/macs': models.Mac, 'network/macs': models.Mac,
'network/domains': models.Domain,
} }
GENERIC_GET_ENDPOINTS = [key for key in ENDPOINT_MODEL.keys() GENERIC_GET_ENDPOINTS = [key for key in ENDPOINT_MODEL.keys()
if key.startswith('inventory') and key != 'inventory/items'] if key.startswith('inventory') and key != 'inventory/items']
...@@ -765,3 +766,31 @@ def test_get_user_profile(client, readonly_token): ...@@ -765,3 +766,31 @@ def test_get_user_profile(client, readonly_token):
assert user['username'] == 'user_ro' assert user['username'] == 'user_ro'
assert user['display_name'] == 'User RO' assert user['display_name'] == 'User RO'
assert user['email'] == 'user_ro@example.com' assert user['email'] == 'user_ro@example.com'
def test_get_domains(client, domain_factory, readonly_token):
# Create some domains
domain1 = domain_factory()
domain2 = domain_factory()
response = get(client, f'{API_URL}/network/domains', token=readonly_token)
assert response.status_code == 200
assert len(response.json) == 2
check_input_is_subset_of_response(response, (domain1.to_dict(), domain2.to_dict()))
def test_create_domain(client, admin_token):
# check that name is mandatory
response = post(client, f'{API_URL}/network/domains', data={}, token=admin_token)
check_response_message(response, "Missing mandatory field 'name'", 422)
data = {'name': 'tn.esss.lu.se'}
response = post(client, f'{API_URL}/network/domains', data=data, token=admin_token)
assert response.status_code == 201
assert {'id', 'name', 'scopes',
'networks', 'created_at',
'updated_at', 'user'} == set(response.json.keys())
assert response.json['name'] == data['name']
# Check that name shall be unique
response = post(client, f'{API_URL}/network/domains', data=data, token=admin_token)
check_response_message(response, '(psycopg2.IntegrityError) duplicate key value violates unique constraint', 422)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment