Skip to content

Commit 49c6e26

Browse files
author
Yalin Li
authored
[ACR] Fix failing samples (Azure#28203)
1 parent 50181f4 commit 49c6e26

16 files changed

+313
-138
lines changed

sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,50 @@
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
20-
"""
2120
21+
This sample assumes your registry has at least four repositories.
22+
"""
2223
import asyncio
23-
from dotenv import find_dotenv, load_dotenv
2424
import os
25-
25+
from dotenv import find_dotenv, load_dotenv
2626
from azure.containerregistry import ArtifactManifestOrder
2727
from azure.containerregistry.aio import ContainerRegistryClient
28-
from azure.identity.aio import DefaultAzureCredential
28+
from samples.sample_utilities import load_registry, get_authority, get_audience, get_credential
2929

3030

3131
class DeleteImagesAsync(object):
3232
def __init__(self):
3333
load_dotenv(find_dotenv())
34+
self.endpoint = os.environ.get("CONTAINERREGISTRY_ENDPOINT")
35+
self.authority = get_authority(self.endpoint)
36+
self.audience = get_audience(self.authority)
37+
self.credential = get_credential(
38+
self.authority, exclude_environment_credential=True, is_async=True
39+
)
3440

3541
async def delete_images(self):
42+
load_registry()
3643
# Instantiate an instance of ContainerRegistryClient
37-
audience = "https://management.azure.com"
38-
endpoint = os.environ["CONTAINERREGISTRY_ENDPOINT"]
39-
credential = DefaultAzureCredential()
40-
41-
async with ContainerRegistryClient(endpoint, credential, audience=audience) as client:
44+
async with ContainerRegistryClient(self.endpoint, self.credential, audience=self.audience) as client:
4245
async for repository in client.list_repository_names():
4346
print(repository)
4447

4548
# Keep the three most recent images, delete everything else
4649
manifest_count = 0
47-
async for manifest in client.list_manifest_properties(repository, order_by=ArtifactManifestOrder.LAST_UPDATED_ON_DESCENDING):
50+
async for manifest in client.list_manifest_properties(
51+
repository, order_by=ArtifactManifestOrder.LAST_UPDATED_ON_DESCENDING
52+
):
4853
manifest_count += 1
4954
if manifest_count > 3:
55+
# Make sure will have the permission to delete the manifest later
56+
await client.update_manifest_properties(
57+
repository,
58+
manifest.digest,
59+
can_write=True,
60+
can_delete=True
61+
)
62+
63+
print(f"Deleting {repository}:{manifest.digest}")
5064
await client.delete_manifest(repository, manifest.digest)
5165

5266

sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_delete_tags_async.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,43 @@
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
20-
"""
2120
21+
This sample assumes your registry has at least one repository with more than three tags.
22+
"""
2223
import asyncio
23-
from dotenv import find_dotenv, load_dotenv
2424
import os
25-
25+
from dotenv import find_dotenv, load_dotenv
2626
from azure.containerregistry import ArtifactTagOrder
2727
from azure.containerregistry.aio import ContainerRegistryClient
28-
from azure.identity.aio import DefaultAzureCredential
28+
from samples.sample_utilities import load_registry, get_authority, get_audience, get_credential
2929

3030

3131
class DeleteTagsAsync(object):
3232
def __init__(self):
3333
load_dotenv(find_dotenv())
34+
self.endpoint = os.environ.get("CONTAINERREGISTRY_ENDPOINT")
35+
self.authority = get_authority(self.endpoint)
36+
self.audience = get_audience(self.authority)
37+
self.credential = get_credential(
38+
self.authority, exclude_environment_credential=True, is_async=True
39+
)
3440

3541
async def delete_tags(self):
36-
# [START list_repository_names]
37-
audience = "https://management.azure.com"
38-
endpoint = os.environ["CONTAINERREGISTRY_ENDPOINT"]
39-
credential = DefaultAzureCredential()
40-
41-
async with ContainerRegistryClient(endpoint, credential, audience=audience) as client:
42+
load_registry()
43+
async with ContainerRegistryClient(self.endpoint, self.credential, audience=self.audience) as client:
44+
# [START list_repository_names]
4245
async for repository in client.list_repository_names():
4346
print(repository)
4447
# [END list_repository_names]
4548

4649
# Keep the three most recent tags, delete everything else
4750
tag_count = 0
48-
async for tag in client.list_tag_properties(repository, order_by=ArtifactTagOrder.LAST_UPDATED_ON_DESCENDING):
51+
async for tag in client.list_tag_properties(
52+
repository, order_by=ArtifactTagOrder.LAST_UPDATED_ON_DESCENDING
53+
):
4954
tag_count += 1
50-
if tag_count > 3:
55+
if tag_count > 3:
56+
print(f"Deleting {repository}:{tag.name}")
5157
await client.delete_tag(repository, tag.name)
5258

5359

sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_hello_world_async.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,52 @@
1717
python sample_hello_world_async.py
1818
1919
Set the environment variables with your own values before running the sample:
20-
1) AZURE_CONTAINERREGISTRY_URL - The URL of you Container Registry account
21-
"""
20+
1) CONTAINERREGISTRY_ENDPOINT - The URL of you Container Registry account
2221
22+
This sample assumes your registry has a repository "library/hello-world".
23+
"""
2324
import asyncio
24-
from dotenv import find_dotenv, load_dotenv
2525
import os
26-
26+
from dotenv import find_dotenv, load_dotenv
2727
from azure.containerregistry.aio import ContainerRegistryClient
28-
from azure.identity.aio import DefaultAzureCredential
28+
from samples.sample_utilities import load_registry, get_authority, get_audience, get_credential
2929

