github.com/openshift-online/ocm-sdk-go@v0.1.473/clustersmgmt/v1/machine_type_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  // MachineTypeClient is the client of the 'machine_type' resource.
    35  //
    36  // Manages a specific machine type.
    37  type MachineTypeClient struct {
    38  	transport http.RoundTripper
    39  	path      string
    40  }
    41  
    42  // NewMachineTypeClient creates a new client for the 'machine_type'
    43  // resource using the given transport to send the requests and receive the
    44  // responses.
    45  func NewMachineTypeClient(transport http.RoundTripper, path string) *MachineTypeClient {
    46  	return &MachineTypeClient{
    47  		transport: transport,
    48  		path:      path,
    49  	}
    50  }
    51  
    52  // Get creates a request for the 'get' method.
    53  //
    54  // Retrieves the details of the machine type.
    55  func (c *MachineTypeClient) Get() *MachineTypeGetRequest {
    56  	return &MachineTypeGetRequest{
    57  		transport: c.transport,
    58  		path:      c.path,
    59  	}
    60  }
    61  
    62  // MachineTypePollRequest is the request for the Poll method.
    63  type MachineTypePollRequest struct {
    64  	request    *MachineTypeGetRequest
    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 *MachineTypePollRequest) Parameter(name string, value interface{}) *MachineTypePollRequest {
    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 *MachineTypePollRequest) Header(name string, value interface{}) *MachineTypePollRequest {
    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 *MachineTypePollRequest) Interval(value time.Duration) *MachineTypePollRequest {
    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 *MachineTypePollRequest) Status(value int) *MachineTypePollRequest {
    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 *MachineTypePollRequest) Predicate(value func(*MachineTypeGetResponse) bool) *MachineTypePollRequest {
    99  	r.predicates = append(r.predicates, func(response interface{}) bool {
   100  		return value(response.(*MachineTypeGetResponse))
   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 *MachineTypePollRequest) StartContext(ctx context.Context) (response *MachineTypePollResponse, err error) {
   111  	result, err := helpers.PollContext(ctx, r.interval, r.statuses, r.predicates, r.task)
   112  	if result != nil {
   113  		response = &MachineTypePollResponse{
   114  			response: result.(*MachineTypeGetResponse),
   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 *MachineTypePollRequest) 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  // MachineTypePollResponse is the response for the Poll method.
   132  type MachineTypePollResponse struct {
   133  	response *MachineTypeGetResponse
   134  }
   135  
   136  // Status returns the response status code.
   137  func (r *MachineTypePollResponse) 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 *MachineTypePollResponse) 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 *MachineTypePollResponse) 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  func (r *MachineTypePollResponse) Body() *MachineType {
   162  	return r.response.Body()
   163  }
   164  
   165  // GetBody returns the value of the 'body' parameter and
   166  // a flag indicating if the parameter has a value.
   167  func (r *MachineTypePollResponse) GetBody() (value *MachineType, ok bool) {
   168  	return r.response.GetBody()
   169  }
   170  
   171  // Poll creates a request to repeatedly retrieve the object till the response has one of a given set
   172  // of states and satisfies a set of predicates.
   173  func (c *MachineTypeClient) Poll() *MachineTypePollRequest {
   174  	return &MachineTypePollRequest{
   175  		request: c.Get(),
   176  	}
   177  }
   178  
   179  // MachineTypeGetRequest is the request for the 'get' method.
   180  type MachineTypeGetRequest struct {
   181  	transport http.RoundTripper
   182  	path      string
   183  	query     url.Values
   184  	header    http.Header
   185  }
   186  
   187  // Parameter adds a query parameter.
   188  func (r *MachineTypeGetRequest) Parameter(name string, value interface{}) *MachineTypeGetRequest {
   189  	helpers.AddValue(&r.query, name, value)
   190  	return r
   191  }
   192  
   193  // Header adds a request header.
   194  func (r *MachineTypeGetRequest) Header(name string, value interface{}) *MachineTypeGetRequest {
   195  	helpers.AddHeader(&r.header, name, value)
   196  	return r
   197  }
   198  
   199  // Impersonate wraps requests on behalf of another user.
   200  // Note: Services that do not support this feature may silently ignore this call.
   201  func (r *MachineTypeGetRequest) Impersonate(user string) *MachineTypeGetRequest {
   202  	helpers.AddImpersonationHeader(&r.header, user)
   203  	return r
   204  }
   205  
   206  // Send sends this request, waits for the response, and returns it.
   207  //
   208  // This is a potentially lengthy operation, as it requires network communication.
   209  // Consider using a context and the SendContext method.
   210  func (r *MachineTypeGetRequest) Send() (result *MachineTypeGetResponse, err error) {
   211  	return r.SendContext(context.Background())
   212  }
   213  
   214  // SendContext sends this request, waits for the response, and returns it.
   215  func (r *MachineTypeGetRequest) SendContext(ctx context.Context) (result *MachineTypeGetResponse, err error) {
   216  	query := helpers.CopyQuery(r.query)
   217  	header := helpers.CopyHeader(r.header)
   218  	uri := &url.URL{
   219  		Path:     r.path,
   220  		RawQuery: query.Encode(),
   221  	}
   222  	request := &http.Request{
   223  		Method: "GET",
   224  		URL:    uri,
   225  		Header: header,
   226  	}
   227  	if ctx != nil {
   228  		request = request.WithContext(ctx)
   229  	}
   230  	response, err := r.transport.RoundTrip(request)
   231  	if err != nil {
   232  		return
   233  	}
   234  	defer response.Body.Close()
   235  	result = &MachineTypeGetResponse{}
   236  	result.status = response.StatusCode
   237  	result.header = response.Header
   238  	reader := bufio.NewReader(response.Body)
   239  	_, err = reader.Peek(1)
   240  	if err == io.EOF {
   241  		err = nil
   242  		return
   243  	}
   244  	if result.status >= 400 {
   245  		result.err, err = errors.UnmarshalErrorStatus(reader, result.status)
   246  		if err != nil {
   247  			return
   248  		}
   249  		err = result.err
   250  		return
   251  	}
   252  	err = readMachineTypeGetResponse(result, reader)
   253  	if err != nil {
   254  		return
   255  	}
   256  	return
   257  }
   258  
   259  // MachineTypeGetResponse is the response for the 'get' method.
   260  type MachineTypeGetResponse struct {
   261  	status int
   262  	header http.Header
   263  	err    *errors.Error
   264  	body   *MachineType
   265  }
   266  
   267  // Status returns the response status code.
   268  func (r *MachineTypeGetResponse) Status() int {
   269  	if r == nil {
   270  		return 0
   271  	}
   272  	return r.status
   273  }
   274  
   275  // Header returns header of the response.
   276  func (r *MachineTypeGetResponse) Header() http.Header {
   277  	if r == nil {
   278  		return nil
   279  	}
   280  	return r.header
   281  }
   282  
   283  // Error returns the response error.
   284  func (r *MachineTypeGetResponse) Error() *errors.Error {
   285  	if r == nil {
   286  		return nil
   287  	}
   288  	return r.err
   289  }
   290  
   291  // Body returns the value of the 'body' parameter.
   292  func (r *MachineTypeGetResponse) Body() *MachineType {
   293  	if r == nil {
   294  		return nil
   295  	}
   296  	return r.body
   297  }
   298  
   299  // GetBody returns the value of the 'body' parameter and
   300  // a flag indicating if the parameter has a value.
   301  func (r *MachineTypeGetResponse) GetBody() (value *MachineType, ok bool) {
   302  	ok = r != nil && r.body != nil
   303  	if ok {
   304  		value = r.body
   305  	}
   306  	return
   307  }