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