Skip to content

Commit

Permalink
docs(sample): added create cluster sample for cloud dataproc (#266)
Browse files Browse the repository at this point in the history
* Added create cluster sample for Cloud Dataproc

* cleaned up dependencies

* Updated READMEs

* ignore createCluster until it lands
  • Loading branch information
bradmiro authored Nov 14, 2019
1 parent 605413e commit ce3e239
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 2 deletions.
63 changes: 63 additions & 0 deletions dataproc/createCluster.js
Original file line number Diff line number Diff line change
@@ -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));
5 changes: 3 additions & 2 deletions dataproc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
48 changes: 48 additions & 0 deletions dataproc/system-test/createCluster.test.js
Original file line number Diff line number Diff line change
@@ -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,
});
});
});

0 comments on commit ce3e239

Please sign in to comment.