Skip to content

Commit 0dafe59

Browse files
authored
[ContainerRegistry] Add more champion scenario samples (Azure#15553)
1 parent 8c00ea1 commit 0dafe59

20 files changed

+395
-19
lines changed

sdk/containerregistry/container-registry/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ async function main() {
137137
const image = client.getArtifact("library/hello-world", "latest");
138138

139139
// List the set of tags on the hello_world image tagged as "latest"
140-
const tags = image.listTagPropertis();
140+
const tagIterator = image.listTagProperties();
141141

142142
// Iterate through the image's tags, listing the tagged alias for the image
143-
console.log(`${image.fullyQualifiedName} has the following aliases:`);
144-
for await (const tag of tags) {
143+
console.log(`${image.fullyQualifiedReference} has the following aliases:`);
144+
for await (const tag of tagIterator) {
145145
console.log(` ${tag.registryLoginServer}/${tag.repositoryName}:${tag.name}`);
146146
}
147147
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* @summary Lists tags for an image in a registry that enables anonymous pull access.
6+
* @azsdk-weight 5
7+
*
8+
*/
9+
10+
// A common use case for Azure Container Registries is to view the repositories, artifacts, or tags
11+
// in a public registry that belongs to someone else. In this case, the user would need to access
12+
// the registry anonymously. Anonymous access allows a user to list all the collections there, but
13+
// they wouldn't have permissions to modify or delete any of the images in the registry.
14+
import { ContainerRegistryClient } from "@azure/container-registry";
15+
import * as dotenv from "dotenv";
16+
dotenv.config();
17+
18+
async function main() {
19+
// Get the service endpoint from the environment
20+
const endpoint = process.env.CONTAINER_REGISTRY_ENDPOINT || "<endpoint>";
21+
22+
// Create a new ContainerRegistryClient for anonymous access
23+
const client = new ContainerRegistryClient(endpoint);
24+
25+
// Obtain a RegistryArtifact object to get access to image operations
26+
const image = client.getArtifact("library/hello-world", "latest");
27+
28+
// List the set of tags on the hello_world image tagged as "latest"
29+
const tagIterator = image.listTagProperties();
30+
31+
// Iterate through the image's tags, listing the tagged alias for the image
32+
console.log(`${image.fullyQualifiedReference} has the following aliases:`);
33+
for await (const tag of tagIterator) {
34+
console.log(` ${tag.registryLoginServer}/${tag.repositoryName}:${tag.name}`);
35+
}
36+
}
37+
38+
main().catch((err) => {
39+
console.error("The sample encountered an error:", err);
40+
});

sdk/containerregistry/container-registry/samples-dev/containerRegistryClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT License.
33

44
/**
5-
* @summary Demonstrates the use of a ContainerRegistryClient.
5+
* @summary Lists repository names and deletes a repository.
66
* @azsdk-weight 10
77
*/
88

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* @summary Deletes all but the latest three images.
6+
* @azsdk-weight 5
7+
*/
8+
9+
// A common use case for Azure Container Registries is to scan the repositories
10+
// in a registry and delete all but the most recent n images, or all images
11+
// older than a certain date.
12+
import { ContainerRegistryClient } from "@azure/container-registry";
13+
import { DefaultAzureCredential } from "@azure/identity";
14+
import * as dotenv from "dotenv";
15+
dotenv.config();
16+
17+
async function main() {
18+
// Get the service endpoint from the environment
19+
const endpoint = process.env.CONTAINER_REGISTRY_ENDPOINT || "<endpoint>";
20+
// Create a new ContainerRegistryClient
21+
const client = new ContainerRegistryClient(endpoint, new DefaultAzureCredential());
22+
23+
// Iterate through repositories
24+
const repositoryNames = client.listRepositoryNames();
25+
for await (const repositoryName of repositoryNames) {
26+
const repository = client.getRepository(repositoryName);
27+
// Obtain the images ordered from newest to oldest
28+
const imageManifests = repository.listManifestProperties({
29+
orderBy: "LastUpdatedOnDescending"
30+
});
31+
const imagesToKeep = 3;
32+
let imageCount = 0;
33+
// Delete images older than the first three.
34+
for await (const manifest of imageManifests) {
35+
if (imageCount++ > imagesToKeep) {
36+
console.log(`Deleting image with digest ${manifest.digest}`);
37+
console.log(` This image has the following tags:`);
38+
for (const tagName of manifest.tags) {
39+
console.log(` ${manifest.repositoryName}:${tagName}`);
40+
}
41+
await repository.getArtifact(manifest.digest).delete();
42+
}
43+
}
44+
}
45+
}
46+
47+
main().catch((err) => {
48+
console.error("The sample encountered an error:", err);
49+
});

sdk/containerregistry/container-registry/samples-dev/repositoryAndArtifact.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT License.
33

44
/**
5-
* @summary Demonstrates the use of ContainerRepository and RegistryArtifact.
5+
* @summary Uses ContainerRepository and RegistryArtifact to work with manifests, tags, and artifacts.
66
* @azsdk-weight 5
77
*/
88

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* @summary Updates the properties on the tag so it can't be overwritten or deleted.
6+
* @azsdk-weight 5
7+
*/
8+
9+
// This sample assumes the registry has a repository `hello-world` with image tagged `v1`.
10+
11+
import { ContainerRegistryClient } from "@azure/container-registry";
12+
import { DefaultAzureCredential } from "@azure/identity";
13+
import * as dotenv from "dotenv";
14+
dotenv.config();
15+
16+
async function main() {
17+
// Get the service endpoint from the environment
18+
const endpoint = process.env.CONTAINER_REGISTRY_ENDPOINT || "<endpoint>";
19+
// Create a new ContainerRegistryClient
20+
const client = new ContainerRegistryClient(endpoint, new DefaultAzureCredential());
21+
const image = client.getArtifact("library/hello-world", "latest");
22+
await image.updateTagProperties("v1", {
23+
canWrite: false,
24+
canDelete: false
25+
});
26+
// After this update, if someone were to push an update to `<registry endpoint>\hello-world:v1`, it would fail.
27+
// It's worth noting that if this image also had another tag, such as `latest`, and that tag did not have
28+
// permissions set to prevent reads or deletes, the image could still be overwritten. For example,
29+
// if someone were to push an update to `<registry endpoint>\hello-world:latest`
30+
// (which references the same image), it would succeed.
31+
}
32+
33+
main().catch((err) => {
34+
console.error("The sample encountered an error:", err);
35+
});

sdk/containerregistry/container-registry/samples/v1/javascript/README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ urlFragment: container-registry-javascript
1212

1313
These sample programs show how to use the JavaScript client libraries for Azure Container Registry in some common scenarios.
1414

15-
| **File Name** | **Description** |
16-
| ----------------------------------------------------- | ----------------------------------------------------------------- |
17-
| [containerRegistryClient.js][containerregistryclient] | Demonstrates the use of a ContainerRegistryClient. |
18-
| [repositoryAndArtifact.js][repositoryandartifact] | Demonstrates the use of ContainerRepository and RegistryArtifact. |
15+
| **File Name** | **Description** |
16+
| ----------------------------------------------------- | ------------------------------------------------------------------------------------------ |
17+
| [containerRegistryClient.js][containerregistryclient] | Lists repository names and deletes a repository. |
18+
| [anonymousListTags.js][anonymouslisttags] | Lists tags for an image in a registry that enables anonymous pull access. |
19+
| [deleteImages.js][deleteimages] | Deletes all but the latest three images. |
20+
| [repositoryAndArtifact.js][repositoryandartifact] | Uses ContainerRepository and RegistryArtifact to work with manifests, tags, and artifacts. |
21+
| [setImageProperties.js][setimageproperties] | Updates the properties on the tag so it can't be overwritten or deleted. |
1922

2023
## Prerequisites
2124

@@ -58,7 +61,10 @@ npx cross-env CONTAINER_REGISTRY_ENDPOINT="<container registry endpoint>" node c
5861
Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients.
5962

6063
[containerregistryclient]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/containerregistry/container-registry/samples/v1/javascript/containerRegistryClient.js
64+
[anonymouslisttags]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/containerregistry/container-registry/samples/v1/javascript/anonymousListTags.js
65+
[deleteimages]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/containerregistry/container-registry/samples/v1/javascript/deleteImages.js
6166
[repositoryandartifact]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/containerregistry/container-registry/samples/v1/javascript/repositoryAndArtifact.js
67+
[setimageproperties]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/containerregistry/container-registry/samples/v1/javascript/setImageProperties.js
6268
[apiref]: https://docs.microsoft.com/javascript/api/@azure/container-registry
6369
[freesub]: https://azure.microsoft.com/free/
6470
[createinstance_azurecontainerregistry]: https://docs.microsoft.com/azure/container-registry/container-registry-get-started-portal
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* @summary Lists tags for an image in a registry that enables anonymous pull access.
6+
*/
7+
8+
// A common use case for Azure Container Registries is to view the repositories, artifacts, or tags
9+
// in a public registry that belongs to someone else. In this case, the user would need to access
10+
// the registry anonymously. Anonymous access allows a user to list all the collections there, but
11+
// they wouldn't have permissions to modify or delete any of the images in the registry.
12+
const { ContainerRegistryClient } = require("@azure/container-registry");
13+
const dotenv = require("dotenv");
14+
dotenv.config();
15+
16+
async function main() {
17+
// Get the service endpoint from the environment
18+
const endpoint = process.env.CONTAINER_REGISTRY_ENDPOINT || "<endpoint>";
19+
20+
// Create a new ContainerRegistryClient for anonymous access
21+
const client = new ContainerRegistryClient(endpoint);
22+
23+
// Obtain a RegistryArtifact object to get access to image operations
24+
const image = client.getArtifact("library/hello-world", "latest");
25+
26+
// List the set of tags on the hello_world image tagged as "latest"
27+
const tagIterator = image.listTagProperties();
28+
29+
// Iterate through the image's tags, listing the tagged alias for the image
30+
console.log(`${image.fullyQualifiedReference} has the following aliases:`);
31+
for await (const tag of tagIterator) {
32+
console.log(` ${tag.registryLoginServer}/${tag.repositoryName}:${tag.name}`);
33+
}
34+
}
35+
36+
main().catch((err) => {
37+
console.error("The sample encountered an error:", err);
38+
});

sdk/containerregistry/container-registry/samples/v1/javascript/containerRegistryClient.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT License.
33

44
/**
5-
* @summary Demonstrates the use of a ContainerRegistryClient.
5+
* @summary Lists repository names and deletes a repository.
66
*/
77

88
const { ContainerRegistryClient } = require("@azure/container-registry");
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* @summary Deletes all but the latest three images.
6+
*/
7+
8+
// A common use case for Azure Container Registries is to scan the repositories
9+
// in a registry and delete all but the most recent n images, or all images
10+
// older than a certain date.
11+
const { ContainerRegistryClient } = require("@azure/container-registry");
12+
const { DefaultAzureCredential } = require("@azure/identity");
13+
const dotenv = require("dotenv");
14+
dotenv.config();
15+
16+
async function main() {
17+
// Get the service endpoint from the environment
18+
const endpoint = process.env.CONTAINER_REGISTRY_ENDPOINT || "<endpoint>";
19+
// Create a new ContainerRegistryClient
20+
const client = new ContainerRegistryClient(endpoint, new DefaultAzureCredential());
21+
22+
// Iterate through repositories
23+
const repositoryNames = client.listRepositoryNames();
24+
for await (const repositoryName of repositoryNames) {
25+
const repository = client.getRepository(repositoryName);
26+
// Obtain the images ordered from newest to oldest
27+
const imageManifests = repository.listManifestProperties({
28+
orderBy: "LastUpdatedOnDescending"
29+
});
30+
const imagesToKeep = 3;
31+
let imageCount = 0;
32+
// Delete images older than the first three.
33+
for await (const manifest of imageManifests) {
34+
if (imageCount++ > imagesToKeep) {
35+
console.log(`Deleting image with digest ${manifest.digest}`);
36+
console.log(` This image has the following tags:`);
37+
for (const tagName of manifest.tags) {
38+
console.log(` ${manifest.repositoryName}:${tagName}`);
39+
}
40+
await repository.getArtifact(manifest.digest).delete();
41+
}
42+
}
43+
}
44+
}
45+
46+
main().catch((err) => {
47+
console.error("The sample encountered an error:", err);
48+
});

0 commit comments

Comments
 (0)