3030

3131
class HelloWorldAsync(object):
3232
def __init__(self):
3333
load_dotenv(find_dotenv())
34+
self.endpoint = os.environ.get("CONTAINERREGISTRY_ENDPOINT")
35+
self.authority = get_authority(self.endpoint)
36+
self.audience = get_audience(self.authority)
37+
self.credential = get_credential(
38+
self.authority, exclude_environment_credential=True, is_async=True
39+
)
3440

3541
async def basic_sample(self):
42+
load_registry()
3643
# Instantiate an instance of ContainerRegistryClient
3744
# [START create_registry_client]
38-
endpoint = os.environ["CONTAINERREGISTRY_ENDPOINT"]
39-
audience = "https://management.azure.com"
40-
client = ContainerRegistryClient(endpoint, DefaultAzureCredential(), audience=audience)
45+
async with ContainerRegistryClient(self.endpoint, self.credential, audience=self.audience) as client:
4146
# [END create_registry_client]
42-
async with client:
4347
# Iterate through all the repositories
4448
async for repository_name in client.list_repository_names():
45-
if repository_name == "hello-world":
46-
# Create a repository client from the registry client
49+
print(repository_name)
50+
if repository_name == "library/hello-world":
51+
print("Tags of repository library/hello-world:")
4752
async for tag in client.list_tag_properties(repository_name):
48-
print(tag.digest)
53+
print(tag.name)
54+
55+
# Make sure will have the permission to delete the repository later
56+
await client.update_manifest_properties(
57+
repository_name,
58+
tag.name,
59+
can_write=True,
60+
can_delete=True
61+
)
4962

63+
print("Deleting " + repository_name)
5064
# [START delete_repository]
51-
await client.delete_repository(repository_name, "hello-world")
65+
await client.delete_repository(repository_name)
5266
# [END delete_repository]
5367

5468

sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_list_tags_async.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,34 @@
2020
Set the environment variables with your own values before running the sample:
2121
1) CONTAINERREGISTRY_ENDPOINT - The URL of you Container Registry account
2222
23-
This sample assumes the registry "myacr.azurecr.io" has a repository "hello-world".
23+
This sample assumes your registry has a repository "library/hello-world".
2424
"""
25-
2625
import asyncio
27-
from dotenv import find_dotenv, load_dotenv
2826
import os
29-
27+
from dotenv import find_dotenv, load_dotenv
3028
from azure.containerregistry.aio import ContainerRegistryClient
31-
from azure.identity.aio import DefaultAzureCredential
29+
from samples.sample_utilities import load_registry, get_authority, get_audience, get_credential
3230

3331

3432
class ListTagsAsync(object):
3533
def __init__(self):
3634
load_dotenv(find_dotenv())
35+
self.endpoint = os.environ.get("CONTAINERREGISTRY_ENDPOINT")
36+
self.authority = get_authority(self.endpoint)
37+
self.audience = get_audience(self.authority)
38+
self.credential = get_credential(
39+
self.authority, exclude_environment_credential=True, is_async=True
40+
)
3741

3842
async def list_tags(self):
43+
load_registry()
3944
# Instantiate an instance of ContainerRegistryClient
40-
audience = "https://management.azure.com"
41-
endpoint = os.environ["CONTAINERREGISTRY_ENDPOINT"]
42-
credential = DefaultAzureCredential()
43-
async with ContainerRegistryClient(endpoint, credential, audience=audience) as client:
45+
async with ContainerRegistryClient(self.endpoint, self.credential, audience=self.audience) as client:
4446
manifest = await client.get_manifest_properties("library/hello-world", "latest")
45-
print(manifest.repository_name + ": ")
47+
print("Tags of " + manifest.repository_name + ": ")
4648
# Iterate through all the tags
4749
for tag in manifest.tags:
48-
print(tag + "\n")
50+
print(tag)
4951

5052

5153
async def main():

sdk/containerregistry/azure-containerregistry/samples/async_samples/sample_set_image_properties_async.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,40 @@
1919
Set the environment variables with your own values before running the sample:
2020
1) CONTAINERREGISTRY_ENDPOINT - The URL of you Container Registry account
2121
22-
This sample assumes the registry "myacr.azurecr.io" has a repository "hello-world" with image tagged "v1".
22+
This sample assumes your registry has a repository "library/hello-world" with image tagged "v1".
2323
"""
24-
2524
import asyncio
26-
from dotenv import find_dotenv, load_dotenv
2725
import os
28-
26+
from dotenv import find_dotenv, load_dotenv
2927
from azure.containerregistry.aio import ContainerRegistryClient
30-
from azure.identity.aio import DefaultAzureCredential
28+
from samples.sample_utilities import load_registry, get_authority, get_audience, get_credential
3129

3230

3331
class SetImagePropertiesAsync(object):
3432
def __init__(self):
3533
load_dotenv(find_dotenv())
34+
self.endpoint = os.environ.get("CONTAINERREGISTRY_ENDPOINT")
35+
self.authority = get_authority(self.endpoint)
36+
self.audience = get_audience(self.authority)
37+
self.credential = get_credential(
38+
self.authority, exclude_environment_credential=True, is_async=True
39+
)
3640

3741
async def set_image_properties(self):
42+
load_registry()
3843
# Instantiate an instance of ContainerRegistryClient
39-
endpoint = os.environ["CONTAINERREGISTRY_ENDPOINT"]
40-
audience = "https://management.azure.com"
41-
credential = DefaultAzureCredential()
42-
async with ContainerRegistryClient(endpoint, credential, audience=audience) as client:
44+
async with ContainerRegistryClient(self.endpoint, self.credential, audience=self.audience) as client:
4345
# Set permissions on the v1 image's "latest" tag
4446
await client.update_manifest_properties(
4547
"library/hello-world",
46-
"latest",
48+
"v1",
4749
can_write=False,
4850
can_delete=False
4951
)
50-
# After this update, if someone were to push an update to "myacr.azurecr.io\hello-world:v1", it would fail.
51-
# It's worth noting that if this image also had another tag, such as "latest", and that tag did not have
52-
# permissions set to prevent reads or deletes, the image could still be overwritten. For example,
53-
# if someone were to push an update to "myacr.azurecr.io\hello-world:latest"
52+
# After this update, if someone were to push an update to `<registry endpoint>\library\hello-world:v1`,
53+
# it would fail. It's worth noting that if this image also had another tag, such as `latest`,
54+
# and that tag did not have permissions set to prevent reads or deletes, the image could still be
55+
# overwritten. For example, if someone were to push an update to `<registry endpoint>\hello-world:latest`
5456
# (which references the same image), it would succeed.
5557

5658

