Skip to content

Commit 2dd1409

Browse files
authored
Align azure-keyvault-administration API with other SDKs (#15717)
1 parent f13b42d commit 2dd1409

File tree

10 files changed

+114
-79
lines changed

10 files changed

+114
-79
lines changed

sdk/keyvault/azure-keyvault-administration/CHANGELOG.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@
22

33
## 4.0.0b3 (Unreleased)
44
### Breaking Changes
5-
- Removed `folder_name` parameter from `KeyVaultBackupClient.begin_full_restore`
6-
and `.begin_selective_restore`
7-
- Renamed `BackupOperation.azure_storage_blob_container_uri` to `.blob_storage_url`
8-
- Renamed `blob_storage_uri` parameters of `KeyVaultBackupClient` methods to
9-
`blob_storage_url`
5+
- Renamed `KeyVaultBackupClient.begin_full_backup()` to `.begin_backup()`
6+
- Renamed `KeyVaultBackupClient.begin_full_restore()` to `.begin_restore()`
7+
- Renamed `BackupOperation.azure_storage_blob_container_uri` to `.folder_url`
8+
- Renamed `id` property of `BackupOperation`, `RestoreOperation`, and
9+
`SelectiveKeyRestoreOperation` to `job_id`
10+
- Renamed `blob_storage_uri` parameters of `KeyVaultBackupClient.begin_restore()`
11+
and `.begin_selective_restore()` to `folder_url`
12+
- Removed redundant `folder_name` parameter from
13+
`KeyVaultBackupClient.begin_restore()` and `.begin_selective_restore()` (the
14+
`folder_url` parameter contains the folder name)
1015
- Renamed `KeyVaultPermission` attributes:
1116
- `actions` -> `allowed_actions`
1217
- `data_actions` -> `allowed_data_actions`
1318
- `not_actions` -> `denied_actions`
1419
- `not_data_actions` -> `denied_data_actions`
15-
20+
- Renamed `KeyVaultRoleAssignment.assignment_id` to `.role_assignment_id`
1621

1722
## 4.0.0b2 (2020-10-06)
1823
### Added

sdk/keyvault/azure-keyvault-administration/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ sas_token = "<your-sas-token>" # replace with a sas token to your storage accou
234234
235235
# performing a full key backup is a long-running operation. Calling result() on the poller will wait
236236
# until the backup is completed, then return an object representing the backup operation.
237-
backup_operation = client.begin_full_backup(blob_storage_url, sas_token).result()
237+
backup_operation = client.begin_backup(blob_storage_url, sas_token).result()
238238
239-
print(backup_operation.blob_storage_url)
239+
print(backup_operation.folder_url)
240240
print(backup_operation.status)
241241
print(backup_operation.job_id)
242242
```
@@ -263,7 +263,7 @@ blob_url = "<your-blob-url>"
263263
264264
# performing a full key restore is a long-running operation. Calling `result()` on the poller will wait
265265
# until the restore is completed, then return an object representing the restore operation.
266-
restore_operation = client.begin_full_restore(blob_url, sas_token).result()
266+
restore_operation = client.begin_restore(blob_url, sas_token).result()
267267
268268
print(restore_operation.status)
269269
print(restore_operation.job_id)

sdk/keyvault/azure-keyvault-administration/azure/keyvault/administration/_backup_client.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from azure.core.polling.base_polling import LROBasePolling
88

99
from ._models import BackupOperation, RestoreOperation, SelectiveKeyRestoreOperation
10-
from ._internal import KeyVaultClientBase, parse_blob_storage_url
10+
from ._internal import KeyVaultClientBase, parse_folder_url
1111
from ._internal.polling import KeyVaultBackupClientPolling
1212

1313
if TYPE_CHECKING:
@@ -25,7 +25,7 @@ class KeyVaultBackupClient(KeyVaultClientBase):
2525
"""
2626

2727
# pylint:disable=protected-access
28-
def begin_full_backup(self, blob_storage_url, sas_token, **kwargs):
28+
def begin_backup(self, blob_storage_url, sas_token, **kwargs):
2929
# type: (str, str, **Any) -> LROPoller[BackupOperation]
3030
"""Begin a full backup of the Key Vault.
3131
@@ -47,18 +47,18 @@ def begin_full_backup(self, blob_storage_url, sas_token, **kwargs):
4747
**kwargs
4848
)
4949

50-
def begin_full_restore(self, blob_storage_url, sas_token, **kwargs):
50+
def begin_restore(self, folder_url, sas_token, **kwargs):
5151
# type: (str, str, **Any) -> LROPoller[RestoreOperation]
5252
"""Restore a full backup of a Key Vault.
5353
54-
:param str blob_storage_url: URL of the blob holding the backup. This would be the `blob_storage_url` of a
55-
:class:`BackupOperation` returned by :func:`begin_full_backup` or :func:`get_backup_status`, for example
54+
:param str folder_url: URL of the blob holding the backup. This would be the `folder_url` of a
55+
:class:`BackupOperation` returned by :func:`begin_backup` or :func:`get_backup_status`, for example
5656
https://<account>.blob.core.windows.net/backup/mhsm-account-2020090117323313
5757
:param str sas_token: a Shared Access Signature (SAS) token authorizing access to the blob storage resource
5858
:rtype: ~azure.core.polling.LROPoller[RestoreOperation]
5959
"""
6060
polling_interval = kwargs.pop("_polling_interval", 5)
61-
container_url, folder_name = parse_blob_storage_url(blob_storage_url)
61+
container_url, folder_name = parse_folder_url(folder_url)
6262
sas_parameter = self._models.SASTokenParameter(storage_resource_uri=container_url, token=sas_token)
6363
restore_details = self._models.RestoreOperationParameters(
6464
sas_token_parameters=sas_parameter, folder_to_restore=folder_name
@@ -72,20 +72,20 @@ def begin_full_restore(self, blob_storage_url, sas_token, **kwargs):
7272
**kwargs
7373
)
7474

75-
def begin_selective_restore(self, blob_storage_url, sas_token, key_name, **kwargs):
75+
def begin_selective_restore(self, folder_url, sas_token, key_name, **kwargs):
7676
# type: (str, str, str, **Any) -> LROPoller[SelectiveKeyRestoreOperation]
7777
"""Restore a single key from a full Key Vault backup.
7878
79-
:param str blob_storage_url: URL for the blob storage resource, including the path to the blob holding the
80-
backup. This would be the `blob_storage_url` of a :class:`BackupOperation` returned by
81-
:func:`begin_full_backup` or :func:`get_backup_status`, for example
79+
:param str folder_url: URL for the blob storage resource, including the path to the blob holding the
80+
backup. This would be the `folder_url` of a :class:`BackupOperation` returned by
81+
:func:`begin_backup` or :func:`get_backup_status`, for example
8282
https://<account>.blob.core.windows.net/backup/mhsm-account-2020090117323313
8383
:param str sas_token: a Shared Access Signature (SAS) token authorizing access to the blob storage resource
8484
:param str key_name: name of the key to restore from the backup
8585
:rtype: ~azure.core.polling.LROPoller[RestoreOperation]
8686
"""
8787
polling_interval = kwargs.pop("_polling_interval", 5)
88-
container_url, folder_name = parse_blob_storage_url(blob_storage_url)
88+
container_url, folder_name = parse_folder_url(folder_url)
8989
sas_parameter = self._models.SASTokenParameter(storage_resource_uri=container_url, token=sas_token)
9090
restore_details = self._models.SelectiveKeyRestoreOperationParameters(
9191
sas_token_parameters=sas_parameter, folder=folder_name

sdk/keyvault/azure-keyvault-administration/azure/keyvault/administration/_internal/__init__.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44
# ------------------------------------
55
from collections import namedtuple
66

7-
try:
8-
import urllib.parse as parse
9-
except ImportError:
10-
# pylint:disable=import-error
11-
import urlparse as parse # type: ignore
7+
from six.moves.urllib_parse import urlparse
128

139
from .challenge_auth_policy import ChallengeAuthPolicy, ChallengeAuthPolicyBase
1410
from .client_base import KeyVaultClientBase
@@ -29,7 +25,7 @@
2925

3026
def parse_vault_id(url):
3127
try:
32-
parsed_uri = parse.urlparse(url)
28+
parsed_uri = urlparse(url)
3329
except Exception: # pylint: disable=broad-except
3430
raise ValueError("'{}' is not not a valid url".format(url))
3531
if not (parsed_uri.scheme and parsed_uri.hostname):
@@ -51,7 +47,7 @@ def parse_vault_id(url):
5147
BackupLocation = namedtuple("BackupLocation", ["container_url", "folder_name"])
5248

5349

54-
def parse_blob_storage_url(blob_storage_url):
50+
def parse_folder_url(folder_url):
5551
# type: (str) -> BackupLocation
5652
"""Parse the blob container URL and folder name from a backup's blob storage URL.
5753
@@ -60,12 +56,22 @@ def parse_blob_storage_url(blob_storage_url):
6056
"""
6157

6258
try:
63-
folder_name = blob_storage_url.rstrip("/").split("/")[-1]
64-
container_url = blob_storage_url[: blob_storage_url.rindex(folder_name) - 1]
59+
parsed = urlparse(folder_url)
60+
61+
# the first segment of the path is the container name
62+
stripped_path = parsed.path.strip("/")
63+
container = stripped_path.split("/")[0]
64+
65+
# the rest of the path is the folder name
66+
folder_name = stripped_path[len(container) + 1 :]
67+
68+
# this intentionally discards any SAS token in the URL--methods require the SAS token as a separate parameter
69+
container_url = "{}://{}/{}".format(parsed.scheme, parsed.netloc, container)
70+
6571
return BackupLocation(container_url, folder_name)
6672
except: # pylint:disable=broad-except
6773
raise ValueError(
68-
'"blob_storage_url" should be the URL of a blob holding a Key Vault backup, for example '
74+
'"folder_url" should be the URL of a blob holding a Key Vault backup, for example '
6975
'"https://<account>.blob.core.windows.net/backup/mhsm-account-2020090117323313"'
7076
)
7177

sdk/keyvault/azure-keyvault-administration/azure/keyvault/administration/_models.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,20 @@ class KeyVaultRoleAssignment(object):
5151

5252
def __init__(self, **kwargs):
5353
# type: (**Any) -> None
54-
self._assignment_id = kwargs.get("assignment_id")
54+
self._role_assignment_id = kwargs.get("role_assignment_id")
5555
self._name = kwargs.get("name")
5656
self._properties = kwargs.get("properties")
5757
self._type = kwargs.get("assignment_type")
5858

5959
def __repr__(self):
6060
# type: () -> str
61-
return "KeyVaultRoleAssignment<{}>".format(self._assignment_id)
61+
return "KeyVaultRoleAssignment<{}>".format(self._role_assignment_id)
6262

6363
@property
64-
def assignment_id(self):
64+
def role_assignment_id(self):
6565
# type: () -> str
6666
"""unique identifier for this assignment"""
67-
return self._assignment_id
67+
return self._role_assignment_id
6868

6969
@property
7070
def name(self):
@@ -102,7 +102,7 @@ def type(self):
102102
@classmethod
103103
def _from_generated(cls, role_assignment):
104104
return cls(
105-
assignment_id=role_assignment.id,
105+
role_assignment_id=role_assignment.id,
106106
name=role_assignment.name,
107107
assignment_type=role_assignment.type,
108108
properties=KeyVaultRoleAssignmentProperties._from_generated(role_assignment.properties),
@@ -183,7 +183,7 @@ def __init__(self, **kwargs):
183183
self.error = kwargs.get("error", None)
184184
self.start_time = kwargs.get("start_time", None)
185185
self.end_time = kwargs.get("end_time", None)
186-
self.id = kwargs.get("job_id", None)
186+
self.job_id = kwargs.get("job_id", None)
187187

188188
@classmethod
189189
def _wrap_generated(cls, response, deserialized_operation, response_headers): # pylint:disable=unused-argument
@@ -200,11 +200,11 @@ class BackupOperation(_Operation):
200200
:ivar datetime.datetime start_time: UTC start time of the operation
201201
:ivar datetime.datetime end_time: UTC end time of the operation
202202
:ivar str job_id: identifier for the operation
203-
:ivar str blob_storage_url: URL of the Azure blob storage container which contains the backup
203+
:ivar str folder_url: URL of the Azure blob storage container which contains the backup
204204
"""
205205

206206
def __init__(self, **kwargs):
207-
self.blob_storage_url = kwargs.pop("azure_storage_blob_container_uri", None)
207+
self.folder_url = kwargs.pop("azure_storage_blob_container_uri", None)
208208
super(BackupOperation, self).__init__(**kwargs)
209209

210210

sdk/keyvault/azure-keyvault-administration/azure/keyvault/administration/aio/_backup_client.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from azure.core.polling.async_base_polling import AsyncLROBasePolling
88

9-
from .._internal import AsyncKeyVaultClientBase, parse_blob_storage_url
9+
from .._internal import AsyncKeyVaultClientBase, parse_folder_url
1010
from .._internal.polling import KeyVaultBackupClientPolling
1111
from .._models import BackupOperation, RestoreOperation, SelectiveKeyRestoreOperation
1212

@@ -25,7 +25,7 @@ class KeyVaultBackupClient(AsyncKeyVaultClientBase):
2525
"""
2626

2727
# pylint:disable=protected-access
28-
async def begin_full_backup(
28+
async def begin_backup(
2929
self, blob_storage_url: str, sas_token: str, **kwargs: "Any"
3030
) -> "AsyncLROPoller[BackupOperation]":
3131
"""Begin a full backup of the Key Vault.
@@ -50,20 +50,20 @@ async def begin_full_backup(
5050
**kwargs
5151
)
5252

53-
async def begin_full_restore(
54-
self, blob_storage_url: str, sas_token: str, **kwargs: "Any"
53+
async def begin_restore(
54+
self, folder_url: str, sas_token: str, **kwargs: "Any"
5555
) -> "AsyncLROPoller[RestoreOperation]":
5656
"""Restore a full backup of a Key Vault.
5757
58-
:param str blob_storage_url: URL for the blob storage resource, including the path to the blob holding the
59-
backup. This would be the `blob_storage_url` of a :class:`BackupOperation` returned by
60-
:func:`begin_full_backup` or :func:`get_backup_status`, for example
58+
:param str folder_url: URL for the blob storage resource, including the path to the blob holding the
59+
backup. This would be the `folder_url` of a :class:`BackupOperation` returned by
60+
:func:`begin_backup` or :func:`get_backup_status`, for example
6161
https://<account>.blob.core.windows.net/backup/mhsm-account-2020090117323313
6262
:param str sas_token: a Shared Access Signature (SAS) token authorizing access to the blob storage resource
6363
:rtype: ~azure.core.polling.AsyncLROPoller[RestoreOperation]
6464
"""
6565
polling_interval = kwargs.pop("_polling_interval", 5)
66-
container_url, folder_name = parse_blob_storage_url(blob_storage_url)
66+
container_url, folder_name = parse_folder_url(folder_url)
6767
sas_parameter = self._models.SASTokenParameter(storage_resource_uri=container_url, token=sas_token)
6868
restore_details = self._models.RestoreOperationParameters(
6969
sas_token_parameters=sas_parameter, folder_to_restore=folder_name
@@ -80,20 +80,20 @@ async def begin_full_restore(
8080
)
8181

8282
async def begin_selective_restore(
83-
self, blob_storage_url: str, sas_token: str, key_name: str, **kwargs: "Any"
83+
self, folder_url: str, sas_token: str, key_name: str, **kwargs: "Any"
8484
) -> "AsyncLROPoller[SelectiveKeyRestoreOperation]":
8585
"""Restore a single key from a full Key Vault backup.
8686
87-
:param str blob_storage_url: URL for the blob storage resource, including the path to the blob holding the
88-
backup. This would be the `blob_storage_url` of a :class:`BackupOperation` returned by
89-
:func:`begin_full_backup` or :func:`get_backup_status`, for example
87+
:param str folder_url: URL for the blob storage resource, including the path to the blob holding the
88+
backup. This would be the `folder_url` of a :class:`BackupOperation` returned by
89+
:func:`begin_backup` or :func:`get_backup_status`, for example
9090
https://<account>.blob.core.windows.net/backup/mhsm-account-2020090117323313
9191
:param str sas_token: a Shared Access Signature (SAS) token authorizing access to the blob storage resource
9292
:param str key_name: name of the key to restore from the backup
9393
:rtype: ~azure.core.polling.AsyncLROPoller[RestoreOperation]
9494
"""
9595
polling_interval = kwargs.pop("_polling_interval", 5)
96-
container_url, folder_name = parse_blob_storage_url(blob_storage_url)
96+
container_url, folder_name = parse_folder_url(folder_url)
9797
sas_parameter = self._models.SASTokenParameter(storage_resource_uri=container_url, token=sas_token)
9898
restore_details = self._models.SelectiveKeyRestoreOperationParameters(
9999
sas_token_parameters=sas_parameter, folder=folder_name

sdk/keyvault/azure-keyvault-administration/tests/test_access_control.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,17 @@ def test_role_assignment(self, client):
8181

8282
# new assignment should be in the list of all assignments
8383
matching_assignments = [
84-
a for a in client.list_role_assignments(scope) if a.assignment_id == created.assignment_id
84+
a for a in client.list_role_assignments(scope) if a.role_assignment_id == created.role_assignment_id
8585
]
8686
assert len(matching_assignments) == 1
8787

8888
# delete the assignment
8989
deleted = client.delete_role_assignment(scope, created.name)
9090
assert deleted.name == created.name
91-
assert deleted.assignment_id == created.assignment_id
91+
assert deleted.role_assignment_id == created.role_assignment_id
9292
assert deleted.scope == scope
9393
assert deleted.role_definition_id == created.role_definition_id
9494

95-
assert not any(a for a in client.list_role_assignments(scope) if a.assignment_id == created.assignment_id)
95+
assert not any(
96+
a for a in client.list_role_assignments(scope) if a.role_assignment_id == created.role_assignment_id
97+
)

sdk/keyvault/azure-keyvault-administration/tests/test_access_control_async.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,18 @@ async def test_role_assignment(self, client):
8787
# new assignment should be in the list of all assignments
8888
matching_assignments = []
8989
async for assignment in client.list_role_assignments(scope):
90-
if assignment.assignment_id == created.assignment_id:
90+
if assignment.role_assignment_id == created.role_assignment_id:
9191
matching_assignments.append(assignment)
9292
assert len(matching_assignments) == 1
9393

9494
# delete the assignment
9595
deleted = await client.delete_role_assignment(scope, created.name)
9696
assert deleted.name == created.name
97-
assert deleted.assignment_id == created.assignment_id
97+
assert deleted.role_assignment_id == created.role_assignment_id
9898
assert deleted.scope == scope
9999
assert deleted.role_definition_id == created.role_definition_id
100100

101101
async for assignment in client.list_role_assignments(scope):
102-
assert assignment.assignment_id != created.assignment_id, "the role assignment should have been deleted"
102+
assert (
103+
assignment.role_assignment_id != created.role_assignment_id
104+
), "the role assignment should have been deleted"

0 commit comments

Comments
 (0)