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