【问题标题】:Batch requests to GCP Compute using Golang client library使用 Golang 客户端库对 GCP Compute 的批量请求
【发布时间】:2018-10-01 17:15:20
【问题描述】:

是否可以使用适用于 Go 的 Google API 客户端库执行批量请求?

更准确地说,我想删除一些磁盘;如果我可以避免对每个磁盘的请求,那就太好了。

我指的批处理请求功能: https://cloud.google.com/compute/docs/api/how-tos/batch

我正在使用的客户端库: https://github.com/googleapis/google-api-go-client

似乎还有另一个客户端库,但不支持 Compute Engine,只有元数据 (?): https://github.com/GoogleCloudPlatform/google-cloud-go

【问题讨论】:

    标签: go google-cloud-platform google-api google-api-client google-api-go-client


    【解决方案1】:

    我不确定,但我认为这是可以做到的,因为我们可以使用 API 调用来删除磁盘。 一个批处理请求由多个 API 调用组合成一个 HTTP 请求,可以发送到API discovery document 中指定的批处理路径。默认路径为 /batch/api_name/api_version。

    Example查看批量请求格式Here

    // method id "compute.disks.delete":
    
    type DisksDeleteCall struct {
        s          *Service
        project    string
        zone       string
        disk       string
        urlParams_ gensupport.URLParams
        ctx_       context.Context
        header_    http.Header
    }
    
    // Delete: Deletes the specified persistent disk. Deleting a disk
    // removes its data permanently and is irreversible. However, deleting a
    // disk does not delete any snapshots previously made from the disk. You
    // must separately delete snapshots.
    // For details, see https://cloud.google.com/compute/docs/reference/latest/disks/delete
    func (r *DisksService) Delete(project string, zone string, disk string) *DisksDeleteCall {
        c := &DisksDeleteCall{s: r.s, urlParams_: make(gensupport.URLParams)}
        c.project = project
        c.zone = zone
        c.disk = disk
        return c
    }
    
    // RequestId sets the optional parameter "requestId": An optional
    // request ID to identify requests. Specify a unique request ID so that
    // if you must retry your request, the server will know to ignore the
    // request if it has already been completed.
    //
    // For example, consider a situation where you make an initial request
    // and the request times out. If you make the request again with the
    // same request ID, the server can check if original operation with the
    // same request ID was received, and if so, will ignore the second
    // request. This prevents clients from accidentally creating duplicate
    // commitments.
    //
    // The request ID must be a valid UUID with the exception that zero UUID
    // is not supported (00000000-0000-0000-0000-000000000000).
    func (c *DisksDeleteCall) RequestId(requestId string) *DisksDeleteCall {
        c.urlParams_.Set("requestId", requestId)
        return c
    }
    
    // Fields allows partial responses to be retrieved. See
    // https://developers.google.com/gdata/docs/2.0/basics#PartialResponse
    // for more information.
    func (c *DisksDeleteCall) Fields(s ...googleapi.Field) *DisksDeleteCall {
        c.urlParams_.Set("fields", googleapi.CombineFields(s))
        return c
    }
    
    // Context sets the context to be used in this call's Do method. Any
    // pending HTTP request will be aborted if the provided context is
    // canceled.
    func (c *DisksDeleteCall) Context(ctx context.Context) *DisksDeleteCall {
        c.ctx_ = ctx
        return c
    }
    
    // Header returns an http.Header that can be modified by the caller to
    // add HTTP headers to the request.
    func (c *DisksDeleteCall) Header() http.Header {
        if c.header_ == nil {
            c.header_ = make(http.Header)
        }
        return c.header_
    }
    
    func (c *DisksDeleteCall) doRequest(alt string) (*http.Response, error) {
        reqHeaders := make(http.Header)
        for k, v := range c.header_ {
            reqHeaders[k] = v
        }
        reqHeaders.Set("User-Agent", c.s.userAgent())
        var body io.Reader = nil
        c.urlParams_.Set("alt", alt)
        c.urlParams_.Set("prettyPrint", "false")
        urls := googleapi.ResolveRelative(c.s.BasePath, "{project}/zones/{zone}/disks/{disk}")
        urls += "?" + c.urlParams_.Encode()
        req, _ := http.NewRequest("DELETE", urls, body)
        req.Header = reqHeaders
        googleapi.Expand(req.URL, map[string]string{
            "project": c.project,
            "zone":    c.zone,
            "disk":    c.disk,
        })
        return gensupport.SendRequest(c.ctx_, c.s.client, req)
    }
    
    // Do executes the "compute.disks.delete" call.
    // Exactly one of *Operation or error will be non-nil. Any non-2xx
    // status code is an error. Response headers are in either
    // *Operation.ServerResponse.Header or (if a response was returned at
    // all) in error.(*googleapi.Error).Header. Use googleapi.IsNotModified
    // to check whether the returned error was because
    // http.StatusNotModified was returned.
    func (c *DisksDeleteCall) Do(opts ...googleapi.CallOption) (*Operation, error) {
        gensupport.SetOptions(c.urlParams_, opts...)
        res, err := c.doRequest("json")
        if res != nil && res.StatusCode == http.StatusNotModified {
            if res.Body != nil {
                res.Body.Close()
            }
            return nil, &googleapi.Error{
                Code:   res.StatusCode,
                Header: res.Header,
            }
        }
        if err != nil {
            return nil, err
        }
        defer googleapi.CloseBody(res)
        if err := googleapi.CheckResponse(res); err != nil {
            return nil, err
        }
        ret := &Operation{
            ServerResponse: googleapi.ServerResponse{
                Header:         res.Header,
                HTTPStatusCode: res.StatusCode,
            },
        }
        target := &ret
        if err := gensupport.DecodeResponse(target, res); err != nil {
            return nil, err
        }
        return ret, nil
        // {
        //   "description": "Deletes the specified persistent disk. Deleting a disk removes its data permanently and is irreversible. However, deleting a disk does not delete any snapshots previously made from the disk. You must separately delete snapshots.",
        //   "httpMethod": "DELETE",
        //   "id": "compute.disks.delete",
        //   "parameterOrder": [
        //     "project",
        //     "zone",
        //     "disk"
        //   ],
        //   "parameters": {
        //     "disk": {
        //       "description": "Name of the persistent disk to delete.",
        //       "location": "path",
        //       "required": true,
        //       "type": "string"
        //     },
        //     "project": {
        //       "description": "Project ID for this request.",
        //       "location": "path",
        //       "pattern": "(?:(?:[-a-z0-9]{1,63}\\.)*(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?):)?(?:[0-9]{1,19}|(?:[a-z0-9](?:[-a-z0-9]{0,61}[a-z0-9])?))",
        //       "required": true,
        //       "type": "string"
        //     },
        //     "requestId": {
        //       "description": "An optional request ID to identify requests. Specify a unique request ID so that if you must retry your request, the server will know to ignore the request if it has already been completed.\n\nFor example, consider a situation where you make an initial request and the request times out. If you make the request again with the same request ID, the server can check if original operation with the same request ID was received, and if so, will ignore the second request. This prevents clients from accidentally creating duplicate commitments.\n\nThe request ID must be a valid UUID with the exception that zero UUID is not supported (00000000-0000-0000-0000-000000000000).",
        //       "location": "query",
        //       "type": "string"
        //     },
        //     "zone": {
        //       "description": "The name of the zone for this request.",
        //       "location": "path",
        //       "pattern": "[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?",
        //       "required": true,
        //       "type": "string"
        //     }
        //   },
        //   "path": "{project}/zones/{zone}/disks/{disk}",
        //   "response": {
        //     "$ref": "Operation"
        //   },
        //   "scopes": [
        //     "https://www.googleapis.com/auth/cloud-platform",
        //     "https://www.googleapis.com/auth/compute"
        //   ]
        // }
    
    }
    

    【讨论】:

    • 问题是,如何从 go 客户端库中获取 HTTP 请求。我只能找到.Do() 之类的client.Zones.List("project").Do() 函数,它们组装HTTP 请求、发送、等待响应、解码等。也许您可以编写一个自定义http.Client 并将其交给客户端库;您必须在自己的 goroutine 中异步运行批处理的每个请求...
    • 澄清一下:我的问题是不是如何对 GCP API 执行批处理请求。这是记录在案的。我的问题是如何使用 Google API Client Library for Go 执行批处理请求。
    • 您尝试过使用slingresty 库吗?
    • 不,但据我所知,他们只能让我到达那里的一部分。主要工作是定义结构(例如,哪些字段是必需的/预期的)和协议(例如,请求返回一个操作 ID,我需要在另一个请求中使用它来获取状态)。这就是 Google API Client Library for Go 为我所做的:它为我提供了消息的静态类型并实现了协议的大部分。我想避免重新实现它。
    • 我确实添加了 GO 的 func (DisksDeleteCall),你可能会感兴趣
    猜你喜欢
    • 2020-06-11
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多