github.com/openshift-online/ocm-sdk-go@v0.1.473/osdfleetmgmt/v1/service_cluster_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/osdfleetmgmt/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  // ServiceClusterClient is the client of the 'service_cluster' resource.
    37  //
    38  // Manages a specific service cluster.
    39  type ServiceClusterClient struct {
    40  	transport http.RoundTripper
    41  	path      string
    42  }
    43  
    44  // NewServiceClusterClient creates a new client for the 'service_cluster'
    45  // resource using the given transport to send the requests and receive the
    46  // responses.
    47  func NewServiceClusterClient(transport http.RoundTripper, path string) *ServiceClusterClient {
    48  	return &ServiceClusterClient{
    49  		transport: transport,
    50  		path:      path,
    51  	}
    52  }
    53  
    54  // Delete creates a request for the 'delete' method.
    55  //
    56  // Deletes the service cluster.
    57  func (c *ServiceClusterClient) Delete() *ServiceClusterDeleteRequest {
    58  	return &ServiceClusterDeleteRequest{
    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 cluster.
    67  func (c *ServiceClusterClient) Get() *ServiceClusterGetRequest {
    68  	return &ServiceClusterGetRequest{
    69  		transport: c.transport,
    70  		path:      c.path,
    71  	}
    72  }
    73  
    74  // Post creates a request for the 'post' method.
    75  //
    76  // Updates the service cluster.
    77  func (c *ServiceClusterClient) Post() *ServiceClusterPostRequest {
    78  	return &ServiceClusterPostRequest{
    79  		transport: c.transport,
    80  		path:      c.path,
    81  	}
    82  }
    83  
    84  // Labels returns the target 'labels' resource.
    85  //
    86  // Reference to the resource that manages the collection of label
    87  func (c *ServiceClusterClient) Labels() *LabelsClient {
    88  	return NewLabelsClient(
    89  		c.transport,
    90  		path.Join(c.path, "labels"),
    91  	)
    92  }
    93  
    94  // ServiceClusterPollRequest is the request for the Poll method.
    95  type ServiceClusterPollRequest struct {
    96  	request    *ServiceClusterGetRequest
    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 *ServiceClusterPollRequest) Parameter(name string, value interface{}) *ServiceClusterPollRequest {
   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 *ServiceClusterPollRequest) Header(name string, value interface{}) *ServiceClusterPollRequest {
   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 *ServiceClusterPollRequest) Interval(value time.Duration) *ServiceClusterPollRequest {
   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 *ServiceClusterPollRequest) Status(value int) *ServiceClusterPollRequest {
   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 *ServiceClusterPollRequest) Predicate(value func(*ServiceClusterGetResponse) bool) *ServiceClusterPollRequest {
   131  	r.predicates = append(r.predicates, func(response interface{}) bool {
   132  		return value(response.(*ServiceClusterGetResponse))
   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 *ServiceClusterPollRequest) StartContext(ctx context.Context) (response *ServiceClusterPollResponse, err error) {
   143  	result, err := helpers.PollContext(ctx, r.interval, r.statuses, r.predicates, r.task)
   144  	if result != nil {
   145  		response = &ServiceClusterPollResponse{
   146  			response: result.(*ServiceClusterGetResponse),
   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 *ServiceClusterPollRequest) 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  // ServiceClusterPollResponse is the response for the Poll method.
   164  type ServiceClusterPollResponse struct {
   165  	response *ServiceClusterGetResponse
   166  }
   167  
   168  // Status returns the response status code.
   169  func (r *ServiceClusterPollResponse) 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 *ServiceClusterPollResponse) 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 *ServiceClusterPollResponse) 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 *ServiceClusterPollResponse) Body() *ServiceCluster {
   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 *ServiceClusterPollResponse) GetBody() (value *ServiceCluster, 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 *ServiceClusterClient) Poll() *ServiceClusterPollRequest {
   206  	return &ServiceClusterPollRequest{
   207  		request: c.Get(),
   208  	}
   209  }
   210  
   211  // ServiceClusterDeleteRequest is the request for the 'delete' method.
   212  type ServiceClusterDeleteRequest 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 *ServiceClusterDeleteRequest) Parameter(name string, value interface{}) *ServiceClusterDeleteRequest {
   221  	helpers.AddValue(&r.query, name, value)
   222  	return r
   223  }
   224  
   225  // Header adds a request header.
   226  func (r *ServiceClusterDeleteRequest) Header(name string, value interface{}) *ServiceClusterDeleteRequest {
   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 *ServiceClusterDeleteRequest) Impersonate(user string) *ServiceClusterDeleteRequest {
   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 *ServiceClusterDeleteRequest) Send() (result *ServiceClusterDeleteResponse, 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 *ServiceClusterDeleteRequest) SendContext(ctx context.Context) (result *ServiceClusterDeleteResponse, 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 = &ServiceClusterDeleteResponse{}
   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  // ServiceClusterDeleteResponse is the response for the 'delete' method.
   288  type ServiceClusterDeleteResponse struct {
   289  	status int
   290  	header http.Header
   291  	err    *errors.Error
   292  }
   293  
   294  // Status returns the response status code.
   295  func (r *ServiceClusterDeleteResponse) 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 *ServiceClusterDeleteResponse) 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 *ServiceClusterDeleteResponse) Error() *errors.Error {
   312  	if r == nil {
   313  		return nil
   314  	}
   315  	return r.err
   316  }
   317  
   318  // ServiceClusterGetRequest is the request for the 'get' method.
   319  type ServiceClusterGetRequest 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 *ServiceClusterGetRequest) Parameter(name string, value interface{}) *ServiceClusterGetRequest {
   328  	helpers.AddValue(&r.query, name, value)
   329  	return r
   330  }
   331  
   332  // Header adds a request header.
   333  func (r *ServiceClusterGetRequest) Header(name string, value interface{}) *ServiceClusterGetRequest {
   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 *ServiceClusterGetRequest) Impersonate(user string) *ServiceClusterGetRequest {
   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 *ServiceClusterGetRequest) Send() (result *ServiceClusterGetResponse, 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 *ServiceClusterGetRequest) SendContext(ctx context.Context) (result *ServiceClusterGetResponse, 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 = &ServiceClusterGetResponse{}
   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 = readServiceClusterGetResponse(result, reader)
   392  	if err != nil {
   393  		return
   394  	}
   395  	return
   396  }
   397  
   398  // ServiceClusterGetResponse is the response for the 'get' method.
   399  type ServiceClusterGetResponse struct {
   400  	status int
   401  	header http.Header
   402  	err    *errors.Error
   403  	body   *ServiceCluster
   404  }
   405  
   406  // Status returns the response status code.
   407  func (r *ServiceClusterGetResponse) 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 *ServiceClusterGetResponse) 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 *ServiceClusterGetResponse) 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 *ServiceClusterGetResponse) Body() *ServiceCluster {
   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 *ServiceClusterGetResponse) GetBody() (value *ServiceCluster, ok bool) {
   441  	ok = r != nil && r.body != nil
   442  	if ok {
   443  		value = r.body
   444  	}
   445  	return
   446  }
   447  
   448  // ServiceClusterPostRequest is the request for the 'post' method.
   449  type ServiceClusterPostRequest struct {
   450  	transport http.RoundTripper
   451  	path      string
   452  	query     url.Values
   453  	header    http.Header
   454  	request   *ServiceClusterRequestPayload
   455  }
   456  
   457  // Parameter adds a query parameter.
   458  func (r *ServiceClusterPostRequest) Parameter(name string, value interface{}) *ServiceClusterPostRequest {
   459  	helpers.AddValue(&r.query, name, value)
   460  	return r
   461  }
   462  
   463  // Header adds a request header.
   464  func (r *ServiceClusterPostRequest) Header(name string, value interface{}) *ServiceClusterPostRequest {
   465  	helpers.AddHeader(&r.header, name, value)
   466  	return r
   467  }
   468  
   469  // Impersonate wraps requests on behalf of another user.
   470  // Note: Services that do not support this feature may silently ignore this call.
   471  func (r *ServiceClusterPostRequest) Impersonate(user string) *ServiceClusterPostRequest {
   472  	helpers.AddImpersonationHeader(&r.header, user)
   473  	return r
   474  }
   475  
   476  // Request sets the value of the 'request' parameter.
   477  func (r *ServiceClusterPostRequest) Request(value *ServiceClusterRequestPayload) *ServiceClusterPostRequest {
   478  	r.request = value
   479  	return r
   480  }
   481  
   482  // Send sends this request, waits for the response, and returns it.
   483  //
   484  // This is a potentially lengthy operation, as it requires network communication.
   485  // Consider using a context and the SendContext method.
   486  func (r *ServiceClusterPostRequest) Send() (result *ServiceClusterPostResponse, err error) {
   487  	return r.SendContext(context.Background())
   488  }
   489  
   490  // SendContext sends this request, waits for the response, and returns it.
   491  func (r *ServiceClusterPostRequest) SendContext(ctx context.Context) (result *ServiceClusterPostResponse, err error) {
   492  	query := helpers.CopyQuery(r.query)
   493  	header := helpers.CopyHeader(r.header)
   494  	buffer := &bytes.Buffer{}
   495  	err = writeServiceClusterPostRequest(r, buffer)
   496  	if err != nil {
   497  		return
   498  	}
   499  	uri := &url.URL{
   500  		Path:     r.path,
   501  		RawQuery: query.Encode(),
   502  	}
   503  	request := &http.Request{
   504  		Method: "POST",
   505  		URL:    uri,
   506  		Header: header,
   507  		Body:   io.NopCloser(buffer),
   508  	}
   509  	if ctx != nil {
   510  		request = request.WithContext(ctx)
   511  	}
   512  	response, err := r.transport.RoundTrip(request)
   513  	if err != nil {
   514  		return
   515  	}
   516  	defer response.Body.Close()
   517  	result = &ServiceClusterPostResponse{}
   518  	result.status = response.StatusCode
   519  	result.header = response.Header
   520  	reader := bufio.NewReader(response.Body)
   521  	_, err = reader.Peek(1)
   522  	if err == io.EOF {
   523  		err = nil
   524  		return
   525  	}
   526  	if result.status >= 400 {
   527  		result.err, err = errors.UnmarshalErrorStatus(reader, result.status)
   528  		if err != nil {
   529  			return
   530  		}
   531  		err = result.err
   532  		return
   533  	}
   534  	err = readServiceClusterPostResponse(result, reader)
   535  	if err != nil {
   536  		return
   537  	}
   538  	return
   539  }
   540  
   541  // ServiceClusterPostResponse is the response for the 'post' method.
   542  type ServiceClusterPostResponse struct {
   543  	status   int
   544  	header   http.Header
   545  	err      *errors.Error
   546  	response *ServiceCluster
   547  }
   548  
   549  // Status returns the response status code.
   550  func (r *ServiceClusterPostResponse) Status() int {
   551  	if r == nil {
   552  		return 0
   553  	}
   554  	return r.status
   555  }
   556  
   557  // Header returns header of the response.
   558  func (r *ServiceClusterPostResponse) Header() http.Header {
   559  	if r == nil {
   560  		return nil
   561  	}
   562  	return r.header
   563  }
   564  
   565  // Error returns the response error.
   566  func (r *ServiceClusterPostResponse) Error() *errors.Error {
   567  	if r == nil {
   568  		return nil
   569  	}
   570  	return r.err
   571  }
   572  
   573  // Response returns the value of the 'response' parameter.
   574  func (r *ServiceClusterPostResponse) Response() *ServiceCluster {
   575  	if r == nil {
   576  		return nil
   577  	}
   578  	return r.response
   579  }
   580  
   581  // GetResponse returns the value of the 'response' parameter and
   582  // a flag indicating if the parameter has a value.
   583  func (r *ServiceClusterPostResponse) GetResponse() (value *ServiceCluster, ok bool) {
   584  	ok = r != nil && r.response != nil
   585  	if ok {
   586  		value = r.response
   587  	}
   588  	return
   589  }