-
Notifications
You must be signed in to change notification settings - Fork 8
/
variable.py
358 lines (286 loc) · 12.5 KB
/
variable.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# Copyright 2016 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Create / interact with Google Cloud RuntimeConfig variables.
.. data:: STATE_UNSPECIFIED
The default variable state. See
https://cloud.google.com/deployment-manager/runtime-configurator/reference/rest/v1beta1/projects.configs.variables#VariableState
.. data:: STATE_UPDATED
Indicates the variable was updated, while `variables.watch` was executing.
See
https://cloud.google.com/deployment-manager/runtime-configurator/reference/rest/v1beta1/projects.configs.variables#VariableState
.. data:: STATE_DELETED
Indicates the variable was deleted, while `variables.watch`_ was executing.
See
https://cloud.google.com/deployment-manager/runtime-configurator/reference/rest/v1beta1/projects.configs.variables#VariableState
.. _variables.watch:
https://cloud.google.com/deployment-manager/runtime-configurator/reference/rest/v1beta1/projects.configs.variables/watch
"""
import base64
import datetime
from google.api_core import datetime_helpers
from google.cloud.exceptions import Conflict, NotFound
from google.cloud.runtimeconfig._helpers import variable_name_from_full_name
from google.cloud.runtimeconfig.exceptions import Error
STATE_UNSPECIFIED = "VARIABLE_STATE_UNSPECIFIED"
STATE_UPDATED = "UPDATED"
STATE_DELETED = "DELETED"
class Variable(object):
"""A variable in the Cloud RuntimeConfig service.
See
https://cloud.google.com/deployment-manager/runtime-configurator/reference/rest/v1beta1/projects.configs.variables
:type name: str
:param name: The name of the variable. This corresponds to the
unique path of the variable in the config.
:type config: :class:`google.cloud.runtimeconfig.config.Config`
:param config: The config to which this variable belongs.
"""
def __init__(self, name, config):
self.name = name
self.config = config
self._properties = {}
@classmethod
def from_api_repr(cls, resource, config):
"""Factory: construct a Variable given its API representation
:type resource: dict
:param resource: change set representation returned from the API.
:type config: :class:`google.cloud.runtimeconfig.config.Config`
:param config: The config to which this variable belongs.
:rtype: :class:`google.cloud.runtimeconfig.variable.Variable`
:returns: Variable parsed from ``resource``.
"""
name = variable_name_from_full_name(resource.get("name"))
variable = cls(name=name, config=config)
variable._set_properties(resource=resource)
return variable
@property
def full_name(self):
"""Fully-qualified name of this variable.
Example:
``projects/my-project/configs/my-config/variables/my-var``
:rtype: str
:returns: The full name based on config and variable names.
:raises: :class:`ValueError` if the variable is missing a name.
"""
if not self.name:
raise ValueError("Missing variable name.")
return "%s/variables/%s" % (self.config.full_name, self.name)
@property
def path(self):
"""URL path for the variable's APIs.
:rtype: str
:returns: The URL path based on config and variable names.
"""
return "/%s" % (self.full_name,)
@property
def client(self):
"""The client bound to this variable."""
return self.config.client
@property
def text(self):
"""Text of the variable, as string.
See
https://cloud.google.com/deployment-manager/runtime-configurator/reference/rest/v1beta1/projects.configs.variables
:rtype: str or ``NoneType``
:returns: The text of the variable or ``None`` if the property
is not set locally.
"""
return self._properties.get("text")
@text.setter
def text(self, value):
"""Set text property.
If the variable is already using value, this will raise
exceptions.Error since text and value are mutually exclusive.
To persist the change, call create() or update().
:type value: str
:param value: The new value for the text property.
"""
if "value" in self._properties:
raise Error("Value and text are mutually exclusive.")
self._properties["text"] = value
@property
def value(self):
"""Value of the variable, as bytes.
See
https://cloud.google.com/deployment-manager/runtime-configurator/reference/rest/v1beta1/projects.configs.variables
:rtype: bytes or ``NoneType``
:returns: The value of the variable or ``None`` if the property
is not set locally.
"""
value = self._properties.get("value")
if value is not None:
value = base64.b64decode(value)
return value
@value.setter
def value(self, value):
"""Set value property.
If the variable is already using text, this will raise exceptions.Error
since text and value are mutually exclusive.
To persist the change, call create() or update().
:type value: bytes
:param value: The new value for the value property.
"""
if "text" in self._properties:
raise Error("Value and text are mutually exclusive.")
self._properties["value"] = value
@property
def state(self):
"""Retrieve the state of the variable.
See
https://cloud.google.com/deployment-manager/runtime-configurator/reference/rest/v1beta1/projects.configs.variables#VariableState
:rtype: str
:returns:
If set, one of "UPDATED", "DELETED", or defaults to
"VARIABLE_STATE_UNSPECIFIED".
"""
return self._properties.get("state", STATE_UNSPECIFIED)
@property
def update_time(self):
"""Retrieve the timestamp at which the variable was updated.
See
https://cloud.google.com/deployment-manager/runtime-configurator/reference/rest/v1beta1/projects.configs.variables
Returns:
:class:`~api_core.datetime_helpers.DatetimeWithNanoseconds`,
:class:`datetime.datetime` or ``NoneType``:
Datetime object parsed from RFC3339 valid timestamp, or
``None`` if the property is not set locally.
Raises:
ValueError: if value is not a valid RFC3339 timestamp
"""
value = self._properties.get("updateTime")
if value is not None:
try:
value = datetime.datetime.strptime(
value, datetime_helpers._RFC3339_MICROS
)
except ValueError:
DatetimeNS = datetime_helpers.DatetimeWithNanoseconds
value = DatetimeNS.from_rfc3339(value)
naive = value.tzinfo is None or value.tzinfo.utcoffset(value) is None
if naive:
value = value.astimezone(tz=datetime.timezone.utc)
return value
def _require_client(self, client):
"""Check client or verify over-ride.
:type client: :class:`google.cloud.runtimconfig.client.Client`
:param client:
(Optional) The client to use. If not passed, falls back to the
``client`` stored on the current zone.
:rtype: :class:`google.cloud.runtimeconfig.client.Client`
:returns: The client passed in or the currently bound client.
"""
if client is None:
client = self.client
return client
def _set_properties(self, resource):
"""Update properties from resource in body of ``api_response``
:type resource: dict
:param resource: variable representation returned from the API.
"""
self._properties.clear()
cleaned = resource.copy()
if "name" in cleaned:
self.name = variable_name_from_full_name(cleaned.pop("name"))
self._properties.update(cleaned)
def _get_payload(self):
"""Return the payload for create and update operations
:rtype: dict
:returns: payload for API call with name and text or value attributes
"""
data = {"name": self.full_name}
if "text" in self._properties:
data["text"] = self._properties["text"]
elif "value" in self._properties:
value = self._properties["value"]
data["value"] = base64.b64encode(value).decode("utf-8")
else:
raise Error("No text or value set.")
return data
def create(self, client=None):
"""API call: create the variable via a POST request
See
https://cloud.google.com/deployment-manager/runtime-configurator/reference/rest/v1beta1/projects.configs.variables/create
:type client: :class:`~google.cloud.runtimeconfig.client.Client`
:param client:
(Optional) The client to use. If not passed, falls back to the
``client`` stored on the variable's config.
:rtype: bool
:returns: True if the variable has been created, False on error.
"""
client = self._require_client(client)
path = "%s/variables" % self.config.path
data = self._get_payload()
try:
resp = client._connection.api_request(method="POST", path=path, data=data)
except Conflict:
return False
self._set_properties(resp)
return True
def update(self, client=None):
"""API call: update the variable via a PUT request
See
https://cloud.google.com/deployment-manager/runtime-configurator/reference/rest/v1beta1/projects.configs.variables/update
:type client: :class:`~google.cloud.runtimeconfig.client.Client`
:param client:
(Optional) The client to use. If not passed, falls back to the
``client`` stored on the variable's config.
:rtype: bool
:returns: True if the variable has been created, False on error.
"""
client = self._require_client(client)
data = self._get_payload()
try:
resp = client._connection.api_request(
method="PUT", path=self.path, data=data
)
except NotFound:
return False
self._set_properties(resp)
return True
def exists(self, client=None):
"""API call: test for the existence of the variable via a GET request
See
https://cloud.google.com/deployment-manager/runtime-configurator/reference/rest/v1beta1/projects.configs.variables/get
:type client: :class:`~google.cloud.runtimeconfig.client.Client`
:param client:
(Optional) The client to use. If not passed, falls back to the
``client`` stored on the variable's config.
:rtype: bool
:returns: True if the variable exists in Cloud RuntimeConfig.
"""
client = self._require_client(client)
try:
# We only need the status code (200 or not) so we seek to
# minimize the returned payload.
query_params = {"fields": "name"}
client._connection.api_request(
method="GET", path=self.path, query_params=query_params
)
return True
except NotFound:
return False
def reload(self, client=None):
"""API call: reload the variable via a ``GET`` request.
This method will reload the newest data for the variable.
See
https://cloud.google.com/deployment-manager/runtime-configurator/reference/rest/v1beta1/projects.configs/get
:type client: :class:`google.cloud.runtimeconfig.client.Client`
:param client:
(Optional) The client to use. If not passed, falls back to the
client stored on the current config.
"""
client = self._require_client(client)
# We assume the variable exists. If it doesn't it will raise a NotFound
# exception.
resp = client._connection.api_request(method="GET", path=self.path)
self._set_properties(resource=resp)