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