-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.py
More file actions
121 lines (107 loc) · 4.17 KB
/
proxy.py
File metadata and controls
121 lines (107 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import requests
from requests.exceptions import HTTPError, ConnectionError
from nio.util.logging import get_nio_logger
class DeploymentProxy(object):
""" Serves as a Proxy to make Product API configuration requests
"""
def __init__(self, url_prefix, manager):
super().__init__()
self.logger = get_nio_logger("DeploymentManager")
self._url_prefix = url_prefix
self._manager = manager
def get_instance_config_ids(self):
""" Gets the conf id and conf version id the instance should be running
Returns: an object with format
{
"instance_configuration_id": "uuid..",
"instance_configuration_version_id": "uuid..",
"deployment_id": "deployment_id..."
}
"""
url = "{}/instances/{}/configuration".format(
self._url_prefix, self._manager.instance_id)
try:
return self._request(
fn=requests.get,
url=url,
failed_msg=("Failed to get configuration version the instance "
"should be running")
)
except HTTPError as e:
if e.response.status_code == 404:
# This likely indicates that no desired configuration was
# found for this particular instance ID, nothing to cause alarm
self.logger.info(e.response.json().get("message"))
else:
self.logger.error("Error fetching instance config: {}".format(
e.response.json().get(
"message", "No error message provided")))
raise
def set_reported_configuration(
self,
config_id,
config_version_id,
deployment_id,
status="",
message=""):
""" Posts instance config ids and status
Args:
config_id (str): instance configuration id
config_version_id (str): instance configuration version id
deployment_id (str): Deployment id that the status
and message belongs to
status (str): deployment status
message (str): deployment message
"""
url = "{}/instances/{}/configuration".format(
self._url_prefix, self._manager.instance_id)
body = {
"reported_configuration_id": config_id,
"reported_configuration_version_id": config_version_id,
"deployment_id": deployment_id,
"status": status,
"message": message,
}
return self._request(
fn=requests.post,
url=url,
failed_msg=("Failed to post new configuration version "
"instance is running"),
json=body
)
def get_configuration(self, config_id, config_version_id):
""" Retrieves an instance configuration
Args:
config_id (str): instance configuration id
config_version_id (str): instance configuration version id
Returns: configuration with format
{
"configuration_data": {
"version": 1.0.0,
"blocks": {...},
"services": {...},
"blockTypes": {...}
},
"message": "Found Instance Configuration...",
"status": 200,
"uuid": "uuid..",
"version_num": "v1.0.0"
}
"""
url = "{}/instance_configurations/{}/versions/{}".format(
self._url_prefix, config_id, config_version_id)
return self._request(
fn=requests.get,
url=url,
failed_msg="Failed to get instance configuration")
def _request(self, fn, url, failed_msg, **kwargs):
headers = {
"authorization": "apikey {}".format(self._manager.api_key),
"content-type": "application/json"
}
try:
response = fn(url, headers=headers, **kwargs)
except ConnectionError:
self.logger.exception(failed_msg)
response.raise_for_status()
return response.json()