github.com/openshift-online/ocm-sdk-go@v0.1.473/clustersmgmt/v1/flavour_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/clustersmgmt/v1 21 22 import ( 23 "bufio" 24 "bytes" 25 "context" 26 "io" 27 "net/http" 28 "net/url" 29 "time" 30 31 "github.com/openshift-online/ocm-sdk-go/errors" 32 "github.com/openshift-online/ocm-sdk-go/helpers" 33 ) 34 35 // FlavourClient is the client of the 'flavour' resource. 36 // 37 // Manages a specific cluster flavour. 38 type FlavourClient struct { 39 transport http.RoundTripper 40 path string 41 } 42 43 // NewFlavourClient creates a new client for the 'flavour' 44 // resource using the given transport to send the requests and receive the 45 // responses. 46 func NewFlavourClient(transport http.RoundTripper, path string) *FlavourClient { 47 return &FlavourClient{ 48 transport: transport, 49 path: path, 50 } 51 } 52 53 // Get creates a request for the 'get' method. 54 // 55 // Retrieves the details of the cluster flavour. 56 func (c *FlavourClient) Get() *FlavourGetRequest { 57 return &FlavourGetRequest{ 58 transport: c.transport, 59 path: c.path, 60 } 61 } 62 63 // Update creates a request for the 'update' method. 64 // 65 // Updates the flavour. 66 // 67 // Attributes that can be updated are: 68 // 69 // - `aws.infra_volume` 70 // - `aws.infra_instance_type` 71 // - `gcp.infra_instance_type` 72 func (c *FlavourClient) Update() *FlavourUpdateRequest { 73 return &FlavourUpdateRequest{ 74 transport: c.transport, 75 path: c.path, 76 } 77 } 78 79 // FlavourPollRequest is the request for the Poll method. 80 type FlavourPollRequest struct { 81 request *FlavourGetRequest 82 interval time.Duration 83 statuses []int 84 predicates []func(interface{}) bool 85 } 86 87 // Parameter adds a query parameter to all the requests that will be used to retrieve the object. 88 func (r *FlavourPollRequest) Parameter(name string, value interface{}) *FlavourPollRequest { 89 r.request.Parameter(name, value) 90 return r 91 } 92 93 // Header adds a request header to all the requests that will be used to retrieve the object. 94 func (r *FlavourPollRequest) Header(name string, value interface{}) *FlavourPollRequest { 95 r.request.Header(name, value) 96 return r 97 } 98 99 // Interval sets the polling interval. This parameter is mandatory and must be greater than zero. 100 func (r *FlavourPollRequest) Interval(value time.Duration) *FlavourPollRequest { 101 r.interval = value 102 return r 103 } 104 105 // Status set the expected status of the response. Multiple values can be set calling this method 106 // multiple times. The response will be considered successful if the status is any of those values. 107 func (r *FlavourPollRequest) Status(value int) *FlavourPollRequest { 108 r.statuses = append(r.statuses, value) 109 return r 110 } 111 112 // Predicate adds a predicate that the response should satisfy be considered successful. Multiple 113 // predicates can be set calling this method multiple times. The response will be considered successful 114 // if all the predicates are satisfied. 115 func (r *FlavourPollRequest) Predicate(value func(*FlavourGetResponse) bool) *FlavourPollRequest { 116 r.predicates = append(r.predicates, func(response interface{}) bool { 117 return value(response.(*FlavourGetResponse)) 118 }) 119 return r 120 } 121 122 // StartContext starts the polling loop. Responses will be considered successful if the status is one of 123 // the values specified with the Status method and if all the predicates specified with the Predicate 124 // method return nil. 125 // 126 // The context must have a timeout or deadline, otherwise this method will immediately return an error. 127 func (r *FlavourPollRequest) StartContext(ctx context.Context) (response *FlavourPollResponse, err error) { 128 result, err := helpers.PollContext(ctx, r.interval, r.statuses, r.predicates, r.task) 129 if result != nil { 130 response = &FlavourPollResponse{ 131 response: result.(*FlavourGetResponse), 132 } 133 } 134 return 135 } 136 137 // task adapts the types of the request/response types so that they can be used with the generic 138 // polling function from the helpers package. 139 func (r *FlavourPollRequest) task(ctx context.Context) (status int, result interface{}, err error) { 140 response, err := r.request.SendContext(ctx) 141 if response != nil { 142 status = response.Status() 143 result = response 144 } 145 return 146 } 147 148 // FlavourPollResponse is the response for the Poll method. 149 type FlavourPollResponse struct { 150 response *FlavourGetResponse 151 } 152 153 // Status returns the response status code. 154 func (r *FlavourPollResponse) Status() int { 155 if r == nil { 156 return 0 157 } 158 return r.response.Status() 159 } 160 161 // Header returns header of the response. 162 func (r *FlavourPollResponse) Header() http.Header { 163 if r == nil { 164 return nil 165 } 166 return r.response.Header() 167 } 168 169 // Error returns the response error. 170 func (r *FlavourPollResponse) Error() *errors.Error { 171 if r == nil { 172 return nil 173 } 174 return r.response.Error() 175 } 176 177 // Body returns the value of the 'body' parameter. 178 func (r *FlavourPollResponse) Body() *Flavour { 179 return r.response.Body() 180 } 181 182 // GetBody returns the value of the 'body' parameter and 183 // a flag indicating if the parameter has a value. 184 func (r *FlavourPollResponse) GetBody() (value *Flavour, ok bool) { 185 return r.response.GetBody() 186 } 187 188 // Poll creates a request to repeatedly retrieve the object till the response has one of a given set 189 // of states and satisfies a set of predicates. 190 func (c *FlavourClient) Poll() *FlavourPollRequest { 191 return &FlavourPollRequest{ 192 request: c.Get(), 193 } 194 } 195 196 // FlavourGetRequest is the request for the 'get' method. 197 type FlavourGetRequest struct { 198 transport http.RoundTripper 199 path string 200 query url.Values 201 header http.Header 202 } 203 204 // Parameter adds a query parameter. 205 func (r *FlavourGetRequest) Parameter(name string, value interface{}) *FlavourGetRequest { 206 helpers.AddValue(&r.query, name, value) 207 return r 208 } 209 210 // Header adds a request header. 211 func (r *FlavourGetRequest) Header(name string, value interface{}) *FlavourGetRequest { 212 helpers.AddHeader(&r.header, name, value) 213 return r 214 } 215 216 // Impersonate wraps requests on behalf of another user. 217 // Note: Services that do not support this feature may silently ignore this call. 218 func (r *FlavourGetRequest) Impersonate(user string) *FlavourGetRequest { 219 helpers.AddImpersonationHeader(&r.header, user) 220 return r 221 } 222 223 // Send sends this request, waits for the response, and returns it. 224 // 225 // This is a potentially lengthy operation, as it requires network communication. 226 // Consider using a context and the SendContext method. 227 func (r *FlavourGetRequest) Send() (result *FlavourGetResponse, err error) { 228 return r.SendContext(context.Background()) 229 } 230 231 // SendContext sends this request, waits for the response, and returns it. 232 func (r *FlavourGetRequest) SendContext(ctx context.Context) (result *FlavourGetResponse, err error) { 233 query := helpers.CopyQuery(r.query) 234 header := helpers.CopyHeader(r.header) 235 uri := &url.URL{ 236 Path: r.path, 237 RawQuery: query.Encode(), 238 } 239 request := &http.Request{ 240 Method: "GET", 241 URL: uri, 242 Header: header, 243 } 244 if ctx != nil { 245 request = request.WithContext(ctx) 246 } 247 response, err := r.transport.RoundTrip(request) 248 if err != nil { 249 return 250 } 251 defer response.Body.Close() 252 result = &FlavourGetResponse{} 253 result.status = response.StatusCode 254 result.header = response.Header 255 reader := bufio.NewReader(response.Body) 256 _, err = reader.Peek(1) 257 if err == io.EOF { 258 err = nil 259 return 260 } 261 if result.status >= 400 { 262 result.err, err = errors.UnmarshalErrorStatus(reader, result.status) 263 if err != nil { 264 return 265 } 266 err = result.err 267 return 268 } 269 err = readFlavourGetResponse(result, reader) 270 if err != nil { 271 return 272 } 273 return 274 } 275 276 // FlavourGetResponse is the response for the 'get' method. 277 type FlavourGetResponse struct { 278 status int 279 header http.Header 280 err *errors.Error 281 body *Flavour 282 } 283 284 // Status returns the response status code. 285 func (r *FlavourGetResponse) Status() int { 286 if r == nil { 287 return 0 288 } 289 return r.status 290 } 291 292 // Header returns header of the response. 293 func (r *FlavourGetResponse) Header() http.Header { 294 if r == nil { 295 return nil 296 } 297 return r.header 298 } 299 300 // Error returns the response error. 301 func (r *FlavourGetResponse) Error() *errors.Error { 302 if r == nil { 303 return nil 304 } 305 return r.err 306 } 307 308 // Body returns the value of the 'body' parameter. 309 func (r *FlavourGetResponse) Body() *Flavour { 310 if r == nil { 311 return nil 312 } 313 return r.body 314 } 315 316 // GetBody returns the value of the 'body' parameter and 317 // a flag indicating if the parameter has a value. 318 func (r *FlavourGetResponse) GetBody() (value *Flavour, ok bool) { 319 ok = r != nil && r.body != nil 320 if ok { 321 value = r.body 322 } 323 return 324 } 325 326 // FlavourUpdateRequest is the request for the 'update' method. 327 type FlavourUpdateRequest struct { 328 transport http.RoundTripper 329 path string 330 query url.Values 331 header http.Header 332 body *Flavour 333 } 334 335 // Parameter adds a query parameter. 336 func (r *FlavourUpdateRequest) Parameter(name string, value interface{}) *FlavourUpdateRequest { 337 helpers.AddValue(&r.query, name, value) 338 return r 339 } 340 341 // Header adds a request header. 342 func (r *FlavourUpdateRequest) Header(name string, value interface{}) *FlavourUpdateRequest { 343 helpers.AddHeader(&r.header, name, value) 344 return r 345 } 346 347 // Impersonate wraps requests on behalf of another user. 348 // Note: Services that do not support this feature may silently ignore this call. 349 func (r *FlavourUpdateRequest) Impersonate(user string) *FlavourUpdateRequest { 350 helpers.AddImpersonationHeader(&r.header, user) 351 return r 352 } 353 354 // Body sets the value of the 'body' parameter. 355 func (r *FlavourUpdateRequest) Body(value *Flavour) *FlavourUpdateRequest { 356 r.body = value 357 return r 358 } 359 360 // Send sends this request, waits for the response, and returns it. 361 // 362 // This is a potentially lengthy operation, as it requires network communication. 363 // Consider using a context and the SendContext method. 364 func (r *FlavourUpdateRequest) Send() (result *FlavourUpdateResponse, err error) { 365 return r.SendContext(context.Background()) 366 } 367 368 // SendContext sends this request, waits for the response, and returns it. 369 func (r *FlavourUpdateRequest) SendContext(ctx context.Context) (result *FlavourUpdateResponse, err error) { 370 query := helpers.CopyQuery(r.query) 371 header := helpers.CopyHeader(r.header) 372 buffer := &bytes.Buffer{} 373 err = writeFlavourUpdateRequest(r, buffer) 374 if err != nil { 375 return 376 } 377 uri := &url.URL{ 378 Path: r.path, 379 RawQuery: query.Encode(), 380 } 381 request := &http.Request{ 382 Method: "PATCH", 383 URL: uri, 384 Header: header, 385 Body: io.NopCloser(buffer), 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 = &FlavourUpdateResponse{} 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 = readFlavourUpdateResponse(result, reader) 413 if err != nil { 414 return 415 } 416 return 417 } 418 419 // FlavourUpdateResponse is the response for the 'update' method. 420 type FlavourUpdateResponse struct { 421 status int 422 header http.Header 423 err *errors.Error 424 body *Flavour 425 } 426 427 // Status returns the response status code. 428 func (r *FlavourUpdateResponse) 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 *FlavourUpdateResponse) 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 *FlavourUpdateResponse) 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 *FlavourUpdateResponse) Body() *Flavour { 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 *FlavourUpdateResponse) GetBody() (value *Flavour, ok bool) { 462 ok = r != nil && r.body != nil 463 if ok { 464 value = r.body 465 } 466 return 467 }