|
| 1 | +# coding=utf-8 |
| 2 | +# ------------------------------------ |
| 3 | +# Copyright (c) Microsoft Corporation. |
| 4 | +# Licensed under the MIT License. |
| 5 | +# ------------------------------------ |
| 6 | +from typing import TYPE_CHECKING |
| 7 | + |
| 8 | +from azure.core.exceptions import ( |
| 9 | + ClientAuthenticationError, |
| 10 | + ResourceNotFoundError, |
| 11 | + ResourceExistsError, |
| 12 | + HttpResponseError, |
| 13 | + map_error, |
| 14 | +) |
| 15 | +from azure.core.paging import ItemPaged |
| 16 | +from azure.core.pipeline import Pipeline |
| 17 | +from azure.core.tracing.decorator import distributed_trace |
| 18 | + |
| 19 | +from ._base_client import ContainerRegistryBaseClient, TransportWrapper |
| 20 | +from ._generated.models import AcrErrors |
| 21 | +from ._helpers import _parse_next_link |
| 22 | +from ._models import ( |
| 23 | + DeleteRepositoryResult, |
| 24 | + ArtifactManifestProperties, |
| 25 | + RepositoryProperties, |
| 26 | +) |
| 27 | +from ._registry_artifact import RegistryArtifact |
| 28 | + |
| 29 | +if TYPE_CHECKING: |
| 30 | + from typing import Any, Dict |
| 31 | + from azure.core.credentials import TokenCredential |
| 32 | + from ._models import ContentProperties |
| 33 | + |
| 34 | + |
| 35 | +class ContainerRepository(ContainerRegistryBaseClient): |
| 36 | + def __init__(self, endpoint, name, credential, **kwargs): |
| 37 | + # type: (str, str, TokenCredential, Dict[str, Any]) -> None |
| 38 | + """Create a ContainerRepository from an endpoint, repository name, and credential |
| 39 | +
|
| 40 | + :param str endpoint: An ACR endpoint |
| 41 | + :param str name: The name of a repository |
| 42 | + :param credential: The credential with which to authenticate |
| 43 | + :type credential: :class:`~azure.core.credentials.TokenCredential` |
| 44 | + :returns: None |
| 45 | + :raises: None |
| 46 | + """ |
| 47 | + if not endpoint.startswith("https://") and not endpoint.startswith("http://"): |
| 48 | + endpoint = "https://" + endpoint |
| 49 | + self._endpoint = endpoint |
| 50 | + self.name = name |
| 51 | + self._credential = credential |
| 52 | + self.fully_qualified_name = self._endpoint + self.name |
| 53 | + super(ContainerRepository, self).__init__(endpoint=self._endpoint, credential=credential, **kwargs) |
| 54 | + |
| 55 | + @distributed_trace |
| 56 | + def delete(self, **kwargs): |
| 57 | + # type: (Dict[str, Any]) -> DeleteRepositoryResult |
| 58 | + """Delete a repository |
| 59 | +
|
| 60 | + :returns: Object containing information about the deleted repository |
| 61 | + :rtype: :class:`~azure.containerregistry.DeleteRepositoryResult` |
| 62 | + :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` |
| 63 | + """ |
| 64 | + return DeleteRepositoryResult._from_generated( # pylint: disable=protected-access |
| 65 | + self._client.container_registry.delete_repository(self.name, **kwargs) |
| 66 | + ) |
| 67 | + |
| 68 | + @distributed_trace |
| 69 | + def get_properties(self, **kwargs): |
| 70 | + # type: (Dict[str, Any]) -> RepositoryProperties |
| 71 | + """Get the properties of a repository |
| 72 | +
|
| 73 | + :returns: :class:`~azure.containerregistry.RepositoryProperties` |
| 74 | + :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` |
| 75 | + """ |
| 76 | + return RepositoryProperties._from_generated( # pylint: disable=protected-access |
| 77 | + self._client.container_registry.get_properties(self.name, **kwargs) |
| 78 | + ) |
| 79 | + |
| 80 | + @distributed_trace |
| 81 | + def list_manifests(self, **kwargs): |
| 82 | + # type: (Dict[str, Any]) -> ItemPaged[ArtifactManifestProperties] |
| 83 | + """List the artifacts for a repository |
| 84 | +
|
| 85 | + :keyword last: Query parameter for the last item in the previous call. Ensuing |
| 86 | + call will return values after last lexically |
| 87 | + :paramtype last: str |
| 88 | + :keyword order_by: Query parameter for ordering by time ascending or descending |
| 89 | + :paramtype order_by: :class:`~azure.containerregistry.ManifestOrder` |
| 90 | + :keyword results_per_page: Number of repositories to return per page |
| 91 | + :paramtype results_per_page: int |
| 92 | + :return: ItemPaged[:class:`ArtifactManifestProperties`] |
| 93 | + :rtype: :class:`~azure.core.paging.ItemPaged` |
| 94 | + :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` |
| 95 | + """ |
| 96 | + name = self.name |
| 97 | + last = kwargs.pop("last", None) |
| 98 | + n = kwargs.pop("results_per_page", None) |
| 99 | + orderby = kwargs.pop("order_by", None) |
| 100 | + cls = kwargs.pop( |
| 101 | + "cls", |
| 102 | + lambda objs: [ |
| 103 | + ArtifactManifestProperties._from_generated(x, repository_name=self.name) # pylint: disable=protected-access |
| 104 | + for x in objs |
| 105 | + ], |
| 106 | + ) |
| 107 | + |
| 108 | + error_map = {401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError} |
| 109 | + error_map.update(kwargs.pop("error_map", {})) |
| 110 | + accept = "application/json" |
| 111 | + |
| 112 | + def prepare_request(next_link=None): |
| 113 | + # Construct headers |
| 114 | + header_parameters = {} # type: Dict[str, Any] |
| 115 | + header_parameters["Accept"] = self._client._serialize.header( # pylint: disable=protected-access |
| 116 | + "accept", accept, "str" |
| 117 | + ) |
| 118 | + |
| 119 | + if not next_link: |
| 120 | + # Construct URL |
| 121 | + url = "/acr/v1/{name}/_manifests" |
| 122 | + path_format_arguments = { |
| 123 | + "url": self._client._serialize.url( # pylint: disable=protected-access |
| 124 | + "self._client._config.url", |
| 125 | + self._client._config.url, # pylint: disable=protected-access |
| 126 | + "str", |
| 127 | + skip_quote=True, |
| 128 | + ), |
| 129 | + "name": self._client._serialize.url("name", name, "str"), # pylint: disable=protected-access |
| 130 | + } |
| 131 | + url = self._client._client.format_url(url, **path_format_arguments) # pylint: disable=protected-access |
| 132 | + # Construct parameters |
| 133 | + query_parameters = {} # type: Dict[str, Any] |
| 134 | + if last is not None: |
| 135 | + query_parameters["last"] = self._client._serialize.query( # pylint: disable=protected-access |
| 136 | + "last", last, "str" |
| 137 | + ) |
| 138 | + if n is not None: |
| 139 | + query_parameters["n"] = self._client._serialize.query( # pylint: disable=protected-access |
| 140 | + "n", n, "int" |
| 141 | + ) |
| 142 | + if orderby is not None: |
| 143 | + query_parameters["orderby"] = self._client._serialize.query( # pylint: disable=protected-access |
| 144 | + "orderby", orderby, "str" |
| 145 | + ) |
| 146 | + |
| 147 | + request = self._client._client.get( # pylint: disable=protected-access |
| 148 | + url, query_parameters, header_parameters |
| 149 | + ) |
| 150 | + else: |
| 151 | + url = next_link |
| 152 | + query_parameters = {} # type: Dict[str, Any] |
| 153 | + path_format_arguments = { |
| 154 | + "url": self._client._serialize.url( # pylint: disable=protected-access |
| 155 | + "self._client._config.url", |
| 156 | + self._client._config.url, # pylint: disable=protected-access |
| 157 | + "str", |
| 158 | + skip_quote=True, |
| 159 | + ), |
| 160 | + "name": self._client._serialize.url("name", name, "str"), # pylint: disable=protected-access |
| 161 | + } |
| 162 | + url = self._client._client.format_url(url, **path_format_arguments) # pylint: disable=protected-access |
| 163 | + request = self._client._client.get( # pylint: disable=protected-access |
| 164 | + url, query_parameters, header_parameters |
| 165 | + ) |
| 166 | + return request |
| 167 | + |
| 168 | + def extract_data(pipeline_response): |
| 169 | + deserialized = self._client._deserialize( # pylint: disable=protected-access |
| 170 | + "AcrManifests", pipeline_response |
| 171 | + ) |
| 172 | + list_of_elem = deserialized.manifests |
| 173 | + if cls: |
| 174 | + list_of_elem = cls(list_of_elem) |
| 175 | + link = None |
| 176 | + if "Link" in pipeline_response.http_response.headers.keys(): |
| 177 | + link = _parse_next_link(pipeline_response.http_response.headers["Link"]) |
| 178 | + return link, iter(list_of_elem) |
| 179 | + |
| 180 | + def get_next(next_link=None): |
| 181 | + request = prepare_request(next_link) |
| 182 | + |
| 183 | + pipeline_response = self._client._client._pipeline.run( # pylint: disable=protected-access |
| 184 | + request, stream=False, **kwargs |
| 185 | + ) |
| 186 | + response = pipeline_response.http_response |
| 187 | + |
| 188 | + if response.status_code not in [200]: |
| 189 | + error = self._client._deserialize.failsafe_deserialize( # pylint: disable=protected-access |
| 190 | + AcrErrors, response |
| 191 | + ) |
| 192 | + map_error(status_code=response.status_code, response=response, error_map=error_map) |
| 193 | + raise HttpResponseError(response=response, model=error) |
| 194 | + |
| 195 | + return pipeline_response |
| 196 | + |
| 197 | + return ItemPaged(get_next, extract_data) |
| 198 | + |
| 199 | + @distributed_trace |
| 200 | + def set_properties(self, properties, **kwargs): |
| 201 | + # type: (RepositoryProperties, Dict[str, Any]) -> RepositoryProperties |
| 202 | + """Set the properties of a repository |
| 203 | +
|
| 204 | + :returns: :class:`~azure.containerregistry.RepositoryProperties` |
| 205 | + :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` |
| 206 | + """ |
| 207 | + return RepositoryProperties._from_generated( # pylint: disable=protected-access |
| 208 | + self._client.container_registry.set_properties( |
| 209 | + self.name, properties._to_generated(), **kwargs # pylint: disable=protected-access |
| 210 | + ) |
| 211 | + ) |
| 212 | + |
| 213 | + @distributed_trace |
| 214 | + def get_artifact(self, tag_or_digest, **kwargs): |
| 215 | + # type: (str, str, Dict[str, Any]) -> RegistryArtifact |
| 216 | + """Get a Registry Artifact object |
| 217 | +
|
| 218 | + :param str repository_name: Name of the repository |
| 219 | + :param str tag_or_digest: The tag or digest of the artifact |
| 220 | + :returns: :class:`~azure.containerregistry.RegistryArtifact` |
| 221 | + :raises: None |
| 222 | + """ |
| 223 | + _pipeline = Pipeline( |
| 224 | + transport=TransportWrapper(self._client._client._pipeline._transport), # pylint: disable=protected-access |
| 225 | + policies=self._client._client._pipeline._impl_policies, # pylint: disable=protected-access |
| 226 | + ) |
| 227 | + return RegistryArtifact( |
| 228 | + self._endpoint, self.name, tag_or_digest, self._credential, pipeline=_pipeline, **kwargs |
| 229 | + ) |
0 commit comments