diff --git a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_container_registry_client.py b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_container_registry_client.py index 2cf8519d0847..8da2f684fb93 100644 --- a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_container_registry_client.py +++ b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_container_registry_client.py @@ -36,6 +36,15 @@ def __init__(self, endpoint, credential, **kwargs): :type credential: :class:`~azure.core.credentials.TokenCredential` :returns: None :raises: None + + .. admonition:: Example: + + .. literalinclude:: ../samples/sample_create_client.py + :start-after: [START create_registry_client] + :end-before: [END create_registry_client] + :language: python + :dedent: 8 + :caption: Instantiate an instance of `ContainerRegistryClient` """ if not endpoint.startswith("https://") and not endpoint.startswith("http://"): endpoint = "https://" + endpoint @@ -52,6 +61,15 @@ def delete_repository(self, repository, **kwargs): :returns: Object containing information about the deleted repository :rtype: :class:`~azure.containerregistry.DeleteRepositoryResult` :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` + + .. admonition:: Example: + + .. literalinclude:: ../samples/sample_create_client.py + :start-after: [START delete_repository] + :end-before: [END delete_repository] + :language: python + :dedent: 8 + :caption: Delete a repository from the `ContainerRegistryClient` """ return DeleteRepositoryResult._from_generated( # pylint: disable=protected-access self._client.container_registry.delete_repository(repository, **kwargs) @@ -72,6 +90,15 @@ def list_repositories(self, **kwargs): :return: ItemPaged[str] :rtype: :class:`~azure.core.paging.ItemPaged` :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` + + .. admonition:: Example: + + .. literalinclude:: ../samples/sample_delete_old_tags.py + :start-after: [START list_repositories] + :end-before: [END list_repositories] + :language: python + :dedent: 8 + :caption: List repositories in a container registry account """ n = kwargs.pop("results_per_page", None) last = kwargs.pop("last", None) @@ -172,6 +199,17 @@ def get_repository_client(self, repository, **kwargs): :param str repository: The repository to create a client for :returns: :class:`~azure.containerregistry.ContainerRepositoryClient` :raises: None + + Example + + .. code-block:: python + + from azure.containerregistry import ContainerRepositoryClient + from azure.identity import DefaultAzureCredential + + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + client = ContainerRegistryClient(account_url, DefaultAzureCredential()) + repository_client = client.get_repository_client("my_repository") """ _pipeline = Pipeline( transport=TransportWrapper(self._client._client._pipeline._transport), # pylint: disable=protected-access diff --git a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_container_repository_client.py b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_container_repository_client.py index 3cbcade4979f..e14742474fb4 100644 --- a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_container_repository_client.py +++ b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_container_repository_client.py @@ -43,6 +43,15 @@ def __init__(self, endpoint, repository, credential, **kwargs): :type credential: :class:`~azure.core.credentials.TokenCredential` :returns: None :raises: None + + .. admonition:: Example: + + .. literalinclude:: ../samples/sample_create_client.py + :start-after: [START create_repository_client] + :end-before: [END create_repository_client] + :language: python + :dedent: 8 + :caption: Instantiate an instance of `ContainerRepositoryClient` """ if not endpoint.startswith("https://") and not endpoint.startswith("http://"): endpoint = "https://" + endpoint @@ -63,6 +72,17 @@ def delete(self, **kwargs): :returns: Object containing information about the deleted repository :rtype: :class:`~azure.containerregistry.DeleteRepositoryResult` :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` + + Example + + .. code-block:: python + + from azure.containerregistry import ContainerRepositoryClient + from azure.identity import DefaultAzureCredential + + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + client = ContainerRepositoryClient(account_url, "my_repository", DefaultAzureCredential()) + client.delete() """ return DeleteRepositoryResult._from_generated( # pylint: disable=protected-access self._client.container_registry.delete_repository(self.repository, **kwargs) @@ -77,6 +97,18 @@ def delete_registry_artifact(self, digest, **kwargs): :type digest: str :returns: None :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` + + Example + + .. code-block:: python + + from azure.containerregistry import ContainerRepositoryClient + from azure.identity import DefaultAzureCredential + + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + client = ContainerRepositoryClient(account_url, "my_repository", DefaultAzureCredential()) + for artifact in client.list_registry_artifacts(): + client.delete_registry_artifact(artifact.digest) """ self._client.container_registry.delete_manifest(self.repository, digest, **kwargs) @@ -88,6 +120,18 @@ def delete_tag(self, tag, **kwargs): :param str tag: The tag to be deleted :returns: None :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` + + Example + + .. code-block:: python + + from azure.containerregistry import ContainerRepositoryClient + from azure.identity import DefaultAzureCredential + + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + client = ContainerRepositoryClient(account_url, "my_repository", DefaultAzureCredential()) + for artifact in client.list_tags(): + client.delete_tag(tag.name) """ self._client.container_registry.delete_tag(self.repository, tag, **kwargs) @@ -98,6 +142,24 @@ def get_properties(self, **kwargs): :returns: :class:`~azure.containerregistry.RepositoryProperties` :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` + + Example + + .. code-block:: python + + from azure.containerregistry import ContainerRepositoryClient + from azure.identity import DefaultAzureCredential + + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + client = ContainerRepositoryClient(account_url, "my_repository", DefaultAzureCredential()) + repository_properties = client.get_properties() + print(repository_properties.name) + print(repository_properties.content_permissions) + print(repository_properties.created_on) + print(repository_properties.last_updated_on) + print(repository_properties.manifest_count) + print(repository_properties.registry) + print(repository_properties.tag_count) """ return RepositoryProperties._from_generated( # pylint: disable=protected-access self._client.container_registry.get_properties(self.repository, **kwargs) @@ -112,6 +174,18 @@ def get_registry_artifact_properties(self, tag_or_digest, **kwargs): :type tag_or_digest: str :returns: :class:`~azure.containerregistry.RegistryArtifactProperties` :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` + + Example + + .. code-block:: python + + from azure.containerregistry import ContainerRepositoryClient + from azure.identity import DefaultAzureCredential + + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + client = ContainerRepositoryClient(account_url, "my_repository", DefaultAzureCredential()) + for artifact in client.list_registry_artifacts(): + properties = client.get_registry_artifact_properties(artifact.digest) """ if _is_tag(tag_or_digest): tag_or_digest = self._get_digest_from_tag(tag_or_digest) @@ -129,6 +203,18 @@ def get_tag_properties(self, tag, **kwargs): :type tag: str :returns: :class:`~azure.containerregistry.ArtifactTagProperties` :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` + + Example + + .. code-block:: python + + from azure.containerregistry import ContainerRepositoryClient + from azure.identity import DefaultAzureCredential + + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + client = ContainerRepositoryClient(account_url, "my_repository", DefaultAzureCredential()) + for tag in client.list_tags(): + tag_properties = client.get_tag_properties(tag.name) """ return ArtifactTagProperties._from_generated( # pylint: disable=protected-access self._client.container_registry.get_tag_properties(self.repository, tag, **kwargs), @@ -150,6 +236,18 @@ def list_registry_artifacts(self, **kwargs): :return: ItemPaged[:class:`RegistryArtifactProperties`] :rtype: :class:`~azure.core.paging.ItemPaged` :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` + + Example + + .. code-block:: python + + from azure.containerregistry import ContainerRepositoryClient + from azure.identity import DefaultAzureCredential + + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + client = ContainerRepositoryClient(account_url, "my_repository", DefaultAzureCredential()) + for artifact in client.list_registry_artifacts(): + print(artifact.digest) """ name = self.repository last = kwargs.pop("last", None) @@ -268,6 +366,18 @@ def list_tags(self, **kwargs): :return: ItemPaged[:class:`~azure.containerregistry.ArtifactTagProperties`] :rtype: :class:`~azure.core.paging.ItemPaged` :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` + + Example + + .. code-block:: python + + from azure.containerregistry import ContainerRepositoryClient + from azure.identity import DefaultAzureCredential + + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + client = ContainerRepositoryClient(account_url, "my_repository", DefaultAzureCredential()) + for tag in client.list_tags(): + tag_properties = client.get_tag_properties(tag.name) """ name = self.repository last = kwargs.pop("last", None) @@ -386,6 +496,26 @@ def set_manifest_properties(self, digest, permissions, **kwargs): :type permissions: ContentProperties :returns: :class:`~azure.containerregistry.RegistryArtifactProperties` :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` + + Example + + .. code-block:: python + + from azure.containerregistry import ContainerRepositoryClient + from azure.identity import DefaultAzureCredential + + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + client = ContainerRepositoryClient(account_url, "my_repository", DefaultAzureCredential()) + for artifact in client.list_registry_artifacts(): + received_permissions = client.set_manifest_properties( + artifact.digest, + ContentPermissions( + can_delete=False, + can_list=False, + can_read=False, + can_write=False, + ), + ) """ return RegistryArtifactProperties._from_generated( # pylint: disable=protected-access self._client.container_registry.update_manifest_properties( @@ -404,6 +534,26 @@ def set_tag_properties(self, tag, permissions, **kwargs): :type permissions: ContentProperties :returns: :class:`~azure.containerregistry.ArtifactTagProperties` :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` + + Example + + .. code-block:: python + + from azure.containerregistry import ContainerRepositoryClient + from azure.identity import DefaultAzureCredential + + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + client = ContainerRepositoryClient(account_url, "my_repository", DefaultAzureCredential()) + tag_identifier = "latest" + received = client.set_tag_properties( + tag_identifier, + ContentPermissions( + can_delete=False, + can_list=False, + can_read=False, + can_write=False, + ), + ) """ return ArtifactTagProperties._from_generated( # pylint: disable=protected-access self._client.container_registry.update_tag_attributes( diff --git a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/aio/_async_container_registry_client.py b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/aio/_async_container_registry_client.py index a9669b17e274..f9d5f7284462 100644 --- a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/aio/_async_container_registry_client.py +++ b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/aio/_async_container_registry_client.py @@ -37,6 +37,15 @@ def __init__(self, endpoint: str, credential: "AsyncTokenCredential", **kwargs: :type credential: :class:`~azure.core.credentials_async.AsyncTokenCredential` :returns: None :raises: None + + .. admonition:: Example: + + .. literalinclude:: ../samples/async_samples/sample_create_client_async.py + :start-after: [START create_registry_client] + :end-before: [END create_registry_client] + :language: python + :dedent: 8 + :caption: Instantiate an instance of `ContainerRegistryClient` """ if not endpoint.startswith("https://") and not endpoint.startswith("http://"): endpoint = "https://" + endpoint @@ -53,6 +62,15 @@ async def delete_repository(self, repository: str, **kwargs: Dict[str, Any]) -> :returns: Object containing information about the deleted repository :rtype: :class:`~azure.containerregistry.DeleteRepositoryResult` :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` + + .. admonition:: Example: + + .. literalinclude:: ../samples/async_samples/sample_create_client_async.py + :start-after: [START delete_repository] + :end-before: [END delete_repository] + :language: python + :dedent: 8 + :caption: Delete a repository from the `ContainerRegistryClient` """ result = await self._client.container_registry.delete_repository(repository, **kwargs) return DeleteRepositoryResult._from_generated(result) # pylint: disable=protected-access @@ -71,6 +89,15 @@ def list_repositories(self, **kwargs: Dict[str, Any]) -> AsyncItemPaged[str]: :return: ItemPaged[str] :rtype: :class:`~azure.core.async_paging.AsyncItemPaged` :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` + + .. admonition:: Example: + + .. literalinclude:: ../samples/async_samples/sample_delete_old_tags_async.py + :start-after: [START list_repositories] + :end-before: [END list_repositories] + :language: python + :dedent: 8 + :caption: List repositories in a container registry account """ n = kwargs.pop("results_per_page", None) last = kwargs.pop("last", None) @@ -168,6 +195,17 @@ def get_repository_client(self, repository: str, **kwargs: Dict[str, Any]) -> Co :param repository: The repository to create a client for :type repository: str :returns: :class:`~azure.containerregistry.aio.ContainerRepositoryClient` + + Example + + .. code-block:: python + + from azure.containerregistry.aio import ContainerRepositoryClient + from azure.identity.aio import DefaultAzureCredential + + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + client = ContainerRegistryClient(account_url, DefaultAzureCredential()) + repository_client = client.get_repository_client("my_repository") """ _pipeline = AsyncPipeline( transport=AsyncTransportWrapper( diff --git a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/aio/_async_container_repository_client.py b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/aio/_async_container_repository_client.py index 77546ccc4ee4..cc4603068b45 100644 --- a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/aio/_async_container_repository_client.py +++ b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/aio/_async_container_repository_client.py @@ -45,6 +45,15 @@ def __init__( :type credential: :class:`~azure.core.credentials_async.AsyncTokenCredential` :returns: None :raises: None + + .. admonition:: Example: + + .. literalinclude:: ../samples/sample_create_client.py + :start-after: [START create_registry_client] + :end-before: [END create_registry_client] + :language: python + :dedent: 8 + :caption: Instantiate an instance of `ContainerRepositoryClient` """ if not endpoint.startswith("https://") and not endpoint.startswith("http://"): endpoint = "https://" + endpoint @@ -64,6 +73,17 @@ async def delete(self, **kwargs: Dict[str, Any]) -> DeleteRepositoryResult: :returns: Object containing information about the deleted repository :rtype: :class:`~azure.containerregistry.DeleteRepositoryResult` :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` + + Example + + .. code-block:: python + + from azure.containerregistry.aio import ContainerRepositoryClient + from azure.identity.aio import DefaultAzureCredential + + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + client = ContainerRepositoryClient(account_url, "my_repository", DefaultAzureCredential()) + await client.delete() """ return DeleteRepositoryResult._from_generated( # pylint: disable=protected-access await self._client.container_registry.delete_repository(self.repository, **kwargs) @@ -77,6 +97,18 @@ async def delete_registry_artifact(self, digest: str, **kwargs: Dict[str, Any]) :type digest: str :returns: None :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` + + Example + + .. code-block:: python + + from azure.containerregistry.aio import ContainerRepositoryClient + from azure.identity.aio import DefaultAzureCredential + + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + client = ContainerRepositoryClient(account_url, "my_repository", DefaultAzureCredential()) + async for artifact in client.list_registry_artifacts(): + await client.delete_registry_artifact(artifact.digest) """ await self._client.container_registry.delete_manifest(self.repository, digest, **kwargs) @@ -87,6 +119,18 @@ async def delete_tag(self, tag: str, **kwargs: Dict[str, Any]) -> None: :param str tag: The tag to be deleted :returns: None :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` + + Example + + .. code-block:: python + + from azure.containerregistry.aio import ContainerRepositoryClient + from azure.identity.aio import DefaultAzureCredential + + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + client = ContainerRepositoryClient(account_url, "my_repository", DefaultAzureCredential()) + async for artifact in client.list_tags(): + await client.delete_tag(tag.name) """ await self._client.container_registry.delete_tag(self.repository, tag, **kwargs) @@ -96,6 +140,24 @@ async def get_properties(self, **kwargs: Dict[str, Any]) -> RepositoryProperties :returns: :class:`~azure.containerregistry.RepositoryProperties` :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` + + Example + + .. code-block:: python + + from azure.containerregistry.aio import ContainerRepositoryClient + from azure.identity.aio import DefaultAzureCredential + + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + client = ContainerRepositoryClient(account_url, "my_repository", DefaultAzureCredential()) + repository_properties = await client.get_properties() + print(repository_properties.name) + print(repository_properties.content_permissions) + print(repository_properties.created_on) + print(repository_properties.last_updated_on) + print(repository_properties.manifest_count) + print(repository_properties.registry) + print(repository_properties.tag_count) """ return RepositoryProperties._from_generated( # pylint: disable=protected-access await self._client.container_registry.get_properties(self.repository, **kwargs) @@ -111,6 +173,18 @@ async def get_registry_artifact_properties( :type tag_or_digest: str :returns: :class:`~azure.containerregistry.RegistryArtifactProperties` :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` + + Example + + .. code-block:: python + + from azure.containerregistry.aio import ContainerRepositoryClient + from azure.identity.aio import DefaultAzureCredential + + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + client = ContainerRepositoryClient(account_url, "my_repository", DefaultAzureCredential()) + async for artifact in client.list_registry_artifacts(): + properties = await client.get_registry_artifact_properties(artifact.digest) """ if _is_tag(tag_or_digest): tag_or_digest = self._get_digest_from_tag(tag_or_digest) @@ -127,6 +201,18 @@ async def get_tag_properties(self, tag: str, **kwargs: Dict[str, Any]) -> Artifa :type tag: str :returns: :class:`~azure.containerregistry.ArtifactTagProperties` :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` + + Example + + .. code-block:: python + + from azure.containerregistry.aio import ContainerRepositoryClient + from azure.identity.aio import DefaultAzureCredential + + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + client = ContainerRepositoryClient(account_url, "my_repository", DefaultAzureCredential()) + async for tag in client.list_tags(): + tag_properties = await client.get_tag_properties(tag.name) """ return ArtifactTagProperties._from_generated( # pylint: disable=protected-access await self._client.container_registry.get_tag_properties(self.repository, tag, **kwargs), @@ -147,6 +233,18 @@ def list_registry_artifacts(self, **kwargs: Dict[str, Any]) -> AsyncItemPaged[Re :return: ItemPaged[:class:`~azure.containerregistry.RegistryArtifactProperties`] :rtype: :class:`~azure.core.async_paging.AsyncItemPaged` :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` + + Example + + .. code-block:: python + + from azure.containerregistry.aio import ContainerRepositoryClient + from azure.identity.aio import DefaultAzureCredential + + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + client = ContainerRepositoryClient(account_url, "my_repository", DefaultAzureCredential()) + async for artifact in client.list_registry_artifacts(): + print(artifact.digest) """ name = self.repository last = kwargs.pop("last", None) @@ -264,6 +362,18 @@ def list_tags(self, **kwargs: Dict[str, Any]) -> AsyncItemPaged[ArtifactTagPrope :return: ItemPaged[:class:`~azure.containerregistry.ArtifactTagProperties`] :rtype: :class:`~azure.core.async_paging.AsyncItemPaged` :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` + + Example + + .. code-block:: python + + from azure.containerregistry.aio import ContainerRepositoryClient + from azure.identity.aio import DefaultAzureCredential + + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + client = ContainerRepositoryClient(account_url, "my_repository", DefaultAzureCredential()) + async for tag in client.list_tags(): + tag_properties = await client.get_tag_properties(tag.name) """ name = self.repository last = kwargs.pop("last", None) @@ -383,6 +493,26 @@ async def set_manifest_properties( :type permissions: ContentProperties :returns: :class:`~azure.containerregistry.RegistryArtifactProperties` :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` + + Example + + .. code-block:: python + + from azure.containerregistry.aio import ContainerRepositoryClient + from azure.identity.aio import DefaultAzureCredential + + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + client = ContainerRepositoryClient(account_url, "my_repository", DefaultAzureCredential()) + async for artifact in client.list_registry_artifacts(): + received_permissions = await client.set_manifest_properties( + artifact.digest, + ContentPermissions( + can_delete=False, + can_list=False, + can_read=False, + can_write=False, + ), + ) """ return RegistryArtifactProperties._from_generated( # pylint: disable=protected-access await self._client.container_registry.update_manifest_properties( @@ -402,6 +532,26 @@ async def set_tag_properties( :type permissions: ContentProperties :returns: :class:`~azure.containerregistry.ArtifactTagProperties` :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` + + Example + + .. code-block:: python + + from azure.containerregistry.aio import ContainerRepositoryClient + from azure.identity.aio import DefaultAzureCredential + + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + client = ContainerRepositoryClient(account_url, "my_repository", DefaultAzureCredential()) + tag_identifier = "latest" + received = await client.set_tag_properties( + tag_identifier, + ContentPermissions( + can_delete=False, + can_list=False, + can_read=False, + can_write=False, + ), + ) """ return ArtifactTagProperties._from_generated( # pylint: disable=protected-access await self._client.container_registry.update_tag_attributes( diff --git a/sdk/containerregistry/azure-containerregistry/samples/README.md b/sdk/containerregistry/azure-containerregistry/samples/README.md index 289a04251778..e29d222f4c5c 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/README.md +++ b/sdk/containerregistry/azure-containerregistry/samples/README.md @@ -16,7 +16,8 @@ The async versions of the samples require Python 3.6 or later. |**File Name**|**Description**| |-------------|---------------| -|[sample_create_client.py][create_client] and [sample_create_client_async.py][create_client_async]|Instantiate a client|Authorizing a `ContainerRegistryClient` object and `ContainerRepositoryClient` object| +|[sample_create_client.py][create_client] ([async version][create_client_async]) |Instantiate a client | Authorizing a `ContainerRegistryClient` object and `ContainerRepositoryClient` object | +|[sample_delete_old_tags.py][delete_old_tags] and [sample_delete_old_tags_async.py][delete_old_tags_async] | Delete tags from a repository | ### Prerequisites * Python 2.7, or 3.6 or later is required to use this package. @@ -49,4 +50,6 @@ Check out the [API reference documentation][rest_docs] to learn more about what [container_registry_docs]: https://docs.microsoft.com/azure/container-registry/container-registry-intro [create_client]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/containerregistry/azure-containerregistry/samples/sample_create_client.py -[create_client_async]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_create_client_async.py \ No newline at end of file +[create_client_async]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_create_client_async.py +[delete_old_tags]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/containerregistry/azure-containerregistry/samples/sample_delete_old_tags.py +[delete_old_tags_async]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_old_tags_async.py diff --git a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_create_client_async.py b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_create_client_async.py index 1e50f9f3dda8..21b26d35094b 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_create_client_async.py +++ b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_create_client_async.py @@ -66,6 +66,9 @@ async def basic_sample(self): async for tag in repository_client.list_tags(): print(tag.digest) + # [START delete_repository] + await client.delete_repository("hello-world") + # [END delete_repository] async def main(): sample = CreateClients() @@ -76,4 +79,4 @@ async def main(): if __name__ == "__main__": loop = asyncio.get_event_loop() - loop.run_until_complete(main()) \ No newline at end of file + loop.run_until_complete(main()) diff --git a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_old_tags_async.py b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_old_tags_async.py new file mode 100644 index 000000000000..4c6b789f4eec --- /dev/null +++ b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_old_tags_async.py @@ -0,0 +1,67 @@ +# coding: utf-8 + +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- + +""" +FILE: sample_delete_old_tags_async.py + +DESCRIPTION: + These samples demonstrates deleting the three oldest tags for each repository asynchronously. + +USAGE: + python sample_delete_old_tags_async.py + + Set the environment variables with your own values before running the sample: + 1) CONTAINERREGISTRY_ENDPOINT - The URL of you Container Registry account +""" + +import asyncio +from dotenv import find_dotenv, load_dotenv +import os + + +class DeleteOperations(object): + def __init__(self): + load_dotenv(find_dotenv()) + self.account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + + async def delete_old_tags(self): + from azure.containerregistry import TagOrderBy + from azure.containerregistry.aio import ( + ContainerRegistryClient, + ContainerRepositoryClient, + ) + from azure.identity.aio import DefaultAzureCredential + + # [START list_repositories] + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + credential = DefaultAzureCredential() + client = ContainerRegistryClient(account_url, credential) + + async for repository in client.list_repositories(): + repository_client = ContainerRepositoryClient(account_url, repository, credential) + # [END list_repositories] + + # [START list_tags] + tag_count = 0 + async for tag in repository_client.list_tags(order_by=TagOrderBy.LAST_UPDATE_TIME_DESCENDING): + tag_count += 1 + if tag_count > 3: + await repository_client.delete_tag(tag.name) + # [END list_tags] + + await client.close() + + +async def main(): + sample = DeleteOperations() + sample.delete_old_tags() + + +if __name__ == '__main__': + loop = asyncio.get_event_loop() + loop.run_until_complete(main()) diff --git a/sdk/containerregistry/azure-containerregistry/samples/sample_create_client.py b/sdk/containerregistry/azure-containerregistry/samples/sample_create_client.py index df9fa8066d4e..1ce7cf38438d 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/sample_create_client.py +++ b/sdk/containerregistry/azure-containerregistry/samples/sample_create_client.py @@ -65,6 +65,10 @@ def basic_sample(self): for tag in repository_client.list_tags(): print(tag.digest) + # [START delete_repository] + client.delete_repository("hello-world") + # [END delete_repository] + if __name__ == "__main__": sample = CreateClients() diff --git a/sdk/containerregistry/azure-containerregistry/samples/sample_delete_old_tags.py b/sdk/containerregistry/azure-containerregistry/samples/sample_delete_old_tags.py new file mode 100644 index 000000000000..521c25fcb4bd --- /dev/null +++ b/sdk/containerregistry/azure-containerregistry/samples/sample_delete_old_tags.py @@ -0,0 +1,56 @@ +# coding: utf-8 + +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- + +""" +FILE: sample_delete_old_tags.py + +DESCRIPTION: + These samples demonstrates deleting the three oldest tags for each repository + +USAGE: + python sample_delete_old_tags.py + + Set the environment variables with your own values before running the sample: + 1) CONTAINERREGISTRY_ENDPOINT - The URL of you Container Registry account +""" + +from dotenv import find_dotenv, load_dotenv +import os + + +class DeleteOperations(object): + def __init__(self): + load_dotenv(find_dotenv()) + self.account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + + def delete_old_tags(self): + from azure.containerregistry import ( + ContainerRegistryClient, + ContainerRepositoryClient, + TagOrderBy + ) + from azure.identity import DefaultAzureCredential + + # [START list_repositories] + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + credential = DefaultAzureCredential() + client = ContainerRegistryClient(account_url, credential) + + for repository in client.list_repositories(): + repository_client = ContainerRepositoryClient(account_url, repository, credential) + # [END list_repositories] + + # [START list_tags] + tag_count = 0 + for tag in repository_client.list_tags(order_by=TagOrderBy.LAST_UPDATE_TIME_DESCENDING): + tag_count += 1 + if tag_count > 3: + repository_client.delete_tag(tag.name) + # [END list_tags] + + client.close()