访问云功能内的标签

时间:2018-02-11 18:20:14

标签: node.js google-cloud-platform google-cloud-functions

当有问题的资源是Google云端功能时,有没有办法访问资源的标签?

我使用了测试功能并记录了process.envprocess个对象,我看不到我为该功能设置的任何标签。

USE CASE:用于配置目的(而不是重新部署)。

谢谢!

1 个答案:

答案 0 :(得分:5)

您可以使用googleapis: cloudfunctions Namespace,使用环境变量可以获得API所需的参数。以下脚本中使用functions/get docs

const google = require('googleapis');
const cloudfunctions = google.cloudfunctions('v1');

const projectId = process.env.GCLOUD_PROJECT;
const regionId = process.env.FUNCTION_REGION;
const functionName = process.env.FUNCTION_NAME;

function getFunctionDetails(callback) {
    google.auth.getApplicationDefault((err, authClient) => {
        if (err) {
            return callback(err);
        }

        if (authClient.createScopedRequired && authClient.createScopedRequired()) {
            authClient = authClient.createScoped([
                'https://www.googleapis.com/auth/cloudfunctions'
            ]);
        }

        cloudfunctions.projects.locations.functions.get({
            name: `projects/${projectId}/locations/${regionId}/functions/${functionName}`,
            auth: authClient
        }, (err, result) => {
            if (err) {
                return callback(err);
            }

            callback(null, result.data)
        });
    });
}

exports.functionDetails = function (req, res) {
    getFunctionDetails((err, data) => {
        if (err) res.status(500).end();
        // const labels = data.labels;
            /* example `data`
{ name: 'projects/xxxx/locations/us-central1/functions/test', httpsTrigger: { url: 'https://us-central1-xxxx.cloudfunctions.net/test' }, status: 'ACTIVE', entryPoint: 'fnc', timeout: '60s', availableMemoryMb: 256, serviceAccountEmail: 'xxxx@appspot.gserviceaccount.com', updateTime: '2018-02-13T00:18:27Z', versionId: '9', labels: { 'deployment-tool': 'console-cloud', key1: '666' }, sourceUploadUrl: 'yyyy' }
            */
        res.status(200).end();
    });
};