Skip to content

Commit 7073c0c

Browse files
author
Joanna Grycz
committed
feat: create_batch_custom_network
1 parent 7c4dc84 commit 7073c0c

File tree

2 files changed

+195
-0
lines changed

2 files changed

+195
-0
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 = 'batch-custom-network';
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+
callCreateBatchCustomNetwork();
128+
// [END batch_create_custom_network]
129+
}
130+
131+
main().catch(err => {
132+
console.error(err.message);
133+
process.exitCode = 1;
134+
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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('assert');
21+
const {describe, it} = require('mocha');
22+
const cp = require('child_process');
23+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
24+
const cwd = path.join(__dirname, '..');
25+
const {BatchServiceClient} = require('@google-cloud/batch').v1;
26+
const {deleteJob} = require('./batchClient_operations');
27+
const batchClient = new BatchServiceClient();
28+
29+
describe('Create batch custom network', async () => {
30+
const runnableName = 'batch-custom-network';
31+
const region = 'europe-central2';
32+
let projectId;
33+
34+
before(async () => {
35+
projectId = await batchClient.getProjectId();
36+
});
37+
38+
after(async () => {
39+
await deleteJob(batchClient, projectId, region, runnableName);
40+
});
41+
42+
it('should create a new job with custom network', async () => {
43+
const expectedNetwork = {
44+
networkInterfaces: [
45+
{
46+
network: 'global/networks/test-network',
47+
noExternalIpAddress: true,
48+
subnetwork: `regions/${region}/subnetworks/subnet`,
49+
},
50+
],
51+
};
52+
53+
const response = JSON.parse(
54+
execSync('node ./create/create_batch_custom_network.js', {
55+
cwd,
56+
})
57+
);
58+
59+
assert.deepEqual(response.allocationPolicy.network, expectedNetwork);
60+
});
61+
});

0 commit comments

Comments
 (0)