From 672e37845088cbf866d350ec9dfab3c4fe95dcb2 Mon Sep 17 00:00:00 2001 From: Ying Chen Date: Tue, 28 Feb 2023 01:21:40 +0800 Subject: [PATCH] [Component/Pipeline]: Add Typescript/JavaScript samples for SDKv2 (#2080) * update * update * [PipelineJob]: Add Typescript/JavaScript samples for SDKv2 (#2087) * add sample * update * update * fix indent * Update sdk/typescript/src/resources/jobs/pipelines/pipelineJobCreateOrUpdateSample.ts Co-authored-by: guanghechen <42513619+guanghechen@users.noreply.github.com> * update gitignore * update folder structure * remove not supported delete sample --------- Co-authored-by: guanghechen <42513619+guanghechen@users.noreply.github.com> Co-authored-by: Clement Wang --------- Co-authored-by: Ying Chen <2601502859@qq.com> Co-authored-by: Han Wang Co-authored-by: guanghechen <42513619+guanghechen@users.noreply.github.com> Co-authored-by: Clement Wang --- .gitignore | 1 + sdk/typescript/package.json | 4 +- .../componentVersionsCreateOrUpdateSample.ts | 85 +++++++++++++++++++ .../component/componentVersionsGetSample.ts | 52 ++++++++++++ .../component/componentVersionsListSample.ts | 55 ++++++++++++ .../pipelineJobCreateOrUpdateSample.ts | 75 ++++++++++++++++ 6 files changed, 270 insertions(+), 2 deletions(-) create mode 100644 sdk/typescript/src/assets/component/componentVersionsCreateOrUpdateSample.ts create mode 100644 sdk/typescript/src/assets/component/componentVersionsGetSample.ts create mode 100644 sdk/typescript/src/assets/component/componentVersionsListSample.ts create mode 100644 sdk/typescript/src/jobs/pipelines/pipelineJobCreateOrUpdateSample.ts diff --git a/.gitignore b/.gitignore index 009b6af790..c25140158f 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ config.json *.log scratch/* config-amlexamples.json +node_modules \ No newline at end of file diff --git a/sdk/typescript/package.json b/sdk/typescript/package.json index 4b0e871748..62295575d9 100644 --- a/sdk/typescript/package.json +++ b/sdk/typescript/package.json @@ -31,8 +31,8 @@ }, "homepage": "https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/machinelearning/arm-machinelearning", "dependencies": { - "@azure/arm-machinelearning": "file:../azure-arm-machinelearning-2.1.0/package", - "@azure/identity": "^2.1.0-beta.2", + "@azure/arm-machinelearning": "^2.1.1", + "@azure/identity": "^2.1.0", "@azure/keyvault-secrets": "latest", "dotenv": "latest" }, diff --git a/sdk/typescript/src/assets/component/componentVersionsCreateOrUpdateSample.ts b/sdk/typescript/src/assets/component/componentVersionsCreateOrUpdateSample.ts new file mode 100644 index 0000000000..d9dabf0774 --- /dev/null +++ b/sdk/typescript/src/assets/component/componentVersionsCreateOrUpdateSample.ts @@ -0,0 +1,85 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import { + ComponentVersion, +} from "@azure/arm-machinelearning"; +import { client, getEnvironmentVariable } from "../../utils"; + +// Load the .env file if it exists +import * as dotenv from "dotenv"; +dotenv.config(); + +const resourceGroupName = getEnvironmentVariable("RESOURCEGROUP_NAME"); +const workspaceName = getEnvironmentVariable("WORKSPACE_NAME"); + +/** + * This sample demonstrates how to Create or update version. + * + * @summary Create or update version. + * x-ms-original-file: specification/machinelearningservices/resource-manager/Microsoft.MachineLearningServices/stable/2022-10-01/examples/ComponentVersion/createOrUpdate.json + */ +export async function createOrUpdateComponentVersion() : Promise { + const name = "command_component_basic"; + const version = "0.0.1"; + const body: ComponentVersion = { + properties: { + description: "This is the basic command component", + componentSpec: { + 'command': 'echo Hello World & echo $[[${{inputs.component_in_number}}]] & echo $[[${{inputs.component_in_path}}]] & echo ${{outputs.component_out_path}} > ${{outputs.component_out_path}}/component_in_number', + 'environment': 'azureml:AzureML-sklearn-0.24-ubuntu18.04-py37-cpu:1', + 'name': 'command_component_basic', + 'description': 'This is the basic command component', + 'tags': { 'tag': 'tagvalue', 'owner': 'sdkteam' }, + 'version': '0.0.1', + '$schema': 'https://azuremlschemas.azureedge.net/development/commandComponent.schema.json', + 'display_name': 'CommandComponentBasic', + 'is_deterministic': true, + 'inputs': { + 'component_in_number': { 'type': 'number', 'optional': true, 'default': '10.99', 'description': 'A number' }, + 'component_in_path': { 'type': 'uri_folder', 'optional': true, 'description': 'A path' } + }, + 'outputs': { 'component_out_path': { 'type': 'uri_folder' } }, + 'type': 'command', + '_source': 'YAML.COMPONENT' + }, + isAnonymous: false, + properties: {}, + tags: { 'tag': 'tagvalue', 'owner': 'sdkteam' } + } + }; + // const credential = new DefaultAzureCredential(); + // const client = new AzureMachineLearningWorkspaces(credential, subscriptionId); + try { + console.log("Create or update component version ...") + const componentVersionsCreateOrUpdateResponse = await client.componentVersions.createOrUpdate( + resourceGroupName, + workspaceName, + name, + version, + body + ); + console.log(componentVersionsCreateOrUpdateResponse); + console.log(`Created or update component ${componentVersionsCreateOrUpdateResponse.name} successfully`); + } catch (err: any) { + console.log( + `errorMessage - ${err.message}\n` + ) + } +} + +// createOrUpdateComponentVersion().catch(console.error); +export async function main(): Promise { + // This sample uses DefaultAzureCredential, which supports a number of authentication mechanisms. + // See https://docs.microsoft.com/javascript/api/overview/azure/identity-readme?view=azure-node-latest for more information + // about DefaultAzureCredential and the other credentials that are available for use. + await createOrUpdateComponentVersion(); +} + +main().catch((error: any) => { + console.error("An error occurred:", error); + console.log("error code: ", error.code); + console.log("error message: ", error.message); + console.log("error stack: ", error.stack); + process.exit(1); +}); \ No newline at end of file diff --git a/sdk/typescript/src/assets/component/componentVersionsGetSample.ts b/sdk/typescript/src/assets/component/componentVersionsGetSample.ts new file mode 100644 index 0000000000..c5aed27558 --- /dev/null +++ b/sdk/typescript/src/assets/component/componentVersionsGetSample.ts @@ -0,0 +1,52 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import { client, getEnvironmentVariable } from "../../utils"; +// Load the .env file if it exists +import * as dotenv from "dotenv"; +dotenv.config(); + +const resourceGroupName = getEnvironmentVariable("RESOURCEGROUP_NAME"); +const workspaceName = getEnvironmentVariable("WORKSPACE_NAME"); + +/** + * This sample demonstrates how to Get version. + * + * @summary Get version. + * x-ms-original-file: specification/machinelearningservices/resource-manager/Microsoft.MachineLearningServices/stable/2022-10-01/examples/ComponentVersion/get.json + */ +async function getComponentVersion(): Promise { + const name = "command_component_basic"; + const version = "0.0.1"; + try { + console.log("Get component ...") + const componentVersionsGetResponse = await client.componentVersions.get( + resourceGroupName, + workspaceName, + name, + version + ); + console.log(componentVersionsGetResponse); + console.log(`Get component ${componentVersionsGetResponse.name} successfully`); + } catch (err: any) { + console.log( + `errorMessage - ${err.message}\n` + ) + } +} + +// getComponentVersion().catch(console.error); +export async function main(): Promise { + // This sample uses DefaultAzureCredential, which supports a number of authentication mechanisms. + // See https://docs.microsoft.com/javascript/api/overview/azure/identity-readme?view=azure-node-latest for more information + // about DefaultAzureCredential and the other credentials that are available for use. + await getComponentVersion(); +} + +main().catch((error: any) => { + console.error("An error occurred:", error); + console.log("error code: ", error.code); + console.log("error message: ", error.message); + console.log("error stack: ", error.stack); + process.exit(1); +}); \ No newline at end of file diff --git a/sdk/typescript/src/assets/component/componentVersionsListSample.ts b/sdk/typescript/src/assets/component/componentVersionsListSample.ts new file mode 100644 index 0000000000..4d9fac0f8f --- /dev/null +++ b/sdk/typescript/src/assets/component/componentVersionsListSample.ts @@ -0,0 +1,55 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. +import { + ComponentVersion, + ComponentVersionsListOptionalParams, +} from "@azure/arm-machinelearning"; + +import { client, getEnvironmentVariable } from "../../utils"; +// Load the .env file if it exists +import * as dotenv from "dotenv"; +dotenv.config(); + +const resourceGroupName = getEnvironmentVariable("RESOURCEGROUP_NAME"); +const workspaceName = getEnvironmentVariable("WORKSPACE_NAME"); + +/** + * This sample demonstrates how to List component versions. + * + * @summary List component versions. + * x-ms-original-file: specification/machinelearningservices/resource-manager/Microsoft.MachineLearningServices/stable/2022-10-01/examples/ComponentVersion/list.json + */ +async function listComponentVersion(): Promise { + const name = "command_component_basic"; + const orderBy = "createdtime desc"; + const top = 1; + const options: ComponentVersionsListOptionalParams = { orderBy, top }; + const resArray = new Array(); + for await (let componentItem of client.componentVersions.list( + resourceGroupName, + workspaceName, + name, + options + )) { + resArray.push(componentItem); + console.log(componentItem); + } + console.log(resArray); +} + +// listComponentVersion().catch(console.error); +export async function main(): Promise { + // This sample uses DefaultAzureCredential, which supports a number of authentication mechanisms. + // See https://docs.microsoft.com/javascript/api/overview/azure/identity-readme?view=azure-node-latest for more information + // about DefaultAzureCredential and the other credentials that are available for use. + console.log("Listing the Component"); + await listComponentVersion(); +} + +main().catch((error) => { + console.error("An error occurred:", error); + console.log("error code: ", error.code); + console.log("error message: ", error.message); + console.log("error stack: ", error.stack); + process.exit(1); +}); \ No newline at end of file diff --git a/sdk/typescript/src/jobs/pipelines/pipelineJobCreateOrUpdateSample.ts b/sdk/typescript/src/jobs/pipelines/pipelineJobCreateOrUpdateSample.ts new file mode 100644 index 0000000000..1145e13f73 --- /dev/null +++ b/sdk/typescript/src/jobs/pipelines/pipelineJobCreateOrUpdateSample.ts @@ -0,0 +1,75 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import { + JobBase, +} from "@azure/arm-machinelearning"; +import { client, getEnvironmentVariable } from "../../utils"; +import { createOrUpdateComponentVersion } from "../../assets/component/componentVersionsCreateOrUpdateSample"; + +// Load the .env file if it exists +import * as dotenv from "dotenv"; +dotenv.config(); + +const resourceGroupName = getEnvironmentVariable("RESOURCEGROUP_NAME"); +const workspaceName = getEnvironmentVariable("WORKSPACE_NAME"); + +/** + * This sample demonstrates how to Create or update pipeline job. + * + * @summary Create or update version. + */ +async function createOrUpdatePipelineJob(): Promise { + // create a simple component + await createOrUpdateComponentVersion(); + const name = "simple_pipeline_job"; + const body: JobBase = { + properties: { + description: "This is the basic pipeline job", + computeId: "cpu-cluster", + jobType: "Pipeline", + jobs: { + "node1": { + "name": "node1", + "type": "command", + "componentId": "command_component_basic:0.0.1" + } + }, + properties: {}, + tags: { 'tag': 'tagvalue', 'owner': 'sdkteam' } + } + }; + // const credential = new DefaultAzureCredential(); + // const client = new AzureMachineLearningWorkspaces(credential, subscriptionId); + try { + console.log("Create or update pipeline job ...") + const pipelineJobCreateOrUpdateResponse = await client.jobs.createOrUpdate( + resourceGroupName, + workspaceName, + name, + body + ); + console.log(pipelineJobCreateOrUpdateResponse); + console.log(`Created or update pipeline job ${pipelineJobCreateOrUpdateResponse.name} successfully`); + } catch (err: any) { + console.log( + `errorMessage - ${err.message}\n` + ) + } +} + +// createOrUpdateComponentVersion().catch(console.error); +export async function main(): Promise { + // This sample uses DefaultAzureCredential, which supports a number of authentication mechanisms. + // See https://docs.microsoft.com/javascript/api/overview/azure/identity-readme?view=azure-node-latest for more information + // about DefaultAzureCredential and the other credentials that are available for use. + await createOrUpdatePipelineJob(); +} + +main().catch((error: any) => { + console.error("An error occurred:", error); + console.log("error code: ", error.code); + console.log("error message: ", error.message); + console.log("error stack: ", error.stack); + process.exit(1); +}); \ No newline at end of file