Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Key Vault] Generate Keys with TypeSpec #36902

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 11 additions & 20 deletions sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,7 @@ def create_key(self, name: str, key_type: Union[str, KeyType], **kwargs: Any) ->
release_policy=policy,
)

bundle = self._client.create_key(
vault_base_url=self.vault_url, key_name=name, parameters=parameters, **kwargs
bundle = self._client.create_key( key_name=name, parameters=parameters, **kwargs
)
return KeyVaultKey._from_key_bundle(bundle)

Expand Down Expand Up @@ -342,7 +341,6 @@ def begin_delete_key(self, name: str, **kwargs: Any) -> LROPoller[DeletedKey]:
if polling_interval is None:
polling_interval = 2
pipeline_response, deleted_key_bundle = self._client.delete_key(
vault_base_url=self.vault_url,
key_name=name,
cls=lambda pipeline_response, deserialized, _: (pipeline_response, deserialized),
**kwargs,
Expand Down Expand Up @@ -385,7 +383,7 @@ def get_key(self, name: str, version: Optional[str] = None, **kwargs: Any) -> Ke
:caption: Get a key
:dedent: 8
"""
bundle = self._client.get_key(self.vault_url, name, key_version=version or "", **kwargs)
bundle = self._client.get_key(name, key_version=version or "", **kwargs)
return KeyVaultKey._from_key_bundle(bundle)

@distributed_trace
Expand All @@ -410,7 +408,7 @@ def get_deleted_key(self, name: str, **kwargs: Any) -> DeletedKey:
:caption: Get a deleted key
:dedent: 8
"""
bundle = self._client.get_deleted_key(self.vault_url, name, **kwargs)
bundle = self._client.get_deleted_key(name, **kwargs)
return DeletedKey._from_deleted_key_bundle(bundle)

@distributed_trace
Expand All @@ -431,7 +429,6 @@ def list_deleted_keys(self, **kwargs: Any) -> ItemPaged[DeletedKey]:
:dedent: 8
"""
return self._client.get_deleted_keys(
self._vault_url,
maxresults=kwargs.pop("max_page_size", None),
cls=lambda objs: [DeletedKey._from_deleted_key_item(x) for x in objs],
**kwargs
Expand All @@ -455,7 +452,6 @@ def list_properties_of_keys(self, **kwargs: Any) -> ItemPaged[KeyProperties]:
:dedent: 8
"""
return self._client.get_keys(
self._vault_url,
maxresults=kwargs.pop("max_page_size", None),
cls=lambda objs: [KeyProperties._from_key_item(x) for x in objs],
**kwargs
Expand All @@ -481,7 +477,6 @@ def list_properties_of_key_versions(self, name: str, **kwargs: Any) -> ItemPaged
:dedent: 8
"""
return self._client.get_key_versions(
self._vault_url,
name,
maxresults=kwargs.pop("max_page_size", None),
cls=lambda objs: [KeyProperties._from_key_item(x) for x in objs],
Expand Down Expand Up @@ -513,7 +508,7 @@ def purge_deleted_key(self, name: str, **kwargs: Any) -> None:
key_client.purge_deleted_key("key-name")

"""
self._client.purge_deleted_key(vault_base_url=self.vault_url, key_name=name, **kwargs)
self._client.purge_deleted_key(key_name=name, **kwargs)

@distributed_trace
def begin_recover_deleted_key(self, name: str, **kwargs: Any) -> LROPoller[KeyVaultKey]:
Expand Down Expand Up @@ -547,7 +542,6 @@ def begin_recover_deleted_key(self, name: str, **kwargs: Any) -> LROPoller[KeyVa
if polling_interval is None:
polling_interval = 2
pipeline_response, recovered_key_bundle = self._client.recover_deleted_key(
vault_base_url=self.vault_url,
key_name=name,
cls=lambda pipeline_response, deserialized, _: (pipeline_response, deserialized),
**kwargs,
Expand Down Expand Up @@ -619,7 +613,7 @@ def update_key_properties(self, name: str, version: Optional[str] = None, **kwar
)

bundle = self._client.update_key(
self.vault_url, name, key_version=version or "", parameters=parameters, **kwargs
name, key_version=version or "", parameters=parameters, **kwargs
)
return KeyVaultKey._from_key_bundle(bundle)

Expand Down Expand Up @@ -649,7 +643,7 @@ def backup_key(self, name: str, **kwargs: Any) -> bytes:
:caption: Get a key backup
:dedent: 8
"""
backup_result = self._client.backup_key(self.vault_url, name, **kwargs)
backup_result = self._client.backup_key(name, **kwargs)
return backup_result.value

@distributed_trace
Expand Down Expand Up @@ -679,7 +673,6 @@ def restore_key_backup(self, backup: bytes, **kwargs: Any) -> KeyVaultKey:
:dedent: 8
"""
bundle = self._client.restore_key(
self.vault_url,
parameters=self._models.KeyRestoreParameters(key_bundle_backup=backup),
**kwargs
)
Expand Down Expand Up @@ -736,7 +729,7 @@ def import_key(self, name: str, key: JsonWebKey, **kwargs: Any) -> KeyVaultKey:
release_policy=policy,
)

bundle = self._client.import_key(self.vault_url, name, parameters=parameters, **kwargs)
bundle = self._client.import_key(name, parameters=parameters, **kwargs)
return KeyVaultKey._from_key_bundle(bundle)

@distributed_trace
Expand All @@ -763,7 +756,6 @@ def release_key(self, name: str, target_attestation_token: str, **kwargs: Any) -
"""
version = kwargs.pop("version", None)
result = self._client.release(
vault_base_url=self._vault_url,
key_name=name,
key_version=version or "",
parameters=self._models.KeyReleaseParameters(
Expand Down Expand Up @@ -798,7 +790,7 @@ def get_random_bytes(self, count: int, **kwargs: Any) -> bytes:
if count < 1:
raise ValueError("At least one random byte must be requested")
parameters = self._models.GetRandomBytesRequest(count=count)
result = self._client.get_random_bytes(vault_base_url=self._vault_url, parameters=parameters, **kwargs)
result = self._client.get_random_bytes(parameters=parameters, **kwargs)
return result.value

@distributed_trace
Expand All @@ -812,7 +804,7 @@ def get_key_rotation_policy(self, key_name: str, **kwargs: Any) -> KeyRotationPo

:raises ~azure.core.exceptions.HttpResponseError:
"""
policy = self._client.get_key_rotation_policy(vault_base_url=self._vault_url, key_name=key_name, **kwargs)
policy = self._client.get_key_rotation_policy(key_name=key_name, **kwargs)
return KeyRotationPolicy._from_generated(policy)

@distributed_trace
Expand All @@ -828,7 +820,7 @@ def rotate_key(self, name: str, **kwargs: Any) -> KeyVaultKey:

:raises ~azure.core.exceptions.HttpResponseError:
"""
bundle = self._client.rotate_key(vault_base_url=self._vault_url, key_name=name, **kwargs)
bundle = self._client.rotate_key(key_name=name, **kwargs)
return KeyVaultKey._from_key_bundle(bundle)

@distributed_trace
Expand Down Expand Up @@ -870,8 +862,7 @@ def update_key_rotation_policy(

attributes = self._models.KeyRotationPolicyAttributes(expiry_time=kwargs.pop("expires_in", policy.expires_in))
new_policy = self._models.KeyRotationPolicy(lifetime_actions=lifetime_actions or [], attributes=attributes)
result = self._client.update_key_rotation_policy(
vault_base_url=self._vault_url, key_name=key_name, key_rotation_policy=new_policy
result = self._client.update_key_rotation_policy( key_name=key_name, key_rotation_policy=new_policy
)
return KeyRotationPolicy._from_generated(result)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# Code generated by Microsoft (R) AutoRest Code Generator.
# Code generated by Microsoft (R) Python Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------

from ._client import KeyVaultClient
from ._version import VERSION

__version__ = VERSION

try:
from ._patch import __all__ as _patch_all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,44 @@
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# Code generated by Microsoft (R) AutoRest Code Generator.
# Code generated by Microsoft (R) Python Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------

from copy import deepcopy
from typing import Any
from typing import Any, TYPE_CHECKING
from typing_extensions import Self

from azure.core import PipelineClient
from azure.core.pipeline import policies
from azure.core.rest import HttpRequest, HttpResponse

from . import models as _models
from ._configuration import KeyVaultClientConfiguration
from ._operations import KeyVaultClientOperationsMixin
from ._serialization import Deserializer, Serializer

if TYPE_CHECKING:
# pylint: disable=unused-import,ungrouped-imports
from azure.core.credentials import TokenCredential

class KeyVaultClient(KeyVaultClientOperationsMixin): # pylint: disable=client-accepts-api-version-keyword
"""The key vault client performs cryptographic key operations and vault operations against the Key
Vault service.

:keyword api_version: Api Version. Default value is "7.6-preview.1". Note that overriding this
default value may result in unsupported behavior.
class KeyVaultClient(KeyVaultClientOperationsMixin): # pylint: disable=client-accepts-api-version-keyword
"""The key vault client performs cryptographic key operations and vault operations
against the Key Vault service.

:param vault_base_url: The key vault client performs cryptographic key operations and vault
operations against the Key Vault service. Required.
:type vault_base_url: str
:param credential: Credential used to authenticate requests to the service. Required.
:type credential: ~azure.core.credentials.TokenCredential
:keyword api_version: The API version to use for this operation. Default value is
"7.6-preview.1". Note that overriding this default value may result in unsupported behavior.
:paramtype api_version: str
"""

def __init__(self, **kwargs: Any) -> None: # pylint: disable=missing-client-constructor-parameter-credential
def __init__(self, vault_base_url: str, credential: "TokenCredential", **kwargs: Any) -> None:
_endpoint = "{vaultBaseUrl}"
self._config = KeyVaultClientConfiguration(**kwargs)
self._config = KeyVaultClientConfiguration(vault_base_url=vault_base_url, credential=credential, **kwargs)
_policies = kwargs.pop("policies", None)
if _policies is None:
_policies = [
Expand All @@ -50,10 +59,8 @@ def __init__(self, **kwargs: Any) -> None: # pylint: disable=missing-client-con
]
self._client: PipelineClient = PipelineClient(base_url=_endpoint, policies=_policies, **kwargs)

client_models = {k: v for k, v in _models._models.__dict__.items() if isinstance(v, type)}
client_models.update({k: v for k, v in _models.__dict__.items() if isinstance(v, type)})
self._serialize = Serializer(client_models)
self._deserialize = Deserializer(client_models)
self._serialize = Serializer()
self._deserialize = Deserializer()
self._serialize.client_side_validation = False

def send_request(self, request: HttpRequest, *, stream: bool = False, **kwargs: Any) -> HttpResponse:
Expand All @@ -75,13 +82,19 @@ def send_request(self, request: HttpRequest, *, stream: bool = False, **kwargs:
"""

request_copy = deepcopy(request)
request_copy.url = self._client.format_url(request_copy.url)
path_format_arguments = {
"vaultBaseUrl": self._serialize.url(
"self._config.vault_base_url", self._config.vault_base_url, "str", skip_quote=True
),
}

request_copy.url = self._client.format_url(request_copy.url, **path_format_arguments)
return self._client.send_request(request_copy, stream=stream, **kwargs) # type: ignore

def close(self) -> None:
self._client.close()

def __enter__(self) -> "KeyVaultClient":
def __enter__(self) -> Self:
self._client.__enter__()
return self

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# Code generated by Microsoft (R) AutoRest Code Generator.
# Code generated by Microsoft (R) Python Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------

from typing import Any
from typing import Any, TYPE_CHECKING

from azure.core.pipeline import policies

VERSION = "unknown"
from ._version import VERSION

if TYPE_CHECKING:
# pylint: disable=unused-import,ungrouped-imports
from azure.core.credentials import TokenCredential


class KeyVaultClientConfiguration: # pylint: disable=too-many-instance-attributes
Expand All @@ -19,16 +23,29 @@ class KeyVaultClientConfiguration: # pylint: disable=too-many-instance-attribut
Note that all parameters used to create this instance are saved as instance
attributes.

:keyword api_version: Api Version. Default value is "7.6-preview.1". Note that overriding this
default value may result in unsupported behavior.
:param vault_base_url: The key vault client performs cryptographic key operations and vault
operations against the Key Vault service. Required.
:type vault_base_url: str
:param credential: Credential used to authenticate requests to the service. Required.
:type credential: ~azure.core.credentials.TokenCredential
:keyword api_version: The API version to use for this operation. Default value is
"7.6-preview.1". Note that overriding this default value may result in unsupported behavior.
:paramtype api_version: str
"""

def __init__(self, **kwargs: Any) -> None:
def __init__(self, vault_base_url: str, credential: "TokenCredential", **kwargs: Any) -> None:
api_version: str = kwargs.pop("api_version", "7.6-preview.1")

if vault_base_url is None:
raise ValueError("Parameter 'vault_base_url' must not be None.")
if credential is None:
raise ValueError("Parameter 'credential' must not be None.")

self.vault_base_url = vault_base_url
self.credential = credential
self.api_version = api_version
kwargs.setdefault("sdk_moniker", "keyvault/{}".format(VERSION))
self.credential_scopes = kwargs.pop("credential_scopes", ["https://vault.azure.net/.default"])
kwargs.setdefault("sdk_moniker", "keyvault-keys/{}".format(VERSION))
self.polling_interval = kwargs.get("polling_interval", 30)
self._configure(**kwargs)

Expand All @@ -42,3 +59,7 @@ def _configure(self, **kwargs: Any) -> None:
self.redirect_policy = kwargs.get("redirect_policy") or policies.RedirectPolicy(**kwargs)
self.retry_policy = kwargs.get("retry_policy") or policies.RetryPolicy(**kwargs)
self.authentication_policy = kwargs.get("authentication_policy")
if self.credential and not self.authentication_policy:
self.authentication_policy = policies.BearerTokenCredentialPolicy(
self.credential, *self.credential_scopes, **kwargs
)
Loading