Skip to content

Commit b2dbc33

Browse files
authored
feat: Operation with resource (#1)
* fix: renamed models YandexLockboxResponse -> Operation, YandexLockboxError -> YandexCloudError * feat: added computed field `resource` to Operation for manipulate with modified Lockbox resource * chore: version bump
1 parent 5d730a9 commit b2dbc33

6 files changed

Lines changed: 135 additions & 94 deletions

File tree

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ from yc_lockbox import YandexLockboxClient, INewSecret, INewSecretPayloadEntry
9292

9393
lockbox = YandexLockboxClient("oauth_or_iam_token")
9494

95-
my_secret = lockbox.create_secret(
95+
create_secret_operation = lockbox.create_secret(
9696
INewSecret(
9797
folder_id="b1xxxxxxxxxxxxxx",
9898
name="my-secret",
@@ -103,8 +103,10 @@ my_secret = lockbox.create_secret(
103103
)
104104
)
105105

106-
if my_secret.done:
107-
print(my_secret.metadata.get("secretId"))
106+
if create_secret_operation.done:
107+
new_secret = create_secret_operation.resource
108+
print(new_secret.id)
109+
new_secret.deactivate()
108110
```
109111

110112

yc_lockbox/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from yc_lockbox._lockbox import YandexLockboxClient
2828
from yc_lockbox._models import Secret, INewSecretPayloadEntry, INewSecret, INewSecretVersion, IUpdateSecret
2929

30-
__version__ = "0.0.2"
30+
__version__ = "0.1.0"
3131
__author__ = "Akim Faskhutdinov"
3232
__author_email__ = "akimstrong@yandex.ru"
3333
__license__ = "MIT"

yc_lockbox/_adapters.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import Any
44

55
from yc_lockbox._abc import AbstractHTTPAdapter
6-
from yc_lockbox._models import YandexLockboxError
6+
from yc_lockbox._models import YandexCloudError
77
from yc_lockbox._types import T
88

99
logger = logging.getLogger(__name__)
@@ -78,7 +78,7 @@ def request(
7878
if raise_for_status:
7979
response.raise_for_status()
8080

81-
return __class__.parse_response(response, response_model=YandexLockboxError)
81+
return __class__.parse_response(response, response_model=YandexCloudError)
8282

8383
return __class__.parse_response(response, response_model=response_model)
8484

@@ -161,6 +161,6 @@ async def request(
161161
if raise_for_status:
162162
response.raise_for_status()
163163

164-
return await __class__.parse_response(response, response_model=YandexLockboxError)
164+
return await __class__.parse_response(response, response_model=YandexCloudError)
165165

166166
return await __class__.parse_response(response, response_model=response_model)

yc_lockbox/_lockbox.py

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
SecretVersion,
1111
SecretsList,
1212
SecretVersionsList,
13-
YandexLockboxResponse,
14-
YandexLockboxError,
13+
Operation,
14+
YandexCloudError,
1515
INewSecret,
1616
IUpdateSecret,
1717
INewSecretVersion,
@@ -141,134 +141,138 @@ def _seekable_response(
141141
item.inject_client(self)
142142
yield item
143143

144-
def activate_secret(
145-
self, secret_id: str, raise_for_status: bool = True
146-
) -> YandexLockboxResponse | YandexLockboxError:
144+
def activate_secret(self, secret_id: str, raise_for_status: bool = True) -> Operation | YandexCloudError:
147145
"""
148146
Activates the specified secret.
149147
150148
:param secret_id: Secret indentifier.
151-
:param raise_for_status: If set to ``False`` returns :class:`YandexLockboxError` instead throw exception.
149+
:param raise_for_status: If set to ``False`` returns :class:`YandexCloudError` instead throw exception.
152150
Defaults to ``True``.
153151
"""
154152
url = f"{self.lockbox_base_url}/secrets/{secret_id}:activate"
155-
return self.adapter.request(
153+
response = self.adapter.request(
156154
"POST",
157155
url,
158156
headers=self.auth_headers,
159-
response_model=YandexLockboxResponse,
157+
response_model=Operation,
160158
raise_for_status=raise_for_status,
161159
)
160+
response.inject_client(self)
161+
return response
162162

163163
def add_secret_version(
164164
self, secret_id: str, version: INewSecretVersion, raise_for_status: bool = True
165-
) -> YandexLockboxResponse | YandexLockboxError:
165+
) -> Operation | YandexCloudError:
166166
"""
167167
Adds new version based on a previous one.
168168
169169
:param secret_id: Secret indentifier.
170170
:param version: A new version object.
171-
:param raise_for_status: If set to ``False`` returns :class:`YandexLockboxError` instead throw exception.
171+
:param raise_for_status: If set to ``False`` returns :class:`YandexCloudError` instead throw exception.
172172
Defaults to ``True``.
173173
"""
174174
url = f"{self.lockbox_base_url}/secrets/{secret_id}:addVersion"
175175
payload = version.model_dump_json(by_alias=True, exclude_none=True)
176-
return self.adapter.request(
176+
response = self.adapter.request(
177177
"POST",
178178
url,
179179
headers=self.auth_headers,
180180
data=payload,
181-
response_model=YandexLockboxResponse,
181+
response_model=Operation,
182182
raise_for_status=raise_for_status,
183183
)
184+
response.inject_client(self)
185+
return response
184186

185-
def create_secret(
186-
self, secret: INewSecret, raise_for_status: bool = True
187-
) -> YandexLockboxResponse | YandexLockboxError:
187+
def create_secret(self, secret: INewSecret, raise_for_status: bool = True) -> Operation | YandexCloudError:
188188
"""
189189
Creates a secret in the specified folder.
190190
191191
:param secret: A new secret object.
192-
:param raise_for_status: If set to ``False`` returns :class:`YandexLockboxError` instead throw exception.
192+
:param raise_for_status: If set to ``False`` returns :class:`YandexCloudError` instead throw exception.
193193
Defaults to ``True``.
194194
"""
195195
url = f"{self.lockbox_base_url}/secrets"
196196
payload = secret.model_dump_json(by_alias=True, exclude_none=True)
197-
return self.adapter.request(
197+
response = self.adapter.request(
198198
"POST",
199199
url,
200200
headers=self.auth_headers,
201201
data=payload,
202-
response_model=YandexLockboxResponse,
202+
response_model=Operation,
203203
raise_for_status=raise_for_status,
204204
)
205+
response.inject_client(self)
206+
return response
205207

206208
def cancel_secret_version_destruction(
207209
self, secret_id: str, version_id: str, raise_for_status: bool = True
208-
) -> YandexLockboxResponse | YandexLockboxError:
210+
) -> Operation | YandexCloudError:
209211
"""
210212
Cancels previously scheduled version destruction, if the version hasn't been destroyed yet.
211213
212214
:param secret_id: Secret indentifier.
213215
:param version_id: Secret version id to cancel destruction.
214-
:param raise_for_status: If set to ``False`` returns :class:`YandexLockboxError` instead throw exception.
216+
:param raise_for_status: If set to ``False`` returns :class:`YandexCloudError` instead throw exception.
215217
Defaults to ``True``.
216218
"""
217219
url = f"{self.lockbox_base_url}/secrets/{secret_id}:cancelVersionDestruction"
218220
payload = {"versionId": version_id}
219-
return self.adapter.request(
221+
response = self.adapter.request(
220222
"POST",
221223
url,
222224
headers=self.auth_headers,
223225
json=payload,
224-
response_model=YandexLockboxResponse,
226+
response_model=Operation,
225227
raise_for_status=raise_for_status,
226228
)
229+
response.inject_client(self)
230+
return response
227231

228-
def deactivate_secret(
229-
self, secret_id: str, raise_for_status: bool = True
230-
) -> YandexLockboxResponse | YandexLockboxError:
232+
def deactivate_secret(self, secret_id: str, raise_for_status: bool = True) -> Operation | YandexCloudError:
231233
"""
232234
Deactivate a secret.
233235
234236
:param secret_id: Secret indentifier.
235-
:param raise_for_status: If set to ``False`` returns :class:`YandexLockboxError` instead throw exception.
237+
:param raise_for_status: If set to ``False`` returns :class:`YandexCloudError` instead throw exception.
236238
Defaults to ``True``.
237239
"""
238240
url = f"{self.lockbox_base_url}/secrets/{secret_id}:deactivate"
239-
return self.adapter.request(
241+
response = self.adapter.request(
240242
"POST",
241243
url,
242244
headers=self.auth_headers,
243-
response_model=YandexLockboxResponse,
245+
response_model=Operation,
244246
raise_for_status=raise_for_status,
245247
)
248+
response.inject_client(self)
249+
return response
246250

247-
def delete_secret(
248-
self, secret_id: str, raise_for_status: bool = True
249-
) -> YandexLockboxResponse | YandexLockboxError:
251+
def delete_secret(self, secret_id: str, raise_for_status: bool = True) -> Operation | YandexCloudError:
250252
"""
251253
Deletes the specified secret.
252254
253255
:param secret_id: Secret indentifier.
254-
:param raise_for_status: If set to ``False`` returns :class:`YandexLockboxError` instead throw exception.
256+
:param raise_for_status: If set to ``False`` returns :class:`YandexCloudError` instead throw exception.
255257
Defaults to ``True``.
256258
"""
257259
url = f"{self.lockbox_base_url}/secrets/{secret_id}"
258-
return self.adapter.request(
260+
response = self.adapter.request(
259261
"DELETE",
260262
url,
261263
headers=self.auth_headers,
262-
response_model=YandexLockboxResponse,
264+
response_model=Operation,
263265
raise_for_status=raise_for_status,
264266
)
267+
response.inject_client(self)
268+
return response
265269

266-
def get_secret(self, secret_id: str, raise_for_status: bool = True) -> Secret | YandexLockboxError:
270+
def get_secret(self, secret_id: str, raise_for_status: bool = True) -> Secret | YandexCloudError:
267271
"""
268272
Get lockbox secret by ID.
269273
270274
:param secret_id: Secret identifier.
271-
:param raise_for_status: If set to ``False`` returns :class:`YandexLockboxError` instead throw exception.
275+
:param raise_for_status: If set to ``False`` returns :class:`YandexCloudError` instead throw exception.
272276
Defaults to ``True``.
273277
"""
274278
url = f"{self.lockbox_base_url}/secrets/{secret_id}"
@@ -287,13 +291,13 @@ def get_secret_payload(
287291
secret_id: str,
288292
version_id: str | None = None,
289293
raise_for_status: bool = True,
290-
) -> SecretPayload | YandexLockboxError:
294+
) -> SecretPayload | YandexCloudError:
291295
"""
292296
Get lockbox secret payload by ID and optional version.
293297
294298
:param secret_id: Secret identifier.
295299
:param version_id: Secret version. Optional.
296-
:param raise_for_status: If set to ``False`` returns :class:`YandexLockboxError` instead throw exception.
300+
:param raise_for_status: If set to ``False`` returns :class:`YandexCloudError` instead throw exception.
297301
Defaults to ``True``.
298302
"""
299303
url = f"{self.payload_lockbox_base_url}/secrets/{secret_id}/payload"
@@ -314,7 +318,7 @@ def list_secrets(
314318
page_token: str | None = None,
315319
raise_for_status: bool = True,
316320
iterator: bool = False,
317-
) -> SecretsList | Iterator[Secret] | YandexLockboxError:
321+
) -> SecretsList | Iterator[Secret] | YandexCloudError:
318322
"""
319323
Retrieves the list of secrets in the specified folder.
320324
@@ -362,7 +366,7 @@ def list_secret_versions(
362366
page_token: str | None = None,
363367
raise_for_status: bool = True,
364368
iterator: bool = False,
365-
) -> SecretVersionsList | Iterator[SecretVersion] | YandexLockboxError:
369+
) -> SecretVersionsList | Iterator[SecretVersion] | YandexCloudError:
366370
"""
367371
Retrieves the list of versions of the specified secret.
368372
@@ -397,7 +401,7 @@ def list_secret_versions(
397401

398402
def schedule_secret_version_destruction(
399403
self, secret_id: str, version_id: str, pending_period: int = 604800, raise_for_status: bool = True
400-
) -> YandexLockboxResponse | YandexLockboxError:
404+
) -> Operation | YandexCloudError:
401405
"""
402406
Schedules the specified version for destruction.
403407
Scheduled destruction can be cancelled with the :func:`cancel_secret_version_destruction()` method.
@@ -406,7 +410,7 @@ def schedule_secret_version_destruction(
406410
:param version_id: ID of the version to be destroyed.
407411
:param pending_period: Time interval in seconds between the version destruction request and actual destruction.
408412
Default value: ``604800`` (i.e. 7 days).
409-
:param raise_for_status: If set to ``False`` returns :class:`YandexLockboxError` instead throw exception.
413+
:param raise_for_status: If set to ``False`` returns :class:`YandexCloudError` instead throw exception.
410414
Defaults to ``True``.
411415
"""
412416
if isinstance(pending_period, int):
@@ -420,22 +424,24 @@ def schedule_secret_version_destruction(
420424

421425
url = f"{self.lockbox_base_url}/secrets/{secret_id}:scheduleVersionDestruction"
422426
payload = {"versionId": version_id, "pendingPeriod": pending_period}
423-
return self.adapter.request(
427+
response = self.adapter.request(
424428
"POST",
425429
url,
426430
headers=self.auth_headers,
427431
json=payload,
428-
response_model=YandexLockboxResponse,
432+
response_model=Operation,
429433
raise_for_status=raise_for_status,
430434
)
435+
response.inject_client(self)
436+
return response
431437

432438
# TODO: implement
433439
def set_secret_access_bindings(self, *args, **kwargs):
434440
raise NotImplementedError
435441

436442
def update_secret(
437443
self, secret_id: str, data: IUpdateSecret, raise_for_status: bool = True
438-
) -> YandexLockboxResponse | YandexLockboxError:
444+
) -> Operation | YandexCloudError:
439445
"""
440446
Updates the specified secret.
441447
@@ -448,19 +454,21 @@ def update_secret(
448454
The default value for most fields is null or 0.
449455
If ``updateMask`` is not sent in the request, all fields values will be updated.
450456
Fields specified in the request will be updated to provided values. The rest of the fields will be reset to the default.
451-
:param raise_for_status: If set to ``False`` returns :class:`YandexLockboxError` instead throw exception.
457+
:param raise_for_status: If set to ``False`` returns :class:`YandexCloudError` instead throw exception.
452458
Defaults to ``True``.
453459
"""
454460
url = f"{self.lockbox_base_url}/secrets/{secret_id}"
455461
payload = data.model_dump_json(by_alias=True)
456-
return self.adapter.request(
462+
response = self.adapter.request(
457463
"PATCH",
458464
url,
459465
headers=self.auth_headers,
460466
data=payload,
461-
response_model=YandexLockboxResponse,
467+
response_model=Operation,
462468
raise_for_status=raise_for_status,
463469
)
470+
response.inject_client(self)
471+
return response
464472

465473
# TODO: implement
466474
def update_secret_access_bindings(self, *args, **kwargs):

0 commit comments

Comments
 (0)