1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | Object.defineProperty(exports, "__esModule", { value: true });
|
24 | exports.convertInvoker = exports.serviceAccountFromShorthand = exports.convertIfPresent = exports.copyIfPresent = exports.durationFromSeconds = void 0;
|
25 | const params_1 = require("../params");
|
26 |
|
27 | function durationFromSeconds(s) {
|
28 | return `${s}s`;
|
29 | }
|
30 | exports.durationFromSeconds = durationFromSeconds;
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | function copyIfPresent(dest, src, ...fields) {
|
37 | if (!src) {
|
38 | return;
|
39 | }
|
40 | for (const field of fields) {
|
41 | if (!Object.prototype.hasOwnProperty.call(src, field)) {
|
42 | continue;
|
43 | }
|
44 | dest[field] = src[field];
|
45 | }
|
46 | }
|
47 | exports.copyIfPresent = copyIfPresent;
|
48 | function convertIfPresent(dest, src, destField, srcField, converter = (from) => {
|
49 | return from;
|
50 | }) {
|
51 | if (!src) {
|
52 | return;
|
53 | }
|
54 | if (!Object.prototype.hasOwnProperty.call(src, srcField)) {
|
55 | return;
|
56 | }
|
57 | dest[destField] = converter(src[srcField]);
|
58 | }
|
59 | exports.convertIfPresent = convertIfPresent;
|
60 | function serviceAccountFromShorthand(serviceAccount) {
|
61 | if (serviceAccount === "default") {
|
62 | return null;
|
63 | }
|
64 | else if (serviceAccount instanceof params_1.Expression) {
|
65 | return serviceAccount;
|
66 | }
|
67 | else if (serviceAccount.endsWith("@")) {
|
68 | if (!process.env.GCLOUD_PROJECT) {
|
69 | throw new Error(`Unable to determine email for service account '${serviceAccount}' because process.env.GCLOUD_PROJECT is not set.`);
|
70 | }
|
71 | return `${serviceAccount}${process.env.GCLOUD_PROJECT}.iam.gserviceaccount.com`;
|
72 | }
|
73 | else if (serviceAccount.includes("@")) {
|
74 | return serviceAccount;
|
75 | }
|
76 | else {
|
77 | throw new Error(`Invalid option for serviceAccount: '${serviceAccount}'. Valid options are 'default', a service account email, or '{serviceAccountName}@'`);
|
78 | }
|
79 | }
|
80 | exports.serviceAccountFromShorthand = serviceAccountFromShorthand;
|
81 | function convertInvoker(invoker) {
|
82 | if (typeof invoker === "string") {
|
83 | invoker = [invoker];
|
84 | }
|
85 | if (invoker.length === 0) {
|
86 | throw new Error("Invalid option for invoker: Must be a non-empty array.");
|
87 | }
|
88 | if (invoker.find((inv) => inv.length === 0)) {
|
89 | throw new Error("Invalid option for invoker: Must be a non-empty string.");
|
90 | }
|
91 | if (invoker.length > 1 && invoker.find((inv) => inv === "public" || inv === "private")) {
|
92 | throw new Error("Invalid option for invoker: Cannot have 'public' or 'private' in an array of service accounts.");
|
93 | }
|
94 | return invoker;
|
95 | }
|
96 | exports.convertInvoker = convertInvoker;
|