Skip to content
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 Delete Sample #22138

Merged
merged 1 commit into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ image.UpdateTagProperties("latest", new ArtifactTagProperties()
### Delete images

```C# Snippet:ContainerRegistry_Tests_Samples_DeleteImage
using Azure.Containers.ContainerRegistry;
using Azure.Identity;

// Get the service endpoint from the environment
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("REGISTRY_ENDPOINT"));

Expand All @@ -177,13 +180,15 @@ foreach (string repositoryName in repositoryNames)
// Delete images older than the first three.
foreach (ArtifactManifestProperties imageManifest in imageManifests.Skip(3))
{
RegistryArtifact image = repository.GetArtifact(imageManifest.Digest);
Console.WriteLine($"Deleting image with digest {imageManifest.Digest}.");
Console.WriteLine($" This image has the following tags: ");
Console.WriteLine($" Deleting the following tags from the image: ");
foreach (var tagName in imageManifest.Tags)
{
Console.WriteLine($" {imageManifest.RepositoryName}:{tagName}");
image.DeleteTag(tagName);
}
repository.GetArtifact(imageManifest.Digest).Delete();
image.Delete();
}
}
```
Expand Down Expand Up @@ -251,6 +256,10 @@ await image.UpdateTagPropertiesAsync("latest", new ArtifactTagProperties()
### Delete images asynchronously

```C# Snippet:ContainerRegistry_Tests_Samples_DeleteImageAsync
using System.Linq;
using Azure.Containers.ContainerRegistry;
using Azure.Identity;

// Get the service endpoint from the environment
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("REGISTRY_ENDPOINT"));

Expand All @@ -270,13 +279,15 @@ await foreach (string repositoryName in repositoryNames)
// Delete images older than the first three.
await foreach (ArtifactManifestProperties imageManifest in imageManifests.Skip(3))
{
RegistryArtifact image = repository.GetArtifact(imageManifest.Digest);
Console.WriteLine($"Deleting image with digest {imageManifest.Digest}.");
Console.WriteLine($" This image has the following tags: ");
Console.WriteLine($" Deleting the following tags from the image: ");
foreach (var tagName in imageManifest.Tags)
{
Console.WriteLine($" {imageManifest.RepositoryName}:{tagName}");
await image.DeleteTagAsync(tagName);
}
await repository.GetArtifact(imageManifest.Digest).DeleteAsync();
await image.DeleteAsync();
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
A common use case for Azure Container Registries is to scan the repositories in a registry and delete all but the most recent *n* images, or all images older than a certain date. This sample illustrates how to use the .NET ACR SDK to delete all but the latest three images.

```C# Snippet:ContainerRegistry_Tests_Samples_DeleteImage
using Azure.Containers.ContainerRegistry;
using Azure.Identity;

// Get the service endpoint from the environment
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("REGISTRY_ENDPOINT"));

Expand All @@ -22,13 +25,15 @@ foreach (string repositoryName in repositoryNames)
// Delete images older than the first three.
foreach (ArtifactManifestProperties imageManifest in imageManifests.Skip(3))
{
RegistryArtifact image = repository.GetArtifact(imageManifest.Digest);
Console.WriteLine($"Deleting image with digest {imageManifest.Digest}.");
Console.WriteLine($" This image has the following tags: ");
Console.WriteLine($" Deleting the following tags from the image: ");
foreach (var tagName in imageManifest.Tags)
{
Console.WriteLine($" {imageManifest.RepositoryName}:{tagName}");
image.DeleteTag(tagName);
}
repository.GetArtifact(imageManifest.Digest).Delete();
image.Delete();
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@

A common use case for Azure Container Registries is to scan the repositories in a registry and delete all but the most recent *n* images, or all images older than a certain date. This sample illustrates how to use the .NET ACR SDK to delete all but the latest three images.

Please note:

- The `System.Linq.Async` package provides the LINQ `Skip` method. For more information on using this package with AsyncPageable, please see [Using System.Linq.Async with AsyncPageable](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/Response.md#using-systemlinqasync-with-asyncpageable).

- The operations in this sample are run in series, but could be parallelized using the new `Parallel.ForEachAsync` method in [.NET 6](https://dotnet.microsoft.com/download/dotnet/6.0).

```C# Snippet:ContainerRegistry_Tests_Samples_DeleteImageAsync
using System.Linq;
using Azure.Containers.ContainerRegistry;
using Azure.Identity;

// Get the service endpoint from the environment
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("REGISTRY_ENDPOINT"));

Expand All @@ -22,13 +32,15 @@ await foreach (string repositoryName in repositoryNames)
// Delete images older than the first three.
await foreach (ArtifactManifestProperties imageManifest in imageManifests.Skip(3))
{
RegistryArtifact image = repository.GetArtifact(imageManifest.Digest);
Console.WriteLine($"Deleting image with digest {imageManifest.Digest}.");
Console.WriteLine($" This image has the following tags: ");
Console.WriteLine($" Deleting the following tags from the image: ");
foreach (var tagName in imageManifest.Tags)
{
Console.WriteLine($" {imageManifest.RepositoryName}:{tagName}");
await image.DeleteTagAsync(tagName);
}
await repository.GetArtifact(imageManifest.Digest).DeleteAsync();
await image.DeleteAsync();
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public void DeleteImages()
Environment.SetEnvironmentVariable("REGISTRY_ENDPOINT", TestEnvironment.Endpoint);

#region Snippet:ContainerRegistry_Tests_Samples_DeleteImage
#if SNIPPET
using Azure.Containers.ContainerRegistry;
using Azure.Identity;
#endif

// Get the service endpoint from the environment
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("REGISTRY_ENDPOINT"));

Expand All @@ -39,13 +44,15 @@ public void DeleteImages()
// Delete images older than the first three.
foreach (ArtifactManifestProperties imageManifest in imageManifests.Skip(3))
{
RegistryArtifact image = repository.GetArtifact(imageManifest.Digest);
Console.WriteLine($"Deleting image with digest {imageManifest.Digest}.");
Console.WriteLine($" This image has the following tags: ");
Console.WriteLine($" Deleting the following tags from the image: ");
foreach (var tagName in imageManifest.Tags)
{
Console.WriteLine($" {imageManifest.RepositoryName}:{tagName}");
image.DeleteTag(tagName);
}
repository.GetArtifact(imageManifest.Digest).Delete();
image.Delete();
}
}
#endregion
Expand All @@ -57,7 +64,13 @@ public async Task DeleteImagesAsync()
{
Environment.SetEnvironmentVariable("REGISTRY_ENDPOINT", TestEnvironment.Endpoint);

# region Snippet:ContainerRegistry_Tests_Samples_DeleteImageAsync
#region Snippet:ContainerRegistry_Tests_Samples_DeleteImageAsync
#if SNIPPET
using System.Linq;
using Azure.Containers.ContainerRegistry;
using Azure.Identity;
#endif

// Get the service endpoint from the environment
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("REGISTRY_ENDPOINT"));

Expand All @@ -77,13 +90,15 @@ public async Task DeleteImagesAsync()
// Delete images older than the first three.
await foreach (ArtifactManifestProperties imageManifest in imageManifests.Skip(3))
{
RegistryArtifact image = repository.GetArtifact(imageManifest.Digest);
Console.WriteLine($"Deleting image with digest {imageManifest.Digest}.");
Console.WriteLine($" This image has the following tags: ");
Console.WriteLine($" Deleting the following tags from the image: ");
foreach (var tagName in imageManifest.Tags)
{
Console.WriteLine($" {imageManifest.RepositoryName}:{tagName}");
await image.DeleteTagAsync(tagName);
}
await repository.GetArtifact(imageManifest.Digest).DeleteAsync();
await image.DeleteAsync();
}
}

Expand Down