github.com/openshift-online/ocm-sdk-go@v0.1.473/clustersmgmt/v1/log_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  // LogClient is the client of the 'log' resource.
    35  //
    36  // Manages a specific log.
    37  type LogClient struct {
    38  	transport http.RoundTripper
    39  	path      string
    40  }
    41  
    42  // NewLogClient creates a new client for the 'log'
    43  // resource using the given transport to send the requests and receive the
    44  // responses.
    45  func NewLogClient(transport http.RoundTripper, path string) *LogClient {
    46  	return &LogClient{
    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 log.
    55  func (c *LogClient) Get() *LogGetRequest {
    56  	return &LogGetRequest{
    57  		transport: c.transport,
    58  		path:      c.path,
    59  	}
    60  }
    61  
    62  // LogPollRequest is the request for the Poll method.
    63  type LogPollRequest struct {
    64  	request    *LogGetRequest
    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 *LogPollRequest) Parameter(name string, value interface{}) *LogPollRequest {
    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 *LogPollRequest) Header(name string, value interface{}) *LogPollRequest {
    78  	r.request.Header(name, value)
    79  	return r
    80  }
    81  
    82  // Offset sets the value of the 'offset' parameter for all the requests that
    83  // will be used to retrieve the object.
    84  //
    85  // Line offset to start logs from. if 0 retreive entire log.
    86  // If offset > #lines return an empty log.
    87  func (r *LogPollRequest) Offset(value int) *LogPollRequest {
    88  	r.request.Offset(value)
    89  	return r
    90  }
    91  
    92  // Tail sets the value of the 'tail' parameter for all the requests that
    93  // will be used to retrieve the object.
    94  //
    95  // Returns the number of tail lines from the end of the log.
    96  // If there are no line breaks or the number of lines < tail
    97  // return the entire log.
    98  // Either 'tail' or 'offset' can be set. Not both.
    99  func (r *LogPollRequest) Tail(value int) *LogPollRequest {
   100  	r.request.Tail(value)
   101  	return r
   102  }
   103  
   104  // Interval sets the polling interval. This parameter is mandatory and must be greater than zero.
   105  func (r *LogPollRequest) Interval(value time.Duration) *LogPollRequest {
   106  	r.interval = value
   107  	return r
   108  }
   109  
   110  // Status set the expected status of the response. Multiple values can be set calling this method
   111  // multiple times. The response will be considered successful if the status is any of those values.
   112  func (r *LogPollRequest) Status(value int) *LogPollRequest {
   113  	r.statuses = append(r.statuses, value)
   114  	return r
   115  }
   116  
   117  // Predicate adds a predicate that the response should satisfy be considered successful. Multiple
   118  // predicates can be set calling this method multiple times. The response will be considered successful
   119  // if all the predicates are satisfied.
   120  func (r *LogPollRequest) Predicate(value func(*LogGetResponse) bool) *LogPollRequest {
   121  	r.predicates = append(r.predicates, func(response interface{}) bool {
   122  		return value(response.(*LogGetResponse))
   123  	})
   124  	return r
   125  }
   126  
   127  // StartContext starts the polling loop. Responses will be considered successful if the status is one of
   128  // the values specified with the Status method and if all the predicates specified with the Predicate
   129  // method return nil.
   130  //
   131  // The context must have a timeout or deadline, otherwise this method will immediately return an error.
   132  func (r *LogPollRequest) StartContext(ctx context.Context) (response *LogPollResponse, err error) {
   133  	result, err := helpers.PollContext(ctx, r.interval, r.statuses, r.predicates, r.task)
   134  	if result != nil {
   135  		response = &LogPollResponse{
   136  			response: result.(*LogGetResponse),
   137  		}
   138  	}
   139  	return
   140  }
   141  
   142  // task adapts the types of the request/response types so that they can be used with the generic
   143  // polling function from the helpers package.
   144  func (r *LogPollRequest) task(ctx context.Context) (status int, result interface{}, err error) {
   145  	response, err := r.request.SendContext(ctx)
   146  	if response != nil {
   147  		status = response.Status()
   148  		result = response
   149  	}
   150  	return
   151  }
   152  
   153  // LogPollResponse is the response for the Poll method.
   154  type LogPollResponse struct {
   155  	response *LogGetResponse
   156  }
   157  
   158  // Status returns the response status code.
   159  func (r *LogPollResponse) Status() int {
   160  	if r == nil {
   161  		return 0
   162  	}
   163  	return r.response.Status()
   164  }
   165  
   166  // Header returns header of the response.
   167  func (r *LogPollResponse) Header() http.Header {
   168  	if r == nil {
   169  		return nil
   170  	}
   171  	return r.response.Header()
   172  }
   173  
   174  // Error returns the response error.
   175  func (r *LogPollResponse) Error() *errors.Error {
   176  	if r == nil {
   177  		return nil
   178  	}
   179  	return r.response.Error()
   180  }
   181  
   182  // Body returns the value of the 'body' parameter.
   183  //
   184  // Retreived log.
   185  func (r *LogPollResponse) Body() *Log {
   186  	return r.response.Body()
   187  }
   188  
   189  // GetBody returns the value of the 'body' parameter and
   190  // a flag indicating if the parameter has a value.
   191  //
   192  // Retreived log.
   193  func (r *LogPollResponse) GetBody() (value *Log, ok bool) {
   194  	return r.response.GetBody()
   195  }
   196  
   197  // Poll creates a request to repeatedly retrieve the object till the response has one of a given set
   198  // of states and satisfies a set of predicates.
   199  func (c *LogClient) Poll() *LogPollRequest {
   200  	return &LogPollRequest{
   201  		request: c.Get(),
   202  	}
   203  }
   204  
   205  // LogGetRequest is the request for the 'get' method.
   206  type LogGetRequest struct {
   207  	transport http.RoundTripper
   208  	path      string
   209  	query     url.Values
   210  	header    http.Header
   211  	offset    *int
   212  	tail      *int
   213  }
   214  
   215  // Parameter adds a query parameter.
   216  func (r *LogGetRequest) Parameter(name string, value interface{}) *LogGetRequest {
   217  	helpers.AddValue(&r.query, name, value)
   218  	return r
   219  }
   220  
   221  // Header adds a request header.
   222  func (r *LogGetRequest) Header(name string, value interface{}) *LogGetRequest {
   223  	helpers.AddHeader(&r.header, name, value)
   224  	return r
   225  }
   226  
   227  // Impersonate wraps requests on behalf of another user.
   228  // Note: Services that do not support this feature may silently ignore this call.
   229  func (r *LogGetRequest) Impersonate(user string) *LogGetRequest {
   230  	helpers.AddImpersonationHeader(&r.header, user)
   231  	return r
   232  }
   233  
   234  // Offset sets the value of the 'offset' parameter.
   235  //
   236  // Line offset to start logs from. if 0 retreive entire log.
   237  // If offset > #lines return an empty log.
   238  func (r *LogGetRequest) Offset(value int) *LogGetRequest {
   239  	r.offset = &value
   240  	return r
   241  }
   242  
   243  // Tail sets the value of the 'tail' parameter.
   244  //
   245  // Returns the number of tail lines from the end of the log.
   246  // If there are no line breaks or the number of lines < tail
   247  // return the entire log.
   248  // Either 'tail' or 'offset' can be set. Not both.
   249  func (r *LogGetRequest) Tail(value int) *LogGetRequest {
   250  	r.tail = &value
   251  	return r
   252  }
   253  
   254  // Send sends this request, waits for the response, and returns it.
   255  //
   256  // This is a potentially lengthy operation, as it requires network communication.
   257  // Consider using a context and the SendContext method.
   258  func (r *LogGetRequest) Send() (result *LogGetResponse, err error) {
   259  	return r.SendContext(context.Background())
   260  }
   261  
   262  // SendContext sends this request, waits for the response, and returns it.
   263  func (r *LogGetRequest) SendContext(ctx context.Context) (result *LogGetResponse, err error) {
   264  	query := helpers.CopyQuery(r.query)
   265  	if r.offset != nil {
   266  		helpers.AddValue(&query, "offset", *r.offset)
   267  	}
   268  	if r.tail != nil {
   269  		helpers.AddValue(&query, "tail", *r.tail)
   270  	}
   271  	header := helpers.CopyHeader(r.header)
   272  	uri := &url.URL{
   273  		Path:     r.path,
   274  		RawQuery: query.Encode(),
   275  	}
   276  	request := &http.Request{
   277  		Method: "GET",
   278  		URL:    uri,
   279  		Header: header,
   280  	}
   281  	if ctx != nil {
   282  		request = request.WithContext(ctx)
   283  	}
   284  	response, err := r.transport.RoundTrip(request)
   285  	if err != nil {
   286  		return
   287  	}
   288  	defer response.Body.Close()
   289  	result = &LogGetResponse{}
   290  	result.status = response.StatusCode
   291  	result.header = response.Header
   292  	reader := bufio.NewReader(response.Body)
   293  	_, err = reader.Peek(1)
   294  	if err == io.EOF {
   295  		err = nil
   296  		return
   297  	}
   298  	if result.status >= 400 {
   299  		result.err, err = errors.UnmarshalErrorStatus(reader, result.status)
   300  		if err != nil {
   301  			return
   302  		}
   303  		err = result.err
   304  		return
   305  	}
   306  	err = readLogGetResponse(result, reader)
   307  	if err != nil {
   308  		return
   309  	}
   310  	return
   311  }
   312  
   313  // LogGetResponse is the response for the 'get' method.
   314  type LogGetResponse struct {
   315  	status int
   316  	header http.Header
   317  	err    *errors.Error
   318  	body   *Log
   319  }
   320  
   321  // Status returns the response status code.
   322  func (r *LogGetResponse) Status() int {
   323  	if r == nil {
   324  		return 0
   325  	}
   326  	return r.status
   327  }
   328  
   329  // Header returns header of the response.
   330  func (r *LogGetResponse) Header() http.Header {
   331  	if r == nil {
   332  		return nil
   333  	}
   334  	return r.header
   335  }
   336  
   337  // Error returns the response error.
   338  func (r *LogGetResponse) Error() *errors.Error {
   339  	if r == nil {
   340  		return nil
   341  	}
   342  	return r.err
   343  }
   344  
   345  // Body returns the value of the 'body' parameter.
   346  //
   347  // Retreived log.
   348  func (r *LogGetResponse) Body() *Log {
   349  	if r == nil {
   350  		return nil
   351  	}
   352  	return r.body
   353  }
   354  
   355  // GetBody returns the value of the 'body' parameter and
   356  // a flag indicating if the parameter has a value.
   357  //
   358  // Retreived log.
   359  func (r *LogGetResponse) GetBody() (value *Log, ok bool) {
   360  	ok = r != nil && r.body != nil
   361  	if ok {
   362  		value = r.body
   363  	}
   364  	return
   365  }