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