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