|
1
|
1 |
|
import requests |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
1 |
|
class NewRelicException(Exception): |
|
5
|
1 |
|
pass |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
1 |
|
class NewRelicDeploymentException(NewRelicException): |
|
9
|
1 |
|
pass |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
1 |
|
class Deployment(object): |
|
13
|
1 |
|
API_HOST_US = 'api.newrelic.com' |
|
14
|
1 |
|
API_HOST_EU = 'api.eu.newrelic.com' |
|
15
|
1 |
|
ENDPOINT = 'https://%(host)s/v2/applications/%(app_id)s/deployments.json' |
|
16
|
|
|
|
|
17
|
1 |
|
def __init__(self, api_key, app_id, user, region): |
|
18
|
1 |
|
self.__api_key = api_key |
|
19
|
1 |
|
self.__app_id = app_id |
|
20
|
1 |
|
self.__user = user |
|
21
|
1 |
|
self.__region = region.lower() if region else 'us' |
|
22
|
|
|
|
|
23
|
1 |
|
@property |
|
24
|
|
|
def endpoint(self): |
|
25
|
1 |
|
if self.__region == 'eu': |
|
26
|
1 |
|
host = self.API_HOST_EU |
|
27
|
|
|
else: |
|
28
|
1 |
|
host = self.API_HOST_US |
|
29
|
1 |
|
return self.ENDPOINT % dict(host=host, app_id=self.__app_id) |
|
30
|
|
|
|
|
31
|
1 |
|
@property |
|
32
|
|
|
def headers(self): |
|
33
|
1 |
|
return { |
|
34
|
|
|
'Content-Type': 'application/json', |
|
35
|
|
|
'X-Api-Key': self.__api_key |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
def get_payload(self, revision, changelog, description): |
|
39
|
1 |
|
return { |
|
40
|
|
|
"deployment" : { |
|
41
|
|
|
"revision": str(revision), |
|
42
|
|
|
"changelog": str(changelog), |
|
43
|
|
|
"description": str(description), |
|
44
|
|
|
"user": str(self.__user) |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
def deploy(self, revision, changelog, description): |
|
49
|
1 |
|
payload = self.get_payload(revision, changelog, description) |
|
50
|
1 |
|
response = requests.post(self.endpoint, headers=self.headers, json=payload) |
|
51
|
|
|
|
|
52
|
1 |
|
if response.status_code != 201: |
|
53
|
1 |
|
try: |
|
54
|
1 |
|
raise NewRelicDeploymentException('Recording deployment failed: %s' % |
|
55
|
|
|
response.json()['error']['title']) |
|
56
|
1 |
|
except (AttributeError, KeyError): |
|
57
|
1 |
|
raise NewRelicDeploymentException('Recording deployment failed') |
|
58
|
|
|
|
|
59
|
|
|
return response |
|
60
|
|
|
|