Skip to content

Commit a6e0d12

Browse files
author
Yalin Li
authored
Update samples in ACR (#20902)
1 parent 7f151c8 commit a6e0d12

12 files changed

+429
-68
lines changed

sdk/containerregistry/azure-containerregistry/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ The [Azure Identity library][identity] provides easy Azure Active Directory supp
4242
from azure.containerregistry import ContainerRegistryClient
4343
from azure.identity import DefaultAzureCredential
4444

45-
account_url = "https://MYCONTAINERREGISTRY.azurecr.io"
46-
client = ContainerRegistryClient(account_url, DefaultAzureCredential())
45+
account_url = "https://mycontainerregistry.azurecr.io"
46+
audience = "https://management.azure.com"
47+
client = ContainerRegistryClient(account_url, DefaultAzureCredential(), audience=audience)
4748
```
4849

4950
## Key concepts

sdk/containerregistry/azure-containerregistry/samples/README.md

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# Samples for Azure Container Registry
2-
31
---
42
page_type: sample
53
languages:
@@ -10,14 +8,19 @@ products:
108
urlFragment: containerregistry-samples
119
---
1210

11+
# Samples for Azure Container Registry
12+
1313
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).
1414
The async versions of the samples require Python 3.6 or later.
1515

1616

1717
|**File Name**|**Description**|
1818
|-------------|---------------|
19-
|[sample_create_client.py][create_client] ([async version][create_client_async]) |Instantiate a client | Authorizing a `ContainerRegistryClient` object and `ContainerRepositoryClient` object |
20-
|[sample_delete_old_tags.py][delete_old_tags] and [sample_delete_old_tags_async.py][delete_old_tags_async] | Delete tags from a repository |
19+
|[sample_hello_world.py][hello_world] ([sample_hello_world_async.py][hello_world_async]) |Instantiate a `ContainerRegistryClient` object and `ContainerRepositoryClient` object |
20+
|[sample_delete_tags.py][delete_tags] and [sample_delete_tags_async.py][delete_tags_async] | Delete tags from a repository |
21+
|[sample_delete_images.py][delete_images] and [sample_delete_images_async.py][delete_images_async] | Delete images from a repository |
22+
|[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 |
23+
|[sample_list_tags.py][list_tags] and [sample_list_tags_async.py][list_tags_async] | List tags on an image using an anonymous access |
2124

2225
### Prerequisites
2326
* 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
4952

5053
[container_registry_docs]: https://docs.microsoft.com/azure/container-registry/container-registry-intro
5154

52-
[create_client]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/sample_create_client.py
53-
[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
54-
[delete_old_tags]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/sample_delete_old_tags.py
55-
[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
55+
[hello_world]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/sample_hello_world.py
56+
[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
57+
[delete_tags]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/sample_delete_tags.py
58+
[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
59+
[delete_images]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/sample_delete_images.py
60+
[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
61+
[set_image_properties]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/sample_set_image_properties.py
62+
[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
63+
[list_tags]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/containerregistry/azure-containerregistry/samples/sample_list_tags.py
64+
[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
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# coding: utf-8
2+
3+
# -------------------------------------------------------------------------
4+
# Copyright (c) Microsoft Corporation. All rights reserved.
5+
# Licensed under the MIT License. See License.txt in the project root for
6+
# license information.
7+
# --------------------------------------------------------------------------
8+
9+
"""
10+
FILE: sample_delete_images_async.py
11+
12+
DESCRIPTION:
13+
This sample demonstrates deleting all but the most recent three images for each repository.
14+
15+
USAGE:
16+
python sample_delete_images_async.py
17+
18+
Set the environment variables with your own values before running the sample:
19+
1) CONTAINERREGISTRY_ENDPOINT - The URL of you Container Registry account
20+
"""
21+
22+
import asyncio
23+
from dotenv import find_dotenv, load_dotenv
24+
import os
25+
26+
from azure.containerregistry import ManifestOrder
27+
from azure.containerregistry.aio import ContainerRegistryClient
28+
from azure.identity.aio import DefaultAzureCredential
29+
30+
31+
class DeleteImagesAsync(object):
32+
def __init__(self):
33+
load_dotenv(find_dotenv())
34+
35+
async def delete_images(self):
36+
# [START list_repository_names]
37+
audience = "https://management.azure.com"
38+
account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"]
39+
credential = DefaultAzureCredential()
40+
client = ContainerRegistryClient(account_url, credential, audience=audience)
41+
42+
async with client:
43+
async for repository in client.list_repository_names():
44+
print(repository)
45+
# [END list_repository_names]
46+
47+
# [START list_manifest_properties]
48+
# Keep the three most recent images, delete everything else
49+
manifest_count = 0
50+
async for manifest in client.list_manifest_properties(repository, order_by=ManifestOrder.LAST_UPDATE_TIME_DESCENDING):
51+
manifest_count += 1
52+
if manifest_count > 3:
53+
await client.delete_manifest(repository, manifest.digest)
54+
# [END list_manifest_properties]
55+
56+
57+
async def main():
58+
sample = DeleteImagesAsync()
59+
await sample.delete_images()
60+
61+
62+
if __name__ == "__main__":
63+
loop = asyncio.get_event_loop()
64+
loop.run_until_complete(main())
Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
# --------------------------------------------------------------------------
88

99
"""
10-
FILE: sample_delete_old_tags_async.py
10+
FILE: sample_delete_tags_async.py
1111
1212
DESCRIPTION:
13-
These samples demonstrates deleting the three oldest tags for each repository asynchronously.
13+
This sample demonstrates deleting all but the most recent three tags for each repository.
1414
1515
USAGE:
16-
python sample_delete_old_tags_async.py
16+
python sample_delete_tags_async.py
1717
1818
Set the environment variables with your own values before running the sample:
1919
1) CONTAINERREGISTRY_ENDPOINT - The URL of you Container Registry account
@@ -23,23 +23,21 @@
2323
from dotenv import find_dotenv, load_dotenv
2424
import os
2525

26+
from azure.containerregistry import TagOrder
27+
from azure.containerregistry.aio import ContainerRegistryClient
28+
from azure.identity.aio import DefaultAzureCredential
2629

27-
class DeleteOperations(object):
30+
31+
class DeleteTagsAsync(object):
2832
def __init__(self):
2933
load_dotenv(find_dotenv())
30-
self.account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"]
31-
32-
async def delete_old_tags(self):
33-
from azure.containerregistry import TagOrder
34-
from azure.containerregistry.aio import (
35-
ContainerRegistryClient,
36-
)
37-
from azure.identity.aio import DefaultAzureCredential
3834

35+
async def delete_tags(self):
3936
# [START list_repository_names]
37+
audience = "https://management.azure.com"
4038
account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"]
4139
credential = DefaultAzureCredential()
42-
client = ContainerRegistryClient(account_url, credential)
40+
client = ContainerRegistryClient(account_url, credential, audience=audience)
4341

4442
async with client:
4543
async for repository in client.list_repository_names():
@@ -57,8 +55,8 @@ async def delete_old_tags(self):
5755

5856

5957
async def main():
60-
sample = DeleteOperations()
61-
await sample.delete_old_tags()
58+
sample = DeleteTagsAsync()
59+
await sample.delete_tags()
6260

6361

6462
if __name__ == "__main__":
Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
# --------------------------------------------------------------------------
88

99
"""
10-
FILE: sample_create_client_async.py
10+
FILE: sample_hello_world_async.py
1111
1212
DESCRIPTION:
1313
These samples demonstrate creating a ContainerRegistryClient and a ContainerRepository
1414
1515
USAGE:
16-
python sample_create_client_async.py
16+
python sample_hello_world_async.py
1717
1818
Set the environment variables with your own values before running the sample:
1919
1) AZURE_CONTAINERREGISTRY_URL - The URL of you Container Registry account
@@ -23,31 +23,27 @@
2323
from dotenv import find_dotenv, load_dotenv
2424
import os
2525

26+
from azure.containerregistry.aio import ContainerRegistryClient
27+
from azure.identity.aio import DefaultAzureCredential
2628

27-
class CreateClients(object):
29+
30+
class CreateClientsAsync(object):
2831
def __init__(self):
2932
load_dotenv(find_dotenv())
3033

3134
async def create_registry_client(self):
3235
# Instantiate the ContainerRegistryClient
3336
# [START create_registry_client]
34-
from azure.containerregistry.aio import ContainerRegistryClient
35-
from azure.identity.aio import DefaultAzureCredential
36-
3737
account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"]
38-
39-
client = ContainerRegistryClient(account_url, DefaultAzureCredential())
38+
audience = "https://management.azure.com"
39+
client = ContainerRegistryClient(account_url, DefaultAzureCredential(), audience=audience)
4040
# [END create_registry_client]
4141

4242
async def basic_sample(self):
43-
44-
from azure.containerregistry.aio import ContainerRegistryClient
45-
from azure.identity.aio import DefaultAzureCredential
46-
47-
account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"]
48-
4943
# Instantiate the client
50-
client = ContainerRegistryClient(account_url, DefaultAzureCredential())
44+
account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"]
45+
audience = "https://management.azure.com"
46+
client = ContainerRegistryClient(account_url, DefaultAzureCredential(), audience=audience)
5147
async with client:
5248
# Iterate through all the repositories
5349
async for repository_name in client.list_repository_names():
@@ -62,7 +58,7 @@ async def basic_sample(self):
6258

6359

6460
async def main():
65-
sample = CreateClients()
61+
sample = CreateClientsAsync()
6662
await sample.create_registry_client()
6763
await sample.basic_sample()
6864

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# coding: utf-8
2+
3+
# -------------------------------------------------------------------------
4+
# Copyright (c) Microsoft Corporation. All rights reserved.
5+
# Licensed under the MIT License. See License.txt in the project root for
6+
# license information.
7+
# --------------------------------------------------------------------------
8+
9+
"""
10+
FILE: sample_list_tags_async.py
11+
12+
DESCRIPTION:
13+
This sample demonstrates listing the tags for an image in a repository with anonymous pull access.
14+
Anonymous access allows a user to list all the collections there, but they wouldn't have permissions to
15+
modify or delete any of the images in the registry.
16+
17+
USAGE:
18+
python sample_list_tags_async.py
19+
20+
Set the environment variables with your own values before running the sample:
21+
1) CONTAINERREGISTRY_ENDPOINT - The URL of you Container Registry account
22+
23+
This sample assumes the registry "myacr.azurecr.io" has a repository "hello-world".
24+
"""
25+
26+
import asyncio
27+
from dotenv import find_dotenv, load_dotenv
28+
import os
29+
30+
from azure.containerregistry.aio import ContainerRegistryClient
31+
from azure.identity.aio import DefaultAzureCredential
32+
33+
34+
class ListTagsAsync(object):
35+
def __init__(self):
36+
load_dotenv(find_dotenv())
37+
38+
async def list_tags(self):
39+
# Create a new ContainerRegistryClient
40+
audience = "https://management.azure.com"
41+
account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"]
42+
credential = DefaultAzureCredential()
43+
client = ContainerRegistryClient(account_url, credential, audience=audience)
44+
45+
manifest = await client.get_manifest_properties("library/hello-world", "latest")
46+
print(manifest.repository_name + ": ")
47+
for tag in manifest.tags:
48+
print(tag + "\n")
49+
50+
51+
async def main():
52+
sample = ListTagsAsync()
53+
await sample.list_tags()
54+
55+
56+
if __name__ == "__main__":
57+
loop = asyncio.get_event_loop()
58+
loop.run_until_complete(main())
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# coding: utf-8
2+
3+
# -------------------------------------------------------------------------
4+
# Copyright (c) Microsoft Corporation. All rights reserved.
5+
# Licensed under the MIT License. See License.txt in the project root for
6+
# license information.
7+
# --------------------------------------------------------------------------
8+
9+
"""
10+
FILE: sample_set_image_properties_async.py
11+
12+
DESCRIPTION:
13+
This sample demonstrates setting an image's properties on the tag so it can't be overwritten during a lengthy
14+
deployment.
15+
16+
USAGE:
17+
python sample_set_image_properties_async.py
18+
19+
Set the environment variables with your own values before running the sample:
20+
1) CONTAINERREGISTRY_ENDPOINT - The URL of you Container Registry account
21+
22+
This sample assumes the registry "myacr.azurecr.io" has a repository "hello-world" with image tagged "v1".
23+
"""
24+
25+
import asyncio
26+
from dotenv import find_dotenv, load_dotenv
27+
import os
28+
29+
from azure.containerregistry.aio import ContainerRegistryClient
30+
from azure.identity.aio import DefaultAzureCredential
31+
32+
33+
class SetImagePropertiesAsync(object):
34+
def __init__(self):
35+
load_dotenv(find_dotenv())
36+
37+
async def set_image_properties(self):
38+
# Create a new ContainerRegistryClient
39+
account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"]
40+
audience = "https://management.azure.com"
41+
credential = DefaultAzureCredential()
42+
client = ContainerRegistryClient(account_url, credential, audience=audience)
43+
44+
# [START update_manifest_properties]
45+
# Set permissions on the v1 image's "latest" tag
46+
await client.update_manifest_properties(
47+
"library/hello-world",
48+
"latest",
49+
can_write=False,
50+
can_delete=False
51+
)
52+
# [END update_manifest_properties]
53+
# After this update, if someone were to push an update to "myacr.azurecr.io\hello-world:v1", it would fail.
54+
# It's worth noting that if this image also had another tag, such as "latest", and that tag did not have
55+
# permissions set to prevent reads or deletes, the image could still be overwritten. For example,
56+
# if someone were to push an update to "myacr.azurecr.io\hello-world:latest"
57+
# (which references the same image), it would succeed.
58+
59+
60+
async def main():
61+
sample = SetImagePropertiesAsync()
62+
await sample.set_image_properties()
63+
64+
65+
if __name__ == "__main__":
66+
loop = asyncio.get_event_loop()
67+
loop.run_until_complete(main())

0 commit comments

Comments
 (0)