Skip to content

Commit 6153f37

Browse files
bradmiroNimJay
authored andcommitted
docs(sample): added create cluster sample for cloud dataproc (#266)
* Added create cluster sample for Cloud Dataproc * cleaned up dependencies * Updated READMEs * ignore createCluster until it lands
1 parent f9cb783 commit 6153f37

3 files changed

Lines changed: 114 additions & 2 deletions

File tree

dataproc/createCluster.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Code for creating a Cloud Dataproc cluster with the Node.js Client Library
16+
17+
function main(projectId, region, clusterName) {
18+
// [START create_cluster]
19+
const dataproc = require('@google-cloud/dataproc');
20+
21+
// Create a client with the endpoint set to the desired cluster region
22+
const client = new dataproc.v1.ClusterControllerClient({
23+
apiEndpoint: `${region}-dataproc.googleapis.com`,
24+
});
25+
26+
async function createCluster() {
27+
// TODO(developer): Uncomment and set the following variables
28+
// project_id = 'YOUR_PROJECT_ID'
29+
// region = 'YOUR_CLUSTER_REGION'
30+
// cluster_name = 'YOUR_CLUSTER_NAME'
31+
32+
// Create your cluster config
33+
const request = {
34+
projectId: projectId,
35+
region: region,
36+
cluster: {
37+
clusterName: clusterName,
38+
config: {
39+
masterConfig: {
40+
numInstances: 1,
41+
machineTypeUri: 'n1-standard-1',
42+
},
43+
workerConfig: {
44+
numInstances: 2,
45+
machineTypeUri: 'n1-standard-1',
46+
},
47+
},
48+
},
49+
};
50+
51+
// Create our cluster
52+
const [operation] = await client.createCluster(request);
53+
const [response] = await operation.promise();
54+
55+
// Output the response
56+
console.log(response);
57+
// [END create_cluster]
58+
}
59+
60+
createCluster();
61+
}
62+
63+
main(...process.argv.slice(2));

dataproc/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
"repository": "googleapis/nodejs-dataproc",
1212
"private": true,
1313
"scripts": {
14-
"test": "mocha system-test --timeout 60000"
14+
"test": "mocha system-test --timeout 600000"
1515
},
1616
"dependencies": {
1717
"@google-cloud/dataproc": "^1.4.1"
1818
},
1919
"devDependencies": {
2020
"chai": "^4.2.0",
21-
"mocha": "^6.0.0"
21+
"mocha": "^6.0.0",
22+
"uuid": "^3.3.3"
2223
}
2324
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const cp = require('child_process');
19+
const uuid = require('uuid');
20+
21+
const region = 'us-central1';
22+
const clusterName = `test-${uuid()}`;
23+
24+
const dataproc = require('@google-cloud/dataproc');
25+
const client = new dataproc.v1.ClusterControllerClient({
26+
apiEndpoint: `${region}-dataproc.googleapis.com`,
27+
});
28+
29+
const projectId = process.env.GCLOUD_PROJECT;
30+
31+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
32+
33+
describe('create a dataproc cluster', () => {
34+
it('should create a dataproc cluster', async () => {
35+
const stdout = execSync(
36+
`node createCluster.js "${projectId}" "${region}" "${clusterName}"`
37+
);
38+
assert.match(stdout, /clusterUuid/);
39+
});
40+
41+
after(async () => {
42+
await client.deleteCluster({
43+
projectId: projectId,
44+
region: region,
45+
clusterName: clusterName,
46+
});
47+
});
48+
});

0 commit comments

Comments
 (0)