【问题标题】:Accessing labels inside a Cloud Function访问云函数内的标签
【发布时间】:2018-02-11 18:20:13
【问题描述】:

当相关资源是 Google Cloud 函数时,是否可以访问资源的标签?

我使用了一个测试函数并记录了process.envprocess 对象,但我看不到我针对该函数设置的任何标签。

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

谢谢!

【问题讨论】:

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


    【解决方案1】:

    您可以使用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();
        });
    };
    

    【讨论】:

    • 天才!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2013-03-23
    • 2018-03-28
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多