-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f151c8
commit a6e0d12
Showing
12 changed files
with
429 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...ainerregistry/azure-containerregistry/samples/async_samples/sample_delete_images_async.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...containerregistry/azure-containerregistry/samples/async_samples/sample_list_tags_async.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
67 changes: 67 additions & 0 deletions
67
...gistry/azure-containerregistry/samples/async_samples/sample_set_image_properties_async.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
Oops, something went wrong.