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