访问云功能内的标签

jld*_*ont 2 node.js google-cloud-platform google-cloud-functions

当有问题的资源是Google Cloud Function时,有没有办法访问资源的标签?

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

使用案例:用于配置目的(而不是重新部署).

谢谢!

小智 5

您可以使用googleapis:cloudfunctions命名空间,使用环境变量可以获得API所需的参数.以下是在以下脚本中使用的函数/ 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();
    });
};
Run Code Online (Sandbox Code Playgroud)