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 406f33d2db40..05ebbee4d5bd 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,14 +8,19 @@ 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_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 | +|[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. @@ -49,7 +52,13 @@ 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 +[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 +[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 new file mode 100644 index 000000000000..2a282c877d2e --- /dev/null +++ b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py @@ -0,0 +1,64 @@ +# 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()) + + async def delete_images(self): + # [START list_repository_names] + audience = "https://management.azure.com" + 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(): + print(repository) + # [END list_repository_names] + + # [START list_manifest_properties] + # Keep the three most recent images, delete everything else + manifest_count = 0 + async for manifest in client.list_manifest_properties(repository, order_by=ManifestOrder.LAST_UPDATE_TIME_DESCENDING): + manifest_count += 1 + if manifest_count > 3: + await client.delete_manifest(repository, manifest.digest) + # [END list_manifest_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_old_tags_async.py b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_tags_async.py similarity index 73% 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..f15a81381216 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,23 +23,21 @@ 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] + audience = "https://management.azure.com" account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] credential = DefaultAzureCredential() - client = ContainerRegistryClient(account_url, credential) + client = ContainerRegistryClient(account_url, credential, audience=audience) async with client: async for repository in client.list_repository_names(): @@ -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/async_samples/sample_create_client_async.py b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_hello_world_async.py similarity index 82% 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..4179a1a969dc 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,31 +23,27 @@ 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()) + audience = "https://management.azure.com" + client = ContainerRegistryClient(account_url, DefaultAzureCredential(), audience=audience) # [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 - client = ContainerRegistryClient(account_url, DefaultAzureCredential()) + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + audience = "https://management.azure.com" + client = ContainerRegistryClient(account_url, DefaultAzureCredential(), audience=audience) async with client: # Iterate through all the repositories async for repository_name in client.list_repository_names(): @@ -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/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..957810241f5d --- /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()) + + async def list_tags(self): + # Create a new ContainerRegistryClient + audience = "https://management.azure.com" + 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 + ": ") + 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/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..ec9854251081 --- /dev/null +++ b/sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_set_image_properties_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_set_image_properties_async.py + +DESCRIPTION: + 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 + + 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()) + + async def set_image_properties(self): + # Create a new ContainerRegistryClient + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + audience = "https://management.azure.com" + credential = DefaultAzureCredential() + client = ContainerRegistryClient(account_url, credential, audience=audience) + + # [START update_manifest_properties] + # Set permissions on the v1 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 new file mode 100644 index 000000000000..1836c7281bcd --- /dev/null +++ b/sdk/containerregistry/azure-containerregistry/samples/sample_delete_images.py @@ -0,0 +1,57 @@ +# 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()) + + def delete_images(self): + # [START list_repository_names] + audience = "https://management.azure.com" + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + credential = DefaultAzureCredential() + client = ContainerRegistryClient(account_url, credential, audience=audience) + + for repository in client.list_repository_names(): + print(repository) + # [END list_repository_names] + + # [START list_manifest_properties] + # Keep the three most recent images, delete everything else + manifest_count = 0 + for manifest in client.list_manifest_properties(repository, order_by=ManifestOrder.LAST_UPDATE_TIME_DESCENDING): + manifest_count += 1 + if manifest_count > 3: + client.delete_manifest(repository, manifest.digest) + # [END list_manifest_properties] + + client.close() + + +if __name__ == "__main__": + sample = DeleteImages() + sample.delete_images() 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 73% 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..6ac6d1c50a92 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,20 +22,20 @@ 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 - # [START list_repository_names] + def delete_tags(self): + # [START list_repository_names] + audience = "https://management.azure.com" account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] credential = DefaultAzureCredential() - client = ContainerRegistryClient(account_url, credential) + client = ContainerRegistryClient(account_url, credential, audience=audience) for repository in client.list_repository_names(): print(repository) @@ -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() diff --git a/sdk/containerregistry/azure-containerregistry/samples/sample_create_client.py b/sdk/containerregistry/azure-containerregistry/samples/sample_hello_world.py similarity index 83% rename from sdk/containerregistry/azure-containerregistry/samples/sample_create_client.py rename to sdk/containerregistry/azure-containerregistry/samples/sample_hello_world.py index 0576105b4e93..4edaf6acd16d 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,23 +33,16 @@ 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()) + audience = "https://management.azure.com" + client = ContainerRegistryClient(account_url, DefaultAzureCredential(), audience=audience) # [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 - client = ContainerRegistryClient(account_url, DefaultAzureCredential()) + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + audience = "https://management.azure.com" + 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 new file mode 100644 index 000000000000..fa6ddd3aa12f --- /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()) + + def list_tags(self): + # Create a new ContainerRegistryClient + audience = "https://management.azure.com" + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + credential = DefaultAzureCredential() + client = ContainerRegistryClient(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() 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..65254d0cef56 --- /dev/null +++ b/sdk/containerregistry/azure-containerregistry/samples/sample_set_image_properties.py @@ -0,0 +1,62 @@ +# 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 on the tag 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()) + + def set_image_properties(self): + # Create a new ContainerRegistryClient + account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] + audience = "https://management.azure.com" + credential = DefaultAzureCredential() + client = ContainerRegistryClient(account_url, credential, audience=audience) + + # [START update_manifest_properties] + # Set permissions on the v1 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()