【问题标题】:How to add a billing label onto a VM from a Go Cloud function?如何从 Go Cloud 功能向 VM 添加计费标签?
【发布时间】:2020-01-03 13:41:33
【问题描述】:

我希望能够相当于:来自 go113 云函数gcloud compute instances add-labels --zone asia-east1-c foobar --labels=hostname=foobar,通过protoPayload.methodName="v1.compute.instances.insert" 给定触发器。

我可以看到这里有一个 API https://cloud.google.com/compute/docs/reference/rest/v1/instances/setLabels,但我希望某种 SDK 可以让这更容易,特别是因为我不太确定 Oauth 是如何工作的,所以我希望找到一个简单的例子这已经处理好了。

【问题讨论】:

标签: go google-cloud-platform gcloud


【解决方案1】:

我在 go link 中找到了方法 id compute.instances.setLabels

type InstancesSetLabelsCall struct {
    s                         *Service
    project                   string
    zone                      string
    instance                  string
    instancessetlabelsrequest *InstancesSetLabelsRequest
    urlParams_                gensupport.URLParams
    ctx_                      context.Context
    header_                   http.Header
}

// SetLabels: Sets labels on an instance. To learn more about labels,
// read the Labeling Resources documentation.
func (r *InstancesService) SetLabels(project string, zone string, instance string, instancessetlabelsrequest *InstancesSetLabelsRequest) *InstancesSetLabelsCall {
    c := &InstancesSetLabelsCall{s: r.s, urlParams_: make(gensupport.URLParams)}
    c.project = project
    c.zone = zone
    c.instance = instance
    c.instancessetlabelsrequest = instancessetlabelsrequest
    return c
}

你可以找到一个简单的例子here

【讨论】:

    【解决方案2】:

    您可以为您的云功能使用的服务帐号设置正确的 IAM 权限。 Google Cloud 功能将使用服务帐号和附加的权限。

    参考:https://cloud.google.com/functions/docs/concepts/iam

    使用go 语言在 Google Compute Engine 上设置标签的参考 - https://godoc.org/google.golang.org/api/compute/v1#InstancesService.SetLabels

    Terraform 是用go 语言编写的,我发现它的源代码有助于golang 示例与 GCP 交互。例如https://github.com/terraform-providers/terraform-provider-google/blob/master/google/resource_compute_instance.go#L1039

    【讨论】:

    【解决方案3】:

    我遇到困难的原因是我在compute.InstancesSetLabelsRequest 通话中错过了LabelFingerprint。文档不清楚它是必需的,并且没有引发错误,因为我认为该操作是异步操作。

    s := strings.Split(instanceEvent.ProtoPayload.ResourceName, "/")
    project, zone, instance := s[1], s[3], s[5]
    log.Printf("Split %s, to project: %s, zone: %s, instance: %s",
        s,
        project,
        zone,
        instance)
    
    inst, err := computeService.Instances.Get(project, zone, instance).Do()
    if err != nil {
        log.Fatal(err)
    }
    
    existingLabels := inst.Labels
    if existingLabels == nil {
        existingLabels = make(map[string]string)
    }
    existingLabels["hostname"] = instance
    
    log.Println("Applying labels:", existingLabels)
    
    rb := &compute.InstancesSetLabelsRequest{
        LabelFingerprint: inst.LabelFingerprint,
        Labels:           existingLabels,
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      • 1970-01-01
      • 2014-03-12
      • 2015-03-23
      • 1970-01-01
      • 2021-07-20
      • 1970-01-01
      相关资源
      最近更新 更多