5
5
Author: Martin Mikita <[email protected] >
6
6
"""
7
7
8
- import json
9
- import errno
10
- import datetime
11
-
12
- from time import sleep
13
-
14
8
try :
15
9
import yaml
16
10
from googleapiclient .discovery import build
@@ -38,14 +32,16 @@ def __init__(self):
38
32
self .client_ce = build ('compute' , 'v1' , credentials = self .credentials )
39
33
40
34
41
- def deployment (self , name , projectId = None ):
42
- return Deployment (name , projectId , self .client_dm , self .client_ce , self .credentials )
35
+ def deployment (self , name , project_id = None ):
36
+ return Deployment (name , project_id , self .client_dm ,
37
+ self .client_ce , self .credentials )
43
38
44
39
45
40
class Deployment (object ):
46
41
47
42
def _format_rfc3339 (self , dt ):
48
- """Formats a datetime per RFC 3339.
43
+ """Format a datetime per RFC 3339.
44
+
49
45
:param dt: Datetime instanec to format, defaults to utcnow
50
46
"""
51
47
return dt .isoformat ("T" ) + "Z"
@@ -88,7 +84,6 @@ def name(self):
88
84
def create (self , preview = None ):
89
85
content = {"resources" : self .resources }
90
86
91
-
92
87
body = {
93
88
"target" : {
94
89
"config" : {
@@ -98,7 +93,7 @@ def create(self, preview=None):
98
93
"name" : self .deploymentName
99
94
}
100
95
try :
101
- if self .has ():
96
+ if self .exists ():
102
97
deploymentState = self .get ()
103
98
fingerprint = deploymentState .get ('fingerprint' )
104
99
body .update ({"fingerprint" : fingerprint })
@@ -134,21 +129,24 @@ def get(self):
134
129
return None
135
130
136
131
137
- def has (self ):
132
+ def exists (self ):
138
133
response = None
139
134
try :
140
135
response = self .get ()
141
136
except Exception :
142
137
pass
143
- return True if response is not None else False
138
+ return response is not None
144
139
145
140
146
141
def has_error (self ):
147
142
deploymentState = self .get ()
148
143
if not deploymentState :
149
144
return True
145
+ # Deployment object should have 'operation' key
146
+ # with an 'error' key in case of the invalid deployment
150
147
operation = deploymentState .get ('operation' , {
151
- 'error' : 'missing operation' })
148
+ 'error' : 'missing operation'
149
+ })
152
150
return 'error' in operation
153
151
154
152
@@ -224,7 +222,8 @@ def addInstanceManagedAutoscaler(self, name, groupName, numRange, coolDown=300,
224
222
225
223
226
224
def addInstanceManagedAutoscalerMetric (self , name , groupName , numRange ,
227
- metricName , metricTarget , metricTargetType , coolDown = 300 ):
225
+ metricName , metricTarget ,
226
+ metricTargetType , coolDown = 300 ):
228
227
utilization = {
229
228
"customMetricUtilizations" : [
230
229
{
@@ -238,7 +237,7 @@ def addInstanceManagedAutoscalerMetric(self, name, groupName, numRange,
238
237
239
238
240
239
def runningInstances (self , groupName ):
241
- if not self .has ():
240
+ if not self .exists ():
242
241
return 0
243
242
try :
244
243
request = self .client_ce .instanceGroupManagers ().get (
0 commit comments