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