1 # ============LICENSE_START=======================================================
2 # Copyright (c) 2019 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
8 # http://www.apache.org/licenses/LICENSE-2.0
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"""
18 import requests as reqs
19 from distributor import errors
20 from distributor.utils import urljoin, get_json
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")
29 def create_graph(runtime_url, graph_id, graph_name):
30 url = urljoin(runtime_url, "api/graph/main")
32 resp = reqs.post(url, json={"name": graph_name, "id": graph_id
33 , "description": "", "main": True})
36 resp.raise_for_status()
37 except Exception as e:
38 raise errors.DistributorAPIError(e)
41 def delete_graph(runtime_url):
42 url = urljoin(runtime_url, "api/graph/main")
45 reqs.delete(url).raise_for_status()
46 except Exception as e:
47 raise errors.DistributorAPIError(e)
50 def post_graph(runtime_url, graph_id, actions):
51 url = urljoin(runtime_url, "api/graph", graph_id, "distribute")
52 graph_request = {"actions": actions}
54 resp = reqs.post(url, json=graph_request)
57 resp.raise_for_status()
58 # REVIEW: Should be blueprint
60 except Exception as e:
61 with open("runtime-request-failed.json", "w+") as f:
63 json.dump(graph_request, f)
64 raise errors.DistributorAPIError(e)
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
73 delete_graph(runtime_url)
75 # Probably a 404, doesn't matter
78 # The attempts are not *really* attempts because attempts equates to looping
80 for i in range(0, max_attempts):
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)
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
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
97 elif resp != None and resp["id"] != pg_id:
98 delete_graph(runtime_url)
99 elif resp != None and resp["id"] == pg_id: