|
6 | 6 |
|
7 | 7 | Follow our quickstart for examples: https://aka.ms/azsdk/python/dpcodegen/python/customize |
8 | 8 | """ |
9 | | -from typing import TYPE_CHECKING |
| 9 | +from typing import TYPE_CHECKING, Any, cast |
| 10 | + |
| 11 | +from azure.core.exceptions import ( |
| 12 | + ClientAuthenticationError, |
| 13 | + HttpResponseError, |
| 14 | + ResourceExistsError, |
| 15 | + ResourceNotFoundError, |
| 16 | + ResourceNotModifiedError, |
| 17 | + map_error, |
| 18 | +) |
| 19 | +from azure.core.pipeline import PipelineResponse |
| 20 | +from azure.core.tracing.decorator_async import distributed_trace_async |
| 21 | + |
| 22 | +from ._operations import ( |
| 23 | + KubernetesOperations as _KubernetesOperations, |
| 24 | + build_kubernetes_get_kubeconfig_request, |
| 25 | +) |
10 | 26 |
|
11 | 27 | if TYPE_CHECKING: |
12 | 28 | # pylint: disable=unused-import,ungrouped-imports |
13 | | - from typing import List |
| 29 | + from typing import MutableMapping, Type |
| 30 | + |
| 31 | +__all__ = ("KubernetesOperations",) |
| 32 | + |
| 33 | + |
| 34 | +# Override: generated client expects JSON but this endpoint returns application/yaml; |
| 35 | +# we return the response body as str instead of deserializing. |
| 36 | +class KubernetesOperations(_KubernetesOperations): |
| 37 | + """Kubernetes operations.""" |
| 38 | + |
| 39 | + @distributed_trace_async |
| 40 | + async def get_kubeconfig( |
| 41 | + self, cluster_id: str, *, expiry_seconds: int = 0, **kwargs: Any |
| 42 | + ) -> str: |
| 43 | + """Retrieve the kubeconfig for a Kubernetes Cluster. |
| 44 | +
|
| 45 | + This endpoint returns a kubeconfig file in YAML format. It can be used to |
| 46 | + connect to and administer the cluster using the Kubernetes command line tool, |
| 47 | + ``kubectl``, or other programs supporting kubeconfig files (e.g., client libraries). |
| 48 | +
|
| 49 | + The resulting kubeconfig file uses token-based authentication for clusters |
| 50 | + supporting it, and certificate-based authentication otherwise. For a list of |
| 51 | + supported versions and more information, see "How to Connect to a DigitalOcean |
| 52 | + Kubernetes Cluster" |
| 53 | + https://docs.digitalocean.com/products/kubernetes/how-to/connect-to-cluster/ |
| 54 | +
|
| 55 | + Clusters supporting token-based authentication may define an expiration by |
| 56 | + passing a duration in seconds as a query parameter (expiry_seconds). |
| 57 | + If not set or 0, then the token will have a 7 day expiry. The query parameter |
| 58 | + has no impact in certificate-based authentication. |
| 59 | +
|
| 60 | + Kubernetes Roles granted to a user with a token-based kubeconfig are derived from that user's |
| 61 | + DigitalOcean role. Custom roles require additional configuration by a cluster administrator. |
| 62 | +
|
| 63 | + :param cluster_id: A unique ID that can be used to reference a Kubernetes cluster. Required. |
| 64 | + :type cluster_id: str |
| 65 | + :keyword expiry_seconds: The duration in seconds that the returned Kubernetes credentials will |
| 66 | + be valid. If not set or 0, the credentials will have a 7 day expiry. Default value is 0. |
| 67 | + :paramtype expiry_seconds: int |
| 68 | + :return: The kubeconfig file contents as a string (YAML). |
| 69 | + :rtype: str |
| 70 | + :raises ~azure.core.exceptions.HttpResponseError: |
| 71 | + """ |
| 72 | + error_map: "MutableMapping[int, Type[HttpResponseError]]" = { |
| 73 | + 404: ResourceNotFoundError, |
| 74 | + 409: ResourceExistsError, |
| 75 | + 304: ResourceNotModifiedError, |
| 76 | + 401: cast( |
| 77 | + "Type[HttpResponseError]", |
| 78 | + lambda response: ClientAuthenticationError(response=response), |
| 79 | + ), |
| 80 | + 429: HttpResponseError, |
| 81 | + 500: HttpResponseError, |
| 82 | + } |
| 83 | + error_map.update(kwargs.pop("error_map", {}) or {}) |
| 84 | + |
| 85 | + _headers = kwargs.pop("headers", {}) or {} |
| 86 | + _params = kwargs.pop("params", {}) or {} |
| 87 | + |
| 88 | + _request = build_kubernetes_get_kubeconfig_request( |
| 89 | + cluster_id=cluster_id, |
| 90 | + expiry_seconds=expiry_seconds, |
| 91 | + headers=_headers, |
| 92 | + params=_params, |
| 93 | + ) |
| 94 | + _request.url = self._client.format_url(_request.url) |
| 95 | + |
| 96 | + # stream=True so the pipeline's content policy skips deserialization (API returns YAML, not JSON) |
| 97 | + pipeline_response: PipelineResponse = ( |
| 98 | + await self._client._pipeline.run( # pylint: disable=protected-access |
| 99 | + _request, stream=True, **kwargs |
| 100 | + ) |
| 101 | + ) |
| 102 | + |
| 103 | + response = pipeline_response.http_response |
| 104 | + |
| 105 | + if response.status_code not in [200]: |
| 106 | + await response.read() |
| 107 | + map_error( |
| 108 | + status_code=response.status_code, |
| 109 | + response=response, |
| 110 | + error_map=error_map, |
| 111 | + ) |
| 112 | + raise HttpResponseError(response=response) |
14 | 113 |
|
15 | | -__all__ = ( |
16 | | - [] |
17 | | -) # type: List[str] # Add all objects you want publicly available to users at this package level |
| 114 | + if hasattr(response, "read"): |
| 115 | + body = await response.read() |
| 116 | + else: |
| 117 | + body = response.content |
| 118 | + return body.decode("utf-8") if body else "" |
18 | 119 |
|
19 | 120 |
|
20 | 121 | def patch_sdk(): |
|
0 commit comments