-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
newrelic.py
59 lines (46 loc) · 1.76 KB
/
newrelic.py
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
import requests
class NewRelicException(Exception):
pass
class NewRelicDeploymentException(NewRelicException):
pass
class Deployment(object):
API_HOST_US = 'api.newrelic.com'
API_HOST_EU = 'api.eu.newrelic.com'
ENDPOINT = 'https://%(host)s/v2/applications/%(app_id)s/deployments.json'
def __init__(self, api_key, app_id, user, region):
self.__api_key = api_key
self.__app_id = app_id
self.__user = user
self.__region = region.lower() if region else 'us'
@property
def endpoint(self):
if self.__region == 'eu':
host = self.API_HOST_EU
else:
host = self.API_HOST_US
return self.ENDPOINT % dict(host=host, app_id=self.__app_id)
@property
def headers(self):
return {
'Content-Type': 'application/json',
'X-Api-Key': self.__api_key
}
def get_payload(self, revision, changelog, description):
return {
"deployment" : {
"revision": str(revision),
"changelog": str(changelog),
"description": str(description),
"user": str(self.__user)
}
}
def deploy(self, revision, changelog, description):
payload = self.get_payload(revision, changelog, description)
response = requests.post(self.endpoint, headers=self.headers, json=payload)
if response.status_code != 201:
try:
raise NewRelicDeploymentException('Recording deployment failed: %s' %
response.json()['error']['title'])
except (AttributeError, KeyError):
raise NewRelicDeploymentException('Recording deployment failed')
return response