Skip to content

Commit 3f3969f

Browse files
authored
Merge branch 'main' into batch-custom-events
2 parents f0000d6 + e222d9d commit 3f3969f

9 files changed

+175
-7
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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_using_secret_manager]
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-job-secret-manager';
39+
// The name of the secret variable.
40+
// This variable name is specified in this job's runnables
41+
// and is accessible to all of the runnables that are in the same environment.
42+
const secretVariableName = 'secretVariableName';
43+
// The name of an existing Secret Manager secret.
44+
const secretName = 'secretName';
45+
// The version of the specified secret that contains the data you want to pass to the job.
46+
// This can be the version number or latest.
47+
const version = 'version';
48+
49+
// Define what will be done as part of the job.
50+
const runnable = new batch.Runnable({
51+
script: new batch.Runnable.Script({
52+
commands: ['-c', `echo This is the secret: ${secretVariableName}`],
53+
}),
54+
});
55+
56+
// Construct the resource path to the secret's version.
57+
const secretValue = `projects/${projectId}/secrets/${secretName}/versions/${version}`;
58+
59+
// Set the secret as an environment variable.
60+
const environment = new batch.Environment();
61+
environment.secretVariables[secretVariableName] = secretValue;
62+
63+
const task = new batch.TaskSpec({
64+
runnables: [runnable],
65+
environment,
66+
maxRetryCount: 2,
67+
maxRunDuration: {seconds: 3600},
68+
});
69+
70+
// Tasks are grouped inside a job using TaskGroups.
71+
const group = new batch.TaskGroup({
72+
taskCount: 3,
73+
taskSpec: task,
74+
});
75+
76+
const job = new batch.Job({
77+
name: jobName,
78+
taskGroups: [group],
79+
labels: {env: 'testing', type: 'script'},
80+
// We use Cloud Logging as it's an option available out of the box
81+
logsPolicy: new batch.LogsPolicy({
82+
destination: batch.LogsPolicy.Destination.CLOUD_LOGGING,
83+
}),
84+
});
85+
86+
// The job's parent is the project and region in which the job will run
87+
const parent = `projects/${projectId}/locations/${region}`;
88+
89+
async function callCreateUsingSecretManager() {
90+
// Construct request
91+
const request = {
92+
parent,
93+
jobId: jobName,
94+
job,
95+
};
96+
97+
// Run request
98+
const [response] = await batchClient.createJob(request);
99+
console.log(JSON.stringify(response));
100+
}
101+
102+
callCreateUsingSecretManager();
103+
// [END batch_create_using_secret_manager]
104+
}
105+
106+
main().catch(err => {
107+
console.error(err);
108+
process.exitCode = 1;
109+
});

batch/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@
2323
"@types/mocha": "^10.0.7",
2424
"c8": "^10.0.0",
2525
"mocha": "^10.0.0",
26-
"nanoid": "^5.0.0"
26+
"nanoid": "^3.3.7"
2727
}
2828
}

batch/test/create_batch_notifications.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 Google LLC
2+
* Copyright 2024 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

batch/test/create_batch_using_service_account.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 Google LLC
2+
* Copyright 2024 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

batch/test/create_gpu_job.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 Google LLC
2+
* Copyright 2024 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

batch/test/create_gpu_job_n1.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 Google LLC
2+
* Copyright 2024 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

batch/test/create_local_ssd_job.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 Google LLC
2+
* Copyright 2024 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

batch/test/create_persistent_disk_job.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 Google LLC
2+
* Copyright 2024 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 using secret manager', async () => {
30+
const jobName = 'batch-job-secret-manager';
31+
const region = 'europe-central2';
32+
33+
let projectId;
34+
35+
before(async () => {
36+
projectId = await batchClient.getProjectId();
37+
});
38+
39+
after(async () => {
40+
await deleteJob(batchClient, projectId, region, jobName);
41+
});
42+
43+
it('should create a new job using secret manager', async () => {
44+
const secretName = 'secretName';
45+
const version = 'version';
46+
const expectedSecretVariables = {
47+
secretVariableName: `projects/${projectId}/secrets/${secretName}/versions/${version}`,
48+
};
49+
50+
const response = execSync('node ./create/create_using_secret_manager.js', {
51+
cwd,
52+
});
53+
54+
assert.deepEqual(
55+
JSON.parse(response).taskGroups[0].taskSpec.environment.secretVariables,
56+
expectedSecretVariables
57+
);
58+
});
59+
});

0 commit comments

Comments
 (0)