Skip to content

Commit 933522c

Browse files
authored
feat: create_batch_custom_network (#3793)
* feat: create_batch_custom_network * Add missing await and strict assertion in tests * Job name adjusted * Reorder imports and throw job deleting error * Delete batchClient_operations
1 parent d7e0845 commit 933522c

16 files changed

+288
-152
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
async function main() {
20+
// [START batch_create_custom_network]
21+
// Imports the Batch library
22+
const batchLib = require('@google-cloud/batch');
23+
const batch = batchLib.protos.google.cloud.batch.v1;
24+
25+
// Instantiates a client
26+
const batchClient = new batchLib.v1.BatchServiceClient();
27+
28+
/**
29+
* TODO(developer): Update these variables before running the sample.
30+
*/
31+
// Project ID or project number of the Google Cloud project you want to use.
32+
const projectId = await batchClient.getProjectId();
33+
// Name of the region you want to use to run the job. Regions that are
34+
// available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
35+
const region = 'europe-central2';
36+
// The name of the job that will be created.
37+
// It needs to be unique for each project and region pair.
38+
const jobName = 'example-job';
39+
// The name of a VPC network in the current project or a Shared VPC network that is hosted by
40+
// or shared with the current project.
41+
const network = 'global/networks/test-network';
42+
// The name of a subnetwork that is part of the VPC network and is located
43+
// in the same region as the VMs for the job.
44+
const subnetwork = `regions/${region}/subnetworks/subnet`;
45+
46+
// Define what will be done as part of the job.
47+
const runnable = new batch.Runnable({
48+
script: new batch.Runnable.Script({
49+
commands: ['-c', 'echo Hello world! This is task ${BATCH_TASK_INDEX}.'],
50+
}),
51+
});
52+
53+
// Specify what resources are requested by each task.
54+
const computeResource = new batch.ComputeResource({
55+
// In milliseconds per cpu-second. This means the task requires 50% of a single CPUs.
56+
cpuMilli: 500,
57+
// In MiB.
58+
memoryMib: 16,
59+
});
60+
61+
const task = new batch.TaskSpec({
62+
runnables: [runnable],
63+
computeResource,
64+
maxRetryCount: 2,
65+
maxRunDuration: {seconds: 3600},
66+
});
67+
68+
// Tasks are grouped inside a job using TaskGroups.
69+
const group = new batch.TaskGroup({
70+
taskCount: 3,
71+
taskSpec: task,
72+
});
73+
74+
// Specify VPC network and a subnet for Allocation Policy
75+
const networkPolicy = new batch.AllocationPolicy.NetworkPolicy({
76+
networkInterfaces: [
77+
new batch.AllocationPolicy.NetworkInterface({
78+
// Set the network name
79+
network,
80+
// Set the subnetwork name
81+
subnetwork,
82+
// Blocks external access for all VMs
83+
noExternalIpAddress: true,
84+
}),
85+
],
86+
});
87+
88+
// Policies are used to define on what kind of virtual machines the tasks will run on.
89+
// In this case, we tell the system to use "e2-standard-4" machine type.
90+
// Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
91+
const instancePolicy = new batch.AllocationPolicy.InstancePolicy({
92+
machineType: 'e2-standard-4',
93+
});
94+
95+
const allocationPolicy = new batch.AllocationPolicy.InstancePolicyOrTemplate({
96+
policy: instancePolicy,
97+
network: networkPolicy,
98+
});
99+
100+
const job = new batch.Job({
101+
name: jobName,
102+
taskGroups: [group],
103+
labels: {env: 'testing', type: 'script'},
104+
allocationPolicy,
105+
// We use Cloud Logging as it's an option available out of the box
106+
logsPolicy: new batch.LogsPolicy({
107+
destination: batch.LogsPolicy.Destination.CLOUD_LOGGING,
108+
}),
109+
});
110+
111+
// The job's parent is the project and region in which the job will run
112+
const parent = `projects/${projectId}/locations/${region}`;
113+
114+
async function callCreateBatchCustomNetwork() {
115+
// Construct request
116+
const request = {
117+
parent,
118+
jobId: jobName,
119+
job,
120+
};
121+
122+
// Run request
123+
const [response] = await batchClient.createJob(request);
124+
console.log(JSON.stringify(response));
125+
}
126+
127+
await callCreateBatchCustomNetwork();
128+
// [END batch_create_custom_network]
129+
}
130+
131+
main().catch(err => {
132+
console.error(err.message);
133+
process.exitCode = 1;
134+
});

batch/test/advanced_creation.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,9 @@ const path = require('path');
1818
const cp = require('child_process');
1919
const {describe, it, before} = require('mocha');
2020
const {BatchServiceClient} = require('@google-cloud/batch').v1;
21-
const batchClient = new BatchServiceClient();
2221
const {Storage} = require('@google-cloud/storage');
23-
const storage = new Storage();
2422
const {ProjectsClient} = require('@google-cloud/resource-manager').v3;
25-
const resourcemanagerClient = new ProjectsClient();
2623
const compute = require('@google-cloud/compute');
27-
const instanceTemplatesClient = new compute.InstanceTemplatesClient();
28-
2924
// get a short ID for this test run that only contains characters that are valid in UUID
3025
// (a plain UUID won't do because we want the "test-job-js" prefix and that would exceed the length limit)
3126
const {customAlphabet} = require('nanoid');
@@ -36,6 +31,11 @@ const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
3631

3732
const cwd = path.join(__dirname, '..');
3833

34+
const batchClient = new BatchServiceClient();
35+
const storage = new Storage();
36+
const resourcemanagerClient = new ProjectsClient();
37+
const instanceTemplatesClient = new compute.InstanceTemplatesClient();
38+
3939
describe('Create jobs with container, template and bucket', () => {
4040
let projectId;
4141
let bucketName;

batch/test/batchClient_operations.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

batch/test/create_batch_custom_events.test.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,28 @@
1717
'use strict';
1818

1919
const path = require('path');
20-
const assert = require('assert');
20+
const assert = require('node:assert/strict');
2121
const {describe, it} = require('mocha');
22-
const cp = require('child_process');
23-
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2422
const cwd = path.join(__dirname, '..');
2523
const {BatchServiceClient} = require('@google-cloud/batch').v1;
26-
const {deleteJob} = require('./batchClient_operations');
27-
const batchClient = new BatchServiceClient();
24+
25+
const cp = require('child_process');
26+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2827

2928
describe('Create batch custom events job', async () => {
3029
const jobName = 'batch-custom-events-job';
3130
const region = 'europe-central2';
31+
const batchClient = new BatchServiceClient();
3232
let projectId;
3333

3434
before(async () => {
3535
projectId = await batchClient.getProjectId();
3636
});
3737

3838
after(async () => {
39-
await deleteJob(batchClient, projectId, region, jobName);
39+
await batchClient.deleteJob({
40+
name: `projects/${projectId}/locations/${region}/jobs/${jobName}`,
41+
});
4042
});
4143

4244
it('should create a new job with custom events', () => {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
const path = require('path');
20+
const assert = require('node:assert/strict');
21+
const {describe, it} = require('mocha');
22+
const cp = require('child_process');
23+
const {BatchServiceClient} = require('@google-cloud/batch').v1;
24+
25+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
26+
const cwd = path.join(__dirname, '..');
27+
28+
describe('Create batch custom network', async () => {
29+
const jobName = 'example-job';
30+
const region = 'europe-central2';
31+
const batchClient = new BatchServiceClient();
32+
let projectId;
33+
34+
before(async () => {
35+
projectId = await batchClient.getProjectId();
36+
});
37+
38+
after(async () => {
39+
await batchClient.deleteJob({
40+
name: `projects/${projectId}/locations/${region}/jobs/${jobName}`,
41+
});
42+
});
43+
44+
it('should create a new job with custom network', async () => {
45+
const expectedNetwork = {
46+
networkInterfaces: [
47+
{
48+
network: 'global/networks/test-network',
49+
noExternalIpAddress: true,
50+
subnetwork: `regions/${region}/subnetworks/subnet`,
51+
},
52+
],
53+
};
54+
55+
const response = JSON.parse(
56+
execSync('node ./create/create_batch_custom_network.js', {
57+
cwd,
58+
})
59+
);
60+
61+
assert.deepEqual(response.allocationPolicy.network, expectedNetwork);
62+
});
63+
});

batch/test/create_batch_labels_allocation.test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,28 @@
1717
'use strict';
1818

1919
const path = require('path');
20-
const assert = require('assert');
20+
const assert = require('node:assert/strict');
2121
const {describe, it} = require('mocha');
2222
const cp = require('child_process');
23+
const {BatchServiceClient} = require('@google-cloud/batch').v1;
24+
2325
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2426
const cwd = path.join(__dirname, '..');
25-
const {BatchServiceClient} = require('@google-cloud/batch').v1;
26-
const {deleteJob} = require('./batchClient_operations');
27-
const batchClient = new BatchServiceClient();
2827

2928
describe('Create batch labels allocation', async () => {
3029
const jobName = 'batch-labels-allocation-job';
3130
const region = 'europe-central2';
31+
const batchClient = new BatchServiceClient();
3232
let projectId;
3333

3434
before(async () => {
3535
projectId = await batchClient.getProjectId();
3636
});
3737

3838
after(async () => {
39-
await deleteJob(batchClient, projectId, region, jobName);
39+
await batchClient.deleteJob({
40+
name: `projects/${projectId}/locations/${region}/jobs/${jobName}`,
41+
});
4042
});
4143

4244
it('should create a new job with allocation policy labels', async () => {

batch/test/create_batch_labels_job.test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,28 @@
1717
'use strict';
1818

1919
const path = require('path');
20-
const assert = require('assert');
20+
const assert = require('node:assert/strict');
2121
const {describe, it} = require('mocha');
2222
const cp = require('child_process');
23+
const {BatchServiceClient} = require('@google-cloud/batch').v1;
24+
2325
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2426
const cwd = path.join(__dirname, '..');
25-
const {BatchServiceClient} = require('@google-cloud/batch').v1;
26-
const {deleteJob} = require('./batchClient_operations');
27-
const batchClient = new BatchServiceClient();
2827

2928
describe('Create batch labels for job', async () => {
3029
const jobName = 'batch-labels-job';
3130
const region = 'europe-central2';
31+
const batchClient = new BatchServiceClient();
3232
let projectId;
3333

3434
before(async () => {
3535
projectId = await batchClient.getProjectId();
3636
});
3737

3838
after(async () => {
39-
await deleteJob(batchClient, projectId, region, jobName);
39+
await batchClient.deleteJob({
40+
name: `projects/${projectId}/locations/${region}/jobs/${jobName}`,
41+
});
4042
});
4143

4244
it('should create a new job with labels', async () => {

batch/test/create_batch_labels_runnable.test.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,25 @@ const path = require('path');
2020
const assert = require('assert');
2121
const {describe, it} = require('mocha');
2222
const cp = require('child_process');
23+
const {BatchServiceClient} = require('@google-cloud/batch').v1;
24+
2325
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2426
const cwd = path.join(__dirname, '..');
25-
const {BatchServiceClient} = require('@google-cloud/batch').v1;
26-
const {deleteJob} = require('./batchClient_operations');
27-
const batchClient = new BatchServiceClient();
2827

2928
describe('Create batch labels runnable', async () => {
3029
const jobName = 'example-job';
3130
const region = 'us-central1';
31+
const batchClient = new BatchServiceClient();
3232
let projectId;
3333

3434
before(async () => {
3535
projectId = await batchClient.getProjectId();
3636
});
3737

3838
after(async () => {
39-
await deleteJob(batchClient, projectId, region, jobName);
39+
await batchClient.deleteJob({
40+
name: `projects/${projectId}/locations/${region}/jobs/${jobName}`,
41+
});
4042
});
4143

4244
it('should create a new job with labels for runnables', async () => {

0 commit comments

Comments
 (0)