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, maybeUnknownEnum(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, maybeUnknownEnum(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, maybeUnknownEnum(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 if err := unm.Unmarshal(buf, rsp); err != nil { 534 return nil, maybeUnknownEnum(err) 535 } 536 return rsp, nil 537} 538 539// GetIamPolicy gets the access control policy for a resource. May be empty if no such policy or resource exists. 540func (c *disksRESTClient) GetIamPolicy(ctx context.Context, req *computepb.GetIamPolicyDiskRequest, opts ...gax.CallOption) (*computepb.Policy, error) { 541 baseUrl, _ := url.Parse(c.endpoint) 542 baseUrl.Path += fmt.Sprintf("/compute/v1/projects/%v/zones/%v/disks/%v/getIamPolicy", req.GetProject(), req.GetZone(), req.GetResource()) 543 544 params := url.Values{} 545 if req != nil && req.OptionsRequestedPolicyVersion != nil { 546 params.Add("optionsRequestedPolicyVersion", fmt.Sprintf("%v", req.GetOptionsRequestedPolicyVersion())) 547 } 548 549 baseUrl.RawQuery = params.Encode() 550 551 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil) 552 if err != nil { 553 return nil, err 554 } 555 httpReq = httpReq.WithContext(ctx) 556 // Set the headers 557 for k, v := range c.xGoogMetadata { 558 httpReq.Header[k] = v 559 } 560 httpReq.Header["Content-Type"] = []string{"application/json"} 561 562 httpRsp, err := c.httpClient.Do(httpReq) 563 if err != nil { 564 return nil, err 565 } 566 defer httpRsp.Body.Close() 567 568 if err = googleapi.CheckResponse(httpRsp); err != nil { 569 return nil, err 570 } 571 572 buf, err := ioutil.ReadAll(httpRsp.Body) 573 if err != nil { 574 return nil, err 575 } 576 577 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} 578 rsp := &computepb.Policy{} 579 580 if err := unm.Unmarshal(buf, rsp); err != nil { 581 return nil, maybeUnknownEnum(err) 582 } 583 return rsp, nil 584} 585 586// 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. 587func (c *disksRESTClient) Insert(ctx context.Context, req *computepb.InsertDiskRequest, opts ...gax.CallOption) (*Operation, error) { 588 m := protojson.MarshalOptions{AllowPartial: true} 589 body := req.GetDiskResource() 590 jsonReq, err := m.Marshal(body) 591 if err != nil { 592 return nil, err 593 } 594 595 baseUrl, _ := url.Parse(c.endpoint) 596 baseUrl.Path += fmt.Sprintf("/compute/v1/projects/%v/zones/%v/disks", req.GetProject(), req.GetZone()) 597 598 params := url.Values{} 599 if req != nil && req.RequestId != nil { 600 params.Add("requestId", fmt.Sprintf("%v", req.GetRequestId())) 601 } 602 if req != nil && req.SourceImage != nil { 603 params.Add("sourceImage", fmt.Sprintf("%v", req.GetSourceImage())) 604 } 605 606 baseUrl.RawQuery = params.Encode() 607 608 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq)) 609 if err != nil { 610 return nil, err 611 } 612 httpReq = httpReq.WithContext(ctx) 613 // Set the headers 614 for k, v := range c.xGoogMetadata { 615 httpReq.Header[k] = v 616 } 617 httpReq.Header["Content-Type"] = []string{"application/json"} 618 619 httpRsp, err := c.httpClient.Do(httpReq) 620 if err != nil { 621 return nil, err 622 } 623 defer httpRsp.Body.Close() 624 625 if err = googleapi.CheckResponse(httpRsp); err != nil { 626 return nil, err 627 } 628 629 buf, err := ioutil.ReadAll(httpRsp.Body) 630 if err != nil { 631 return nil, err 632 } 633 634 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} 635 rsp := &computepb.Operation{} 636 637 if err := unm.Unmarshal(buf, rsp); err != nil { 638 return nil, maybeUnknownEnum(err) 639 } 640 op := &Operation{proto: rsp} 641 return op, err 642} 643 644// List retrieves a list of persistent disks contained within the specified zone. 645func (c *disksRESTClient) List(ctx context.Context, req *computepb.ListDisksRequest, opts ...gax.CallOption) *DiskIterator { 646 it := &DiskIterator{} 647 req = proto.Clone(req).(*computepb.ListDisksRequest) 648 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} 649 it.InternalFetch = func(pageSize int, pageToken string) ([]*computepb.Disk, string, error) { 650 resp := &computepb.DiskList{} 651 if pageToken != "" { 652 req.PageToken = proto.String(pageToken) 653 } 654 if pageSize > math.MaxInt32 { 655 req.MaxResults = proto.Uint32(math.MaxInt32) 656 } else if pageSize != 0 { 657 req.MaxResults = proto.Uint32(uint32(pageSize)) 658 } 659 baseUrl, _ := url.Parse(c.endpoint) 660 baseUrl.Path += fmt.Sprintf("/compute/v1/projects/%v/zones/%v/disks", req.GetProject(), req.GetZone()) 661 662 params := url.Values{} 663 if req != nil && req.Filter != nil { 664 params.Add("filter", fmt.Sprintf("%v", req.GetFilter())) 665 } 666 if req != nil && req.MaxResults != nil { 667 params.Add("maxResults", fmt.Sprintf("%v", req.GetMaxResults())) 668 } 669 if req != nil && req.OrderBy != nil { 670 params.Add("orderBy", fmt.Sprintf("%v", req.GetOrderBy())) 671 } 672 if req != nil && req.PageToken != nil { 673 params.Add("pageToken", fmt.Sprintf("%v", req.GetPageToken())) 674 } 675 if req != nil && req.ReturnPartialSuccess != nil { 676 params.Add("returnPartialSuccess", fmt.Sprintf("%v", req.GetReturnPartialSuccess())) 677 } 678 679 baseUrl.RawQuery = params.Encode() 680 681 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil) 682 if err != nil { 683 return nil, "", err 684 } 685 686 // Set the headers 687 for k, v := range c.xGoogMetadata { 688 httpReq.Header[k] = v 689 } 690 691 httpReq.Header["Content-Type"] = []string{"application/json"} 692 httpRsp, err := c.httpClient.Do(httpReq) 693 if err != nil { 694 return nil, "", err 695 } 696 defer httpRsp.Body.Close() 697 698 if err = googleapi.CheckResponse(httpRsp); err != nil { 699 return nil, "", err 700 } 701 702 buf, err := ioutil.ReadAll(httpRsp.Body) 703 if err != nil { 704 return nil, "", err 705 } 706 707 unm.Unmarshal(buf, resp) 708 it.Response = resp 709 return resp.GetItems(), resp.GetNextPageToken(), nil 710 } 711 712 fetch := func(pageSize int, pageToken string) (string, error) { 713 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 714 if err != nil { 715 return "", err 716 } 717 it.items = append(it.items, items...) 718 return nextPageToken, nil 719 } 720 721 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 722 it.pageInfo.MaxSize = int(req.GetMaxResults()) 723 it.pageInfo.Token = req.GetPageToken() 724 725 return it 726} 727 728// RemoveResourcePolicies removes resource policies from a disk. 729func (c *disksRESTClient) RemoveResourcePolicies(ctx context.Context, req *computepb.RemoveResourcePoliciesDiskRequest, opts ...gax.CallOption) (*Operation, error) { 730 m := protojson.MarshalOptions{AllowPartial: true} 731 body := req.GetDisksRemoveResourcePoliciesRequestResource() 732 jsonReq, err := m.Marshal(body) 733 if err != nil { 734 return nil, err 735 } 736 737 baseUrl, _ := url.Parse(c.endpoint) 738 baseUrl.Path += fmt.Sprintf("/compute/v1/projects/%v/zones/%v/disks/%v/removeResourcePolicies", req.GetProject(), req.GetZone(), req.GetDisk()) 739 740 params := url.Values{} 741 if req != nil && req.RequestId != nil { 742 params.Add("requestId", fmt.Sprintf("%v", req.GetRequestId())) 743 } 744 745 baseUrl.RawQuery = params.Encode() 746 747 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq)) 748 if err != nil { 749 return nil, err 750 } 751 httpReq = httpReq.WithContext(ctx) 752 // Set the headers 753 for k, v := range c.xGoogMetadata { 754 httpReq.Header[k] = v 755 } 756 httpReq.Header["Content-Type"] = []string{"application/json"} 757 758 httpRsp, err := c.httpClient.Do(httpReq) 759 if err != nil { 760 return nil, err 761 } 762 defer httpRsp.Body.Close() 763 764 if err = googleapi.CheckResponse(httpRsp); err != nil { 765 return nil, err 766 } 767 768 buf, err := ioutil.ReadAll(httpRsp.Body) 769 if err != nil { 770 return nil, err 771 } 772 773 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} 774 rsp := &computepb.Operation{} 775 776 if err := unm.Unmarshal(buf, rsp); err != nil { 777 return nil, maybeUnknownEnum(err) 778 } 779 op := &Operation{proto: rsp} 780 return op, err 781} 782 783// Resize resizes the specified persistent disk. You can only increase the size of the disk. 784func (c *disksRESTClient) Resize(ctx context.Context, req *computepb.ResizeDiskRequest, opts ...gax.CallOption) (*Operation, error) { 785 m := protojson.MarshalOptions{AllowPartial: true} 786 body := req.GetDisksResizeRequestResource() 787 jsonReq, err := m.Marshal(body) 788 if err != nil { 789 return nil, err 790 } 791 792 baseUrl, _ := url.Parse(c.endpoint) 793 baseUrl.Path += fmt.Sprintf("/compute/v1/projects/%v/zones/%v/disks/%v/resize", req.GetProject(), req.GetZone(), req.GetDisk()) 794 795 params := url.Values{} 796 if req != nil && req.RequestId != nil { 797 params.Add("requestId", fmt.Sprintf("%v", req.GetRequestId())) 798 } 799 800 baseUrl.RawQuery = params.Encode() 801 802 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq)) 803 if err != nil { 804 return nil, err 805 } 806 httpReq = httpReq.WithContext(ctx) 807 // Set the headers 808 for k, v := range c.xGoogMetadata { 809 httpReq.Header[k] = v 810 } 811 httpReq.Header["Content-Type"] = []string{"application/json"} 812 813 httpRsp, err := c.httpClient.Do(httpReq) 814 if err != nil { 815 return nil, err 816 } 817 defer httpRsp.Body.Close() 818 819 if err = googleapi.CheckResponse(httpRsp); err != nil { 820 return nil, err 821 } 822 823 buf, err := ioutil.ReadAll(httpRsp.Body) 824 if err != nil { 825 return nil, err 826 } 827 828 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} 829 rsp := &computepb.Operation{} 830 831 if err := unm.Unmarshal(buf, rsp); err != nil { 832 return nil, maybeUnknownEnum(err) 833 } 834 op := &Operation{proto: rsp} 835 return op, err 836} 837 838// SetIamPolicy sets the access control policy on the specified resource. Replaces any existing policy. 839func (c *disksRESTClient) SetIamPolicy(ctx context.Context, req *computepb.SetIamPolicyDiskRequest, opts ...gax.CallOption) (*computepb.Policy, error) { 840 m := protojson.MarshalOptions{AllowPartial: true} 841 body := req.GetZoneSetPolicyRequestResource() 842 jsonReq, err := m.Marshal(body) 843 if err != nil { 844 return nil, err 845 } 846 847 baseUrl, _ := url.Parse(c.endpoint) 848 baseUrl.Path += fmt.Sprintf("/compute/v1/projects/%v/zones/%v/disks/%v/setIamPolicy", req.GetProject(), req.GetZone(), req.GetResource()) 849 850 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq)) 851 if err != nil { 852 return nil, err 853 } 854 httpReq = httpReq.WithContext(ctx) 855 // Set the headers 856 for k, v := range c.xGoogMetadata { 857 httpReq.Header[k] = v 858 } 859 httpReq.Header["Content-Type"] = []string{"application/json"} 860 861 httpRsp, err := c.httpClient.Do(httpReq) 862 if err != nil { 863 return nil, err 864 } 865 defer httpRsp.Body.Close() 866 867 if err = googleapi.CheckResponse(httpRsp); err != nil { 868 return nil, err 869 } 870 871 buf, err := ioutil.ReadAll(httpRsp.Body) 872 if err != nil { 873 return nil, err 874 } 875 876 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} 877 rsp := &computepb.Policy{} 878 879 if err := unm.Unmarshal(buf, rsp); err != nil { 880 return nil, maybeUnknownEnum(err) 881 } 882 return rsp, nil 883} 884 885// SetLabels sets the labels on a disk. To learn more about labels, read the Labeling Resources documentation. 886func (c *disksRESTClient) SetLabels(ctx context.Context, req *computepb.SetLabelsDiskRequest, opts ...gax.CallOption) (*Operation, error) { 887 m := protojson.MarshalOptions{AllowPartial: true} 888 body := req.GetZoneSetLabelsRequestResource() 889 jsonReq, err := m.Marshal(body) 890 if err != nil { 891 return nil, err 892 } 893 894 baseUrl, _ := url.Parse(c.endpoint) 895 baseUrl.Path += fmt.Sprintf("/compute/v1/projects/%v/zones/%v/disks/%v/setLabels", req.GetProject(), req.GetZone(), req.GetResource()) 896 897 params := url.Values{} 898 if req != nil && req.RequestId != nil { 899 params.Add("requestId", fmt.Sprintf("%v", req.GetRequestId())) 900 } 901 902 baseUrl.RawQuery = params.Encode() 903 904 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq)) 905 if err != nil { 906 return nil, err 907 } 908 httpReq = httpReq.WithContext(ctx) 909 // Set the headers 910 for k, v := range c.xGoogMetadata { 911 httpReq.Header[k] = v 912 } 913 httpReq.Header["Content-Type"] = []string{"application/json"} 914 915 httpRsp, err := c.httpClient.Do(httpReq) 916 if err != nil { 917 return nil, err 918 } 919 defer httpRsp.Body.Close() 920 921 if err = googleapi.CheckResponse(httpRsp); err != nil { 922 return nil, err 923 } 924 925 buf, err := ioutil.ReadAll(httpRsp.Body) 926 if err != nil { 927 return nil, err 928 } 929 930 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} 931 rsp := &computepb.Operation{} 932 933 if err := unm.Unmarshal(buf, rsp); err != nil { 934 return nil, maybeUnknownEnum(err) 935 } 936 op := &Operation{proto: rsp} 937 return op, err 938} 939 940// TestIamPermissions returns permissions that a caller has on the specified resource. 941func (c *disksRESTClient) TestIamPermissions(ctx context.Context, req *computepb.TestIamPermissionsDiskRequest, opts ...gax.CallOption) (*computepb.TestPermissionsResponse, error) { 942 m := protojson.MarshalOptions{AllowPartial: true} 943 body := req.GetTestPermissionsRequestResource() 944 jsonReq, err := m.Marshal(body) 945 if err != nil { 946 return nil, err 947 } 948 949 baseUrl, _ := url.Parse(c.endpoint) 950 baseUrl.Path += fmt.Sprintf("/compute/v1/projects/%v/zones/%v/disks/%v/testIamPermissions", req.GetProject(), req.GetZone(), req.GetResource()) 951 952 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq)) 953 if err != nil { 954 return nil, err 955 } 956 httpReq = httpReq.WithContext(ctx) 957 // Set the headers 958 for k, v := range c.xGoogMetadata { 959 httpReq.Header[k] = v 960 } 961 httpReq.Header["Content-Type"] = []string{"application/json"} 962 963 httpRsp, err := c.httpClient.Do(httpReq) 964 if err != nil { 965 return nil, err 966 } 967 defer httpRsp.Body.Close() 968 969 if err = googleapi.CheckResponse(httpRsp); err != nil { 970 return nil, err 971 } 972 973 buf, err := ioutil.ReadAll(httpRsp.Body) 974 if err != nil { 975 return nil, err 976 } 977 978 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} 979 rsp := &computepb.TestPermissionsResponse{} 980 981 if err := unm.Unmarshal(buf, rsp); err != nil { 982 return nil, maybeUnknownEnum(err) 983 } 984 return rsp, nil 985} 986 987// DiskIterator manages a stream of *computepb.Disk. 988type DiskIterator struct { 989 items []*computepb.Disk 990 pageInfo *iterator.PageInfo 991 nextFunc func() error 992 993 // Response is the raw response for the current page. 994 // It must be cast to the RPC response type. 995 // Calling Next() or InternalFetch() updates this value. 996 Response interface{} 997 998 // InternalFetch is for use by the Google Cloud Libraries only. 999 // It is not part of the stable interface of this package. 1000 // 1001 // InternalFetch returns results from a single call to the underlying RPC. 1002 // The number of results is no greater than pageSize. 1003 // If there are no more results, nextPageToken is empty and err is nil. 1004 InternalFetch func(pageSize int, pageToken string) (results []*computepb.Disk, nextPageToken string, err error) 1005} 1006 1007// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 1008func (it *DiskIterator) PageInfo() *iterator.PageInfo { 1009 return it.pageInfo 1010} 1011 1012// Next returns the next result. Its second return value is iterator.Done if there are no more 1013// results. Once Next returns Done, all subsequent calls will return Done. 1014func (it *DiskIterator) Next() (*computepb.Disk, error) { 1015 var item *computepb.Disk 1016 if err := it.nextFunc(); err != nil { 1017 return item, err 1018 } 1019 item = it.items[0] 1020 it.items = it.items[1:] 1021 return item, nil 1022} 1023 1024func (it *DiskIterator) bufLen() int { 1025 return len(it.items) 1026} 1027 1028func (it *DiskIterator) takeBuf() interface{} { 1029 b := it.items 1030 it.items = nil 1031 return b 1032} 1033 1034// DisksScopedListPair is a holder type for string/*computepb.DisksScopedList map entries 1035type DisksScopedListPair struct { 1036 Key string 1037 Value *computepb.DisksScopedList 1038} 1039 1040// DisksScopedListPairIterator manages a stream of DisksScopedListPair. 1041type DisksScopedListPairIterator struct { 1042 items []DisksScopedListPair 1043 pageInfo *iterator.PageInfo 1044 nextFunc func() error 1045 1046 // Response is the raw response for the current page. 1047 // It must be cast to the RPC response type. 1048 // Calling Next() or InternalFetch() updates this value. 1049 Response interface{} 1050 1051 // InternalFetch is for use by the Google Cloud Libraries only. 1052 // It is not part of the stable interface of this package. 1053 // 1054 // InternalFetch returns results from a single call to the underlying RPC. 1055 // The number of results is no greater than pageSize. 1056 // If there are no more results, nextPageToken is empty and err is nil. 1057 InternalFetch func(pageSize int, pageToken string) (results []DisksScopedListPair, nextPageToken string, err error) 1058} 1059 1060// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 1061func (it *DisksScopedListPairIterator) PageInfo() *iterator.PageInfo { 1062 return it.pageInfo 1063} 1064 1065// Next returns the next result. Its second return value is iterator.Done if there are no more 1066// results. Once Next returns Done, all subsequent calls will return Done. 1067func (it *DisksScopedListPairIterator) Next() (DisksScopedListPair, error) { 1068 var item DisksScopedListPair 1069 if err := it.nextFunc(); err != nil { 1070 return item, err 1071 } 1072 item = it.items[0] 1073 it.items = it.items[1:] 1074 return item, nil 1075} 1076 1077func (it *DisksScopedListPairIterator) bufLen() int { 1078 return len(it.items) 1079} 1080 1081func (it *DisksScopedListPairIterator) takeBuf() interface{} { 1082 b := it.items 1083 it.items = nil 1084 return b 1085} 1086