github.com/openshift-online/ocm-sdk-go@v0.1.473/accountsmgmt/v1/account_client.go (about) 1 /* 2 Copyright (c) 2020 Red Hat, Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // IMPORTANT: This file has been generated automatically, refrain from modifying it manually as all 18 // your changes will be lost when the file is generated again. 19 20 package v1 // github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1 21 22 import ( 23 "bufio" 24 "bytes" 25 "context" 26 "io" 27 "net/http" 28 "net/url" 29 "path" 30 "time" 31 32 "github.com/openshift-online/ocm-sdk-go/errors" 33 "github.com/openshift-online/ocm-sdk-go/helpers" 34 ) 35 36 // AccountClient is the client of the 'account' resource. 37 // 38 // Manages a specific account. 39 type AccountClient struct { 40 transport http.RoundTripper 41 path string 42 } 43 44 // NewAccountClient creates a new client for the 'account' 45 // resource using the given transport to send the requests and receive the 46 // responses. 47 func NewAccountClient(transport http.RoundTripper, path string) *AccountClient { 48 return &AccountClient{ 49 transport: transport, 50 path: path, 51 } 52 } 53 54 // Delete creates a request for the 'delete' method. 55 func (c *AccountClient) Delete() *AccountDeleteRequest { 56 return &AccountDeleteRequest{ 57 transport: c.transport, 58 path: c.path, 59 } 60 } 61 62 // Get creates a request for the 'get' method. 63 // 64 // Retrieves the details of the account. 65 func (c *AccountClient) Get() *AccountGetRequest { 66 return &AccountGetRequest{ 67 transport: c.transport, 68 path: c.path, 69 } 70 } 71 72 // Update creates a request for the 'update' method. 73 // 74 // Updates the account. 75 func (c *AccountClient) Update() *AccountUpdateRequest { 76 return &AccountUpdateRequest{ 77 transport: c.transport, 78 path: c.path, 79 } 80 } 81 82 // Labels returns the target 'generic_labels' resource. 83 // 84 // Reference to the list of labels of a specific account. 85 func (c *AccountClient) Labels() *GenericLabelsClient { 86 return NewGenericLabelsClient( 87 c.transport, 88 path.Join(c.path, "labels"), 89 ) 90 } 91 92 // AccountPollRequest is the request for the Poll method. 93 type AccountPollRequest struct { 94 request *AccountGetRequest 95 interval time.Duration 96 statuses []int 97 predicates []func(interface{}) bool 98 } 99 100 // Parameter adds a query parameter to all the requests that will be used to retrieve the object. 101 func (r *AccountPollRequest) Parameter(name string, value interface{}) *AccountPollRequest { 102 r.request.Parameter(name, value) 103 return r 104 } 105 106 // Header adds a request header to all the requests that will be used to retrieve the object. 107 func (r *AccountPollRequest) Header(name string, value interface{}) *AccountPollRequest { 108 r.request.Header(name, value) 109 return r 110 } 111 112 // Interval sets the polling interval. This parameter is mandatory and must be greater than zero. 113 func (r *AccountPollRequest) Interval(value time.Duration) *AccountPollRequest { 114 r.interval = value 115 return r 116 } 117 118 // Status set the expected status of the response. Multiple values can be set calling this method 119 // multiple times. The response will be considered successful if the status is any of those values. 120 func (r *AccountPollRequest) Status(value int) *AccountPollRequest { 121 r.statuses = append(r.statuses, value) 122 return r 123 } 124 125 // Predicate adds a predicate that the response should satisfy be considered successful. Multiple 126 // predicates can be set calling this method multiple times. The response will be considered successful 127 // if all the predicates are satisfied. 128 func (r *AccountPollRequest) Predicate(value func(*AccountGetResponse) bool) *AccountPollRequest { 129 r.predicates = append(r.predicates, func(response interface{}) bool { 130 return value(response.(*AccountGetResponse)) 131 }) 132 return r 133 } 134 135 // StartContext starts the polling loop. Responses will be considered successful if the status is one of 136 // the values specified with the Status method and if all the predicates specified with the Predicate 137 // method return nil. 138 // 139 // The context must have a timeout or deadline, otherwise this method will immediately return an error. 140 func (r *AccountPollRequest) StartContext(ctx context.Context) (response *AccountPollResponse, err error) { 141 result, err := helpers.PollContext(ctx, r.interval, r.statuses, r.predicates, r.task) 142 if result != nil { 143 response = &AccountPollResponse{ 144 response: result.(*AccountGetResponse), 145 } 146 } 147 return 148 } 149 150 // task adapts the types of the request/response types so that they can be used with the generic 151 // polling function from the helpers package. 152 func (r *AccountPollRequest) task(ctx context.Context) (status int, result interface{}, err error) { 153 response, err := r.request.SendContext(ctx) 154 if response != nil { 155 status = response.Status() 156 result = response 157 } 158 return 159 } 160 161 // AccountPollResponse is the response for the Poll method. 162 type AccountPollResponse struct { 163 response *AccountGetResponse 164 } 165 166 // Status returns the response status code. 167 func (r *AccountPollResponse) Status() int { 168 if r == nil { 169 return 0 170 } 171 return r.response.Status() 172 } 173 174 // Header returns header of the response. 175 func (r *AccountPollResponse) Header() http.Header { 176 if r == nil { 177 return nil 178 } 179 return r.response.Header() 180 } 181 182 // Error returns the response error. 183 func (r *AccountPollResponse) Error() *errors.Error { 184 if r == nil { 185 return nil 186 } 187 return r.response.Error() 188 } 189 190 // Body returns the value of the 'body' parameter. 191 func (r *AccountPollResponse) Body() *Account { 192 return r.response.Body() 193 } 194 195 // GetBody returns the value of the 'body' parameter and 196 // a flag indicating if the parameter has a value. 197 func (r *AccountPollResponse) GetBody() (value *Account, ok bool) { 198 return r.response.GetBody() 199 } 200 201 // Poll creates a request to repeatedly retrieve the object till the response has one of a given set 202 // of states and satisfies a set of predicates. 203 func (c *AccountClient) Poll() *AccountPollRequest { 204 return &AccountPollRequest{ 205 request: c.Get(), 206 } 207 } 208 209 // AccountDeleteRequest is the request for the 'delete' method. 210 type AccountDeleteRequest struct { 211 transport http.RoundTripper 212 path string 213 query url.Values 214 header http.Header 215 deleteAssociatedResources *bool 216 } 217 218 // Parameter adds a query parameter. 219 func (r *AccountDeleteRequest) Parameter(name string, value interface{}) *AccountDeleteRequest { 220 helpers.AddValue(&r.query, name, value) 221 return r 222 } 223 224 // Header adds a request header. 225 func (r *AccountDeleteRequest) Header(name string, value interface{}) *AccountDeleteRequest { 226 helpers.AddHeader(&r.header, name, value) 227 return r 228 } 229 230 // Impersonate wraps requests on behalf of another user. 231 // Note: Services that do not support this feature may silently ignore this call. 232 func (r *AccountDeleteRequest) Impersonate(user string) *AccountDeleteRequest { 233 helpers.AddImpersonationHeader(&r.header, user) 234 return r 235 } 236 237 // DeleteAssociatedResources sets the value of the 'delete_associated_resources' parameter. 238 func (r *AccountDeleteRequest) DeleteAssociatedResources(value bool) *AccountDeleteRequest { 239 r.deleteAssociatedResources = &value 240 return r 241 } 242 243 // Send sends this request, waits for the response, and returns it. 244 // 245 // This is a potentially lengthy operation, as it requires network communication. 246 // Consider using a context and the SendContext method. 247 func (r *AccountDeleteRequest) Send() (result *AccountDeleteResponse, err error) { 248 return r.SendContext(context.Background()) 249 } 250 251 // SendContext sends this request, waits for the response, and returns it. 252 func (r *AccountDeleteRequest) SendContext(ctx context.Context) (result *AccountDeleteResponse, err error) { 253 query := helpers.CopyQuery(r.query) 254 if r.deleteAssociatedResources != nil { 255 helpers.AddValue(&query, "deleteAssociatedResources", *r.deleteAssociatedResources) 256 } 257 header := helpers.CopyHeader(r.header) 258 uri := &url.URL{ 259 Path: r.path, 260 RawQuery: query.Encode(), 261 } 262 request := &http.Request{ 263 Method: "DELETE", 264 URL: uri, 265 Header: header, 266 } 267 if ctx != nil { 268 request = request.WithContext(ctx) 269 } 270 response, err := r.transport.RoundTrip(request) 271 if err != nil { 272 return 273 } 274 defer response.Body.Close() 275 result = &AccountDeleteResponse{} 276 result.status = response.StatusCode 277 result.header = response.Header 278 reader := bufio.NewReader(response.Body) 279 _, err = reader.Peek(1) 280 if err == io.EOF { 281 err = nil 282 return 283 } 284 if result.status >= 400 { 285 result.err, err = errors.UnmarshalErrorStatus(reader, result.status) 286 if err != nil { 287 return 288 } 289 err = result.err 290 return 291 } 292 return 293 } 294 295 // AccountDeleteResponse is the response for the 'delete' method. 296 type AccountDeleteResponse struct { 297 status int 298 header http.Header 299 err *errors.Error 300 } 301 302 // Status returns the response status code. 303 func (r *AccountDeleteResponse) Status() int { 304 if r == nil { 305 return 0 306 } 307 return r.status 308 } 309 310 // Header returns header of the response. 311 func (r *AccountDeleteResponse) Header() http.Header { 312 if r == nil { 313 return nil 314 } 315 return r.header 316 } 317 318 // Error returns the response error. 319 func (r *AccountDeleteResponse) Error() *errors.Error { 320 if r == nil { 321 return nil 322 } 323 return r.err 324 } 325 326 // AccountGetRequest is the request for the 'get' method. 327 type AccountGetRequest struct { 328 transport http.RoundTripper 329 path string 330 query url.Values 331 header http.Header 332 } 333 334 // Parameter adds a query parameter. 335 func (r *AccountGetRequest) Parameter(name string, value interface{}) *AccountGetRequest { 336 helpers.AddValue(&r.query, name, value) 337 return r 338 } 339 340 // Header adds a request header. 341 func (r *AccountGetRequest) Header(name string, value interface{}) *AccountGetRequest { 342 helpers.AddHeader(&r.header, name, value) 343 return r 344 } 345 346 // Impersonate wraps requests on behalf of another user. 347 // Note: Services that do not support this feature may silently ignore this call. 348 func (r *AccountGetRequest) Impersonate(user string) *AccountGetRequest { 349 helpers.AddImpersonationHeader(&r.header, user) 350 return r 351 } 352 353 // Send sends this request, waits for the response, and returns it. 354 // 355 // This is a potentially lengthy operation, as it requires network communication. 356 // Consider using a context and the SendContext method. 357 func (r *AccountGetRequest) Send() (result *AccountGetResponse, err error) { 358 return r.SendContext(context.Background()) 359 } 360 361 // SendContext sends this request, waits for the response, and returns it. 362 func (r *AccountGetRequest) SendContext(ctx context.Context) (result *AccountGetResponse, err error) { 363 query := helpers.CopyQuery(r.query) 364 header := helpers.CopyHeader(r.header) 365 uri := &url.URL{ 366 Path: r.path, 367 RawQuery: query.Encode(), 368 } 369 request := &http.Request{ 370 Method: "GET", 371 URL: uri, 372 Header: header, 373 } 374 if ctx != nil { 375 request = request.WithContext(ctx) 376 } 377 response, err := r.transport.RoundTrip(request) 378 if err != nil { 379 return 380 } 381 defer response.Body.Close() 382 result = &AccountGetResponse{} 383 result.status = response.StatusCode 384 result.header = response.Header 385 reader := bufio.NewReader(response.Body) 386 _, err = reader.Peek(1) 387 if err == io.EOF { 388 err = nil 389 return 390 } 391 if result.status >= 400 { 392 result.err, err = errors.UnmarshalErrorStatus(reader, result.status) 393 if err != nil { 394 return 395 } 396 err = result.err 397 return 398 } 399 err = readAccountGetResponse(result, reader) 400 if err != nil { 401 return 402 } 403 return 404 } 405 406 // AccountGetResponse is the response for the 'get' method. 407 type AccountGetResponse struct { 408 status int 409 header http.Header 410 err *errors.Error 411 body *Account 412 } 413 414 // Status returns the response status code. 415 func (r *AccountGetResponse) Status() int { 416 if r == nil { 417 return 0 418 } 419 return r.status 420 } 421 422 // Header returns header of the response. 423 func (r *AccountGetResponse) Header() http.Header { 424 if r == nil { 425 return nil 426 } 427 return r.header 428 } 429 430 // Error returns the response error. 431 func (r *AccountGetResponse) Error() *errors.Error { 432 if r == nil { 433 return nil 434 } 435 return r.err 436 } 437 438 // Body returns the value of the 'body' parameter. 439 func (r *AccountGetResponse) Body() *Account { 440 if r == nil { 441 return nil 442 } 443 return r.body 444 } 445 446 // GetBody returns the value of the 'body' parameter and 447 // a flag indicating if the parameter has a value. 448 func (r *AccountGetResponse) GetBody() (value *Account, ok bool) { 449 ok = r != nil && r.body != nil 450 if ok { 451 value = r.body 452 } 453 return 454 } 455 456 // AccountUpdateRequest is the request for the 'update' method. 457 type AccountUpdateRequest struct { 458 transport http.RoundTripper 459 path string 460 query url.Values 461 header http.Header 462 body *Account 463 } 464 465 // Parameter adds a query parameter. 466 func (r *AccountUpdateRequest) Parameter(name string, value interface{}) *AccountUpdateRequest { 467 helpers.AddValue(&r.query, name, value) 468 return r 469 } 470 471 // Header adds a request header. 472 func (r *AccountUpdateRequest) Header(name string, value interface{}) *AccountUpdateRequest { 473 helpers.AddHeader(&r.header, name, value) 474 return r 475 } 476 477 // Impersonate wraps requests on behalf of another user. 478 // Note: Services that do not support this feature may silently ignore this call. 479 func (r *AccountUpdateRequest) Impersonate(user string) *AccountUpdateRequest { 480 helpers.AddImpersonationHeader(&r.header, user) 481 return r 482 } 483 484 // Body sets the value of the 'body' parameter. 485 func (r *AccountUpdateRequest) Body(value *Account) *AccountUpdateRequest { 486 r.body = value 487 return r 488 } 489 490 // Send sends this request, waits for the response, and returns it. 491 // 492 // This is a potentially lengthy operation, as it requires network communication. 493 // Consider using a context and the SendContext method. 494 func (r *AccountUpdateRequest) Send() (result *AccountUpdateResponse, err error) { 495 return r.SendContext(context.Background()) 496 } 497 498 // SendContext sends this request, waits for the response, and returns it. 499 func (r *AccountUpdateRequest) SendContext(ctx context.Context) (result *AccountUpdateResponse, err error) { 500 query := helpers.CopyQuery(r.query) 501 header := helpers.CopyHeader(r.header) 502 buffer := &bytes.Buffer{} 503 err = writeAccountUpdateRequest(r, buffer) 504 if err != nil { 505 return 506 } 507 uri := &url.URL{ 508 Path: r.path, 509 RawQuery: query.Encode(), 510 } 511 request := &http.Request{ 512 Method: "PATCH", 513 URL: uri, 514 Header: header, 515 Body: io.NopCloser(buffer), 516 } 517 if ctx != nil { 518 request = request.WithContext(ctx) 519 } 520 response, err := r.transport.RoundTrip(request) 521 if err != nil { 522 return 523 } 524 defer response.Body.Close() 525 result = &AccountUpdateResponse{} 526 result.status = response.StatusCode 527 result.header = response.Header 528 reader := bufio.NewReader(response.Body) 529 _, err = reader.Peek(1) 530 if err == io.EOF { 531 err = nil 532 return 533 } 534 if result.status >= 400 { 535 result.err, err = errors.UnmarshalErrorStatus(reader, result.status) 536 if err != nil { 537 return 538 } 539 err = result.err 540 return 541 } 542 err = readAccountUpdateResponse(result, reader) 543 if err != nil { 544 return 545 } 546 return 547 } 548 549 // AccountUpdateResponse is the response for the 'update' method. 550 type AccountUpdateResponse struct { 551 status int 552 header http.Header 553 err *errors.Error 554 body *Account 555 } 556 557 // Status returns the response status code. 558 func (r *AccountUpdateResponse) Status() int { 559 if r == nil { 560 return 0 561 } 562 return r.status 563 } 564 565 // Header returns header of the response. 566 func (r *AccountUpdateResponse) Header() http.Header { 567 if r == nil { 568 return nil 569 } 570 return r.header 571 } 572 573 // Error returns the response error. 574 func (r *AccountUpdateResponse) Error() *errors.Error { 575 if r == nil { 576 return nil 577 } 578 return r.err 579 } 580 581 // Body returns the value of the 'body' parameter. 582 func (r *AccountUpdateResponse) Body() *Account { 583 if r == nil { 584 return nil 585 } 586 return r.body 587 } 588 589 // GetBody returns the value of the 'body' parameter and 590 // a flag indicating if the parameter has a value. 591 func (r *AccountUpdateResponse) GetBody() (value *Account, ok bool) { 592 ok = r != nil && r.body != nil 593 if ok { 594 value = r.body 595 } 596 return 597 }