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