forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchanges.py
More file actions
277 lines (225 loc) · 8.88 KB
/
changes.py
File metadata and controls
277 lines (225 loc) · 8.88 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
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
# Copyright 2015 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.
"""Define API ResourceRecordSets."""
from google.cloud._helpers import _rfc3339_to_datetime
from google.cloud.exceptions import NotFound
from google.cloud.dns.resource_record_set import ResourceRecordSet
class Changes(object):
"""Changes are bundled additions / deletions of DNS resource records.
Changes are owned by a :class:`google.cloud.dns.zone.ManagedZone` instance.
See
https://cloud.google.com/dns/api/v1/changes
:type zone: :class:`google.cloud.dns.zone.ManagedZone`
:param zone: A zone which holds one or more record sets.
"""
def __init__(self, zone):
self.zone = zone
self._properties = {}
self._additions = self._deletions = ()
@classmethod
def from_api_repr(cls, resource, zone):
"""Factory: construct a change set given its API representation
:type resource: dict
:param resource: change set representation returned from the API.
:type zone: :class:`google.cloud.dns.zone.ManagedZone`
:param zone: A zone which holds zero or more change sets.
:rtype: :class:`google.cloud.dns.changes.Changes`
:returns: RRS parsed from ``resource``.
"""
changes = cls(zone=zone)
changes._set_properties(resource)
return changes
def _set_properties(self, resource):
"""Helper method for :meth:`from_api_repr`, :meth:`create`, etc.
:type resource: dict
:param resource: change set representation returned from the API.
"""
resource = resource.copy()
self._additions = tuple(
[
ResourceRecordSet.from_api_repr(added_res, self.zone)
for added_res in resource.pop("additions", ())
]
)
self._deletions = tuple(
[
ResourceRecordSet.from_api_repr(added_res, self.zone)
for added_res in resource.pop("deletions", ())
]
)
self._properties = resource
@property
def path(self):
"""URL path for change set APIs.
:rtype: str
:returns: the path based on project, zone, and change set names.
"""
return "/projects/%s/managedZones/%s/changes/%s" % (
self.zone.project,
self.zone.name,
self.name,
)
@property
def name(self):
"""Name of the change set.
:rtype: str or ``NoneType``
:returns: Name, as set by the back-end, or None.
"""
return self._properties.get("id")
@name.setter
def name(self, value):
"""Update name of the change set.
:type value: str
:param value: New name for the changeset.
"""
if not isinstance(value, str):
raise ValueError("Pass a string")
self._properties["id"] = value
@property
def status(self):
"""Status of the change set.
:rtype: str or ``NoneType``
:returns: Status, as set by the back-end, or None.
"""
return self._properties.get("status")
@property
def started(self):
"""Time when the change set was started.
:rtype: ``datetime.datetime`` or ``NoneType``
:returns: Time, as set by the back-end, or None.
"""
stamp = self._properties.get("startTime")
if stamp is not None:
return _rfc3339_to_datetime(stamp)
@property
def additions(self):
"""Resource record sets to be added to the zone.
:rtype: sequence of
:class:`google.cloud.dns.resource_record_set.ResourceRecordSet`.
:returns: record sets appended via :meth:`add_record_set`.
"""
return self._additions
@property
def deletions(self):
"""Resource record sets to be deleted from the zone.
:rtype: sequence of
:class:`google.cloud.dns.resource_record_set.ResourceRecordSet`.
:returns: record sets appended via :meth:`delete_record_set`.
"""
return self._deletions
def add_record_set(self, record_set):
"""Append a record set to the 'additions' for the change set.
:type record_set:
:class:`google.cloud.dns.resource_record_set.ResourceRecordSet`
:param record_set: the record set to append.
:raises: ``ValueError`` if ``record_set`` is not of the required type.
"""
if not isinstance(record_set, ResourceRecordSet):
raise ValueError("Pass a ResourceRecordSet")
self._additions += (record_set,)
def delete_record_set(self, record_set):
"""Append a record set to the 'deletions' for the change set.
:type record_set:
:class:`google.cloud.dns.resource_record_set.ResourceRecordSet`
:param record_set: the record set to append.
:raises: ``ValueError`` if ``record_set`` is not of the required type.
"""
if not isinstance(record_set, ResourceRecordSet):
raise ValueError("Pass a ResourceRecordSet")
self._deletions += (record_set,)
def _require_client(self, client):
"""Check client or verify over-ride.
:type client: :class:`google.cloud.dns.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.dns.client.Client`
:returns: The client passed in or the currently bound client.
"""
if client is None:
client = self.zone._client
return client
def _build_resource(self):
"""Generate a resource for ``create``."""
additions = [
{
"name": added.name,
"type": added.record_type,
"ttl": str(added.ttl),
"rrdatas": added.rrdatas,
}
for added in self.additions
]
deletions = [
{
"name": deleted.name,
"type": deleted.record_type,
"ttl": str(deleted.ttl),
"rrdatas": deleted.rrdatas,
}
for deleted in self.deletions
]
return {"additions": additions, "deletions": deletions}
def create(self, client=None):
"""API call: create the change set via a POST request.
See
https://cloud.google.com/dns/api/v1/changes/create
:type client: :class:`google.cloud.dns.client.Client`
:param client:
(Optional) the client to use. If not passed, falls back to the
``client`` stored on the current zone.
"""
if len(self.additions) == 0 and len(self.deletions) == 0:
raise ValueError("No record sets added or deleted")
client = self._require_client(client)
path = "/projects/%s/managedZones/%s/changes" % (
self.zone.project,
self.zone.name,
)
api_response = client._connection.api_request(
method="POST", path=path, data=self._build_resource()
)
self._set_properties(api_response)
def exists(self, client=None):
"""API call: test for the existence of the change set via a GET request.
See
https://cloud.google.com/dns/api/v1/changes/get
:type client: :class:`google.cloud.dns.client.Client`
:param client:
(Optional) the client to use. If not passed, falls back to the
``client`` stored on the current zone.
:rtype: bool
:returns: Boolean indicating existence of the changes.
"""
client = self._require_client(client)
try:
client._connection.api_request(
method="GET", path=self.path, query_params={"fields": "id"}
)
except NotFound:
return False
else:
return True
def reload(self, client=None):
"""API call: refresh zone properties via a GET request.
See
https://cloud.google.com/dns/api/v1/changes/get
:type client: :class:`google.cloud.dns.client.Client`
:param client:
(Optional) the client to use. If not passed, falls back to the
``client`` stored on the current zone.
"""
client = self._require_client(client)
api_response = client._connection.api_request(method="GET", path=self.path)
self._set_properties(api_response)