-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update samples in ACR #20902
Merged
Merged
Update samples in ACR #20902
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
38cf1fb
Rename sample_create_client.py to sample_hello_world.py
YalinLi0312 3f7b422
Rename sample_delete_old_tags.py to sample_delete_tags.py
YalinLi0312 13652c6
Add delete images sample
YalinLi0312 f1a0cee
Add set image properties sample
YalinLi0312 dc75e6a
Add list tags sample
YalinLi0312 2a10885
Add an empty space after ':' in output
YalinLi0312 5256307
Address comments
YalinLi0312 8f4e3fa
Address comments
YalinLi0312 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The note above says you're using tag 'v1'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is setting permissions to the "latest" tag, I'll update the comment in line44 to be more clear.