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