sdk/containerregistry/azure-containerregistry/samples/sample_delete_images.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,46 @@
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
20-
"""
2120
22-
from dotenv import find_dotenv, load_dotenv
21+
This sample assumes your registry has at least four repositories.
22+
"""
2323
import os
24-
24+
from dotenv import find_dotenv, load_dotenv
2525
from azure.containerregistry import ContainerRegistryClient, ArtifactManifestOrder
26-
from azure.identity import DefaultAzureCredential
26+
from sample_utilities import load_registry, get_authority, get_audience, get_credential
27+
2728

2829
class DeleteImages(object):
2930
def __init__(self):
3031
load_dotenv(find_dotenv())
32+
self.endpoint = os.environ.get("CONTAINERREGISTRY_ENDPOINT")
33+
self.authority = get_authority(self.endpoint)
34+
self.audience = get_audience(self.authority)
35+
self.credential = get_credential(
36+
self.authority, exclude_environment_credential=True
37+
)
3138

3239
def delete_images(self):
40+
load_registry()
3341
# Instantiate an instance of ContainerRegistryClient
34-
audience = "https://management.azure.com"
35-
endpoint = os.environ["CONTAINERREGISTRY_ENDPOINT"]
36-
37-
with ContainerRegistryClient(endpoint, DefaultAzureCredential(), audience=audience) as client:
42+
with ContainerRegistryClient(self.endpoint, self.credential, audience=self.audience) as client:
3843
for repository in client.list_repository_names():
3944
print(repository)
4045

4146
# Keep the three most recent images, delete everything else
4247
manifest_count = 0
4348
for manifest in client.list_manifest_properties(
44-
repository, order_by=ArtifactManifestOrder.LAST_UPDATED_ON_DESCENDING):
49+
repository, order_by=ArtifactManifestOrder.LAST_UPDATED_ON_DESCENDING
50+
):
4551
manifest_count += 1
4652
if manifest_count > 3:
53+
# Make sure will have the permission to delete the manifest later
54+
client.update_manifest_properties(
55+
repository,
56+
manifest.digest,
57+
can_write=True,
58+
can_delete=True
59+
)
4760
print(f"Deleting {repository}:{manifest.digest}")
4861
client.delete_manifest(repository, manifest.digest)
4962

sdk/containerregistry/azure-containerregistry/samples/sample_delete_tags.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,38 @@
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
20-
"""
2120
22-
from dotenv import find_dotenv, load_dotenv
21+
This sample assumes your registry has at least one repository with more than three tags.
22+
"""
2323
import os
24-
24+
from dotenv import find_dotenv, load_dotenv
2525
from azure.containerregistry import ContainerRegistryClient, ArtifactTagOrder
26-
from azure.identity import DefaultAzureCredential
26+
from sample_utilities import load_registry, get_authority, get_audience, get_credential
2727

2828

2929
class DeleteTags(object):
3030
def __init__(self):
3131
load_dotenv(find_dotenv())
32+
self.endpoint = os.environ.get("CONTAINERREGISTRY_ENDPOINT")
33+
self.authority = get_authority(self.endpoint)
34+
self.audience = get_audience(self.authority)
35+
self.credential = get_credential(
36+
self.authority, exclude_environment_credential=True
37+
)
3238

3339
def delete_tags(self):
34-
# [START list_repository_names]
35-
audience = "https://management.azure.com"
36-
endpoint = os.environ["CONTAINERREGISTRY_ENDPOINT"]
37-
38-
with ContainerRegistryClient(endpoint, DefaultAzureCredential(), audience=audience) as client:
40+
load_registry()
41+
with ContainerRegistryClient(self.endpoint, self.credential, audience=self.audience) as client:
42+
# [START list_repository_names]
3943
for repository in client.list_repository_names():
4044
print(repository)
4145
# [END list_repository_names]
4246

4347
# Keep the three most recent tags, delete everything else
4448
tag_count = 0
45-
for tag in client.list_tag_properties(repository, order_by=ArtifactTagOrder.LAST_UPDATED_ON_DESCENDING):
49+
for tag in client.list_tag_properties(
50+
repository, order_by=ArtifactTagOrder.LAST_UPDATED_ON_DESCENDING
51+
):
4652
tag_count += 1
4753
if tag_count > 3:
4854
print(f"Deleting {repository}:{tag.name}")

0 commit comments

Comments
 (0)