|
| 1 | +# coding=utf-8 |
| 2 | +# -------------------------------------------------------------------------- |
| 3 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 5 | +# Code generated by Microsoft (R) AutoRest Code Generator. |
| 6 | +# Changes may cause incorrect behavior and will be lost if the code is regenerated. |
| 7 | +# -------------------------------------------------------------------------- |
| 8 | +import sys |
| 9 | +from typing import Any, AsyncIterable, Callable, Dict, Optional, TypeVar |
| 10 | +import urllib.parse |
| 11 | + |
| 12 | +from azure.core.async_paging import AsyncItemPaged, AsyncList |
| 13 | +from azure.core.exceptions import ( |
| 14 | + ClientAuthenticationError, |
| 15 | + HttpResponseError, |
| 16 | + ResourceExistsError, |
| 17 | + ResourceNotFoundError, |
| 18 | + ResourceNotModifiedError, |
| 19 | + map_error, |
| 20 | +) |
| 21 | +from azure.core.pipeline import PipelineResponse |
| 22 | +from azure.core.rest import AsyncHttpResponse, HttpRequest |
| 23 | +from azure.core.tracing.decorator import distributed_trace |
| 24 | +from azure.core.tracing.decorator_async import distributed_trace_async |
| 25 | +from azure.core.utils import case_insensitive_dict |
| 26 | +from azure.mgmt.core.exceptions import ARMErrorFormat |
| 27 | + |
| 28 | +from ... import models as _models |
| 29 | +from ...operations._net_app_resource_usages_operations import build_get_request, build_list_request |
| 30 | + |
| 31 | +if sys.version_info >= (3, 9): |
| 32 | + from collections.abc import MutableMapping |
| 33 | +else: |
| 34 | + from typing import MutableMapping # type: ignore |
| 35 | +T = TypeVar("T") |
| 36 | +ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]] |
| 37 | + |
| 38 | + |
| 39 | +class NetAppResourceUsagesOperations: |
| 40 | + """ |
| 41 | + .. warning:: |
| 42 | + **DO NOT** instantiate this class directly. |
| 43 | +
|
| 44 | + Instead, you should access the following operations through |
| 45 | + :class:`~azure.mgmt.netapp.aio.NetAppManagementClient`'s |
| 46 | + :attr:`net_app_resource_usages` attribute. |
| 47 | + """ |
| 48 | + |
| 49 | + models = _models |
| 50 | + |
| 51 | + def __init__(self, *args, **kwargs) -> None: |
| 52 | + input_args = list(args) |
| 53 | + self._client = input_args.pop(0) if input_args else kwargs.pop("client") |
| 54 | + self._config = input_args.pop(0) if input_args else kwargs.pop("config") |
| 55 | + self._serialize = input_args.pop(0) if input_args else kwargs.pop("serializer") |
| 56 | + self._deserialize = input_args.pop(0) if input_args else kwargs.pop("deserializer") |
| 57 | + |
| 58 | + @distributed_trace |
| 59 | + def list(self, location: str, **kwargs: Any) -> AsyncIterable["_models.UsageResult"]: |
| 60 | + """Get usages. |
| 61 | +
|
| 62 | + Get current subscription usages. |
| 63 | +
|
| 64 | + :param location: The name of the Azure region. Required. |
| 65 | + :type location: str |
| 66 | + :return: An iterator like instance of either UsageResult or the result of cls(response) |
| 67 | + :rtype: ~azure.core.async_paging.AsyncItemPaged[~azure.mgmt.netapp.models.UsageResult] |
| 68 | + :raises ~azure.core.exceptions.HttpResponseError: |
| 69 | + """ |
| 70 | + _headers = kwargs.pop("headers", {}) or {} |
| 71 | + _params = case_insensitive_dict(kwargs.pop("params", {}) or {}) |
| 72 | + |
| 73 | + api_version: str = kwargs.pop("api_version", _params.pop("api-version", self._config.api_version)) |
| 74 | + cls: ClsType[_models.UsagesListResult] = kwargs.pop("cls", None) |
| 75 | + |
| 76 | + error_map: MutableMapping = { |
| 77 | + 401: ClientAuthenticationError, |
| 78 | + 404: ResourceNotFoundError, |
| 79 | + 409: ResourceExistsError, |
| 80 | + 304: ResourceNotModifiedError, |
| 81 | + } |
| 82 | + error_map.update(kwargs.pop("error_map", {}) or {}) |
| 83 | + |
| 84 | + def prepare_request(next_link=None): |
| 85 | + if not next_link: |
| 86 | + |
| 87 | + _request = build_list_request( |
| 88 | + location=location, |
| 89 | + subscription_id=self._config.subscription_id, |
| 90 | + api_version=api_version, |
| 91 | + headers=_headers, |
| 92 | + params=_params, |
| 93 | + ) |
| 94 | + _request.url = self._client.format_url(_request.url) |
| 95 | + |
| 96 | + else: |
| 97 | + # make call to next link with the client's api-version |
| 98 | + _parsed_next_link = urllib.parse.urlparse(next_link) |
| 99 | + _next_request_params = case_insensitive_dict( |
| 100 | + { |
| 101 | + key: [urllib.parse.quote(v) for v in value] |
| 102 | + for key, value in urllib.parse.parse_qs(_parsed_next_link.query).items() |
| 103 | + } |
| 104 | + ) |
| 105 | + _next_request_params["api-version"] = self._config.api_version |
| 106 | + _request = HttpRequest( |
| 107 | + "GET", urllib.parse.urljoin(next_link, _parsed_next_link.path), params=_next_request_params |
| 108 | + ) |
| 109 | + _request.url = self._client.format_url(_request.url) |
| 110 | + _request.method = "GET" |
| 111 | + return _request |
| 112 | + |
| 113 | + async def extract_data(pipeline_response): |
| 114 | + deserialized = self._deserialize("UsagesListResult", pipeline_response) |
| 115 | + list_of_elem = deserialized.value |
| 116 | + if cls: |
| 117 | + list_of_elem = cls(list_of_elem) # type: ignore |
| 118 | + return deserialized.next_link or None, AsyncList(list_of_elem) |
| 119 | + |
| 120 | + async def get_next(next_link=None): |
| 121 | + _request = prepare_request(next_link) |
| 122 | + |
| 123 | + _stream = False |
| 124 | + pipeline_response: PipelineResponse = await self._client._pipeline.run( # pylint: disable=protected-access |
| 125 | + _request, stream=_stream, **kwargs |
| 126 | + ) |
| 127 | + response = pipeline_response.http_response |
| 128 | + |
| 129 | + if response.status_code not in [200]: |
| 130 | + map_error(status_code=response.status_code, response=response, error_map=error_map) |
| 131 | + error = self._deserialize.failsafe_deserialize(_models.ErrorResponse, pipeline_response) |
| 132 | + raise HttpResponseError(response=response, model=error, error_format=ARMErrorFormat) |
| 133 | + |
| 134 | + return pipeline_response |
| 135 | + |
| 136 | + return AsyncItemPaged(get_next, extract_data) |
| 137 | + |
| 138 | + @distributed_trace_async |
| 139 | + async def get(self, location: str, usage_type: str, **kwargs: Any) -> _models.UsageResult: |
| 140 | + """Get specific type of usage. |
| 141 | +
|
| 142 | + Get current subscription usage of the specific type. |
| 143 | +
|
| 144 | + :param location: The name of the Azure region. Required. |
| 145 | + :type location: str |
| 146 | + :param usage_type: The type of usage. Required. |
| 147 | + :type usage_type: str |
| 148 | + :return: UsageResult or the result of cls(response) |
| 149 | + :rtype: ~azure.mgmt.netapp.models.UsageResult |
| 150 | + :raises ~azure.core.exceptions.HttpResponseError: |
| 151 | + """ |
| 152 | + error_map: MutableMapping = { |
| 153 | + 401: ClientAuthenticationError, |
| 154 | + 404: ResourceNotFoundError, |
| 155 | + 409: ResourceExistsError, |
| 156 | + 304: ResourceNotModifiedError, |
| 157 | + } |
| 158 | + error_map.update(kwargs.pop("error_map", {}) or {}) |
| 159 | + |
| 160 | + _headers = kwargs.pop("headers", {}) or {} |
| 161 | + _params = case_insensitive_dict(kwargs.pop("params", {}) or {}) |
| 162 | + |
| 163 | + api_version: str = kwargs.pop("api_version", _params.pop("api-version", self._config.api_version)) |
| 164 | + cls: ClsType[_models.UsageResult] = kwargs.pop("cls", None) |
| 165 | + |
| 166 | + _request = build_get_request( |
| 167 | + location=location, |
| 168 | + usage_type=usage_type, |
| 169 | + subscription_id=self._config.subscription_id, |
| 170 | + api_version=api_version, |
| 171 | + headers=_headers, |
| 172 | + params=_params, |
| 173 | + ) |
| 174 | + _request.url = self._client.format_url(_request.url) |
| 175 | + |
| 176 | + _stream = False |
| 177 | + pipeline_response: PipelineResponse = await self._client._pipeline.run( # pylint: disable=protected-access |
| 178 | + _request, stream=_stream, **kwargs |
| 179 | + ) |
| 180 | + |
| 181 | + response = pipeline_response.http_response |
| 182 | + |
| 183 | + if response.status_code not in [200]: |
| 184 | + map_error(status_code=response.status_code, response=response, error_map=error_map) |
| 185 | + error = self._deserialize.failsafe_deserialize(_models.ErrorResponse, pipeline_response) |
| 186 | + raise HttpResponseError(response=response, model=error, error_format=ARMErrorFormat) |
| 187 | + |
| 188 | + deserialized = self._deserialize("UsageResult", pipeline_response.http_response) |
| 189 | + |
| 190 | + if cls: |
| 191 | + return cls(pipeline_response, deserialized, {}) # type: ignore |
| 192 | + |
| 193 | + return deserialized # type: ignore |
0 commit comments