github.com/openshift-online/ocm-sdk-go@v0.1.473/clustersmgmt/v1/cluster_resources_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  	"context"
    25  	"io"
    26  	"net/http"
    27  	"net/url"
    28  	"time"
    29  
    30  	"github.com/openshift-online/ocm-sdk-go/errors"
    31  	"github.com/openshift-online/ocm-sdk-go/helpers"
    32  )
    33  
    34  // ClusterResourcesClient is the client of the 'cluster_resources' resource.
    35  //
    36  // Manages currently available cluster resources
    37  type ClusterResourcesClient struct {
    38  	transport http.RoundTripper
    39  	path      string
    40  }
    41  
    42  // NewClusterResourcesClient creates a new client for the 'cluster_resources'
    43  // resource using the given transport to send the requests and receive the
    44  // responses.
    45  func NewClusterResourcesClient(transport http.RoundTripper, path string) *ClusterResourcesClient {
    46  	return &ClusterResourcesClient{
    47  		transport: transport,
    48  		path:      path,
    49  	}
    50  }
    51  
    52  // Get creates a request for the 'get' method.
    53  //
    54  // Retrieves currently available cluster resources
    55  func (c *ClusterResourcesClient) Get() *ClusterResourcesGetRequest {
    56  	return &ClusterResourcesGetRequest{
    57  		transport: c.transport,
    58  		path:      c.path,
    59  	}
    60  }
    61  
    62  // ClusterResourcesPollRequest is the request for the Poll method.
    63  type ClusterResourcesPollRequest struct {
    64  	request    *ClusterResourcesGetRequest
    65  	interval   time.Duration
    66  	statuses   []int
    67  	predicates []func(interface{}) bool
    68  }
    69  
    70  // Parameter adds a query parameter to all the requests that will be used to retrieve the object.
    71  func (r *ClusterResourcesPollRequest) Parameter(name string, value interface{}) *ClusterResourcesPollRequest {
    72  	r.request.Parameter(name, value)
    73  	return r
    74  }
    75  
    76  // Header adds a request header to all the requests that will be used to retrieve the object.
    77  func (r *ClusterResourcesPollRequest) Header(name string, value interface{}) *ClusterResourcesPollRequest {
    78  	r.request.Header(name, value)
    79  	return r
    80  }
    81  
    82  // Interval sets the polling interval. This parameter is mandatory and must be greater than zero.
    83  func (r *ClusterResourcesPollRequest) Interval(value time.Duration) *ClusterResourcesPollRequest {
    84  	r.interval = value
    85  	return r
    86  }
    87  
    88  // Status set the expected status of the response. Multiple values can be set calling this method
    89  // multiple times. The response will be considered successful if the status is any of those values.
    90  func (r *ClusterResourcesPollRequest) Status(value int) *ClusterResourcesPollRequest {
    91  	r.statuses = append(r.statuses, value)
    92  	return r
    93  }
    94  
    95  // Predicate adds a predicate that the response should satisfy be considered successful. Multiple
    96  // predicates can be set calling this method multiple times. The response will be considered successful
    97  // if all the predicates are satisfied.
    98  func (r *ClusterResourcesPollRequest) Predicate(value func(*ClusterResourcesGetResponse) bool) *ClusterResourcesPollRequest {
    99  	r.predicates = append(r.predicates, func(response interface{}) bool {
   100  		return value(response.(*ClusterResourcesGetResponse))
   101  	})
   102  	return r
   103  }
   104  
   105  // StartContext starts the polling loop. Responses will be considered successful if the status is one of
   106  // the values specified with the Status method and if all the predicates specified with the Predicate
   107  // method return nil.
   108  //
   109  // The context must have a timeout or deadline, otherwise this method will immediately return an error.
   110  func (r *ClusterResourcesPollRequest) StartContext(ctx context.Context) (response *ClusterResourcesPollResponse, err error) {
   111  	result, err := helpers.PollContext(ctx, r.interval, r.statuses, r.predicates, r.task)
   112  	if result != nil {
   113  		response = &ClusterResourcesPollResponse{
   114  			response: result.(*ClusterResourcesGetResponse),
   115  		}
   116  	}
   117  	return
   118  }
   119  
   120  // task adapts the types of the request/response types so that they can be used with the generic
   121  // polling function from the helpers package.
   122  func (r *ClusterResourcesPollRequest) task(ctx context.Context) (status int, result interface{}, err error) {
   123  	response, err := r.request.SendContext(ctx)
   124  	if response != nil {
   125  		status = response.Status()
   126  		result = response
   127  	}
   128  	return
   129  }
   130  
   131  // ClusterResourcesPollResponse is the response for the Poll method.
   132  type ClusterResourcesPollResponse struct {
   133  	response *ClusterResourcesGetResponse
   134  }
   135  
   136  // Status returns the response status code.
   137  func (r *ClusterResourcesPollResponse) Status() int {
   138  	if r == nil {
   139  		return 0
   140  	}
   141  	return r.response.Status()
   142  }
   143  
   144  // Header returns header of the response.
   145  func (r *ClusterResourcesPollResponse) Header() http.Header {
   146  	if r == nil {
   147  		return nil
   148  	}
   149  	return r.response.Header()
   150  }
   151  
   152  // Error returns the response error.
   153  func (r *ClusterResourcesPollResponse) Error() *errors.Error {
   154  	if r == nil {
   155  		return nil
   156  	}
   157  	return r.response.Error()
   158  }
   159  
   160  // Body returns the value of the 'body' parameter.
   161  //
   162  // List of cluster resources
   163  func (r *ClusterResourcesPollResponse) Body() *ClusterResources {
   164  	return r.response.Body()
   165  }
   166  
   167  // GetBody returns the value of the 'body' parameter and
   168  // a flag indicating if the parameter has a value.
   169  //
   170  // List of cluster resources
   171  func (r *ClusterResourcesPollResponse) GetBody() (value *ClusterResources, ok bool) {
   172  	return r.response.GetBody()
   173  }
   174  
   175  // Poll creates a request to repeatedly retrieve the object till the response has one of a given set
   176  // of states and satisfies a set of predicates.
   177  func (c *ClusterResourcesClient) Poll() *ClusterResourcesPollRequest {
   178  	return &ClusterResourcesPollRequest{
   179  		request: c.Get(),
   180  	}
   181  }
   182  
   183  // ClusterResourcesGetRequest is the request for the 'get' method.
   184  type ClusterResourcesGetRequest struct {
   185  	transport http.RoundTripper
   186  	path      string
   187  	query     url.Values
   188  	header    http.Header
   189  }
   190  
   191  // Parameter adds a query parameter.
   192  func (r *ClusterResourcesGetRequest) Parameter(name string, value interface{}) *ClusterResourcesGetRequest {
   193  	helpers.AddValue(&r.query, name, value)
   194  	return r
   195  }
   196  
   197  // Header adds a request header.
   198  func (r *ClusterResourcesGetRequest) Header(name string, value interface{}) *ClusterResourcesGetRequest {
   199  	helpers.AddHeader(&r.header, name, value)
   200  	return r
   201  }
   202  
   203  // Impersonate wraps requests on behalf of another user.
   204  // Note: Services that do not support this feature may silently ignore this call.
   205  func (r *ClusterResourcesGetRequest) Impersonate(user string) *ClusterResourcesGetRequest {
   206  	helpers.AddImpersonationHeader(&r.header, user)
   207  	return r
   208  }
   209  
   210  // Send sends this request, waits for the response, and returns it.
   211  //
   212  // This is a potentially lengthy operation, as it requires network communication.
   213  // Consider using a context and the SendContext method.
   214  func (r *ClusterResourcesGetRequest) Send() (result *ClusterResourcesGetResponse, err error) {
   215  	return r.SendContext(context.Background())
   216  }
   217  
   218  // SendContext sends this request, waits for the response, and returns it.
   219  func (r *ClusterResourcesGetRequest) SendContext(ctx context.Context) (result *ClusterResourcesGetResponse, err error) {
   220  	query := helpers.CopyQuery(r.query)
   221  	header := helpers.CopyHeader(r.header)
   222  	uri := &url.URL{
   223  		Path:     r.path,
   224  		RawQuery: query.Encode(),
   225  	}
   226  	request := &http.Request{
   227  		Method: "GET",
   228  		URL:    uri,
   229  		Header: header,
   230  	}
   231  	if ctx != nil {
   232  		request = request.WithContext(ctx)
   233  	}
   234  	response, err := r.transport.RoundTrip(request)
   235  	if err != nil {
   236  		return
   237  	}
   238  	defer response.Body.Close()
   239  	result = &ClusterResourcesGetResponse{}
   240  	result.status = response.StatusCode
   241  	result.header = response.Header
   242  	reader := bufio.NewReader(response.Body)
   243  	_, err = reader.Peek(1)
   244  	if err == io.EOF {
   245  		err = nil
   246  		return
   247  	}
   248  	if result.status >= 400 {
   249  		result.err, err = errors.UnmarshalErrorStatus(reader, result.status)
   250  		if err != nil {
   251  			return
   252  		}
   253  		err = result.err
   254  		return
   255  	}
   256  	err = readClusterResourcesGetResponse(result, reader)
   257  	if err != nil {
   258  		return
   259  	}
   260  	return
   261  }
   262  
   263  // ClusterResourcesGetResponse is the response for the 'get' method.
   264  type ClusterResourcesGetResponse struct {
   265  	status int
   266  	header http.Header
   267  	err    *errors.Error
   268  	body   *ClusterResources
   269  }
   270  
   271  // Status returns the response status code.
   272  func (r *ClusterResourcesGetResponse) Status() int {
   273  	if r == nil {
   274  		return 0
   275  	}
   276  	return r.status
   277  }
   278  
   279  // Header returns header of the response.
   280  func (r *ClusterResourcesGetResponse) Header() http.Header {
   281  	if r == nil {
   282  		return nil
   283  	}
   284  	return r.header
   285  }
   286  
   287  // Error returns the response error.
   288  func (r *ClusterResourcesGetResponse) Error() *errors.Error {
   289  	if r == nil {
   290  		return nil
   291  	}
   292  	return r.err
   293  }
   294  
   295  // Body returns the value of the 'body' parameter.
   296  //
   297  // List of cluster resources
   298  func (r *ClusterResourcesGetResponse) Body() *ClusterResources {
   299  	if r == nil {
   300  		return nil
   301  	}
   302  	return r.body
   303  }
   304  
   305  // GetBody returns the value of the 'body' parameter and
   306  // a flag indicating if the parameter has a value.
   307  //
   308  // List of cluster resources
   309  func (r *ClusterResourcesGetResponse) GetBody() (value *ClusterResources, ok bool) {
   310  	ok = r != nil && r.body != nil
   311  	if ok {
   312  		value = r.body
   313  	}
   314  	return
   315  }