codecoverage improvement
[dcaegen2/platform.git] / mod / distributorapi / distributor / runtime_client.py
1 # ============LICENSE_START=======================================================
2 # Copyright (c) 2019-2022 AT&T Intellectual Property. All rights reserved.
3 # ================================================================================
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 # ============LICENSE_END=========================================================
16 """Runtime API client"""
17
18 import requests as reqs
19 from distributor import errors
20 from distributor.utils import urljoin, get_json
21
22
23 def get_graph(runtime_url, graph_id):
24     # REVIEW: There's only support for one graph right now..
25     url = urljoin(runtime_url, "api/graph/main")
26     return get_json(url)
27
28
29 def create_graph(runtime_url, graph_id, graph_name):
30     url = urljoin(runtime_url, "api/graph/main")
31
32     resp = reqs.post(url, json={"name": graph_name, "id": graph_id, "description": "", "main": True})
33
34     try:
35         resp.raise_for_status()
36     except Exception as e:
37         raise errors.DistributorAPIError(e)
38
39
40 def delete_graph(runtime_url):
41     url = urljoin(runtime_url, "api/graph/main")
42
43     try:
44         reqs.delete(url).raise_for_status()
45     except Exception as e:
46         raise errors.DistributorAPIError(e)
47
48
49 def post_graph(runtime_url, graph_id, actions):
50     url = urljoin(runtime_url, "api/graph", graph_id, "distribute")
51     graph_request = {"actions": actions}
52
53     resp = reqs.post(url, json=graph_request)
54
55     try:
56         resp.raise_for_status()
57         # REVIEW: Should be blueprint
58         return resp.json()
59     except Exception as e:
60         with open("runtime-request-failed.json", "w+") as f:
61             import json
62
63             json.dump(graph_request, f)
64         raise errors.DistributorAPIError(e)
65
66
67 def ensure_graph(runtime_url, pg_id, pg_name, max_attempts=6):
68     """Ensures the graph with the specified id will exist"""
69     # TODO: Remove this when runtime API more mature
70     # Added this attempted delete call here to make sure repeated calls to post
71     # flows works by making sure the runtime API main graph is always empty
72     try:
73         delete_graph(runtime_url)
74     except:
75         # Probably a 404, doesn't matter
76         pass
77
78     # The attempts are not *really* attempts because attempts equates to looping
79     # twice
80     for i in range(0, max_attempts):
81         resp = None
82
83         try:
84             resp = get_graph(runtime_url, pg_id)
85         except Exception as e:
86             # Assuming you are here because graph needs to be created
87             create_graph(runtime_url, pg_id, pg_name)
88
89         # TODO: Runtime api only supports 1 graph which is why this check is
90         # here. Make sure it will support many graphs and remove this
91
92         if resp == None:
93             # You are here because the graph was created for first time or
94             # the graph was deleted then created. Anyways next loop should
95             # check if it got created ok
96             continue
97         elif resp != None and resp["id"] != pg_id:
98             delete_graph(runtime_url)
99         elif resp != None and resp["id"] == pg_id:
100             return True
101
102     return False