public void DeleteImages()
        {
            Environment.SetEnvironmentVariable("REGISTRY_ENDPOINT", TestEnvironment.Endpoint);

            #region Snippet:ContainerRegistry_Tests_Samples_DeleteImage
            // Get the service endpoint from the environment
            Uri endpoint = new Uri(Environment.GetEnvironmentVariable("REGISTRY_ENDPOINT"));

            // Create a new ContainerRegistryClient
            ContainerRegistryClient client = new ContainerRegistryClient(endpoint, new DefaultAzureCredential());

            // Iterate through repositories
            Pageable <string> repositoryNames = client.GetRepositoryNames();
            foreach (string repositoryName in repositoryNames)
            {
                ContainerRepository repository = client.GetRepository(repositoryName);

                // Obtain the images ordered from newest to oldest
                Pageable <ArtifactManifestProperties> imageManifests =
                    repository.GetManifestPropertiesCollection(orderBy: ArtifactManifestOrderBy.LastUpdatedOnDescending);

                // Delete images older than the first three.
                foreach (ArtifactManifestProperties imageManifest in imageManifests.Skip(3))
                {
                    Console.WriteLine($"Deleting image with digest {imageManifest.Digest}.");
                    Console.WriteLine($"   This image has the following tags: ");
                    foreach (var tagName in imageManifest.Tags)
                    {
                        Console.WriteLine($"        {imageManifest.RepositoryName}:{tagName}");
                    }
                    repository.GetArtifact(imageManifest.Digest).Delete();
                }
            }
            #endregion
        }
Ejemplo n.º 2
0
        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"));

            // Create a new ContainerRegistryClient
            ContainerRegistryClient client = new ContainerRegistryClient(endpoint, new DefaultAzureCredential(),
                                                                         new ContainerRegistryClientOptions()
            {
                Audience = ContainerRegistryAudience.AzureResourceManagerPublicCloud
            });

            // Iterate through repositories
            Pageable <string> repositoryNames = client.GetRepositoryNames();
            foreach (string repositoryName in repositoryNames)
            {
                ContainerRepository repository = client.GetRepository(repositoryName);

                // Obtain the images ordered from newest to oldest
                Pageable <ArtifactManifestProperties> imageManifests =
                    repository.GetAllManifestProperties(manifestOrder: ArtifactManifestOrder.LastUpdatedOnDescending);

                // 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($"   Deleting the following tags from the image: ");
                    foreach (var tagName in imageManifest.Tags)
                    {
                        Console.WriteLine($"        {imageManifest.RepositoryName}:{tagName}");
                        image.DeleteTag(tagName);
                    }
                    image.Delete();
                }
            }
            #endregion
        }