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