-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathruntime-config.js
91 lines (83 loc) · 2.81 KB
/
runtime-config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const {google} = require('googleapis');
const runtimeConfig = google.runtimeconfig('v1beta1');
module.exports = {
getVariables: getVariables,
getVariable: getVariable,
};
/**
* runtimeConfig.getVariables
*
* @desc Reads a list of runtime config values
*
* @param {string} configName The config name of the variables to return
* @param {Array} variableNames The list of variable names to read
* @return {Promise} Promise that resolves an array of the variable values
*/
function getVariables(configName, variableNames) {
return Promise.all(variableNames.map(function(variableName) {
return getVariable(configName, variableName);
}));
}
/**
* runtimeConfig.getVariable
*
* @desc Reads a runtime config value
*
* @param {string} configName The config name of the variable to return
* @param {string} variableName The variable name of the variable to return
* @return {Promise} Promise that resolves the variable value
*/
function getVariable(configName, variableName) {
return new Promise(function(resolve, reject) {
auth().then(function(authClient) {
const projectId = process.env.GCLOUD_PROJECT;
const fullyQualifiedName = 'projects/' + projectId
+ '/configs/' + configName
+ '/variables/' + variableName;
runtimeConfig.projects.configs.variables.get({
auth: authClient,
name: fullyQualifiedName,
}, function(err, res) {
if (err) {
reject(err);
return;
}
const variable = res.data;
if (typeof variable.text !== 'undefined') {
resolve(variable.text);
} else if (typeof variable.value !== 'undefined') {
resolve(Buffer.from(variable.value, 'base64').toString());
} else {
reject(new Error('Property text or value not defined'));
}
});
});
});
}
/**
* auth
*
* @desc Authenticates using default credentials
*
* @return {Promise} Promise that resolves an authClient
*/
function auth() {
return new Promise(function(resolve, reject) {
google.auth.getApplicationDefault(function(err, authClient, projectId) {
if (err) {
reject(err);
return;
}
if (authClient.createScopedRequired
&& authClient.createScopedRequired()
) {
const scopes = [
'https://www.googleapis.com/auth/cloud-platform',
'https://www.googleapis.com/auth/cloudruntimeconfig',
];
authClient = authClient.createScoped(scopes);
}
resolve(authClient);
});
});
}