1// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Code generated by protoc-gen-go_gapic. DO NOT EDIT.
16
17package compute
18
19import (
20	"bytes"
21	"context"
22	"fmt"
23	"io/ioutil"
24	"math"
25	"net/http"
26	"net/url"
27	"sort"
28
29	gax "github.com/googleapis/gax-go/v2"
30	"google.golang.org/api/googleapi"
31	"google.golang.org/api/iterator"
32	"google.golang.org/api/option"
33	"google.golang.org/api/option/internaloption"
34	httptransport "google.golang.org/api/transport/http"
35	computepb "google.golang.org/genproto/googleapis/cloud/compute/v1"
36	"google.golang.org/grpc"
37	"google.golang.org/grpc/metadata"
38	"google.golang.org/protobuf/encoding/protojson"
39	"google.golang.org/protobuf/proto"
40)
41
42var newDisksClientHook clientHook
43
44// DisksCallOptions contains the retry settings for each method of DisksClient.
45type DisksCallOptions struct {
46	AddResourcePolicies    []gax.CallOption
47	AggregatedList         []gax.CallOption
48	CreateSnapshot         []gax.CallOption
49	Delete                 []gax.CallOption
50	Get                    []gax.CallOption
51	GetIamPolicy           []gax.CallOption
52	Insert                 []gax.CallOption
53	List                   []gax.CallOption
54	RemoveResourcePolicies []gax.CallOption
55	Resize                 []gax.CallOption
56	SetIamPolicy           []gax.CallOption
57	SetLabels              []gax.CallOption
58	TestIamPermissions     []gax.CallOption
59}
60
61// internalDisksClient is an interface that defines the methods availaible from Google Compute Engine API.
62type internalDisksClient interface {
63	Close() error
64	setGoogleClientInfo(...string)
65	Connection() *grpc.ClientConn
66	AddResourcePolicies(context.Context, *computepb.AddResourcePoliciesDiskRequest, ...gax.CallOption) (*Operation, error)
67	AggregatedList(context.Context, *computepb.AggregatedListDisksRequest, ...gax.CallOption) *DisksScopedListPairIterator
68	CreateSnapshot(context.Context, *computepb.CreateSnapshotDiskRequest, ...gax.CallOption) (*Operation, error)
69	Delete(context.Context, *computepb.DeleteDiskRequest, ...gax.CallOption) (*Operation, error)
70	Get(context.Context, *computepb.GetDiskRequest, ...gax.CallOption) (*computepb.Disk, error)
71	GetIamPolicy(context.Context, *computepb.GetIamPolicyDiskRequest, ...gax.CallOption) (*computepb.Policy, error)
72	Insert(context.Context, *computepb.InsertDiskRequest, ...gax.CallOption) (*Operation, error)
73	List(context.Context, *computepb.ListDisksRequest, ...gax.CallOption) *DiskIterator
74	RemoveResourcePolicies(context.Context, *computepb.RemoveResourcePoliciesDiskRequest, ...gax.CallOption) (*Operation, error)
75	Resize(context.Context, *computepb.ResizeDiskRequest, ...gax.CallOption) (*Operation, error)
76	SetIamPolicy(context.Context, *computepb.SetIamPolicyDiskRequest, ...gax.CallOption) (*computepb.Policy, error)
77	SetLabels(context.Context, *computepb.SetLabelsDiskRequest, ...gax.CallOption) (*Operation, error)
78	TestIamPermissions(context.Context, *computepb.TestIamPermissionsDiskRequest, ...gax.CallOption) (*computepb.TestPermissionsResponse, error)
79}
80
81// DisksClient is a client for interacting with Google Compute Engine API.
82// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
83//
84// The Disks API.
85type DisksClient struct {
86	// The internal transport-dependent client.
87	internalClient internalDisksClient
88
89	// The call options for this service.
90	CallOptions *DisksCallOptions
91}
92
93// Wrapper methods routed to the internal client.
94
95// Close closes the connection to the API service. The user should invoke this when
96// the client is no longer required.
97func (c *DisksClient) Close() error {
98	return c.internalClient.Close()
99}
100
101// setGoogleClientInfo sets the name and version of the application in
102// the `x-goog-api-client` header passed on each request. Intended for
103// use by Google-written clients.
104func (c *DisksClient) setGoogleClientInfo(keyval ...string) {
105	c.internalClient.setGoogleClientInfo(keyval...)
106}
107
108// Connection returns a connection to the API service.
109//
110// Deprecated.
111func (c *DisksClient) Connection() *grpc.ClientConn {
112	return c.internalClient.Connection()
113}
114
115// AddResourcePolicies adds existing resource policies to a disk. You can only add one policy which will be applied to this disk for scheduling snapshot creation.
116func (c *DisksClient) AddResourcePolicies(ctx context.Context, req *computepb.AddResourcePoliciesDiskRequest, opts ...gax.CallOption) (*Operation, error) {
117	return c.internalClient.AddResourcePolicies(ctx, req, opts...)
118}
119
120// AggregatedList retrieves an aggregated list of persistent disks.
121func (c *DisksClient) AggregatedList(ctx context.Context, req *computepb.AggregatedListDisksRequest, opts ...gax.CallOption) *DisksScopedListPairIterator {
122	return c.internalClient.AggregatedList(ctx, req, opts...)
123}
124
125// CreateSnapshot creates a snapshot of a specified persistent disk.
126func (c *DisksClient) CreateSnapshot(ctx context.Context, req *computepb.CreateSnapshotDiskRequest, opts ...gax.CallOption) (*Operation, error) {
127	return c.internalClient.CreateSnapshot(ctx, req, opts...)
128}
129
130// 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.
131func (c *DisksClient) Delete(ctx context.Context, req *computepb.DeleteDiskRequest, opts ...gax.CallOption) (*Operation, error) {
132	return c.internalClient.Delete(ctx, req, opts...)
133}
134
135// Get returns a specified persistent disk. Gets a list of available persistent disks by making a list() request.
136func (c *DisksClient) Get(ctx context.Context, req *computepb.GetDiskRequest, opts ...gax.CallOption) (*computepb.Disk, error) {
137	return c.internalClient.Get(ctx, req, opts...)
138}
139
140// GetIamPolicy gets the access control policy for a resource. May be empty if no such policy or resource exists.
141func (c *DisksClient) GetIamPolicy(ctx context.Context, req *computepb.GetIamPolicyDiskRequest, opts ...gax.CallOption) (*computepb.Policy, error) {
142	return c.internalClient.GetIamPolicy(ctx, req, opts...)
143}
144
145// Insert creates a persistent disk in the specified project using the data in the request. You can create a disk from a source (sourceImage, sourceSnapshot, or sourceDisk) or create an empty 500 GB data disk by omitting all properties. You can also create a disk that is larger than the default size by specifying the sizeGb property.
146func (c *DisksClient) Insert(ctx context.Context, req *computepb.InsertDiskRequest, opts ...gax.CallOption) (*Operation, error) {
147	return c.internalClient.Insert(ctx, req, opts...)
148}
149
150// List retrieves a list of persistent disks contained within the specified zone.
151func (c *DisksClient) List(ctx context.Context, req *computepb.ListDisksRequest, opts ...gax.CallOption) *DiskIterator {
152	return c.internalClient.List(ctx, req, opts...)
153}
154
155// RemoveResourcePolicies removes resource policies from a disk.
156func (c *DisksClient) RemoveResourcePolicies(ctx context.Context, req *computepb.RemoveResourcePoliciesDiskRequest, opts ...gax.CallOption) (*Operation, error) {
157	return c.internalClient.RemoveResourcePolicies(ctx, req, opts...)
158}
159
160// Resize resizes the specified persistent disk. You can only increase the size of the disk.
161func (c *DisksClient) Resize(ctx context.Context, req *computepb.ResizeDiskRequest, opts ...gax.CallOption) (*Operation, error) {
162	return c.internalClient.Resize(ctx, req, opts...)
163}
164
165// SetIamPolicy sets the access control policy on the specified resource. Replaces any existing policy.
166func (c *DisksClient) SetIamPolicy(ctx context.Context, req *computepb.SetIamPolicyDiskRequest, opts ...gax.CallOption) (*computepb.Policy, error) {
167	return c.internalClient.SetIamPolicy(ctx, req, opts...)
168}
169
170// SetLabels sets the labels on a disk. To learn more about labels, read the Labeling Resources documentation.
171func (c *DisksClient) SetLabels(ctx context.Context, req *computepb.SetLabelsDiskRequest, opts ...gax.CallOption) (*Operation, error) {
172	return c.internalClient.SetLabels(ctx, req, opts...)
173}
174
175// TestIamPermissions returns permissions that a caller has on the specified resource.
176func (c *DisksClient) TestIamPermissions(ctx context.Context, req *computepb.TestIamPermissionsDiskRequest, opts ...gax.CallOption) (*computepb.TestPermissionsResponse, error) {
177	return c.internalClient.TestIamPermissions(ctx, req, opts...)
178}
179
180// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
181type disksRESTClient struct {
182	// The http endpoint to connect to.
183	endpoint string
184
185	// The http client.
186	httpClient *http.Client
187
188	// The x-goog-* metadata to be sent with each request.
189	xGoogMetadata metadata.MD
190}
191
192// NewDisksRESTClient creates a new disks rest client.
193//
194// The Disks API.
195func NewDisksRESTClient(ctx context.Context, opts ...option.ClientOption) (*DisksClient, error) {
196	clientOpts := append(defaultDisksRESTClientOptions(), opts...)
197	httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
198	if err != nil {
199		return nil, err
200	}
201
202	c := &disksRESTClient{
203		endpoint:   endpoint,
204		httpClient: httpClient,
205	}
206	c.setGoogleClientInfo()
207
208	return &DisksClient{internalClient: c, CallOptions: &DisksCallOptions{}}, nil
209}
210
211func defaultDisksRESTClientOptions() []option.ClientOption {
212	return []option.ClientOption{
213		internaloption.WithDefaultEndpoint("https://compute.googleapis.com"),
214		internaloption.WithDefaultMTLSEndpoint("https://compute.mtls.googleapis.com"),
215		internaloption.WithDefaultAudience("https://compute.googleapis.com/"),
216		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
217	}
218}
219
220// setGoogleClientInfo sets the name and version of the application in
221// the `x-goog-api-client` header passed on each request. Intended for
222// use by Google-written clients.
223func (c *disksRESTClient) setGoogleClientInfo(keyval ...string) {
224	kv := append([]string{"gl-go", versionGo()}, keyval...)
225	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "rest", "UNKNOWN")
226	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
227}
228
229// Close closes the connection to the API service. The user should invoke this when
230// the client is no longer required.
231func (c *disksRESTClient) Close() error {
232	// Replace httpClient with nil to force cleanup.
233	c.httpClient = nil
234	return nil
235}
236
237// Connection returns a connection to the API service.
238//
239// Deprecated.
240func (c *disksRESTClient) Connection() *grpc.ClientConn {
241	return nil
242}
243
244// AddResourcePolicies adds existing resource policies to a disk. You can only add one policy which will be applied to this disk for scheduling snapshot creation.
245func (c *disksRESTClient) AddResourcePolicies(ctx context.Context, req *computepb.AddResourcePoliciesDiskRequest, opts ...gax.CallOption) (*Operation, error) {
246	m := protojson.MarshalOptions{AllowPartial: true}
247	body := req.GetDisksAddResourcePoliciesRequestResource()
248	jsonReq, err := m.Marshal(body)
249	if err != nil {
250		return nil, err
251	}
252
253	baseUrl, _ := url.Parse(c.endpoint)
254	baseUrl.Path += fmt.Sprintf("/compute/v1/projects/%v/zones/%v/disks/%v/addResourcePolicies", req.GetProject(), req.GetZone(), req.GetDisk())
255
256	params := url.Values{}
257	if req != nil && req.RequestId != nil {
258		params.Add("requestId", fmt.Sprintf("%v", req.GetRequestId()))
259	}
260
261	baseUrl.RawQuery = params.Encode()
262
263	httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
264	if err != nil {
265		return nil, err
266	}
267	httpReq = httpReq.WithContext(ctx)
268	// Set the headers
269	for k, v := range c.xGoogMetadata {
270		httpReq.Header[k] = v
271	}
272	httpReq.Header["Content-Type"] = []string{"application/json"}
273
274	httpRsp, err := c.httpClient.Do(httpReq)
275	if err != nil {
276		return nil, err
277	}
278	defer httpRsp.Body.Close()
279
280	if err = googleapi.CheckResponse(httpRsp); err != nil {
281		return nil, err
282	}
283
284	buf, err := ioutil.ReadAll(httpRsp.Body)
285	if err != nil {
286		return nil, err
287	}
288
289	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
290	rsp := &computepb.Operation{}
291
292	if err := unm.Unmarshal(buf, rsp); err != nil {
293		return nil, err
294	}
295	op := &Operation{proto: rsp}
296	return op, err
297}
298
299// AggregatedList retrieves an aggregated list of persistent disks.
300func (c *disksRESTClient) AggregatedList(ctx context.Context, req *computepb.AggregatedListDisksRequest, opts ...gax.CallOption) *DisksScopedListPairIterator {
301	it := &DisksScopedListPairIterator{}
302	req = proto.Clone(req).(*computepb.AggregatedListDisksRequest)
303	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
304	it.InternalFetch = func(pageSize int, pageToken string) ([]DisksScopedListPair, string, error) {
305		resp := &computepb.DiskAggregatedList{}
306		if pageToken != "" {
307			req.PageToken = proto.String(pageToken)
308		}
309		if pageSize > math.MaxInt32 {
310			req.MaxResults = proto.Uint32(math.MaxInt32)
311		} else if pageSize != 0 {
312			req.MaxResults = proto.Uint32(uint32(pageSize))
313		}
314		baseUrl, _ := url.Parse(c.endpoint)
315		baseUrl.Path += fmt.Sprintf("/compute/v1/projects/%v/aggregated/disks", req.GetProject())
316
317		params := url.Values{}
318		if req != nil && req.Filter != nil {
319			params.Add("filter", fmt.Sprintf("%v", req.GetFilter()))
320		}
321		if req != nil && req.IncludeAllScopes != nil {
322			params.Add("includeAllScopes", fmt.Sprintf("%v", req.GetIncludeAllScopes()))
323		}
324		if req != nil && req.MaxResults != nil {
325			params.Add("maxResults", fmt.Sprintf("%v", req.GetMaxResults()))
326		}
327		if req != nil && req.OrderBy != nil {
328			params.Add("orderBy", fmt.Sprintf("%v", req.GetOrderBy()))
329		}
330		if req != nil && req.PageToken != nil {
331			params.Add("pageToken", fmt.Sprintf("%v", req.GetPageToken()))
332		}
333		if req != nil && req.ReturnPartialSuccess != nil {
334			params.Add("returnPartialSuccess", fmt.Sprintf("%v", req.GetReturnPartialSuccess()))
335		}
336
337		baseUrl.RawQuery = params.Encode()
338
339		httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
340		if err != nil {
341			return nil, "", err
342		}
343
344		// Set the headers
345		for k, v := range c.xGoogMetadata {
346			httpReq.Header[k] = v
347		}
348
349		httpReq.Header["Content-Type"] = []string{"application/json"}
350		httpRsp, err := c.httpClient.Do(httpReq)
351		if err != nil {
352			return nil, "", err
353		}
354		defer httpRsp.Body.Close()
355
356		if err = googleapi.CheckResponse(httpRsp); err != nil {
357			return nil, "", err
358		}
359
360		buf, err := ioutil.ReadAll(httpRsp.Body)
361		if err != nil {
362			return nil, "", err
363		}
364
365		unm.Unmarshal(buf, resp)
366		it.Response = resp
367
368		elems := make([]DisksScopedListPair, 0, len(resp.GetItems()))
369		for k, v := range resp.GetItems() {
370			elems = append(elems, DisksScopedListPair{k, v})
371		}
372		sort.Slice(elems, func(i, j int) bool { return elems[i].Key < elems[j].Key })
373
374		return elems, resp.GetNextPageToken(), nil
375	}
376
377	fetch := func(pageSize int, pageToken string) (string, error) {
378		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
379		if err != nil {
380			return "", err
381		}
382		it.items = append(it.items, items...)
383		return nextPageToken, nil
384	}
385
386	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
387	it.pageInfo.MaxSize = int(req.GetMaxResults())
388	it.pageInfo.Token = req.GetPageToken()
389
390	return it
391}
392
393// CreateSnapshot creates a snapshot of a specified persistent disk.
394func (c *disksRESTClient) CreateSnapshot(ctx context.Context, req *computepb.CreateSnapshotDiskRequest, opts ...gax.CallOption) (*Operation, error) {
395	m := protojson.MarshalOptions{AllowPartial: true}
396	body := req.GetSnapshotResource()
397	jsonReq, err := m.Marshal(body)
398	if err != nil {
399		return nil, err
400	}
401
402	baseUrl, _ := url.Parse(c.endpoint)
403	baseUrl.Path += fmt.Sprintf("/compute/v1/projects/%v/zones/%v/disks/%v/createSnapshot", req.GetProject(), req.GetZone(), req.GetDisk())
404
405	params := url.Values{}
406	if req != nil && req.GuestFlush != nil {
407		params.Add("guestFlush", fmt.Sprintf("%v", req.GetGuestFlush()))
408	}
409	if req != nil && req.RequestId != nil {
410		params.Add("requestId", fmt.Sprintf("%v", req.GetRequestId()))
411	}
412
413	baseUrl.RawQuery = params.Encode()
414
415	httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
416	if err != nil {
417		return nil, err
418	}
419	httpReq = httpReq.WithContext(ctx)
420	// Set the headers
421	for k, v := range c.xGoogMetadata {
422		httpReq.Header[k] = v
423	}
424	httpReq.Header["Content-Type"] = []string{"application/json"}
425
426	httpRsp, err := c.httpClient.Do(httpReq)
427	if err != nil {
428		return nil, err
429	}
430	defer httpRsp.Body.Close()
431
432	if err = googleapi.CheckResponse(httpRsp); err != nil {
433		return nil, err
434	}
435
436	buf, err := ioutil.ReadAll(httpRsp.Body)
437	if err != nil {
438		return nil, err
439	}
440
441	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
442	rsp := &computepb.Operation{}
443
444	if err := unm.Unmarshal(buf, rsp); err != nil {
445		return nil, err
446	}
447	op := &Operation{proto: rsp}
448	return op, err
449}
450
451// 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.
452func (c *disksRESTClient) Delete(ctx context.Context, req *computepb.DeleteDiskRequest, opts ...gax.CallOption) (*Operation, error) {
453	baseUrl, _ := url.Parse(c.endpoint)
454	baseUrl.Path += fmt.Sprintf("/compute/v1/projects/%v/zones/%v/disks/%v", req.GetProject(), req.GetZone(), req.GetDisk())
455
456	params := url.Values{}
457	if req != nil && req.RequestId != nil {
458		params.Add("requestId", fmt.Sprintf("%v", req.GetRequestId()))
459	}
460
461	baseUrl.RawQuery = params.Encode()
462
463	httpReq, err := http.NewRequest("DELETE", baseUrl.String(), nil)
464	if err != nil {
465		return nil, err
466	}
467	httpReq = httpReq.WithContext(ctx)
468	// Set the headers
469	for k, v := range c.xGoogMetadata {
470		httpReq.Header[k] = v
471	}
472	httpReq.Header["Content-Type"] = []string{"application/json"}
473
474	httpRsp, err := c.httpClient.Do(httpReq)
475	if err != nil {
476		return nil, err
477	}
478	defer httpRsp.Body.Close()
479
480	if err = googleapi.CheckResponse(httpRsp); err != nil {
481		return nil, err
482	}
483
484	buf, err := ioutil.ReadAll(httpRsp.Body)
485	if err != nil {
486		return nil, err
487	}
488
489	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
490	rsp := &computepb.Operation{}
491
492	if err := unm.Unmarshal(buf, rsp); err != nil {
493		return nil, err
494	}
495	op := &Operation{proto: rsp}
496	return op, err
497}
498
499// Get returns a specified persistent disk. Gets a list of available persistent disks by making a list() request.
500func (c *disksRESTClient) Get(ctx context.Context, req *computepb.GetDiskRequest, opts ...gax.CallOption) (*computepb.Disk, error) {
501	baseUrl, _ := url.Parse(c.endpoint)
502	baseUrl.Path += fmt.Sprintf("/compute/v1/projects/%v/zones/%v/disks/%v", req.GetProject(), req.GetZone(), req.GetDisk())
503
504	httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
505	if err != nil {
506		return nil, err
507	}
508	httpReq = httpReq.WithContext(ctx)
509	// Set the headers
510	for k, v := range c.xGoogMetadata {
511		httpReq.Header[k] = v
512	}
513	httpReq.Header["Content-Type"] = []string{"application/json"}
514
515	httpRsp, err := c.httpClient.Do(httpReq)
516	if err != nil {
517		return nil, err
518	}
519	defer httpRsp.Body.Close()
520
521	if err = googleapi.CheckResponse(httpRsp); err != nil {
522		return nil, err
523	}
524
525	buf, err := ioutil.ReadAll(httpRsp.Body)
526	if err != nil {
527		return nil, err
528	}
529
530	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
531	rsp := &computepb.Disk{}
532
533	return rsp, unm.Unmarshal(buf, rsp)
534}
535
536// GetIamPolicy gets the access control policy for a resource. May be empty if no such policy or resource exists.
537func (c *disksRESTClient) GetIamPolicy(ctx context.Context, req *computepb.GetIamPolicyDiskRequest, opts ...gax.CallOption) (*computepb.Policy, error) {
538	baseUrl, _ := url.Parse(c.endpoint)
539	baseUrl.Path += fmt.Sprintf("/compute/v1/projects/%v/zones/%v/disks/%v/getIamPolicy", req.GetProject(), req.GetZone(), req.GetResource())
540
541	params := url.Values{}
542	if req != nil && req.OptionsRequestedPolicyVersion != nil {
543		params.Add("optionsRequestedPolicyVersion", fmt.Sprintf("%v", req.GetOptionsRequestedPolicyVersion()))
544	}
545
546	baseUrl.RawQuery = params.Encode()
547
548	httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
549	if err != nil {
550		return nil, err
551	}
552	httpReq = httpReq.WithContext(ctx)
553	// Set the headers
554	for k, v := range c.xGoogMetadata {
555		httpReq.Header[k] = v
556	}
557	httpReq.Header["Content-Type"] = []string{"application/json"}
558
559	httpRsp, err := c.httpClient.Do(httpReq)
560	if err != nil {
561		return nil, err
562	}
563	defer httpRsp.Body.Close()
564
565	if err = googleapi.CheckResponse(httpRsp); err != nil {
566		return nil, err
567	}
568
569	buf, err := ioutil.ReadAll(httpRsp.Body)
570	if err != nil {
571		return nil, err
572	}
573
574	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
575	rsp := &computepb.Policy{}
576
577	return rsp, unm.Unmarshal(buf, rsp)
578}
579
580// Insert creates a persistent disk in the specified project using the data in the request. You can create a disk from a source (sourceImage, sourceSnapshot, or sourceDisk) or create an empty 500 GB data disk by omitting all properties. You can also create a disk that is larger than the default size by specifying the sizeGb property.
581func (c *disksRESTClient) Insert(ctx context.Context, req *computepb.InsertDiskRequest, opts ...gax.CallOption) (*Operation, error) {
582	m := protojson.MarshalOptions{AllowPartial: true}
583	body := req.GetDiskResource()
584	jsonReq, err := m.Marshal(body)
585	if err != nil {
586		return nil, err
587	}
588
589	baseUrl, _ := url.Parse(c.endpoint)
590	baseUrl.Path += fmt.Sprintf("/compute/v1/projects/%v/zones/%v/disks", req.GetProject(), req.GetZone())
591
592	params := url.Values{}
593	if req != nil && req.RequestId != nil {
594		params.Add("requestId", fmt.Sprintf("%v", req.GetRequestId()))
595	}
596	if req != nil && req.SourceImage != nil {
597		params.Add("sourceImage", fmt.Sprintf("%v", req.GetSourceImage()))
598	}
599
600	baseUrl.RawQuery = params.Encode()
601
602	httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
603	if err != nil {
604		return nil, err
605	}
606	httpReq = httpReq.WithContext(ctx)
607	// Set the headers
608	for k, v := range c.xGoogMetadata {
609		httpReq.Header[k] = v
610	}
611	httpReq.Header["Content-Type"] = []string{"application/json"}
612
613	httpRsp, err := c.httpClient.Do(httpReq)
614	if err != nil {
615		return nil, err
616	}
617	defer httpRsp.Body.Close()
618
619	if err = googleapi.CheckResponse(httpRsp); err != nil {
620		return nil, err
621	}
622
623	buf, err := ioutil.ReadAll(httpRsp.Body)
624	if err != nil {
625		return nil, err
626	}
627
628	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
629	rsp := &computepb.Operation{}
630
631	if err := unm.Unmarshal(buf, rsp); err != nil {
632		return nil, err
633	}
634	op := &Operation{proto: rsp}
635	return op, err
636}
637
638// List retrieves a list of persistent disks contained within the specified zone.
639func (c *disksRESTClient) List(ctx context.Context, req *computepb.ListDisksRequest, opts ...gax.CallOption) *DiskIterator {
640	it := &DiskIterator{}
641	req = proto.Clone(req).(*computepb.ListDisksRequest)
642	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
643	it.InternalFetch = func(pageSize int, pageToken string) ([]*computepb.Disk, string, error) {
644		resp := &computepb.DiskList{}
645		if pageToken != "" {
646			req.PageToken = proto.String(pageToken)
647		}
648		if pageSize > math.MaxInt32 {
649			req.MaxResults = proto.Uint32(math.MaxInt32)
650		} else if pageSize != 0 {
651			req.MaxResults = proto.Uint32(uint32(pageSize))
652		}
653		baseUrl, _ := url.Parse(c.endpoint)
654		baseUrl.Path += fmt.Sprintf("/compute/v1/projects/%v/zones/%v/disks", req.GetProject(), req.GetZone())
655
656		params := url.Values{}
657		if req != nil && req.Filter != nil {
658			params.Add("filter", fmt.Sprintf("%v", req.GetFilter()))
659		}
660		if req != nil && req.MaxResults != nil {
661			params.Add("maxResults", fmt.Sprintf("%v", req.GetMaxResults()))
662		}
663		if req != nil && req.OrderBy != nil {
664			params.Add("orderBy", fmt.Sprintf("%v", req.GetOrderBy()))
665		}
666		if req != nil && req.PageToken != nil {
667			params.Add("pageToken", fmt.Sprintf("%v", req.GetPageToken()))
668		}
669		if req != nil && req.ReturnPartialSuccess != nil {
670			params.Add("returnPartialSuccess", fmt.Sprintf("%v", req.GetReturnPartialSuccess()))
671		}
672
673		baseUrl.RawQuery = params.Encode()
674
675		httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
676		if err != nil {
677			return nil, "", err
678		}
679
680		// Set the headers
681		for k, v := range c.xGoogMetadata {
682			httpReq.Header[k] = v
683		}
684
685		httpReq.Header["Content-Type"] = []string{"application/json"}
686		httpRsp, err := c.httpClient.Do(httpReq)
687		if err != nil {
688			return nil, "", err
689		}
690		defer httpRsp.Body.Close()
691
692		if err = googleapi.CheckResponse(httpRsp); err != nil {
693			return nil, "", err
694		}
695
696		buf, err := ioutil.ReadAll(httpRsp.Body)
697		if err != nil {
698			return nil, "", err
699		}
700
701		unm.Unmarshal(buf, resp)
702		it.Response = resp
703		return resp.GetItems(), resp.GetNextPageToken(), nil
704	}
705
706	fetch := func(pageSize int, pageToken string) (string, error) {
707		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
708		if err != nil {
709			return "", err
710		}
711		it.items = append(it.items, items...)
712		return nextPageToken, nil
713	}
714
715	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
716	it.pageInfo.MaxSize = int(req.GetMaxResults())
717	it.pageInfo.Token = req.GetPageToken()
718
719	return it
720}
721
722// RemoveResourcePolicies removes resource policies from a disk.
723func (c *disksRESTClient) RemoveResourcePolicies(ctx context.Context, req *computepb.RemoveResourcePoliciesDiskRequest, opts ...gax.CallOption) (*Operation, error) {
724	m := protojson.MarshalOptions{AllowPartial: true}
725	body := req.GetDisksRemoveResourcePoliciesRequestResource()
726	jsonReq, err := m.Marshal(body)
727	if err != nil {
728		return nil, err
729	}
730
731	baseUrl, _ := url.Parse(c.endpoint)
732	baseUrl.Path += fmt.Sprintf("/compute/v1/projects/%v/zones/%v/disks/%v/removeResourcePolicies", req.GetProject(), req.GetZone(), req.GetDisk())
733
734	params := url.Values{}
735	if req != nil && req.RequestId != nil {
736		params.Add("requestId", fmt.Sprintf("%v", req.GetRequestId()))
737	}
738
739	baseUrl.RawQuery = params.Encode()
740
741	httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
742	if err != nil {
743		return nil, err
744	}
745	httpReq = httpReq.WithContext(ctx)
746	// Set the headers
747	for k, v := range c.xGoogMetadata {
748		httpReq.Header[k] = v
749	}
750	httpReq.Header["Content-Type"] = []string{"application/json"}
751
752	httpRsp, err := c.httpClient.Do(httpReq)
753	if err != nil {
754		return nil, err
755	}
756	defer httpRsp.Body.Close()
757
758	if err = googleapi.CheckResponse(httpRsp); err != nil {
759		return nil, err
760	}
761
762	buf, err := ioutil.ReadAll(httpRsp.Body)
763	if err != nil {
764		return nil, err
765	}
766
767	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
768	rsp := &computepb.Operation{}
769
770	if err := unm.Unmarshal(buf, rsp); err != nil {
771		return nil, err
772	}
773	op := &Operation{proto: rsp}
774	return op, err
775}
776
777// Resize resizes the specified persistent disk. You can only increase the size of the disk.
778func (c *disksRESTClient) Resize(ctx context.Context, req *computepb.ResizeDiskRequest, opts ...gax.CallOption) (*Operation, error) {
779	m := protojson.MarshalOptions{AllowPartial: true}
780	body := req.GetDisksResizeRequestResource()
781	jsonReq, err := m.Marshal(body)
782	if err != nil {
783		return nil, err
784	}
785
786	baseUrl, _ := url.Parse(c.endpoint)
787	baseUrl.Path += fmt.Sprintf("/compute/v1/projects/%v/zones/%v/disks/%v/resize", req.GetProject(), req.GetZone(), req.GetDisk())
788
789	params := url.Values{}
790	if req != nil && req.RequestId != nil {
791		params.Add("requestId", fmt.Sprintf("%v", req.GetRequestId()))
792	}
793
794	baseUrl.RawQuery = params.Encode()
795
796	httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
797	if err != nil {
798		return nil, err
799	}
800	httpReq = httpReq.WithContext(ctx)
801	// Set the headers
802	for k, v := range c.xGoogMetadata {
803		httpReq.Header[k] = v
804	}
805	httpReq.Header["Content-Type"] = []string{"application/json"}
806
807	httpRsp, err := c.httpClient.Do(httpReq)
808	if err != nil {
809		return nil, err
810	}
811	defer httpRsp.Body.Close()
812
813	if err = googleapi.CheckResponse(httpRsp); err != nil {
814		return nil, err
815	}
816
817	buf, err := ioutil.ReadAll(httpRsp.Body)
818	if err != nil {
819		return nil, err
820	}
821
822	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
823	rsp := &computepb.Operation{}
824
825	if err := unm.Unmarshal(buf, rsp); err != nil {
826		return nil, err
827	}
828	op := &Operation{proto: rsp}
829	return op, err
830}
831
832// SetIamPolicy sets the access control policy on the specified resource. Replaces any existing policy.
833func (c *disksRESTClient) SetIamPolicy(ctx context.Context, req *computepb.SetIamPolicyDiskRequest, opts ...gax.CallOption) (*computepb.Policy, error) {
834	m := protojson.MarshalOptions{AllowPartial: true}
835	body := req.GetZoneSetPolicyRequestResource()
836	jsonReq, err := m.Marshal(body)
837	if err != nil {
838		return nil, err
839	}
840
841	baseUrl, _ := url.Parse(c.endpoint)
842	baseUrl.Path += fmt.Sprintf("/compute/v1/projects/%v/zones/%v/disks/%v/setIamPolicy", req.GetProject(), req.GetZone(), req.GetResource())
843
844	httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
845	if err != nil {
846		return nil, err
847	}
848	httpReq = httpReq.WithContext(ctx)
849	// Set the headers
850	for k, v := range c.xGoogMetadata {
851		httpReq.Header[k] = v
852	}
853	httpReq.Header["Content-Type"] = []string{"application/json"}
854
855	httpRsp, err := c.httpClient.Do(httpReq)
856	if err != nil {
857		return nil, err
858	}
859	defer httpRsp.Body.Close()
860
861	if err = googleapi.CheckResponse(httpRsp); err != nil {
862		return nil, err
863	}
864
865	buf, err := ioutil.ReadAll(httpRsp.Body)
866	if err != nil {
867		return nil, err
868	}
869
870	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
871	rsp := &computepb.Policy{}
872
873	return rsp, unm.Unmarshal(buf, rsp)
874}
875
876// SetLabels sets the labels on a disk. To learn more about labels, read the Labeling Resources documentation.
877func (c *disksRESTClient) SetLabels(ctx context.Context, req *computepb.SetLabelsDiskRequest, opts ...gax.CallOption) (*Operation, error) {
878	m := protojson.MarshalOptions{AllowPartial: true}
879	body := req.GetZoneSetLabelsRequestResource()
880	jsonReq, err := m.Marshal(body)
881	if err != nil {
882		return nil, err
883	}
884
885	baseUrl, _ := url.Parse(c.endpoint)
886	baseUrl.Path += fmt.Sprintf("/compute/v1/projects/%v/zones/%v/disks/%v/setLabels", req.GetProject(), req.GetZone(), req.GetResource())
887
888	params := url.Values{}
889	if req != nil && req.RequestId != nil {
890		params.Add("requestId", fmt.Sprintf("%v", req.GetRequestId()))
891	}
892
893	baseUrl.RawQuery = params.Encode()
894
895	httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
896	if err != nil {
897		return nil, err
898	}
899	httpReq = httpReq.WithContext(ctx)
900	// Set the headers
901	for k, v := range c.xGoogMetadata {
902		httpReq.Header[k] = v
903	}
904	httpReq.Header["Content-Type"] = []string{"application/json"}
905
906	httpRsp, err := c.httpClient.Do(httpReq)
907	if err != nil {
908		return nil, err
909	}
910	defer httpRsp.Body.Close()
911
912	if err = googleapi.CheckResponse(httpRsp); err != nil {
913		return nil, err
914	}
915
916	buf, err := ioutil.ReadAll(httpRsp.Body)
917	if err != nil {
918		return nil, err
919	}
920
921	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
922	rsp := &computepb.Operation{}
923
924	if err := unm.Unmarshal(buf, rsp); err != nil {
925		return nil, err
926	}
927	op := &Operation{proto: rsp}
928	return op, err
929}
930
931// TestIamPermissions returns permissions that a caller has on the specified resource.
932func (c *disksRESTClient) TestIamPermissions(ctx context.Context, req *computepb.TestIamPermissionsDiskRequest, opts ...gax.CallOption) (*computepb.TestPermissionsResponse, error) {
933	m := protojson.MarshalOptions{AllowPartial: true}
934	body := req.GetTestPermissionsRequestResource()
935	jsonReq, err := m.Marshal(body)
936	if err != nil {
937		return nil, err
938	}
939
940	baseUrl, _ := url.Parse(c.endpoint)
941	baseUrl.Path += fmt.Sprintf("/compute/v1/projects/%v/zones/%v/disks/%v/testIamPermissions", req.GetProject(), req.GetZone(), req.GetResource())
942
943	httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
944	if err != nil {
945		return nil, err
946	}
947	httpReq = httpReq.WithContext(ctx)
948	// Set the headers
949	for k, v := range c.xGoogMetadata {
950		httpReq.Header[k] = v
951	}
952	httpReq.Header["Content-Type"] = []string{"application/json"}
953
954	httpRsp, err := c.httpClient.Do(httpReq)
955	if err != nil {
956		return nil, err
957	}
958	defer httpRsp.Body.Close()
959
960	if err = googleapi.CheckResponse(httpRsp); err != nil {
961		return nil, err
962	}
963
964	buf, err := ioutil.ReadAll(httpRsp.Body)
965	if err != nil {
966		return nil, err
967	}
968
969	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
970	rsp := &computepb.TestPermissionsResponse{}
971
972	return rsp, unm.Unmarshal(buf, rsp)
973}
974
975// DiskIterator manages a stream of *computepb.Disk.
976type DiskIterator struct {
977	items    []*computepb.Disk
978	pageInfo *iterator.PageInfo
979	nextFunc func() error
980
981	// Response is the raw response for the current page.
982	// It must be cast to the RPC response type.
983	// Calling Next() or InternalFetch() updates this value.
984	Response interface{}
985
986	// InternalFetch is for use by the Google Cloud Libraries only.
987	// It is not part of the stable interface of this package.
988	//
989	// InternalFetch returns results from a single call to the underlying RPC.
990	// The number of results is no greater than pageSize.
991	// If there are no more results, nextPageToken is empty and err is nil.
992	InternalFetch func(pageSize int, pageToken string) (results []*computepb.Disk, nextPageToken string, err error)
993}
994
995// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
996func (it *DiskIterator) PageInfo() *iterator.PageInfo {
997	return it.pageInfo
998}
999
1000// Next returns the next result. Its second return value is iterator.Done if there are no more
1001// results. Once Next returns Done, all subsequent calls will return Done.
1002func (it *DiskIterator) Next() (*computepb.Disk, error) {
1003	var item *computepb.Disk
1004	if err := it.nextFunc(); err != nil {
1005		return item, err
1006	}
1007	item = it.items[0]
1008	it.items = it.items[1:]
1009	return item, nil
1010}
1011
1012func (it *DiskIterator) bufLen() int {
1013	return len(it.items)
1014}
1015
1016func (it *DiskIterator) takeBuf() interface{} {
1017	b := it.items
1018	it.items = nil
1019	return b
1020}
1021
1022// DisksScopedListPair is a holder type for string/*computepb.DisksScopedList map entries
1023type DisksScopedListPair struct {
1024	Key   string
1025	Value *computepb.DisksScopedList
1026}
1027
1028// DisksScopedListPairIterator manages a stream of DisksScopedListPair.
1029type DisksScopedListPairIterator struct {
1030	items    []DisksScopedListPair
1031	pageInfo *iterator.PageInfo
1032	nextFunc func() error
1033
1034	// Response is the raw response for the current page.
1035	// It must be cast to the RPC response type.
1036	// Calling Next() or InternalFetch() updates this value.
1037	Response interface{}
1038
1039	// InternalFetch is for use by the Google Cloud Libraries only.
1040	// It is not part of the stable interface of this package.
1041	//
1042	// InternalFetch returns results from a single call to the underlying RPC.
1043	// The number of results is no greater than pageSize.
1044	// If there are no more results, nextPageToken is empty and err is nil.
1045	InternalFetch func(pageSize int, pageToken string) (results []DisksScopedListPair, nextPageToken string, err error)
1046}
1047
1048// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
1049func (it *DisksScopedListPairIterator) PageInfo() *iterator.PageInfo {
1050	return it.pageInfo
1051}
1052
1053// Next returns the next result. Its second return value is iterator.Done if there are no more
1054// results. Once Next returns Done, all subsequent calls will return Done.
1055func (it *DisksScopedListPairIterator) Next() (DisksScopedListPair, error) {
1056	var item DisksScopedListPair
1057	if err := it.nextFunc(); err != nil {
1058		return item, err
1059	}
1060	item = it.items[0]
1061	it.items = it.items[1:]
1062	return item, nil
1063}
1064
1065func (it *DisksScopedListPairIterator) bufLen() int {
1066	return len(it.items)
1067}
1068
1069func (it *DisksScopedListPairIterator) takeBuf() interface{} {
1070	b := it.items
1071	it.items = nil
1072	return b
1073}
1074