From b9cd851e73b28c2b2149ad7ca61bac7527742aff Mon Sep 17 00:00:00 2001 From: Eishun Kondoh Date: Fri, 11 Nov 2022 02:57:21 +0900 Subject: [PATCH 1/2] blueprint: Add support for GET /api/blueprints/{bp_id}/relationships --- aos/blueprint.py | 38 ++++++++ setup.py | 2 +- .../3.3.0/blueprints/get_relationships.json | 28 ++++++ .../4.0.0/blueprints/get_relationships.json | 28 ++++++ tests/test_blueprint.py | 88 +++++++++++++++++++ 5 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/aos/3.3.0/blueprints/get_relationships.json create mode 100644 tests/fixtures/aos/4.0.0/blueprints/get_relationships.json diff --git a/aos/blueprint.py b/aos/blueprint.py index f81efc5..30574b6 100644 --- a/aos/blueprint.py +++ b/aos/blueprint.py @@ -2238,3 +2238,41 @@ def update_fabric_addressing_policy( url = f'/api/blueprints/{bp_id}/fabric-addressing-policy' return self.rest.patch(url, data=data) + + def get_node_relationships( + self, + bp_id, + relationship_type: str = None, + source_id: str = None, + target_id: str = None + ): + """ + Return node relationships in a given blueprint + + Parameters + --------- + bp_id + (str) - ID of AOS Blueprint + relationship_type + (str) - (optional) type of relationship + source_id + (str) - (optional) ID of source of relationship + target_id + (str) - (optional) ID of target of relationship + """ + + url = f'/api/blueprints/{bp_id}/relationships' + queries = [] + + if relationship_type: + queries.append(f'relationship_type={relationship_type}') + if source_id: + queries.append(f'source_id={source_id}') + if target_id: + queries.append(f'target_id={target_id}') + + if queries: + query_str = '&'.join(queries) + url += f'?{query_str}' + + return self.rest.json_resp_get(url)['relationships'] diff --git a/setup.py b/setup.py index 4c2fd9d..7887771 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ NAME = "apstra-api-python" -VERSION = '0.2.0' +VERSION = '0.2.1' REQUIRES = (["requests==2.24.0"],) diff --git a/tests/fixtures/aos/3.3.0/blueprints/get_relationships.json b/tests/fixtures/aos/3.3.0/blueprints/get_relationships.json new file mode 100644 index 0000000..ee63cae --- /dev/null +++ b/tests/fixtures/aos/3.3.0/blueprints/get_relationships.json @@ -0,0 +1,28 @@ +{ + "relationships": { + "nnJEIevQlNSjwAgVE3I": { + "tags": null, + "source_id": "y0D9CFzGPmBmGILP3Mk", + "target_id": "C36GOMzvZZW1uQqGQQY", + "property_set": null, + "type": "composed_of_systems", + "id": "nnJEIevQlNSjwAgVE3I" + }, + "jbQgo_63gfCxgiKIe6g": { + "tags": null, + "source_id": "Generic_Server_1RU_2x10G_Centos__AOS-2x10-1", + "target_id": "Nw5QmnTrk8-0_5ISLWs", + "property_set": null, + "type": "device_profile", + "id": "jbQgo_63gfCxgiKIe6g" + }, + "TqXFFIYLduOVpqW9eU0": { + "tags": null, + "source_id": "xX00o5Alv6f6WGbMprw", + "target_id": "bTqiHdi9nhVWqt1jhUE", + "property_set": null, + "type": "composed_of", + "id": "TqXFFIYLduOVpqW9eU0" + } + } +} diff --git a/tests/fixtures/aos/4.0.0/blueprints/get_relationships.json b/tests/fixtures/aos/4.0.0/blueprints/get_relationships.json new file mode 100644 index 0000000..ee63cae --- /dev/null +++ b/tests/fixtures/aos/4.0.0/blueprints/get_relationships.json @@ -0,0 +1,28 @@ +{ + "relationships": { + "nnJEIevQlNSjwAgVE3I": { + "tags": null, + "source_id": "y0D9CFzGPmBmGILP3Mk", + "target_id": "C36GOMzvZZW1uQqGQQY", + "property_set": null, + "type": "composed_of_systems", + "id": "nnJEIevQlNSjwAgVE3I" + }, + "jbQgo_63gfCxgiKIe6g": { + "tags": null, + "source_id": "Generic_Server_1RU_2x10G_Centos__AOS-2x10-1", + "target_id": "Nw5QmnTrk8-0_5ISLWs", + "property_set": null, + "type": "device_profile", + "id": "jbQgo_63gfCxgiKIe6g" + }, + "TqXFFIYLduOVpqW9eU0": { + "tags": null, + "source_id": "xX00o5Alv6f6WGbMprw", + "target_id": "bTqiHdi9nhVWqt1jhUE", + "property_set": null, + "type": "composed_of", + "id": "TqXFFIYLduOVpqW9eU0" + } + } +} diff --git a/tests/test_blueprint.py b/tests/test_blueprint.py index a421524..eaab890 100644 --- a/tests/test_blueprint.py +++ b/tests/test_blueprint.py @@ -1623,3 +1623,91 @@ def test_update_fabric_addressing_policy( params=None, headers=expected_auth_headers, ) + + +def test_get_node_relationships( + aos_logged_in, aos_session, expected_auth_headers, aos_api_version +): + bp_id = 'test-bp-1' + url = f"http://aos:80/api/blueprints/{bp_id}/relationships" + aos_session.add_response( + 'GET', + url, + status=200, + resp=read_fixture( + f'aos/{aos_api_version}/blueprints/' f'get_relationships.json' + ), + ) + aos_logged_in.blueprint.get_node_relationships(bp_id=bp_id) + + url = ( + f"http://aos:80/api/blueprints/{bp_id}/relationships?" + "relationship_type=composed_of_systems" + ) + aos_session.add_response( + 'GET', + url, + status=200, + resp=read_fixture( + f'aos/{aos_api_version}/blueprints/' f'get_relationships.json' + ), + ) + aos_logged_in.blueprint.get_node_relationships( + bp_id=bp_id, + relationship_type='composed_of_systems' + ) + + url = ( + f"http://aos:80/api/blueprints/{bp_id}/relationships?" + "source_id=y0D9CFzGPmBmGILP3Mk" + ) + aos_session.add_response( + 'GET', + url, + status=200, + resp=read_fixture( + f'aos/{aos_api_version}/blueprints/' f'get_relationships.json' + ), + ) + aos_logged_in.blueprint.get_node_relationships( + bp_id=bp_id, + source_id='y0D9CFzGPmBmGILP3Mk' + ) + + url = ( + f"http://aos:80/api/blueprints/{bp_id}/relationships?" + "target_id=C36GOMzvZZW1uQqGQQY" + ) + aos_session.add_response( + 'GET', + url, + status=200, + resp=read_fixture( + f'aos/{aos_api_version}/blueprints/' f'get_relationships.json' + ), + ) + aos_logged_in.blueprint.get_node_relationships( + bp_id=bp_id, + target_id='C36GOMzvZZW1uQqGQQY' + ) + + url = ( + f"http://aos:80/api/blueprints/{bp_id}/relationships?" + "relationship_type=composed_of_systems&" + "source_id=y0D9CFzGPmBmGILP3Mk&" + "target_id=C36GOMzvZZW1uQqGQQY" + ) + aos_session.add_response( + 'GET', + url, + status=200, + resp=read_fixture( + f'aos/{aos_api_version}/blueprints/' f'get_relationships.json' + ), + ) + aos_logged_in.blueprint.get_node_relationships( + bp_id=bp_id, + relationship_type='composed_of_systems', + source_id='y0D9CFzGPmBmGILP3Mk', + target_id='C36GOMzvZZW1uQqGQQY' + ) From 9bc3df2f7a6484c4e56a9b3976dc4d9a513437b5 Mon Sep 17 00:00:00 2001 From: shun159 Date: Fri, 23 Dec 2022 14:47:50 +0900 Subject: [PATCH 2/2] blueprint: fix to use params instead of querystring --- aos/blueprint.py | 18 ++++++----------- tests/test_blueprint.py | 43 ++++++++++++++++++++++++----------------- 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/aos/blueprint.py b/aos/blueprint.py index 30574b6..d367f48 100644 --- a/aos/blueprint.py +++ b/aos/blueprint.py @@ -2262,17 +2262,11 @@ def get_node_relationships( """ url = f'/api/blueprints/{bp_id}/relationships' - queries = [] - if relationship_type: - queries.append(f'relationship_type={relationship_type}') - if source_id: - queries.append(f'source_id={source_id}') - if target_id: - queries.append(f'target_id={target_id}') - - if queries: - query_str = '&'.join(queries) - url += f'?{query_str}' + params = { + 'relationship_type': relationship_type, + 'source_id': source_id, + 'target_id': target_id + } - return self.rest.json_resp_get(url)['relationships'] + return self.rest.json_resp_get(url, params=params)['relationships'] diff --git a/tests/test_blueprint.py b/tests/test_blueprint.py index eaab890..2c0d853 100644 --- a/tests/test_blueprint.py +++ b/tests/test_blueprint.py @@ -1634,20 +1634,26 @@ def test_get_node_relationships( 'GET', url, status=200, + params={ + 'relationship_type': None, + 'source_id': None, + 'target_id': None + }, resp=read_fixture( f'aos/{aos_api_version}/blueprints/' f'get_relationships.json' ), ) aos_logged_in.blueprint.get_node_relationships(bp_id=bp_id) - url = ( - f"http://aos:80/api/blueprints/{bp_id}/relationships?" - "relationship_type=composed_of_systems" - ) aos_session.add_response( 'GET', url, status=200, + params={ + 'relationship_type': 'composed_of_systems', + 'source_id': None, + 'target_id': None + }, resp=read_fixture( f'aos/{aos_api_version}/blueprints/' f'get_relationships.json' ), @@ -1657,14 +1663,15 @@ def test_get_node_relationships( relationship_type='composed_of_systems' ) - url = ( - f"http://aos:80/api/blueprints/{bp_id}/relationships?" - "source_id=y0D9CFzGPmBmGILP3Mk" - ) aos_session.add_response( 'GET', url, status=200, + params={ + 'relationship_type': None, + 'source_id': 'y0D9CFzGPmBmGILP3Mk', + 'target_id': None + }, resp=read_fixture( f'aos/{aos_api_version}/blueprints/' f'get_relationships.json' ), @@ -1674,14 +1681,15 @@ def test_get_node_relationships( source_id='y0D9CFzGPmBmGILP3Mk' ) - url = ( - f"http://aos:80/api/blueprints/{bp_id}/relationships?" - "target_id=C36GOMzvZZW1uQqGQQY" - ) aos_session.add_response( 'GET', url, status=200, + params={ + 'relationship_type': None, + 'source_id': None, + 'target_id': 'C36GOMzvZZW1uQqGQQY' + }, resp=read_fixture( f'aos/{aos_api_version}/blueprints/' f'get_relationships.json' ), @@ -1691,16 +1699,15 @@ def test_get_node_relationships( target_id='C36GOMzvZZW1uQqGQQY' ) - url = ( - f"http://aos:80/api/blueprints/{bp_id}/relationships?" - "relationship_type=composed_of_systems&" - "source_id=y0D9CFzGPmBmGILP3Mk&" - "target_id=C36GOMzvZZW1uQqGQQY" - ) aos_session.add_response( 'GET', url, status=200, + params={ + 'relationship_type': 'composed_of_systems', + 'source_id': 'y0D9CFzGPmBmGILP3Mk', + 'target_id': 'C36GOMzvZZW1uQqGQQY' + }, resp=read_fixture( f'aos/{aos_api_version}/blueprints/' f'get_relationships.json' ),