【问题标题】:When deploying GCP cloud function with go client I receive: "Error 400: Precondition check failed., failedPrecondition"使用 go 客户端部署 GCP 云功能时,我收到:“错误 400:前提条件检查失败。,失败的前提条件”
【发布时间】:2020-06-27 19:58:42
【问题描述】:

我正在尝试使用谷歌的 go 客户端包部署谷歌云功能。

(https://pkg.go.dev/google.golang.org/api/cloudfunctions/v1?tab=doc#pkg-overview)

我已将其分解为我认为最相关的 sn-p:

import (
    "context"
    log "github.com/sirupsen/logrus"
    functions "google.golang.org/api/cloudfunctions/v1"
)

func main() {
    ctx := context.Background()
    CloudFunctionService, err := functions.NewService(ctx)
    if err != nil {
        log.Printf("Error at functions.NewService(ctx): \"%v\"\n", err)
    }
    FunctionSpec := functions.CloudFunction{
            EntryPoint:        "DeployThisFunctionEntryPoint",
            EventTrigger: &functions.EventTrigger{
                EventType: "google.pubsub.topic.publish",
                Resource:  "projects/mytestproject/topics/cloud-builds",
            },
            Name:                "DeployThisFunction",
            Runtime:             "go111",
            SourceRepository:    &functions.SourceRepository{Url: "https://source.developers.google.com/projects/mytestproject/repos/deploythisfunction/moveable-aliases/master/paths//"},
    }
    CloudFunctionDeploymentService := functions.NewProjectsLocationsFunctionsService(CloudFunctionService)
    createCall := CloudFunctionDeploymentService.Create("projects/mytestproject/locations/us-central1", &FunctionSpec)
    resp, err := createCall.Context(ctx).Do()
    if err != nil {
        log.Printf("Error at createCall.Context(ctx).Do(): \"%v\"\n", err)
    }
    log.Printf("response createCall.Context(ctx).Do(): \"%v\"\n", resp)
}

但是,无论我如何格式化或尝试。我总是收到以下消息:

googleapi: Error 400: Precondition check failed., failedPrecondition 

通过 google api explorer,我使用他们的身份验证和 json 方案运行了请求,我收到了同样的错误。

https://cloud.google.com/functions/docs/reference/rest/v1/projects.locations.functions/create

回复:

{
  "error": {
    "code": 400,
    "message": "Precondition check failed.",
    "status": "FAILED_PRECONDITION"
  }
}

我不知道出了什么问题。我已经开始了自己的项目并且是管理员。当使用 GCP 运行 go 客户端的另一部分时,例如创建存储、IAM 用户、服务帐户、数据库,我可以让它工作并创建这些资源。

如果有人遇到这个问题,我将不胜感激。

【问题讨论】:

    标签: go google-cloud-platform google-cloud-functions


    【解决方案1】:

    eventType 应该匹配模式:providers/*/eventTypes/*.*.

    例如:providers/cloud.pubsub/eventTypes/topic.publish

    SourceRepository url 也应该是https://source.developers.google.com/projects/*/repos/*/revisions/*/paths/ 你有https://source.developers.google.com/projects/mytestproject/repos/deploythisfunction/moveable-aliases/master/paths//

    错误消息说这是您配置 FunctionSpec 的方式的问题,我怀疑是 EventTriggerSourceRepository 字段。

    编辑

    code: 400 是一个错误请求,客户端错误,在这种情况下是格式问题,首先要检查的是每个 cloud function fileds

    【讨论】:

    • 感谢您的反馈。实际上,我从控制台中成功的部署日志消息中获取了很多这些值。 ``` "request": { "location": "projects/mytestproject/locations/us-central1", "function": { "entryPoint": "DeployThisFu ...", "eventTrigger": { "eventType": " google.pubsub.topic.publish", "resource": "projects/mytestproject/topics/cloud-builds"}, "sourceRepository": { "url":"source.developers.google.com/projects/mytestproject/repos/…"}}}, ``` 我同意我认为functionSpec有问题谢谢
    猜你喜欢
    • 2019-04-17
    • 2022-11-09
    • 2017-01-08
    • 1970-01-01
    • 2022-09-01
    • 2020-05-08
    • 1970-01-01
    • 2018-08-12
    • 1970-01-01
    相关资源
    最近更新 更多