Skip to content

Instantly share code, notes, and snippets.

@thoughtspeed7
Created January 27, 2018 21:12
Show Gist options
  • Save thoughtspeed7/1db0e2bd0d863afced516c3f787d94e6 to your computer and use it in GitHub Desktop.
Save thoughtspeed7/1db0e2bd0d863afced516c3f787d94e6 to your computer and use it in GitHub Desktop.
Google Cloud Functions Best Practices - Using Runtime Configurator to Manage Config Variables
// om namah shivay
// run gcloud beta functions deploy gcf-runtime-configurator --entry-point start --trigger-http
// to deploy this function to gcf using the gcloud tool
const start = (req, res) => {
let html;
if (!global.config) {
loadConfig()
.then(result => {
global.config = result;
html = 'config absent in global cache<br>';
html += 'successfully loaded config in global cache<br>';
html += `config: ${JSON.stringify(global.config)}`;
res.send(html);
})
.catch(err => {
html = 'config absent in global cache<br>';
html += 'error loading config in global cache<br>';
html += `error: ${err}`;
res.send(html);
});
} else {
html = 'config already present in global cache<br>';
html += `config: ${JSON.stringify(global.config)}`;
res.send(html);
}
};
const loadConfig = () => {
const google = require('googleapis');
const key = require('./runtime-configurator-credentials'); // make sure your json credentials file is present at this location
const jwtClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
['https://www.googleapis.com/auth/cloudruntimeconfig'], // an array of auth scopes
null
);
const runtimeConfig = google.runtimeconfig('v1beta1');
const projectId = 'replace-with-your-project-id'; // replace with your project id
const configName = 'replace-with-your-config-name'; // replace with your config name
return new Promise((resolve, reject) => {
jwtClient.authorize(function (err, tokens) {
if (err) {
console.error(err);
reject(err);
} else {
console.log('jwt authorization successful');
}
runtimeConfig.projects.configs.variables.list({
auth: jwtClient,
parent: `projects/${projectId}/configs/${configName}`,
returnValues: true
},
function (err, response) {
if (err) {
console.error(err);
reject(err);
} else {
console.log(`response: ${JSON.stringify(response.data)}`);
resolve(response.data);
}
});
});
});
};
module.exports = {
start
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment