Skip to content

Commit

Permalink
Update samples in ACR (#20902)
Browse files Browse the repository at this point in the history
  • Loading branch information
YalinLi0312 authored Sep 29, 2021
1 parent 7f151c8 commit a6e0d12
Show file tree
Hide file tree
Showing 12 changed files with 429 additions and 68 deletions.
5 changes: 3 additions & 2 deletions sdk/containerregistry/azure-containerregistry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 17 additions & 8 deletions sdk/containerregistry/azure-containerregistry/samples/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Samples for Azure Container Registry

---
page_type: sample
languages:
Expand All @@ -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.
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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())
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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():
Expand All @@ -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__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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():
Expand All @@ -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()

Expand Down
Original file line number Diff line number Diff line change
@@ -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())
Original file line number Diff line number Diff line change
@@ -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())
Loading

0 comments on commit a6e0d12

Please sign in to comment.