diff --git a/dataproc/createCluster.js b/dataproc/createCluster.js new file mode 100644 index 0000000000..1254da0aba --- /dev/null +++ b/dataproc/createCluster.js @@ -0,0 +1,63 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code for creating a Cloud Dataproc cluster with the Node.js Client Library + +function main(projectId, region, clusterName) { + // [START create_cluster] + const dataproc = require('@google-cloud/dataproc'); + + // Create a client with the endpoint set to the desired cluster region + const client = new dataproc.v1.ClusterControllerClient({ + apiEndpoint: `${region}-dataproc.googleapis.com`, + }); + + async function createCluster() { + // TODO(developer): Uncomment and set the following variables + // project_id = 'YOUR_PROJECT_ID' + // region = 'YOUR_CLUSTER_REGION' + // cluster_name = 'YOUR_CLUSTER_NAME' + + // Create your cluster config + const request = { + projectId: projectId, + region: region, + cluster: { + clusterName: clusterName, + config: { + masterConfig: { + numInstances: 1, + machineTypeUri: 'n1-standard-1', + }, + workerConfig: { + numInstances: 2, + machineTypeUri: 'n1-standard-1', + }, + }, + }, + }; + + // Create our cluster + const [operation] = await client.createCluster(request); + const [response] = await operation.promise(); + + // Output the response + console.log(response); + // [END create_cluster] + } + + createCluster(); +} + +main(...process.argv.slice(2)); diff --git a/dataproc/package.json b/dataproc/package.json index ed38f3380c..1c003b896f 100644 --- a/dataproc/package.json +++ b/dataproc/package.json @@ -11,13 +11,14 @@ "repository": "googleapis/nodejs-dataproc", "private": true, "scripts": { - "test": "mocha system-test --timeout 60000" + "test": "mocha system-test --timeout 600000" }, "dependencies": { "@google-cloud/dataproc": "^1.4.1" }, "devDependencies": { "chai": "^4.2.0", - "mocha": "^6.0.0" + "mocha": "^6.0.0", + "uuid": "^3.3.3" } } diff --git a/dataproc/system-test/createCluster.test.js b/dataproc/system-test/createCluster.test.js new file mode 100644 index 0000000000..c8547a30d7 --- /dev/null +++ b/dataproc/system-test/createCluster.test.js @@ -0,0 +1,48 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const cp = require('child_process'); +const uuid = require('uuid'); + +const region = 'us-central1'; +const clusterName = `test-${uuid()}`; + +const dataproc = require('@google-cloud/dataproc'); +const client = new dataproc.v1.ClusterControllerClient({ + apiEndpoint: `${region}-dataproc.googleapis.com`, +}); + +const projectId = process.env.GCLOUD_PROJECT; + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +describe('create a dataproc cluster', () => { + it('should create a dataproc cluster', async () => { + const stdout = execSync( + `node createCluster.js "${projectId}" "${region}" "${clusterName}"` + ); + assert.match(stdout, /clusterUuid/); + }); + + after(async () => { + await client.deleteCluster({ + projectId: projectId, + region: region, + clusterName: clusterName, + }); + }); +});