From 38cf1fb67154d96a495565655a902a5d89a957ea Mon Sep 17 00:00:00 2001 From: YalinLi0312 Date: Tue, 21 Sep 2021 11:54:26 -0700 Subject: [PATCH 1/8] Rename sample_create_client.py to sample_hello_world.py --- .../azure-containerregistry/samples/README.md | 6 +++--- ...nt_async.py => sample_hello_world_async.py} | 18 +++++++----------- ..._create_client.py => sample_hello_world.py} | 14 +++++--------- 3 files changed, 15 insertions(+), 23 deletions(-) rename sdk/containerregistry/azure-containerregistry/samples/async_samples/{sample_create_client_async.py => sample_hello_world_async.py} (83%) rename sdk/containerregistry/azure-containerregistry/samples/{sample_create_client.py => sample_hello_world.py} (85%) diff --git a/sdk/containerregistry/azure-containerregistry/samples/README.md b/sdk/containerregistry/azure-containerregistry/samples/README.md index 406f33d2db40..dbf94a39dfec 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/README.md +++ b/sdk/containerregistry/azure-containerregistry/samples/README.md @@ -1,5 +1,3 @@ -# Samples for Azure Container Registry - --- page_type: sample languages: @@ -10,13 +8,15 @@ products: urlFragment: containerregistry-samples --- +# Samples for Azure Container Registry + These code samples show common scenario operations with the Azure Container Registry client library. The code samples assume an environment variable `CONTAINERREGISTRY_ENDPOINT` is set, which includes the name of the login server and the `https://` prefix. For more information on using AAD with Azure Container Registry, please see the service's [Authentication Overview](https://docs.microsoft.com/azure/container-registry/container-registry-authentication). The async versions of the samples require Python 3.6 or later. |**File Name**|**Description**| |-------------|---------------| -|[sample_create_client.py][create_client] ([async version][create_client_async]) |Instantiate a client | Authorizing a `ContainerRegistryClient` object and `ContainerRepositoryClient` object | +|[sample_hello_world.py][create_client] ([sample_hello_world_async.py][create_client_async]) |Instantiate 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 diff --git a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_create_client_async.py b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_hello_world_async.py similarity index 83% rename from sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_create_client_async.py rename to sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_hello_world_async.py index 2c11c19bac60..9eeeead2ee79 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_create_client_async.py +++ b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_hello_world_async.py @@ -7,13 +7,13 @@ # -------------------------------------------------------------------------- """ -FILE: sample_create_client_async.py +FILE: sample_hello_world_async.py DESCRIPTION: These samples demonstrate creating a ContainerRegistryClient and a ContainerRepository USAGE: - python sample_create_client_async.py + python sample_hello_world_async.py Set the environment variables with your own values before running the sample: 1) AZURE_CONTAINERREGISTRY_URL - The URL of you Container Registry account @@ -23,27 +23,23 @@ from dotenv import find_dotenv, load_dotenv import os +from azure.containerregistry.aio import ContainerRegistryClient +from azure.identity.aio import DefaultAzureCredential -class CreateClients(object): + +class CreateClientsAsync(object): def __init__(self): load_dotenv(find_dotenv()) async def create_registry_client(self): # Instantiate the ContainerRegistryClient # [START create_registry_client] - from azure.containerregistry.aio import ContainerRegistryClient - from azure.identity.aio import DefaultAzureCredential - account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] client = ContainerRegistryClient(account_url, DefaultAzureCredential()) # [END create_registry_client] async def basic_sample(self): - - from azure.containerregistry.aio import ContainerRegistryClient - from azure.identity.aio import DefaultAzureCredential - account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] # Instantiate the client @@ -62,7 +58,7 @@ async def basic_sample(self): async def main(): - sample = CreateClients() + sample = CreateClientsAsync() await sample.create_registry_client() await sample.basic_sample() diff --git a/sdk/containerregistry/azure-containerregistry/samples/sample_create_client.py b/sdk/containerregistry/azure-containerregistry/samples/sample_hello_world.py similarity index 85% rename from sdk/containerregistry/azure-containerregistry/samples/sample_create_client.py rename to sdk/containerregistry/azure-containerregistry/samples/sample_hello_world.py index 0576105b4e93..8c67123109cb 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/sample_create_client.py +++ b/sdk/containerregistry/azure-containerregistry/samples/sample_hello_world.py @@ -7,13 +7,13 @@ # -------------------------------------------------------------------------- """ -FILE: sample_create_client.py +FILE: sample_hello_world.py DESCRIPTION: These samples demonstrate creating a ContainerRegistryClient and a ContainerRepository USAGE: - python sample_create_client.py + python sample_hello_world.py Set the environment variables with your own values before running the sample: 1) CONTAINERREGISTRY_ENDPOINT - The URL of you Container Registry account @@ -22,6 +22,9 @@ from dotenv import find_dotenv, load_dotenv import os +from azure.containerregistry import ContainerRegistryClient +from azure.identity import DefaultAzureCredential + class CreateClients(object): def __init__(self): @@ -30,19 +33,12 @@ def __init__(self): def create_registry_client(self): # Instantiate the ContainerRegistryClient # [START create_registry_client] - from azure.containerregistry import ContainerRegistryClient - from azure.identity import DefaultAzureCredential - account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] client = ContainerRegistryClient(account_url, DefaultAzureCredential()) # [END create_registry_client] def basic_sample(self): - - from azure.containerregistry import ContainerRegistryClient - from azure.identity import DefaultAzureCredential - account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] # Instantiate the client From 3f7b4220d3f8f0248bbae96c324e36f928f848ce Mon Sep 17 00:00:00 2001 From: YalinLi0312 Date: Mon, 27 Sep 2021 14:10:40 -0700 Subject: [PATCH 2/8] Rename sample_delete_old_tags.py to sample_delete_tags.py --- .../azure-containerregistry/samples/README.md | 12 +++++----- ...s_async.py => sample_delete_tags_async.py} | 24 +++++++++---------- ...lete_old_tags.py => sample_delete_tags.py} | 20 ++++++++-------- 3 files changed, 27 insertions(+), 29 deletions(-) rename sdk/containerregistry/azure-containerregistry/samples/async_samples/{sample_delete_old_tags_async.py => sample_delete_tags_async.py} (76%) rename sdk/containerregistry/azure-containerregistry/samples/{sample_delete_old_tags.py => sample_delete_tags.py} (78%) diff --git a/sdk/containerregistry/azure-containerregistry/samples/README.md b/sdk/containerregistry/azure-containerregistry/samples/README.md index dbf94a39dfec..ea5a66bb3216 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/README.md +++ b/sdk/containerregistry/azure-containerregistry/samples/README.md @@ -16,8 +16,8 @@ The async versions of the samples require Python 3.6 or later. |**File Name**|**Description**| |-------------|---------------| -|[sample_hello_world.py][create_client] ([sample_hello_world_async.py][create_client_async]) |Instantiate 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 | +|[sample_hello_world.py][hello_world] ([sample_hello_world_async.py][hello_world_async]) |Instantiate a `ContainerRegistryClient` object and `ContainerRepositoryClient` object | +|[sample_delete_tags.py][delete_tags] and [sample_delete_tags_async.py][delete_tags_async] | Delete tags from a repository | ### Prerequisites * Python 2.7, or 3.6 or later is required to use this package. @@ -49,7 +49,7 @@ 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/main/sdk/containerregistry/azure-containerregistry/samples/sample_create_client.py -[create_client_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_create_client_async.py -[delete_old_tags]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/sample_delete_old_tags.py -[delete_old_tags_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_old_tags_async.py +[hello_world]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/sample_hello_world.py +[hello_world_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_hello_world_async.py +[delete_tags]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/sample_delete_tags.py +[delete_tags_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_tags_async.py 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_tags_async.py similarity index 76% rename from sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_old_tags_async.py rename to sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_tags_async.py index 9d6461250415..4030ec021222 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_old_tags_async.py +++ b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_tags_async.py @@ -7,13 +7,13 @@ # -------------------------------------------------------------------------- """ -FILE: sample_delete_old_tags_async.py +FILE: sample_delete_tags_async.py DESCRIPTION: - These samples demonstrates deleting the three oldest tags for each repository asynchronously. + This sample demonstrates deleting all but the most recent three tags for each repository. USAGE: - python sample_delete_old_tags_async.py + python sample_delete_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 @@ -23,19 +23,17 @@ from dotenv import find_dotenv, load_dotenv import os +from azure.containerregistry import TagOrder +from azure.containerregistry.aio import ContainerRegistryClient +from azure.identity.aio import DefaultAzureCredential -class DeleteOperations(object): + +class DeleteTagsAsync(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 TagOrder - from azure.containerregistry.aio import ( - ContainerRegistryClient, - ) - from azure.identity.aio import DefaultAzureCredential - + async def delete_tags(self): # [START list_repository_names] account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] credential = DefaultAzureCredential() @@ -57,8 +55,8 @@ async def delete_old_tags(self): async def main(): - sample = DeleteOperations() - await sample.delete_old_tags() + sample = DeleteTagsAsync() + await sample.delete_tags() if __name__ == "__main__": diff --git a/sdk/containerregistry/azure-containerregistry/samples/sample_delete_old_tags.py b/sdk/containerregistry/azure-containerregistry/samples/sample_delete_tags.py similarity index 78% rename from sdk/containerregistry/azure-containerregistry/samples/sample_delete_old_tags.py rename to sdk/containerregistry/azure-containerregistry/samples/sample_delete_tags.py index d309cc0a935d..7cb8d750ac0f 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/sample_delete_old_tags.py +++ b/sdk/containerregistry/azure-containerregistry/samples/sample_delete_tags.py @@ -7,13 +7,13 @@ # -------------------------------------------------------------------------- """ -FILE: sample_delete_old_tags.py +FILE: sample_delete_tags.py DESCRIPTION: - These samples demonstrates deleting the three oldest tags for each repository + This sample demonstrates deleting all but the most recent three tags for each repository. USAGE: - python sample_delete_old_tags.py + python sample_delete_tags.py Set the environment variables with your own values before running the sample: 1) CONTAINERREGISTRY_ENDPOINT - The URL of you Container Registry account @@ -22,16 +22,16 @@ from dotenv import find_dotenv, load_dotenv import os +from azure.containerregistry import ContainerRegistryClient, TagOrder +from azure.identity import DefaultAzureCredential -class DeleteOperations(object): + +class DeleteTags(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, TagOrder - from azure.identity import DefaultAzureCredential - + def delete_tags(self): # [START list_repository_names] account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] credential = DefaultAzureCredential() @@ -54,5 +54,5 @@ def delete_old_tags(self): if __name__ == "__main__": - sample = DeleteOperations() - sample.delete_old_tags() \ No newline at end of file + sample = DeleteTags() + sample.delete_tags() From 13652c6a49049604a41900b02fafd87b60a83e4b Mon Sep 17 00:00:00 2001 From: YalinLi0312 Date: Mon, 27 Sep 2021 14:28:56 -0700 Subject: [PATCH 3/8] Add delete images sample --- .../sample_delete_images_async.py | 63 +++++++++++++++++++ .../async_samples/sample_delete_tags_async.py | 3 +- .../async_samples/sample_hello_world_async.py | 9 +-- .../samples/sample_delete_images.py | 56 +++++++++++++++++ .../samples/sample_delete_tags.py | 3 +- 5 files changed, 124 insertions(+), 10 deletions(-) create mode 100644 sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py create mode 100644 sdk/containerregistry/azure-containerregistry/samples/sample_delete_images.py diff --git a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py new file mode 100644 index 000000000000..7149eab184d2 --- /dev/null +++ b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py @@ -0,0 +1,63 @@ +# 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_images_async.py + +DESCRIPTION: + This sample demonstrates deleting all but the most recent three images for each repository. + +USAGE: + python sample_delete_images_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 + +from azure.containerregistry import ManifestOrder +from azure.containerregistry.aio import ContainerRegistryClient +from azure.identity.aio import DefaultAzureCredential + + +class DeleteImagesAsync(object): + def __init__(self): + load_dotenv(find_dotenv()) + self.account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + + async def delete_images(self): + # [START list_repository_names] + credential = DefaultAzureCredential() + client = ContainerRegistryClient(self.account_url, credential) + + async with client: + async for repository in client.list_repository_names(): + print(repository) + # [END list_repository_names] + + # [START list_tag_properties] + # Keep the three most recent images, delete everything else + tag_count = 0 + async for tag in client.list_tag_properties(repository, order_by=ManifestOrder.LAST_UPDATE_TIME_DESCENDING): + tag_count += 1 + if tag_count > 3: + await client.delete_manifest(repository, tag.name) + # [END list_tag_properties] + + +async def main(): + sample = DeleteImagesAsync() + await sample.delete_images() + + +if __name__ == "__main__": + loop = asyncio.get_event_loop() + loop.run_until_complete(main()) diff --git a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_tags_async.py b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_tags_async.py index 4030ec021222..b18d61fdf9ad 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_tags_async.py +++ b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_tags_async.py @@ -35,9 +35,8 @@ def __init__(self): async def delete_tags(self): # [START list_repository_names] - account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] credential = DefaultAzureCredential() - client = ContainerRegistryClient(account_url, credential) + client = ContainerRegistryClient(self.account_url, credential) async with client: async for repository in client.list_repository_names(): diff --git a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_hello_world_async.py b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_hello_world_async.py index 9eeeead2ee79..edeff9ae625f 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_hello_world_async.py +++ b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_hello_world_async.py @@ -30,20 +30,17 @@ class CreateClientsAsync(object): def __init__(self): load_dotenv(find_dotenv()) + self.account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] async def create_registry_client(self): # Instantiate the ContainerRegistryClient # [START create_registry_client] - account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] - - client = ContainerRegistryClient(account_url, DefaultAzureCredential()) + client = ContainerRegistryClient(self.account_url, DefaultAzureCredential()) # [END create_registry_client] async def basic_sample(self): - account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] - # Instantiate the client - client = ContainerRegistryClient(account_url, DefaultAzureCredential()) + client = ContainerRegistryClient(self.account_url, DefaultAzureCredential()) async with client: # Iterate through all the repositories async for repository_name in client.list_repository_names(): diff --git a/sdk/containerregistry/azure-containerregistry/samples/sample_delete_images.py b/sdk/containerregistry/azure-containerregistry/samples/sample_delete_images.py new file mode 100644 index 000000000000..29290284c55b --- /dev/null +++ b/sdk/containerregistry/azure-containerregistry/samples/sample_delete_images.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_images.py + +DESCRIPTION: + This sample demonstrates deleting all but the most recent three images for each repository. + +USAGE: + python sample_delete_images.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 + +from azure.containerregistry import ContainerRegistryClient, ManifestOrder +from azure.identity import DefaultAzureCredential + +class DeleteImages(object): + def __init__(self): + load_dotenv(find_dotenv()) + self.account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + + def delete_images(self): + # [START list_repository_names] + credential = DefaultAzureCredential() + client = ContainerRegistryClient(self.account_url, credential) + + for repository in client.list_repository_names(): + print(repository) + # [END list_repository_names] + + # [START list_tag_properties] + # Keep the three most recent images, delete everything else + tag_count = 0 + for tag in client.list_tag_properties(repository, order_by=ManifestOrder.LAST_UPDATE_TIME_DESCENDING): + tag_count += 1 + if tag_count > 3: + client.delete_manifest(repository, tag.name) + # [END list_tag_properties] + + client.close() + + +if __name__ == "__main__": + sample = DeleteImages() + sample.delete_images() diff --git a/sdk/containerregistry/azure-containerregistry/samples/sample_delete_tags.py b/sdk/containerregistry/azure-containerregistry/samples/sample_delete_tags.py index 7cb8d750ac0f..d0d7638328ec 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/sample_delete_tags.py +++ b/sdk/containerregistry/azure-containerregistry/samples/sample_delete_tags.py @@ -33,9 +33,8 @@ def __init__(self): def delete_tags(self): # [START list_repository_names] - account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] credential = DefaultAzureCredential() - client = ContainerRegistryClient(account_url, credential) + client = ContainerRegistryClient(self.account_url, credential) for repository in client.list_repository_names(): print(repository) From f1a0cee7496499436a4f72caa440905b67184d17 Mon Sep 17 00:00:00 2001 From: YalinLi0312 Date: Mon, 27 Sep 2021 16:04:48 -0700 Subject: [PATCH 4/8] Add set image properties sample --- .../azure-containerregistry/README.md | 5 +- .../azure-containerregistry/samples/README.md | 6 ++ .../sample_delete_images_async.py | 3 +- .../async_samples/sample_delete_tags_async.py | 3 +- .../async_samples/sample_hello_world_async.py | 6 +- .../sample_set_image_properties_async.py | 66 +++++++++++++++++++ .../samples/sample_delete_images.py | 3 +- .../samples/sample_delete_tags.py | 3 +- .../samples/sample_hello_world.py | 11 ++-- .../samples/sample_set_image_properties.py | 61 +++++++++++++++++ 10 files changed, 153 insertions(+), 14 deletions(-) create mode 100644 sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_set_image_properties_async.py create mode 100644 sdk/containerregistry/azure-containerregistry/samples/sample_set_image_properties.py diff --git a/sdk/containerregistry/azure-containerregistry/README.md b/sdk/containerregistry/azure-containerregistry/README.md index e697ed14ecc8..b9914e753d98 100644 --- a/sdk/containerregistry/azure-containerregistry/README.md +++ b/sdk/containerregistry/azure-containerregistry/README.md @@ -42,8 +42,9 @@ The [Azure Identity library][identity] provides easy Azure Active Directory supp from azure.containerregistry import ContainerRegistryClient from azure.identity import DefaultAzureCredential -account_url = "https://MYCONTAINERREGISTRY.azurecr.io" -client = ContainerRegistryClient(account_url, DefaultAzureCredential()) +account_url = "https://mycontainerregistry.azurecr.io" +audience = "https://management.azure.com" +client = ContainerRegistryClient(account_url, DefaultAzureCredential(), audience=audience) ``` ## Key concepts diff --git a/sdk/containerregistry/azure-containerregistry/samples/README.md b/sdk/containerregistry/azure-containerregistry/samples/README.md index ea5a66bb3216..b78001b14dcc 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/README.md +++ b/sdk/containerregistry/azure-containerregistry/samples/README.md @@ -18,6 +18,8 @@ The async versions of the samples require Python 3.6 or later. |-------------|---------------| |[sample_hello_world.py][hello_world] ([sample_hello_world_async.py][hello_world_async]) |Instantiate a `ContainerRegistryClient` object and `ContainerRepositoryClient` object | |[sample_delete_tags.py][delete_tags] and [sample_delete_tags_async.py][delete_tags_async] | Delete tags from a repository | +|[sample_delete_images.py][delete_images] and [sample_delete_images_async.py][delete_images_async] | Delete images from a repository | +|[sample_set_image_properties.py][set_image_properties] and [sample_set_image_properties_async.py][set_image_properties_async] | Set read/write/delete properties on an image | ### Prerequisites * Python 2.7, or 3.6 or later is required to use this package. @@ -53,3 +55,7 @@ Check out the [API reference documentation][rest_docs] to learn more about what [hello_world_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_hello_world_async.py [delete_tags]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/sample_delete_tags.py [delete_tags_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_tags_async.py +[delete_images]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/sample_delete_images.py +[delete_images_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py +[set_image_properties]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/sample_set_image_properties.py +[set_image_properties_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_set_image_properties_async.py diff --git a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py index 7149eab184d2..d22247384035 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py +++ b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py @@ -36,7 +36,8 @@ def __init__(self): async def delete_images(self): # [START list_repository_names] credential = DefaultAzureCredential() - client = ContainerRegistryClient(self.account_url, credential) + audience = "https://management.azure.com" + client = ContainerRegistryClient(self.account_url, credential, audience=audience) async with client: async for repository in client.list_repository_names(): diff --git a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_tags_async.py b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_tags_async.py index b18d61fdf9ad..e033d1e734a2 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_tags_async.py +++ b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_tags_async.py @@ -36,7 +36,8 @@ def __init__(self): async def delete_tags(self): # [START list_repository_names] credential = DefaultAzureCredential() - client = ContainerRegistryClient(self.account_url, credential) + audience = "https://management.azure.com" + client = ContainerRegistryClient(self.account_url, credential, audience=audience) async with client: async for repository in client.list_repository_names(): diff --git a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_hello_world_async.py b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_hello_world_async.py index edeff9ae625f..6681d048bdf7 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_hello_world_async.py +++ b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_hello_world_async.py @@ -35,12 +35,14 @@ def __init__(self): async def create_registry_client(self): # Instantiate the ContainerRegistryClient # [START create_registry_client] - client = ContainerRegistryClient(self.account_url, DefaultAzureCredential()) + audience = "https://management.azure.com" + client = ContainerRegistryClient(self.account_url, DefaultAzureCredential(), audience=audience) # [END create_registry_client] async def basic_sample(self): # Instantiate the client - client = ContainerRegistryClient(self.account_url, DefaultAzureCredential()) + audience = "https://management.azure.com" + client = ContainerRegistryClient(self.account_url, DefaultAzureCredential(), audience=audience) async with client: # Iterate through all the repositories async for repository_name in client.list_repository_names(): diff --git a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_set_image_properties_async.py b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_set_image_properties_async.py new file mode 100644 index 000000000000..3028225d7053 --- /dev/null +++ b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_set_image_properties_async.py @@ -0,0 +1,66 @@ +# 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_set_image_properties_async.py + +DESCRIPTION: + This sample demonstrates setting an image's properties so it can't be overwritten during a lengthy deployment. + +USAGE: + python sample_set_image_properties_async.py + + Set the environment variables with your own values before running the sample: + 1) CONTAINERREGISTRY_ENDPOINT - The URL of you Container Registry account + + This sample assumes the registry "myacr.azurecr.io" has a repository "hello-world" with image tagged "v1". +""" + +import asyncio +from dotenv import find_dotenv, load_dotenv +import os + +from azure.containerregistry.aio import ContainerRegistryClient +from azure.identity.aio import DefaultAzureCredential + + +class SetImagePropertiesAsync(object): + def __init__(self): + load_dotenv(find_dotenv()) + self.account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + + async def set_image_properties(self): + # Create a new ContainerRegistryClient + credential = DefaultAzureCredential() + audience = "https://management.azure.com" + client = ContainerRegistryClient(self.account_url, credential, audience=audience) + + # [START update_manifest_properties] + # Set permissions on the image's "latest" tag + await client.update_manifest_properties( + "library/hello-world", + "latest", + can_write=False, + can_delete=False + ) + # [END update_manifest_properties] + # After this update, if someone were to push an update to "myacr.azurecr.io\hello-world:v1", it would fail. + # It's worth noting that if this image also had another tag, such as "latest", and that tag did not have + # permissions set to prevent reads or deletes, the image could still be overwritten. For example, + # if someone were to push an update to "myacr.azurecr.io\hello-world:latest" + # (which references the same image), it would succeed. + + +async def main(): + sample = SetImagePropertiesAsync() + await sample.set_image_properties() + + +if __name__ == "__main__": + loop = asyncio.get_event_loop() + loop.run_until_complete(main()) diff --git a/sdk/containerregistry/azure-containerregistry/samples/sample_delete_images.py b/sdk/containerregistry/azure-containerregistry/samples/sample_delete_images.py index 29290284c55b..fc4046fb46dd 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/sample_delete_images.py +++ b/sdk/containerregistry/azure-containerregistry/samples/sample_delete_images.py @@ -33,7 +33,8 @@ def __init__(self): def delete_images(self): # [START list_repository_names] credential = DefaultAzureCredential() - client = ContainerRegistryClient(self.account_url, credential) + audience = "https://management.azure.com" + client = ContainerRegistryClient(self.account_url, credential, audience=audience) for repository in client.list_repository_names(): print(repository) diff --git a/sdk/containerregistry/azure-containerregistry/samples/sample_delete_tags.py b/sdk/containerregistry/azure-containerregistry/samples/sample_delete_tags.py index d0d7638328ec..230e57eccfa5 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/sample_delete_tags.py +++ b/sdk/containerregistry/azure-containerregistry/samples/sample_delete_tags.py @@ -34,7 +34,8 @@ def __init__(self): def delete_tags(self): # [START list_repository_names] credential = DefaultAzureCredential() - client = ContainerRegistryClient(self.account_url, credential) + audience = "https://management.azure.com" + client = ContainerRegistryClient(self.account_url, credential, audience=audience) for repository in client.list_repository_names(): print(repository) diff --git a/sdk/containerregistry/azure-containerregistry/samples/sample_hello_world.py b/sdk/containerregistry/azure-containerregistry/samples/sample_hello_world.py index 8c67123109cb..78a749ace9d1 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/sample_hello_world.py +++ b/sdk/containerregistry/azure-containerregistry/samples/sample_hello_world.py @@ -29,20 +29,19 @@ class CreateClients(object): def __init__(self): load_dotenv(find_dotenv()) + self.account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] def create_registry_client(self): # Instantiate the ContainerRegistryClient # [START create_registry_client] - account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] - - client = ContainerRegistryClient(account_url, DefaultAzureCredential()) + audience = "https://management.azure.com" + client = ContainerRegistryClient(self.account_url, DefaultAzureCredential(), audience=audience) # [END create_registry_client] def basic_sample(self): - account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] - # Instantiate the client - client = ContainerRegistryClient(account_url, DefaultAzureCredential()) + audience = "https://management.azure.com" + client = ContainerRegistryClient(self.account_url, DefaultAzureCredential(), audience=audience) with client: # Iterate through all the repositories for repository_name in client.list_repository_names(): diff --git a/sdk/containerregistry/azure-containerregistry/samples/sample_set_image_properties.py b/sdk/containerregistry/azure-containerregistry/samples/sample_set_image_properties.py new file mode 100644 index 000000000000..bdd222a5aad6 --- /dev/null +++ b/sdk/containerregistry/azure-containerregistry/samples/sample_set_image_properties.py @@ -0,0 +1,61 @@ +# 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_set_image_properties.py + +DESCRIPTION: + This sample demonstrates setting an image's properties so it can't be overwritten during a lengthy deployment. + +USAGE: + python sample_set_image_properties.py + + Set the environment variables with your own values before running the sample: + 1) CONTAINERREGISTRY_ENDPOINT - The URL of you Container Registry account + + This sample assumes the registry "myacr.azurecr.io" has a repository "hello-world" with image tagged "v1". +""" + +from dotenv import find_dotenv, load_dotenv +import os + +from azure.containerregistry import ContainerRegistryClient +from azure.identity import DefaultAzureCredential + +class SetImageProperties(object): + def __init__(self): + load_dotenv(find_dotenv()) + self.account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + + def set_image_properties(self): + # Create a new ContainerRegistryClient + credential = DefaultAzureCredential() + audience = "https://myacr.azurecr.io" + client = ContainerRegistryClient(self.account_url, credential, audience=audience) + + # [START update_manifest_properties] + # Set permissions on the image's "latest" tag + client.update_manifest_properties( + "library/hello-world", + "latest", + can_write=False, + can_delete=False + ) + # [END update_manifest_properties] + # After this update, if someone were to push an update to `myacr.azurecr.io\hello-world:v1`, it would fail. + # It's worth noting that if this image also had another tag, such as `latest`, and that tag did not have + # permissions set to prevent reads or deletes, the image could still be overwritten. For example, + # if someone were to push an update to `\hello-world:latest` + # (which references the same image), it would succeed. + + client.close() + + +if __name__ == "__main__": + sample = SetImageProperties() + sample.set_image_properties() From dc75e6af0c9675ac6f7edb0cf568391e4cd59dd2 Mon Sep 17 00:00:00 2001 From: YalinLi0312 Date: Mon, 27 Sep 2021 17:40:23 -0700 Subject: [PATCH 5/8] Add list tags sample --- .../azure-containerregistry/samples/README.md | 3 + .../sample_delete_images_async.py | 8 +-- .../async_samples/sample_list_tags_async.py | 58 +++++++++++++++++++ .../samples/sample_delete_images.py | 8 +-- .../samples/sample_list_tags.py | 53 +++++++++++++++++ 5 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_list_tags_async.py create mode 100644 sdk/containerregistry/azure-containerregistry/samples/sample_list_tags.py diff --git a/sdk/containerregistry/azure-containerregistry/samples/README.md b/sdk/containerregistry/azure-containerregistry/samples/README.md index b78001b14dcc..05ebbee4d5bd 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/README.md +++ b/sdk/containerregistry/azure-containerregistry/samples/README.md @@ -20,6 +20,7 @@ The async versions of the samples require Python 3.6 or later. |[sample_delete_tags.py][delete_tags] and [sample_delete_tags_async.py][delete_tags_async] | Delete tags from a repository | |[sample_delete_images.py][delete_images] and [sample_delete_images_async.py][delete_images_async] | Delete images from a repository | |[sample_set_image_properties.py][set_image_properties] and [sample_set_image_properties_async.py][set_image_properties_async] | Set read/write/delete properties on an image | +|[sample_list_tags.py][list_tags] and [sample_list_tags_async.py][list_tags_async] | List tags on an image using an anonymous access | ### Prerequisites * Python 2.7, or 3.6 or later is required to use this package. @@ -59,3 +60,5 @@ Check out the [API reference documentation][rest_docs] to learn more about what [delete_images_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py [set_image_properties]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/sample_set_image_properties.py [set_image_properties_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_set_image_properties_async.py +[list_tags]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/sample_list_tags.py +[list_tags_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_list_tags_async.py diff --git a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py index d22247384035..9cc9dbf80ba3 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py +++ b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py @@ -44,14 +44,14 @@ async def delete_images(self): print(repository) # [END list_repository_names] - # [START list_tag_properties] + # [START list_manifest_properties] # Keep the three most recent images, delete everything else tag_count = 0 - async for tag in client.list_tag_properties(repository, order_by=ManifestOrder.LAST_UPDATE_TIME_DESCENDING): + async for manifest in client.list_manifest_properties(repository, order_by=ManifestOrder.LAST_UPDATE_TIME_DESCENDING): tag_count += 1 if tag_count > 3: - await client.delete_manifest(repository, tag.name) - # [END list_tag_properties] + await client.delete_manifest(repository, manifest.digest) + # [END list_manifest_properties] async def main(): diff --git a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_list_tags_async.py b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_list_tags_async.py new file mode 100644 index 000000000000..ecd4042ad785 --- /dev/null +++ b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_list_tags_async.py @@ -0,0 +1,58 @@ +# 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_list_tags_async.py + +DESCRIPTION: + This sample demonstrates listing the tags for an image in a repository with anonymous pull access. + Anonymous access allows a user to list all the collections there, but they wouldn't have permissions to + modify or delete any of the images in the registry. + +USAGE: + python sample_list_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 + + This sample assumes the registry "myacr.azurecr.io" has a repository "hello-world". +""" + +import asyncio +from dotenv import find_dotenv, load_dotenv +import os + +from azure.containerregistry.aio import ContainerRegistryClient +from azure.identity.aio import DefaultAzureCredential + + +class ListTagsAsync(object): + def __init__(self): + load_dotenv(find_dotenv()) + self.account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + + async def list_tags(self): + # Create a new ContainerRegistryClient + credential = DefaultAzureCredential() + audience = "https://management.azure.com" + client = ContainerRegistryClient(self.account_url, credential, audience=audience) + + manifest = await client.get_manifest_properties("library/hello-world", "latest") + print(manifest.repository_name + ":") + for tag in manifest.tags: + print(tag + "\n") + + +async def main(): + sample = ListTagsAsync() + await sample.list_tags() + + +if __name__ == "__main__": + loop = asyncio.get_event_loop() + loop.run_until_complete(main()) diff --git a/sdk/containerregistry/azure-containerregistry/samples/sample_delete_images.py b/sdk/containerregistry/azure-containerregistry/samples/sample_delete_images.py index fc4046fb46dd..0fceb7410af9 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/sample_delete_images.py +++ b/sdk/containerregistry/azure-containerregistry/samples/sample_delete_images.py @@ -40,14 +40,14 @@ def delete_images(self): print(repository) # [END list_repository_names] - # [START list_tag_properties] + # [START list_manifest_properties] # Keep the three most recent images, delete everything else tag_count = 0 - for tag in client.list_tag_properties(repository, order_by=ManifestOrder.LAST_UPDATE_TIME_DESCENDING): + for manifest in client.list_manifest_properties(repository, order_by=ManifestOrder.LAST_UPDATE_TIME_DESCENDING): tag_count += 1 if tag_count > 3: - client.delete_manifest(repository, tag.name) - # [END list_tag_properties] + client.delete_manifest(repository, manifest.digest) + # [END list_manifest_properties] client.close() diff --git a/sdk/containerregistry/azure-containerregistry/samples/sample_list_tags.py b/sdk/containerregistry/azure-containerregistry/samples/sample_list_tags.py new file mode 100644 index 000000000000..ebb083f9fce1 --- /dev/null +++ b/sdk/containerregistry/azure-containerregistry/samples/sample_list_tags.py @@ -0,0 +1,53 @@ +# 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_list_tags.py + +DESCRIPTION: + This sample demonstrates listing the tags for an image in a repository with anonymous pull access. + Anonymous access allows a user to list all the collections there, but they wouldn't have permissions to + modify or delete any of the images in the registry. + +USAGE: + python sample_list_tags.py + + Set the environment variables with your own values before running the sample: + 1) CONTAINERREGISTRY_ENDPOINT - The URL of you Container Registry account + + This sample assumes the registry "myacr.azurecr.io" has a repository "hello-world". +""" + +from dotenv import find_dotenv, load_dotenv +import os + +from azure.containerregistry import ContainerRegistryClient +from azure.identity import DefaultAzureCredential + +class ListTags(object): + def __init__(self): + load_dotenv(find_dotenv()) + self.account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + + def list_tags(self): + # Create a new ContainerRegistryClient + credential = DefaultAzureCredential() + audience = "https://myacr.azurecr.io" + client = ContainerRegistryClient(self.account_url, credential, audience=audience) + + manifest = client.get_manifest_properties("library/hello-world", "latest") + print(manifest.repository_name + ":") + for tag in manifest.tags: + print(tag + "\n") + + client.close() + + +if __name__ == "__main__": + sample = ListTags() + sample.list_tags() From 2a10885c63695137da0431f63db43f7cd165a75b Mon Sep 17 00:00:00 2001 From: YalinLi0312 Date: Mon, 27 Sep 2021 17:48:03 -0700 Subject: [PATCH 6/8] Add an empty space after ':' in output --- .../samples/async_samples/sample_list_tags_async.py | 2 +- .../azure-containerregistry/samples/sample_list_tags.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_list_tags_async.py b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_list_tags_async.py index ecd4042ad785..cb3778192914 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_list_tags_async.py +++ b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_list_tags_async.py @@ -43,7 +43,7 @@ async def list_tags(self): client = ContainerRegistryClient(self.account_url, credential, audience=audience) manifest = await client.get_manifest_properties("library/hello-world", "latest") - print(manifest.repository_name + ":") + print(manifest.repository_name + ": ") for tag in manifest.tags: print(tag + "\n") diff --git a/sdk/containerregistry/azure-containerregistry/samples/sample_list_tags.py b/sdk/containerregistry/azure-containerregistry/samples/sample_list_tags.py index ebb083f9fce1..08924e63f67e 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/sample_list_tags.py +++ b/sdk/containerregistry/azure-containerregistry/samples/sample_list_tags.py @@ -41,7 +41,7 @@ def list_tags(self): client = ContainerRegistryClient(self.account_url, credential, audience=audience) manifest = client.get_manifest_properties("library/hello-world", "latest") - print(manifest.repository_name + ":") + print(manifest.repository_name + ": ") for tag in manifest.tags: print(tag + "\n") From 52563071f6323def86ca717947c5a41184be6634 Mon Sep 17 00:00:00 2001 From: YalinLi0312 Date: Tue, 28 Sep 2021 16:08:44 -0700 Subject: [PATCH 7/8] Address comments --- .../async_samples/sample_delete_images_async.py | 8 ++++---- .../samples/async_samples/sample_delete_tags_async.py | 6 +++--- .../samples/async_samples/sample_hello_world_async.py | 7 ++++--- .../samples/async_samples/sample_list_tags_async.py | 8 ++++---- .../sample_set_image_properties_async.py | 11 ++++++----- .../samples/sample_delete_images.py | 6 +++--- .../samples/sample_delete_tags.py | 8 ++++---- .../samples/sample_hello_world.py | 7 ++++--- .../samples/sample_list_tags.py | 8 ++++---- .../samples/sample_set_image_properties.py | 11 ++++++----- 10 files changed, 42 insertions(+), 38 deletions(-) diff --git a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py index 9cc9dbf80ba3..f433c1b19e7f 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py +++ b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py @@ -31,13 +31,13 @@ class DeleteImagesAsync(object): def __init__(self): load_dotenv(find_dotenv()) - self.account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] async def delete_images(self): - # [START list_repository_names] - credential = DefaultAzureCredential() + # [START list_repository_names] audience = "https://management.azure.com" - client = ContainerRegistryClient(self.account_url, credential, audience=audience) + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + credential = DefaultAzureCredential() + client = ContainerRegistryClient(account_url, credential, audience=audience) async with client: async for repository in client.list_repository_names(): diff --git a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_tags_async.py b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_tags_async.py index e033d1e734a2..f15a81381216 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_tags_async.py +++ b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_tags_async.py @@ -31,13 +31,13 @@ class DeleteTagsAsync(object): def __init__(self): load_dotenv(find_dotenv()) - self.account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] async def delete_tags(self): # [START list_repository_names] - credential = DefaultAzureCredential() audience = "https://management.azure.com" - client = ContainerRegistryClient(self.account_url, credential, audience=audience) + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + credential = DefaultAzureCredential() + client = ContainerRegistryClient(account_url, credential, audience=audience) async with client: async for repository in client.list_repository_names(): diff --git a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_hello_world_async.py b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_hello_world_async.py index 6681d048bdf7..4179a1a969dc 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_hello_world_async.py +++ b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_hello_world_async.py @@ -30,19 +30,20 @@ class CreateClientsAsync(object): def __init__(self): load_dotenv(find_dotenv()) - self.account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] async def create_registry_client(self): # Instantiate the ContainerRegistryClient # [START create_registry_client] + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] audience = "https://management.azure.com" - client = ContainerRegistryClient(self.account_url, DefaultAzureCredential(), audience=audience) + client = ContainerRegistryClient(account_url, DefaultAzureCredential(), audience=audience) # [END create_registry_client] async def basic_sample(self): # Instantiate the client + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] audience = "https://management.azure.com" - client = ContainerRegistryClient(self.account_url, DefaultAzureCredential(), audience=audience) + client = ContainerRegistryClient(account_url, DefaultAzureCredential(), audience=audience) async with client: # Iterate through all the repositories async for repository_name in client.list_repository_names(): diff --git a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_list_tags_async.py b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_list_tags_async.py index cb3778192914..957810241f5d 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_list_tags_async.py +++ b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_list_tags_async.py @@ -34,13 +34,13 @@ class ListTagsAsync(object): def __init__(self): load_dotenv(find_dotenv()) - self.account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] async def list_tags(self): - # Create a new ContainerRegistryClient - credential = DefaultAzureCredential() + # Create a new ContainerRegistryClient audience = "https://management.azure.com" - client = ContainerRegistryClient(self.account_url, credential, audience=audience) + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + credential = DefaultAzureCredential() + client = ContainerRegistryClient(account_url, credential, audience=audience) manifest = await client.get_manifest_properties("library/hello-world", "latest") print(manifest.repository_name + ": ") diff --git a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_set_image_properties_async.py b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_set_image_properties_async.py index 3028225d7053..ec9854251081 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_set_image_properties_async.py +++ b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_set_image_properties_async.py @@ -10,7 +10,8 @@ FILE: sample_set_image_properties_async.py DESCRIPTION: - This sample demonstrates setting an image's properties so it can't be overwritten during a lengthy deployment. + This sample demonstrates setting an image's properties on the tag so it can't be overwritten during a lengthy + deployment. USAGE: python sample_set_image_properties_async.py @@ -32,16 +33,16 @@ class SetImagePropertiesAsync(object): def __init__(self): load_dotenv(find_dotenv()) - self.account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] async def set_image_properties(self): # Create a new ContainerRegistryClient - credential = DefaultAzureCredential() + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] audience = "https://management.azure.com" - client = ContainerRegistryClient(self.account_url, credential, audience=audience) + credential = DefaultAzureCredential() + client = ContainerRegistryClient(account_url, credential, audience=audience) # [START update_manifest_properties] - # Set permissions on the image's "latest" tag + # Set permissions on the v1 image's "latest" tag await client.update_manifest_properties( "library/hello-world", "latest", diff --git a/sdk/containerregistry/azure-containerregistry/samples/sample_delete_images.py b/sdk/containerregistry/azure-containerregistry/samples/sample_delete_images.py index 0fceb7410af9..4cc634b50c37 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/sample_delete_images.py +++ b/sdk/containerregistry/azure-containerregistry/samples/sample_delete_images.py @@ -28,13 +28,13 @@ class DeleteImages(object): def __init__(self): load_dotenv(find_dotenv()) - self.account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] def delete_images(self): # [START list_repository_names] - credential = DefaultAzureCredential() audience = "https://management.azure.com" - client = ContainerRegistryClient(self.account_url, credential, audience=audience) + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + credential = DefaultAzureCredential() + client = ContainerRegistryClient(account_url, credential, audience=audience) for repository in client.list_repository_names(): print(repository) diff --git a/sdk/containerregistry/azure-containerregistry/samples/sample_delete_tags.py b/sdk/containerregistry/azure-containerregistry/samples/sample_delete_tags.py index 230e57eccfa5..6ac6d1c50a92 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/sample_delete_tags.py +++ b/sdk/containerregistry/azure-containerregistry/samples/sample_delete_tags.py @@ -29,13 +29,13 @@ class DeleteTags(object): def __init__(self): load_dotenv(find_dotenv()) - self.account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] def delete_tags(self): - # [START list_repository_names] - credential = DefaultAzureCredential() + # [START list_repository_names] audience = "https://management.azure.com" - client = ContainerRegistryClient(self.account_url, credential, audience=audience) + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + credential = DefaultAzureCredential() + client = ContainerRegistryClient(account_url, credential, audience=audience) for repository in client.list_repository_names(): print(repository) diff --git a/sdk/containerregistry/azure-containerregistry/samples/sample_hello_world.py b/sdk/containerregistry/azure-containerregistry/samples/sample_hello_world.py index 78a749ace9d1..4edaf6acd16d 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/sample_hello_world.py +++ b/sdk/containerregistry/azure-containerregistry/samples/sample_hello_world.py @@ -29,19 +29,20 @@ class CreateClients(object): def __init__(self): load_dotenv(find_dotenv()) - self.account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] def create_registry_client(self): # Instantiate the ContainerRegistryClient # [START create_registry_client] + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] audience = "https://management.azure.com" - client = ContainerRegistryClient(self.account_url, DefaultAzureCredential(), audience=audience) + client = ContainerRegistryClient(account_url, DefaultAzureCredential(), audience=audience) # [END create_registry_client] def basic_sample(self): # Instantiate the client + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] audience = "https://management.azure.com" - client = ContainerRegistryClient(self.account_url, DefaultAzureCredential(), audience=audience) + client = ContainerRegistryClient(account_url, DefaultAzureCredential(), audience=audience) with client: # Iterate through all the repositories for repository_name in client.list_repository_names(): diff --git a/sdk/containerregistry/azure-containerregistry/samples/sample_list_tags.py b/sdk/containerregistry/azure-containerregistry/samples/sample_list_tags.py index 08924e63f67e..fa6ddd3aa12f 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/sample_list_tags.py +++ b/sdk/containerregistry/azure-containerregistry/samples/sample_list_tags.py @@ -32,13 +32,13 @@ class ListTags(object): def __init__(self): load_dotenv(find_dotenv()) - self.account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] def list_tags(self): - # Create a new ContainerRegistryClient + # Create a new ContainerRegistryClient + audience = "https://management.azure.com" + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] credential = DefaultAzureCredential() - audience = "https://myacr.azurecr.io" - client = ContainerRegistryClient(self.account_url, credential, audience=audience) + client = ContainerRegistryClient(account_url, credential, audience=audience) manifest = client.get_manifest_properties("library/hello-world", "latest") print(manifest.repository_name + ": ") diff --git a/sdk/containerregistry/azure-containerregistry/samples/sample_set_image_properties.py b/sdk/containerregistry/azure-containerregistry/samples/sample_set_image_properties.py index bdd222a5aad6..65254d0cef56 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/sample_set_image_properties.py +++ b/sdk/containerregistry/azure-containerregistry/samples/sample_set_image_properties.py @@ -10,7 +10,8 @@ FILE: sample_set_image_properties.py DESCRIPTION: - This sample demonstrates setting an image's properties so it can't be overwritten during a lengthy deployment. + This sample demonstrates setting an image's properties on the tag so it can't be overwritten during a lengthy + deployment. USAGE: python sample_set_image_properties.py @@ -30,16 +31,16 @@ class SetImageProperties(object): def __init__(self): load_dotenv(find_dotenv()) - self.account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] def set_image_properties(self): # Create a new ContainerRegistryClient + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + audience = "https://management.azure.com" credential = DefaultAzureCredential() - audience = "https://myacr.azurecr.io" - client = ContainerRegistryClient(self.account_url, credential, audience=audience) + client = ContainerRegistryClient(account_url, credential, audience=audience) # [START update_manifest_properties] - # Set permissions on the image's "latest" tag + # Set permissions on the v1 image's "latest" tag client.update_manifest_properties( "library/hello-world", "latest", From 8f4e3fad128858c7cad8e6e586959c661f2367be Mon Sep 17 00:00:00 2001 From: YalinLi0312 Date: Wed, 29 Sep 2021 15:28:47 -0700 Subject: [PATCH 8/8] Address comments --- .../samples/async_samples/sample_delete_images_async.py | 6 +++--- .../azure-containerregistry/samples/sample_delete_images.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py index f433c1b19e7f..2a282c877d2e 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py +++ b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py @@ -46,10 +46,10 @@ async def delete_images(self): # [START list_manifest_properties] # Keep the three most recent images, delete everything else - tag_count = 0 + manifest_count = 0 async for manifest in client.list_manifest_properties(repository, order_by=ManifestOrder.LAST_UPDATE_TIME_DESCENDING): - tag_count += 1 - if tag_count > 3: + manifest_count += 1 + if manifest_count > 3: await client.delete_manifest(repository, manifest.digest) # [END list_manifest_properties] diff --git a/sdk/containerregistry/azure-containerregistry/samples/sample_delete_images.py b/sdk/containerregistry/azure-containerregistry/samples/sample_delete_images.py index 4cc634b50c37..1836c7281bcd 100644 --- a/sdk/containerregistry/azure-containerregistry/samples/sample_delete_images.py +++ b/sdk/containerregistry/azure-containerregistry/samples/sample_delete_images.py @@ -42,10 +42,10 @@ def delete_images(self): # [START list_manifest_properties] # Keep the three most recent images, delete everything else - tag_count = 0 + manifest_count = 0 for manifest in client.list_manifest_properties(repository, order_by=ManifestOrder.LAST_UPDATE_TIME_DESCENDING): - tag_count += 1 - if tag_count > 3: + manifest_count += 1 + if manifest_count > 3: client.delete_manifest(repository, manifest.digest) # [END list_manifest_properties]