From 3d1c99fb801d8c864e4f77210da4eb06e0cee7e3 Mon Sep 17 00:00:00 2001 From: Jeremy Meng Date: Fri, 9 Apr 2021 11:54:01 -0700 Subject: [PATCH] [ContainerRegistry] Migrate samples to v2 workflow (#14750) * [ContainerRegistry] Migrate samples to v2 workflow * Add comments about endpoint format * Address CR feedback - move listing by pages to separate methods as they are more advanced scenarios. - also update apidocs link for beta.1 as docs haven't been published * Use javascript tag in JS samples' package.json * Update sdk/containerregistry/container-registry/sample.env Co-authored-by: Will Temple Co-authored-by: Will Temple --- .../container-registry/package.json | 18 ++- .../container-registry/sample.env | 3 +- .../samples-dev/containerRegistryClient.ts | 69 +++++++++ .../samples-dev/containerRepositoryClient.ts | 135 ++++++++++++++++++ .../javascript/containerRegistryClient.js | 51 ------- .../samples/javascript/sample.env | 11 -- .../container-registry/samples/tsconfig.json | 10 -- .../samples/typescript/sample.env | 11 -- .../samples/{ => v1}/javascript/README.md | 58 +++----- .../v1/javascript/containerRegistryClient.js | 68 +++++++++ .../javascript/containerRepositoryClient.js | 30 ++-- .../samples/{ => v1}/javascript/package.json | 17 ++- .../samples/v1/javascript/sample.env | 21 +++ .../samples/{ => v1}/typescript/README.md | 62 ++++---- .../samples/{ => v1}/typescript/package.json | 22 ++- .../samples/v1/typescript/sample.env | 21 +++ .../typescript/src/containerRegistryClient.ts | 37 +++-- .../src/containerRepositoryClient.ts | 33 +++-- .../samples/{ => v1}/typescript/tsconfig.json | 11 +- .../container-registry/tsconfig.json | 16 +-- 20 files changed, 481 insertions(+), 223 deletions(-) create mode 100644 sdk/containerregistry/container-registry/samples-dev/containerRegistryClient.ts create mode 100644 sdk/containerregistry/container-registry/samples-dev/containerRepositoryClient.ts delete mode 100644 sdk/containerregistry/container-registry/samples/javascript/containerRegistryClient.js delete mode 100644 sdk/containerregistry/container-registry/samples/javascript/sample.env delete mode 100644 sdk/containerregistry/container-registry/samples/tsconfig.json delete mode 100644 sdk/containerregistry/container-registry/samples/typescript/sample.env rename sdk/containerregistry/container-registry/samples/{ => v1}/javascript/README.md (50%) create mode 100644 sdk/containerregistry/container-registry/samples/v1/javascript/containerRegistryClient.js rename sdk/containerregistry/container-registry/samples/{ => v1}/javascript/containerRepositoryClient.js (84%) rename sdk/containerregistry/container-registry/samples/{ => v1}/javascript/package.json (75%) create mode 100644 sdk/containerregistry/container-registry/samples/v1/javascript/sample.env rename sdk/containerregistry/container-registry/samples/{ => v1}/typescript/README.md (51%) rename sdk/containerregistry/container-registry/samples/{ => v1}/typescript/package.json (71%) create mode 100644 sdk/containerregistry/container-registry/samples/v1/typescript/sample.env rename sdk/containerregistry/container-registry/samples/{ => v1}/typescript/src/containerRegistryClient.ts (50%) rename sdk/containerregistry/container-registry/samples/{ => v1}/typescript/src/containerRepositoryClient.ts (82%) rename sdk/containerregistry/container-registry/samples/{ => v1}/typescript/tsconfig.json (65%) diff --git a/sdk/containerregistry/container-registry/package.json b/sdk/containerregistry/container-registry/package.json index 047f6bd6ac34..908e25362f6f 100644 --- a/sdk/containerregistry/container-registry/package.json +++ b/sdk/containerregistry/container-registry/package.json @@ -25,15 +25,15 @@ "audit": "node ../../../common/scripts/rush-audit.js && rimraf node_modules package-lock.json && npm i --package-lock-only 2>&1 && npm audit", "build:browser": "tsc -p . && cross-env ONLY_BROWSER=true rollup -c 2>&1", "build:node": "tsc -p . && cross-env ONLY_NODE=true rollup -c 2>&1", - "build:samples": "dev-tool samples prep && cd dist-samples && tsc -p .", + "build:samples": "echo Obsolete.", "build:test": "tsc -p . && rollup -c 2>&1", "build": "tsc -p . && rollup -c 2>&1 && api-extractor run --local", - "check-format": "prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"", + "check-format": "prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"", "clean": "rimraf dist dist-* temp types *.tgz *.log", "docs": "typedoc --excludePrivate --excludeNotExported --excludeExternals --stripInternal --mode file --out ./dist/docs ./src", - "execute:samples": "npm run build:samples && dev-tool samples run dist-samples/javascript dist-samples/typescript/dist/dist-samples/typescript/src/", + "execute:samples": "dev-tool samples run samples-dev", "extract-api": "tsc -p . && api-extractor run --local", - "format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"", + "format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"", "generate:client": "autorest --typescript --v3 swagger", "integration-test:browser": "echo skipped", "integration-test:node": "nyc mocha -r esm --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --timeout 5000000 --full-trace \"dist-esm/test/{,!(browser)/**/}/*.spec.js\"", @@ -125,6 +125,14 @@ "util": "^0.12.1" }, "//sampleConfiguration": { - "skipFolder": true + "skipFolder": true, + "productName": "Azure Container Registry", + "productSlugs": [ + "azure", + "azure-container-registry" + ], + "requiredResources": { + "Azure Container Registry": "https://docs.microsoft.com/azure/container-registry/container-registry-get-started-portal" + } } } diff --git a/sdk/containerregistry/container-registry/sample.env b/sdk/containerregistry/container-registry/sample.env index 446c46e8dfc7..e1cc50fae02f 100644 --- a/sdk/containerregistry/container-registry/sample.env +++ b/sdk/containerregistry/container-registry/sample.env @@ -1,7 +1,8 @@ # Used in most samples. Retrieve these values from an instance in the Azure # Portal. - +# Retrieve this value from a Container Registry instance in the Azure Portal. CONTAINER_REGISTRY_ENDPOINT: "", +REPOSITORY_NAME="" # Used to authenticate using Azure AD as a service principal for role-based # authentication in the tokenAuth sample. diff --git a/sdk/containerregistry/container-registry/samples-dev/containerRegistryClient.ts b/sdk/containerregistry/container-registry/samples-dev/containerRegistryClient.ts new file mode 100644 index 000000000000..2ebf23d45c92 --- /dev/null +++ b/sdk/containerregistry/container-registry/samples-dev/containerRegistryClient.ts @@ -0,0 +1,69 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/** + * @summary Demonstrates the use of a ContainerRegistryClient. + * @azsdk-weight 10 + */ + +import { ContainerRegistryClient } from "@azure/container-registry"; +import { DefaultAzureCredential } from "@azure/identity"; +import * as dotenv from "dotenv"; +dotenv.config(); + +export async function main() { + // endpoint should be in the form of "https://myregistryname.azurecr.io" + // where "myregistryname" is the actual name of your registry + const endpoint = process.env.CONTAINER_REGISTRY_ENDPOINT || ""; + const client = new ContainerRegistryClient(endpoint, new DefaultAzureCredential()); + await listRepositories(client); + + // Advanced: listing by pages + const pageSize = 2; + await listRepositoriesByPages(client, pageSize); + + const repositoryName = "repository-name-to-delete"; + await deleteRepository(client, repositoryName); +} + +async function listRepositories(client: ContainerRegistryClient) { + console.log("Listing repositories"); + const iterator = client.listRepositories(); + for await (const repository of iterator) { + console.log(` repository: ${repository}`); + } +} + +async function listRepositoriesByPages(client: any, pageSize: number) { + console.log("Listing repositories by pages"); + const pages = client.listRepositories().byPage({ maxPageSize: pageSize }); + let result = await pages.next(); + while (!result.done) { + console.log(" -- page -- "); + for (const repository of result.value) { + console.log(` repository: ${repository}`); + } + result = await pages.next(); + } +} + +async function deleteRepository(client: ContainerRegistryClient, repositoryName: string) { + console.log("Deleting a repository"); + const response = await client.deleteRepository(repositoryName); + console.log( + `Artifacts deleted: ${(response && + response.deletedRegistryArtifactDigests && + response.deletedRegistryArtifactDigests.length) || + 0}` + ); + console.log( + `Tags deleted: ${(response && + response.deletedRegistryArtifactDigests && + response.deletedRegistryArtifactDigests.length) || + 0}` + ); +} + +main().catch((err) => { + console.error("The sample encountered an error:", err); +}); diff --git a/sdk/containerregistry/container-registry/samples-dev/containerRepositoryClient.ts b/sdk/containerregistry/container-registry/samples-dev/containerRepositoryClient.ts new file mode 100644 index 000000000000..a4059f0225db --- /dev/null +++ b/sdk/containerregistry/container-registry/samples-dev/containerRepositoryClient.ts @@ -0,0 +1,135 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/** + * @summary Demonstrates the use of a ContainerRepositoryClient. + * @azsdk-weight 5 + */ + +import { ContainerRepositoryClient, RegistryArtifactProperties } from "@azure/container-registry"; +import { DefaultAzureCredential } from "@azure/identity"; +import * as dotenv from "dotenv"; +dotenv.config(); + +export async function main() { + // endpoint should be in the form of "https://myregistryname.azurecr.io" + // where "myregistryname" is the actual name of your registry + const endpoint = process.env.CONTAINER_REGISTRY_ENDPOINT || ""; + const repository = process.env.REPOSITORY_NAME || ""; + + const client = new ContainerRepositoryClient(endpoint, repository, new DefaultAzureCredential()); + await getProperties(client); + await listTags(client); + + const artifacts = await listArtifacts(client); + + if (artifacts && artifacts.length) { + const digest = artifacts[0].digest; + if (digest) { + await getArtifactProperties(client, digest); + + await deleteArtifact(client, digest); + } + } + + // Advanced: listing by pages + const pageSize = 2; + await listTagsByPages(client, pageSize); + await listArtifactsByPages(client, pageSize); +} + +async function listTags(client: ContainerRepositoryClient) { + console.log("Listing tags"); + const iterator = client.listTags({ orderBy: "timeasc" }); + for await (const tag of iterator) { + console.log(` tag: ${tag.name}`); + console.log(` digest: ${tag.digest}`); + console.log(` created on: ${tag.createdOn}`); + console.log(` last updated on: ${tag.lastUpdatedOn}`); + } +} + +async function listTagsByPages(client: ContainerRepositoryClient, pagesSize: number) { + console.log("Listing tags by pages"); + const pages = client.listTags().byPage({ maxPageSize: pagesSize }); + let result = await pages.next(); + while (!result.done) { + console.log(" -- page -- "); + for (const tag of result.value) { + console.log(` tag: ${tag.name}`); + console.log(` digest: ${tag.digest}`); + console.log(` created on: ${tag.createdOn}`); + console.log(` last updated on: ${tag.lastUpdatedOn}`); + console.log(""); + } + result = await pages.next(); + } +} + +async function listArtifacts( + client: ContainerRepositoryClient +): Promise { + console.log("Listing artifacts"); + const artifacts: RegistryArtifactProperties[] = []; + const iterator = client.listRegistryArtifacts(); + for await (const artifact of iterator) { + artifacts.push(artifact); + console.log(` digest: ${artifact.digest}`); + console.log(` created on: ${artifact.createdOn}`); + console.log(` last updated on: ${artifact.lastUpdatedOn}`); + } + + return artifacts; +} + +async function listArtifactsByPages(client: any, pageSize: number) { + console.log("Listing artifacts by pages"); + const pages = client.listRegistryArtifacts().byPage({ maxPageSize: pageSize }); + let result = await pages.next(); + while (!result.done) { + console.log(" -- page -- "); + for (const artifact of result.value) { + console.log(` digest: ${artifact.digest}`); + console.log(` created on: ${artifact.createdOn}`); + console.log(` last updated on: ${artifact.lastUpdatedOn}`); + console.log(""); + } + result = await pages.next(); + } +} + +async function getProperties(client: ContainerRepositoryClient) { + console.log("Retrieving repository properties..."); + const properties = await client.getProperties(); + console.log(` name: ${properties.name}`); + console.log(` created on: ${properties.createdOn}`); + console.log(` last updated on: ${properties.lastUpdatedOn}`); + console.log(` artifact count: ${properties.registryArtifactCount}`); + console.log(` tag count: ${properties.tagCount}`); + const writableProps = properties.writeableProperties; + if (writableProps) { + console.log(" writable properties:"); + console.log( + ` { canDelete: ${writableProps.canDelete}, canList: ${writableProps.canList}, canRead: ${writableProps.canRead}, canWrite: ${writableProps.canWrite}}` + ); + } +} + +async function getArtifactProperties(client: ContainerRepositoryClient, digest: string) { + console.log(`Retrieving registry artifact properties for ${digest}`); + const properties = await client.getRegistryArtifactProperties(digest); + console.log(` created on: ${properties.createdOn}`); + console.log(` last updated on: ${properties.lastUpdatedOn}`); + console.log(` arch : ${properties.cpuArchitecture}`); + console.log(` os : ${properties.operatingSystem}`); + console.log(` size : ${properties.size} bytes`); +} + +async function deleteArtifact(client: ContainerRepositoryClient, digest: string) { + console.log(`Deleting registry artifact for ${digest}`); + await client.deleteRegistryArtifact(digest); +} + +main().catch((err) => { + console.error("The sample encountered an error:", err); +}); diff --git a/sdk/containerregistry/container-registry/samples/javascript/containerRegistryClient.js b/sdk/containerregistry/container-registry/samples/javascript/containerRegistryClient.js deleted file mode 100644 index 1d61b84fdd00..000000000000 --- a/sdk/containerregistry/container-registry/samples/javascript/containerRegistryClient.js +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -/** - * @summary Demonstrates the use of a ContainerRegistryClient. - */ - -const { ContainerRegistryClient } = require("@azure/container-registry"); -const { DefaultAzureCredential } = require("@azure/identity"); -require("dotenv").config(); - -async function main() { - const endpoint = process.env.ENDPOINT || ""; - - const client = new ContainerRegistryClient(endpoint, new DefaultAzureCredential()); - await listRepositories(client); - await deleteRepository(client); -} - -async function listRepositories(client) { - console.log("Listing repositories"); - const iterator = client.listRepositories(); - for await (const repository of iterator) { - console.log(` repository: ${repository}`); - } - - console.log(" by pages"); - const pages = client.listRepositories().byPage({ maxPageSize: 2 }); - let result = await pages.next(); - while (!result.done) { - console.log(" -- page -- "); - for (const repository of result.value) { - console.log(` repository: ${repository}`); - } - result = await pages.next(); - } -} - -async function deleteRepository(client) { - const response = await client.deleteRepository("hello-world"); - console.log(`Artifacts deleted: ${response.deletedRegistryArtifactDigests.length || 0}`); - console.log(`Tags deleted: ${response.deletedRegistryArtifactDigests.length || 0}`); -} - -main() - .then(() => { - console.log("Sample completes successfully."); - }) - .catch((err) => { - console.error("The sample encountered an error:", err); - }); diff --git a/sdk/containerregistry/container-registry/samples/javascript/sample.env b/sdk/containerregistry/container-registry/samples/javascript/sample.env deleted file mode 100644 index ae9dadb4bc84..000000000000 --- a/sdk/containerregistry/container-registry/samples/javascript/sample.env +++ /dev/null @@ -1,11 +0,0 @@ -# Retrieve this value from a Container Registry instance in the Azure Portal. -ENDPOINT="Your Endpoint URL" -REPOSITORY_NAME="Repository Name" - -# Used to authenticate using Azure AD as a service principal for role-based authentication -# -# See the documentation for `EnvironmentCredential` at the following link: -# https://docs.microsoft.com/javascript/api/@azure/identity/environmentcredential -AZURE_TENANT_ID= -AZURE_CLIENT_ID= -AZURE_CLIENT_SECRET= diff --git a/sdk/containerregistry/container-registry/samples/tsconfig.json b/sdk/containerregistry/container-registry/samples/tsconfig.json deleted file mode 100644 index 0e83d1fd597c..000000000000 --- a/sdk/containerregistry/container-registry/samples/tsconfig.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "extends": "../tsconfig.json", - "compilerOptions": { - "module": "commonjs", - "outDir": "typescript/dist", - "lib": ["ES6"] - }, - "include": ["typescript/src/**.ts"], - "exclude": ["typescript/*.json", "**/node_modules/", "../node_modules", "../typings"] -} diff --git a/sdk/containerregistry/container-registry/samples/typescript/sample.env b/sdk/containerregistry/container-registry/samples/typescript/sample.env deleted file mode 100644 index ae9dadb4bc84..000000000000 --- a/sdk/containerregistry/container-registry/samples/typescript/sample.env +++ /dev/null @@ -1,11 +0,0 @@ -# Retrieve this value from a Container Registry instance in the Azure Portal. -ENDPOINT="Your Endpoint URL" -REPOSITORY_NAME="Repository Name" - -# Used to authenticate using Azure AD as a service principal for role-based authentication -# -# See the documentation for `EnvironmentCredential` at the following link: -# https://docs.microsoft.com/javascript/api/@azure/identity/environmentcredential -AZURE_TENANT_ID= -AZURE_CLIENT_ID= -AZURE_CLIENT_SECRET= diff --git a/sdk/containerregistry/container-registry/samples/javascript/README.md b/sdk/containerregistry/container-registry/samples/v1/javascript/README.md similarity index 50% rename from sdk/containerregistry/container-registry/samples/javascript/README.md rename to sdk/containerregistry/container-registry/samples/v1/javascript/README.md index 28508bbfc5e2..7d332191c0c8 100644 --- a/sdk/containerregistry/container-registry/samples/javascript/README.md +++ b/sdk/containerregistry/container-registry/samples/v1/javascript/README.md @@ -1,45 +1,31 @@ - - -## + - javascript +products: + - azure + - azure-container-registry +urlFragment: container-registry-javascript +--- # Azure Container Registry client library samples for JavaScript These sample programs show how to use the JavaScript client libraries for Azure Container Registry in some common scenarios. -| **File Name** | **Description** | -| ----------------------------------------------------- | -------------------------------------------------- | -| [containerRegistryClient.js][containerregistryclient] | Demonstrates the use of a ContainerRegistryClient. | +| **File Name** | **Description** | +| --------------------------------------------------------- | ---------------------------------------------------- | +| [containerRegistryClient.js][containerregistryclient] | Demonstrates the use of a ContainerRegistryClient. | +| [containerRepositoryClient.js][containerrepositoryclient] | Demonstrates the use of a ContainerRepositoryClient. | ## Prerequisites -The samples are compatible with Node.js >= 8.0.0. +The sample programs are compatible with Node.js >=12.0.0. + +You need [an Azure subscription][freesub] and the following Azure resources to run these sample programs: + +- [Azure Container Registry][createinstance_azurecontainerregistry] -You need [an Azure subscription][freesub] to run these sample programs. Samples retrieve credentials to access the endpoint from environment variables. Alternatively, edit the source code to include the appropriate credentials. See each individual sample for details on which environment variables/credentials it requires to function. +Samples retrieve credentials to access the service endpoint from environment variables. Alternatively, edit the source code to include the appropriate credentials. See each individual sample for details on which environment variables/credentials it requires to function. Adapting the samples to run in the browser may require some additional consideration. For details, please see the [package README][package]. @@ -61,17 +47,19 @@ npm install node containerRegistryClient.js ``` -Alternatively, run a single sample with the correct environment variables set (step 3 is not required if you do this), for example (cross-platform): +Alternatively, run a single sample with the correct environment variables set (setting up the `.env` file is not required if you do this), for example (cross-platform): ```bash -npx cross-env ENDPOINT="" API_KEY="" node containerRegistryClient.js +npx cross-env CONTAINER_REGISTRY_ENDPOINT="" node containerRegistryClient.js ``` ## Next Steps Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients. -[containerregistryclient]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/containerregistry/container-registry/samples/javascript/containerRegistryClient.js -[apiref]: https://docs.microsoft.com/javascript/api +[containerregistryclient]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/containerregistry/container-registry/samples/v1/javascript/containerRegistryClient.js +[containerrepositoryclient]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/containerregistry/container-registry/samples/v1/javascript/containerRepositoryClient.js +[apiref]: https://azuresdkdocs.blob.core.windows.net/$web/javascript/azure-container-registry/1.0.0-beta.1/index.html [freesub]: https://azure.microsoft.com/free/ +[createinstance_azurecontainerregistry]: https://docs.microsoft.com/azure/container-registry/container-registry-get-started-portal [package]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/containerregistry/container-registry/README.md diff --git a/sdk/containerregistry/container-registry/samples/v1/javascript/containerRegistryClient.js b/sdk/containerregistry/container-registry/samples/v1/javascript/containerRegistryClient.js new file mode 100644 index 000000000000..a8001bfae60f --- /dev/null +++ b/sdk/containerregistry/container-registry/samples/v1/javascript/containerRegistryClient.js @@ -0,0 +1,68 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/** + * @summary Demonstrates the use of a ContainerRegistryClient. + */ + +const { ContainerRegistryClient } = require("@azure/container-registry"); +const { DefaultAzureCredential } = require("@azure/identity"); +const dotenv = require("dotenv"); +dotenv.config(); + +async function main() { + // endpoint should be in the form of "https://myregistryname.azurecr.io" + // where "myregistryname" is the actual name of your registry + const endpoint = process.env.CONTAINER_REGISTRY_ENDPOINT || ""; + const client = new ContainerRegistryClient(endpoint, new DefaultAzureCredential()); + await listRepositories(client); + + // Advanced: listing by pages + const pageSize = 2; + await listRepositoriesByPages(client, pageSize); + + const repositoryName = "repository-name-to-delete"; + await deleteRepository(client, repositoryName); +} + +async function listRepositories(client) { + console.log("Listing repositories"); + const iterator = client.listRepositories(); + for await (const repository of iterator) { + console.log(` repository: ${repository}`); + } +} + +async function listRepositoriesByPages(client, pageSize) { + console.log("Listing repositories by pages"); + const pages = client.listRepositories().byPage({ maxPageSize: pageSize }); + let result = await pages.next(); + while (!result.done) { + console.log(" -- page -- "); + for (const repository of result.value) { + console.log(` repository: ${repository}`); + } + result = await pages.next(); + } +} + +async function deleteRepository(client, repositoryName) { + console.log("Deleting a repository"); + const response = await client.deleteRepository(repositoryName); + console.log( + `Artifacts deleted: ${(response && + response.deletedRegistryArtifactDigests && + response.deletedRegistryArtifactDigests.length) || + 0}` + ); + console.log( + `Tags deleted: ${(response && + response.deletedRegistryArtifactDigests && + response.deletedRegistryArtifactDigests.length) || + 0}` + ); +} + +main().catch((err) => { + console.error("The sample encountered an error:", err); +}); diff --git a/sdk/containerregistry/container-registry/samples/javascript/containerRepositoryClient.js b/sdk/containerregistry/container-registry/samples/v1/javascript/containerRepositoryClient.js similarity index 84% rename from sdk/containerregistry/container-registry/samples/javascript/containerRepositoryClient.js rename to sdk/containerregistry/container-registry/samples/v1/javascript/containerRepositoryClient.js index 971b3f8bdbbd..a20951494a6b 100644 --- a/sdk/containerregistry/container-registry/samples/javascript/containerRepositoryClient.js +++ b/sdk/containerregistry/container-registry/samples/v1/javascript/containerRepositoryClient.js @@ -7,16 +7,21 @@ const { ContainerRepositoryClient } = require("@azure/container-registry"); const { DefaultAzureCredential } = require("@azure/identity"); -require("dotenv").config(); +const dotenv = require("dotenv"); +dotenv.config(); async function main() { - const endpoint = process.env.ENDPOINT || ""; + // endpoint should be in the form of "https://myregistryname.azurecr.io" + // where "myregistryname" is the actual name of your registry + const endpoint = process.env.CONTAINER_REGISTRY_ENDPOINT || ""; const repository = process.env.REPOSITORY_NAME || ""; const client = new ContainerRepositoryClient(endpoint, repository, new DefaultAzureCredential()); await getProperties(client); await listTags(client); + const artifacts = await listArtifacts(client); + if (artifacts && artifacts.length) { const digest = artifacts[0].digest; if (digest) { @@ -25,6 +30,11 @@ async function main() { await deleteArtifact(client, digest); } } + + // Advanced: listing by pages + const pageSize = 2; + await listTagsByPages(client, pageSize); + await listArtifactsByPages(client, pageSize); } async function listTags(client) { @@ -36,9 +46,11 @@ async function listTags(client) { console.log(` created on: ${tag.createdOn}`); console.log(` last updated on: ${tag.lastUpdatedOn}`); } +} - console.log(" by pages"); - const pages = client.listTags().byPage({ maxPageSize: 2 }); +async function listTagsByPages(client, pagesSize) { + console.log("Listing tags by pages"); + const pages = client.listTags().byPage({ maxPageSize: pagesSize }); let result = await pages.next(); while (!result.done) { console.log(" -- page -- "); @@ -64,8 +76,12 @@ async function listArtifacts(client) { console.log(` last updated on: ${artifact.lastUpdatedOn}`); } - console.log(" by pages"); - const pages = client.listRegistryArtifacts().byPage({ maxPageSize: 2 }); + return artifacts; +} + +async function listArtifactsByPages(client, pageSize) { + console.log("Listing artifacts by pages"); + const pages = client.listRegistryArtifacts().byPage({ maxPageSize: pageSize }); let result = await pages.next(); while (!result.done) { console.log(" -- page -- "); @@ -77,8 +93,6 @@ async function listArtifacts(client) { } result = await pages.next(); } - - return artifacts; } async function getProperties(client) { diff --git a/sdk/containerregistry/container-registry/samples/javascript/package.json b/sdk/containerregistry/container-registry/samples/v1/javascript/package.json similarity index 75% rename from sdk/containerregistry/container-registry/samples/javascript/package.json rename to sdk/containerregistry/container-registry/samples/v1/javascript/package.json index 4def10ab9c7a..f576c0b6fa01 100644 --- a/sdk/containerregistry/container-registry/samples/javascript/package.json +++ b/sdk/containerregistry/container-registry/samples/v1/javascript/package.json @@ -1,10 +1,10 @@ { "name": "azure-container-registry-samples-js", "private": true, - "version": "0.1.0", + "version": "1.0.0", "description": "Azure Container Registry client library samples for JavaScript", "engine": { - "node": ">=8.0.0" + "node": ">=12.0.0" }, "repository": { "type": "git", @@ -12,9 +12,9 @@ "directory": "sdk/containerregistry/container-registry" }, "keywords": [ - "Azure", - "Node.js", - "JavaScript" + "azure", + "cloud", + "javascript" ], "author": "Microsoft Corporation", "license": "MIT", @@ -22,10 +22,9 @@ "url": "https://github.com/Azure/azure-sdk-for-js/issues" }, "homepage": "https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/containerregistry/container-registry", - "sideEffects": false, "dependencies": { - "@azure/container-registry": "dev", - "@azure/identity": "latest", - "dotenv": "^8.2.0" + "@azure/container-registry": "next", + "dotenv": "latest", + "@azure/identity": "^1.1.0" } } diff --git a/sdk/containerregistry/container-registry/samples/v1/javascript/sample.env b/sdk/containerregistry/container-registry/samples/v1/javascript/sample.env new file mode 100644 index 000000000000..d59b955c63ec --- /dev/null +++ b/sdk/containerregistry/container-registry/samples/v1/javascript/sample.env @@ -0,0 +1,21 @@ +# Used in most samples. Retrieve these values from an instance in the Azure +# Portal. +# Retrieve this value from a Container Registry instance in the Azure Portal. +CONTAINER_REGISTRY_ENDPOINT: "", +REPOSITORY_NAME="Repository Name" + +# Used to authenticate using Azure AD as a service principal for role-based +# authentication in the tokenAuth sample. +# +# See the documentation for `EnvironmentCredential` at the following link: +# https://docs.microsoft.com/javascript/api/@azure/identity/environmentcredential + +AZURE_TENANT_ID="" +AZURE_CLIENT_ID="" +AZURE_CLIENT_SECRET="" + +# Our tests assume that TEST_MODE is "playback" by default. You can change it +# to "record" to generate new recordings, or "live" to bypass the recorder +# entirely. + +# TEST_MODE="playback" diff --git a/sdk/containerregistry/container-registry/samples/typescript/README.md b/sdk/containerregistry/container-registry/samples/v1/typescript/README.md similarity index 51% rename from sdk/containerregistry/container-registry/samples/typescript/README.md rename to sdk/containerregistry/container-registry/samples/v1/typescript/README.md index 141c4ec708c1..130f18421964 100644 --- a/sdk/containerregistry/container-registry/samples/typescript/README.md +++ b/sdk/containerregistry/container-registry/samples/v1/typescript/README.md @@ -1,49 +1,37 @@ - - -## + - typescript +products: + - azure + - azure-container-registry +urlFragment: container-registry-typescript +--- # Azure Container Registry client library samples for TypeScript These sample programs show how to use the TypeScript client libraries for Azure Container Registry in some common scenarios. -| **File Name** | **Description** | -| ----------------------------------------------------- | -------------------------------------------------- | -| [containerRegistryClient.ts][containerregistryclient] | Demonstrates the use of a ContainerRegistryClient. | +| **File Name** | **Description** | +| --------------------------------------------------------- | ---------------------------------------------------- | +| [containerRegistryClient.ts][containerregistryclient] | Demonstrates the use of a ContainerRegistryClient. | +| [containerRepositoryClient.ts][containerrepositoryclient] | Demonstrates the use of a ContainerRepositoryClient. | ## Prerequisites -The samples are compatible with Node.js >= 8.0.0. +The sample programs are compatible with Node.js >=12.0.0. -Before running the samples in Node, they must be compiled to JavaScript using the TypeScript compiler. For more information on TypeScript, see the [TypeScript documentation][typescript]. Install the TypeScript compiler using +Before running the samples in Node, they must be compiled to JavaScript using the TypeScript compiler. For more information on TypeScript, see the [TypeScript documentation][typescript]. Install the TypeScript compiler using: ```bash npm install -g typescript ``` -You need [an Azure subscription][freesub] to run these sample programs. Samples retrieve credentials to access the endpoint from environment variables. Alternatively, edit the source code to include the appropriate credentials. See each individual sample for details on which environment variables/credentials it requires to function. +You need [an Azure subscription][freesub] and the following Azure resources to run these sample programs: + +- [Azure Container Registry][createinstance_azurecontainerregistry] + +Samples retrieve credentials to access the service endpoint from environment variables. Alternatively, edit the source code to include the appropriate credentials. See each individual sample for details on which environment variables/credentials it requires to function. Adapting the samples to run in the browser may require some additional consideration. For details, please see the [package README][package]. @@ -57,7 +45,7 @@ To run the samples using the published version of the package: npm install ``` -2. Compile the samples +2. Compile the samples: ```bash npm run build @@ -68,21 +56,23 @@ npm run build 4. Run whichever samples you like (note that some samples may require additional setup, see the table above): ```bash -node dist/containerRegistryClient.js +node dist/containerRegistryClient.ts ``` -Alternatively, run a single sample with the correct environment variables set (step 3 is not required if you do this), for example (cross-platform): +Alternatively, run a single sample with the correct environment variables set (setting up the `.env` file is not required if you do this), for example (cross-platform): ```bash -npx cross-env ENDPOINT="" API_KEY="" node dist/containerRegistryClient.js +npx cross-env CONTAINER_REGISTRY_ENDPOINT="" node dist/containerRegistryClient.js ``` ## Next Steps Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients. -[containerregistryclient]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/containerregistry/container-registry/samples/typescript/src/containerRegistryClient.ts -[apiref]: https://docs.microsoft.com/javascript/api +[containerregistryclient]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/containerregistry/container-registry/samples/v1/typescript/src/containerRegistryClient.ts +[containerrepositoryclient]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/containerregistry/container-registry/samples/v1/typescript/src/containerRepositoryClient.ts +[apiref]: https://azuresdkdocs.blob.core.windows.net/$web/javascript/azure-container-registry/1.0.0-beta.1/index.html [freesub]: https://azure.microsoft.com/free/ +[createinstance_azurecontainerregistry]: https://docs.microsoft.com/azure/container-registry/container-registry-get-started-portal [package]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/containerregistry/container-registry/README.md [typescript]: https://www.typescriptlang.org/docs/home.html diff --git a/sdk/containerregistry/container-registry/samples/typescript/package.json b/sdk/containerregistry/container-registry/samples/v1/typescript/package.json similarity index 71% rename from sdk/containerregistry/container-registry/samples/typescript/package.json rename to sdk/containerregistry/container-registry/samples/v1/typescript/package.json index 953116056dc3..db74f19edc22 100644 --- a/sdk/containerregistry/container-registry/samples/typescript/package.json +++ b/sdk/containerregistry/container-registry/samples/v1/typescript/package.json @@ -1,10 +1,10 @@ { "name": "azure-container-registry-samples-ts", "private": true, - "version": "0.1.0", + "version": "1.0.0", "description": "Azure Container Registry client library samples for TypeScript", "engine": { - "node": ">=8.0.0" + "node": ">=12.0.0" }, "scripts": { "build": "tsc", @@ -16,9 +16,9 @@ "directory": "sdk/containerregistry/container-registry" }, "keywords": [ - "Azure", - "Node.js", - "TypeScript" + "azure", + "cloud", + "typescript" ], "author": "Microsoft Corporation", "license": "MIT", @@ -26,15 +26,13 @@ "url": "https://github.com/Azure/azure-sdk-for-js/issues" }, "homepage": "https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/containerregistry/container-registry", - "sideEffects": false, "dependencies": { - "@azure/container-registry": "dev", - "@azure/identity": "latest", - "dotenv": "^8.2.0" + "@azure/container-registry": "next", + "dotenv": "latest", + "@azure/identity": "^1.1.0" }, "devDependencies": { - "@types/node": "^8.0.0", - "rimraf": "^3.0.0", - "typescript": "~3.6.4" + "typescript": "~4.2.0", + "rimraf": "latest" } } diff --git a/sdk/containerregistry/container-registry/samples/v1/typescript/sample.env b/sdk/containerregistry/container-registry/samples/v1/typescript/sample.env new file mode 100644 index 000000000000..d59b955c63ec --- /dev/null +++ b/sdk/containerregistry/container-registry/samples/v1/typescript/sample.env @@ -0,0 +1,21 @@ +# Used in most samples. Retrieve these values from an instance in the Azure +# Portal. +# Retrieve this value from a Container Registry instance in the Azure Portal. +CONTAINER_REGISTRY_ENDPOINT: "", +REPOSITORY_NAME="Repository Name" + +# Used to authenticate using Azure AD as a service principal for role-based +# authentication in the tokenAuth sample. +# +# See the documentation for `EnvironmentCredential` at the following link: +# https://docs.microsoft.com/javascript/api/@azure/identity/environmentcredential + +AZURE_TENANT_ID="" +AZURE_CLIENT_ID="" +AZURE_CLIENT_SECRET="" + +# Our tests assume that TEST_MODE is "playback" by default. You can change it +# to "record" to generate new recordings, or "live" to bypass the recorder +# entirely. + +# TEST_MODE="playback" diff --git a/sdk/containerregistry/container-registry/samples/typescript/src/containerRegistryClient.ts b/sdk/containerregistry/container-registry/samples/v1/typescript/src/containerRegistryClient.ts similarity index 50% rename from sdk/containerregistry/container-registry/samples/typescript/src/containerRegistryClient.ts rename to sdk/containerregistry/container-registry/samples/v1/typescript/src/containerRegistryClient.ts index 9395a1db97c7..5b06d83aeead 100644 --- a/sdk/containerregistry/container-registry/samples/typescript/src/containerRegistryClient.ts +++ b/sdk/containerregistry/container-registry/samples/v1/typescript/src/containerRegistryClient.ts @@ -11,10 +11,18 @@ import * as dotenv from "dotenv"; dotenv.config(); export async function main() { - const endpoint = process.env.ENDPOINT ?? ""; + // endpoint should be in the form of "https://myregistryname.azurecr.io" + // where "myregistryname" is the actual name of your registry + const endpoint = process.env.CONTAINER_REGISTRY_ENDPOINT || ""; const client = new ContainerRegistryClient(endpoint, new DefaultAzureCredential()); await listRepositories(client); - await deleteRepository(client); + + // Advanced: listing by pages + const pageSize = 2; + await listRepositoriesByPages(client, pageSize); + + const repositoryName = "hello-world"; + await deleteRepository(client, repositoryName); } async function listRepositories(client: ContainerRegistryClient) { @@ -23,9 +31,11 @@ async function listRepositories(client: ContainerRegistryClient) { for await (const repository of iterator) { console.log(` repository: ${repository}`); } +} - console.log(" by pages"); - const pages = client.listRepositories().byPage({ maxPageSize: 2 }); +async function listRepositoriesByPages(client: any, pageSize: number) { + console.log("Listing repositories by pages"); + const pages = client.listRepositories().byPage({ maxPageSize: pageSize }); let result = await pages.next(); while (!result.done) { console.log(" -- page -- "); @@ -36,10 +46,21 @@ async function listRepositories(client: ContainerRegistryClient) { } } -async function deleteRepository(client: ContainerRegistryClient) { - const response = await client.deleteRepository("hello-world"); - console.log(`Artifacts deleted: ${response?.deletedRegistryArtifactDigests?.length ?? 0}`); - console.log(`Tags deleted: ${response?.deletedRegistryArtifactDigests?.length ?? 0}`); +async function deleteRepository(client: ContainerRegistryClient, repositoryName: string) { + console.log("Deleting a repository"); + const response = await client.deleteRepository(repositoryName); + console.log( + `Artifacts deleted: ${(response && + response.deletedRegistryArtifactDigests && + response.deletedRegistryArtifactDigests.length) || + 0}` + ); + console.log( + `Tags deleted: ${(response && + response.deletedRegistryArtifactDigests && + response.deletedRegistryArtifactDigests.length) || + 0}` + ); } main().catch((err) => { diff --git a/sdk/containerregistry/container-registry/samples/typescript/src/containerRepositoryClient.ts b/sdk/containerregistry/container-registry/samples/v1/typescript/src/containerRepositoryClient.ts similarity index 82% rename from sdk/containerregistry/container-registry/samples/typescript/src/containerRepositoryClient.ts rename to sdk/containerregistry/container-registry/samples/v1/typescript/src/containerRepositoryClient.ts index 3e456fb3910d..e7f40a067ef4 100644 --- a/sdk/containerregistry/container-registry/samples/typescript/src/containerRepositoryClient.ts +++ b/sdk/containerregistry/container-registry/samples/v1/typescript/src/containerRepositoryClient.ts @@ -2,7 +2,7 @@ // Licensed under the MIT License. /** - * @summary Demonstrates the use of a ContainerRegistryClient. + * @summary Demonstrates the use of a ContainerRepositoryClient. */ import { ContainerRepositoryClient, RegistryArtifactProperties } from "@azure/container-registry"; @@ -11,14 +11,18 @@ import * as dotenv from "dotenv"; dotenv.config(); export async function main() { - const endpoint = process.env.ENDPOINT ?? ""; - const repository = process.env.REPOSITORY_NAME ?? ""; + // endpoint should be in the form of "https://myregistryname.azurecr.io" + // where "myregistryname" is the actual name of your registry + const endpoint = process.env.CONTAINER_REGISTRY_ENDPOINT || ""; + const repository = process.env.REPOSITORY_NAME || ""; const client = new ContainerRepositoryClient(endpoint, repository, new DefaultAzureCredential()); await getProperties(client); await listTags(client); + const artifacts = await listArtifacts(client); - if (artifacts?.length) { + + if (artifacts && artifacts.length) { const digest = artifacts[0].digest; if (digest) { await getArtifactProperties(client, digest); @@ -26,6 +30,11 @@ export async function main() { await deleteArtifact(client, digest); } } + + // Advanced: listing by pages + const pageSize = 2; + await listTagsByPages(client, pageSize); + await listArtifactsByPages(client, pageSize); } async function listTags(client: ContainerRepositoryClient) { @@ -37,9 +46,11 @@ async function listTags(client: ContainerRepositoryClient) { console.log(` created on: ${tag.createdOn}`); console.log(` last updated on: ${tag.lastUpdatedOn}`); } +} - console.log(" by pages"); - const pages = client.listTags().byPage({ maxPageSize: 2 }); +async function listTagsByPages(client: ContainerRepositoryClient, pagesSize: number) { + console.log("Listing tags by pages"); + const pages = client.listTags().byPage({ maxPageSize: pagesSize }); let result = await pages.next(); while (!result.done) { console.log(" -- page -- "); @@ -67,8 +78,12 @@ async function listArtifacts( console.log(` last updated on: ${artifact.lastUpdatedOn}`); } - console.log(" by pages"); - const pages = client.listRegistryArtifacts().byPage({ maxPageSize: 2 }); + return artifacts; +} + +async function listArtifactsByPages(client: any, pageSize: number) { + console.log("Listing artifacts by pages"); + const pages = client.listRegistryArtifacts().byPage({ maxPageSize: pageSize }); let result = await pages.next(); while (!result.done) { console.log(" -- page -- "); @@ -80,8 +95,6 @@ async function listArtifacts( } result = await pages.next(); } - - return artifacts; } async function getProperties(client: ContainerRepositoryClient) { diff --git a/sdk/containerregistry/container-registry/samples/typescript/tsconfig.json b/sdk/containerregistry/container-registry/samples/v1/typescript/tsconfig.json similarity index 65% rename from sdk/containerregistry/container-registry/samples/typescript/tsconfig.json rename to sdk/containerregistry/container-registry/samples/v1/typescript/tsconfig.json index 5ed056486b40..416c2dd82e00 100644 --- a/sdk/containerregistry/container-registry/samples/typescript/tsconfig.json +++ b/sdk/containerregistry/container-registry/samples/v1/typescript/tsconfig.json @@ -1,16 +1,17 @@ { "compilerOptions": { + "target": "ES2018", "module": "commonjs", "moduleResolution": "node", - + "resolveJsonModule": true, + "esModuleInterop": true, "allowSyntheticDefaultImports": true, - "strict": true, "alwaysStrict": true, - "outDir": "dist", "rootDir": "src" }, - "include": ["src/**.ts"], - "exclude": ["node_modules"] + "include": [ + "src/**.ts" + ] } diff --git a/sdk/containerregistry/container-registry/tsconfig.json b/sdk/containerregistry/container-registry/tsconfig.json index 9784ff1f1ed3..1989b61b88a3 100644 --- a/sdk/containerregistry/container-registry/tsconfig.json +++ b/sdk/containerregistry/container-registry/tsconfig.json @@ -2,16 +2,10 @@ "extends": "../../../tsconfig.package", "compilerOptions": { "outDir": "./dist-esm", - "declarationDir": "./types" + "declarationDir": "./types", + "paths": { + "@azure/container-registry": ["./src/index"] + } }, - "exclude": [ - "node_modules", - "types", - "temp", - "browser", - "dist", - "dist-samples", - "dist-esm", - "./samples/**/*.ts" - ] + "include": ["src/**/*.ts", "test/**/*.ts", "samples-dev/**/*.ts"] }