Keun Soo Yim | b293fdb | 2016-09-21 16:03:44 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2016 - The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | """Base Cloud API Client. |
| 18 | |
| 19 | BasicCloudApiCliend does basic setup for a cloud API. |
| 20 | """ |
| 21 | import httplib |
| 22 | import logging |
| 23 | import os |
| 24 | import socket |
| 25 | import ssl |
| 26 | |
Keun Soo Yim | b293fdb | 2016-09-21 16:03:44 -0700 | [diff] [blame] | 27 | from apiclient import errors as gerrors |
| 28 | from apiclient.discovery import build |
| 29 | import apiclient.http |
| 30 | import httplib2 |
| 31 | from oauth2client import client |
| 32 | |
| 33 | from acloud.internal.lib import utils |
| 34 | from acloud.public import errors |
| 35 | |
| 36 | logger = logging.getLogger(__name__) |
| 37 | |
| 38 | |
| 39 | class BaseCloudApiClient(object): |
| 40 | """A class that does basic setup for a cloud API.""" |
| 41 | |
| 42 | # To be overriden by subclasses. |
| 43 | API_NAME = "" |
| 44 | API_VERSION = "v1" |
| 45 | SCOPE = "" |
| 46 | |
| 47 | # Defaults for retry. |
| 48 | RETRY_COUNT = 5 |
| 49 | RETRY_BACKOFF_FACTOR = 1.5 |
| 50 | RETRY_SLEEP_MULTIPLIER = 2 |
| 51 | RETRY_HTTP_CODES = [ |
| 52 | # 403 is to retry the "Rate Limit Exceeded" error. |
| 53 | # We could retry on a finer-grained error message later if necessary. |
| 54 | 403, |
| 55 | 500, # Internal Server Error |
| 56 | 502, # Bad Gateway |
| 57 | 503, # Service Unavailable |
| 58 | ] |
| 59 | RETRIABLE_ERRORS = (httplib.HTTPException, httplib2.HttpLib2Error, |
| 60 | socket.error, ssl.SSLError) |
| 61 | RETRIABLE_AUTH_ERRORS = (client.AccessTokenRefreshError, ) |
| 62 | |
| 63 | def __init__(self, oauth2_credentials): |
| 64 | """Initialize. |
| 65 | |
| 66 | Args: |
| 67 | oauth2_credentials: An oauth2client.OAuth2Credentials instance. |
| 68 | """ |
| 69 | self._service = self.InitResourceHandle(oauth2_credentials) |
| 70 | |
| 71 | @classmethod |
| 72 | def InitResourceHandle(cls, oauth2_credentials): |
| 73 | """Authenticate and initialize a Resource object. |
| 74 | |
| 75 | Authenticate http and create a Resource object with methods |
| 76 | for interacting with the service. |
| 77 | |
| 78 | Args: |
| 79 | oauth2_credentials: An oauth2client.OAuth2Credentials instance. |
| 80 | |
| 81 | Returns: |
| 82 | An apiclient.discovery.Resource object |
| 83 | """ |
| 84 | http_auth = oauth2_credentials.authorize(httplib2.Http()) |
| 85 | return utils.RetryException(exc_retry=cls.RETRIABLE_AUTH_ERRORS, |
| 86 | max_retry=cls.RETRY_COUNT, |
| 87 | functor=build, |
| 88 | sleep=cls.RETRY_SLEEP_MULTIPLIER, |
| 89 | backoff_factor=cls.RETRY_BACKOFF_FACTOR, |
| 90 | serviceName=cls.API_NAME, |
| 91 | version=cls.API_VERSION, |
| 92 | http=http_auth) |
| 93 | |
| 94 | def _ShouldRetry(self, exception, retry_http_codes, |
| 95 | other_retriable_errors): |
| 96 | """Check if exception is retriable. |
| 97 | |
| 98 | Args: |
| 99 | exception: An instance of Exception. |
| 100 | retry_http_codes: a list of integers, retriable HTTP codes of |
| 101 | HttpError |
| 102 | other_retriable_errors: a tuple of error types to retry other than |
| 103 | HttpError. |
| 104 | |
| 105 | Returns: |
| 106 | Boolean, True if retriable, False otherwise. |
| 107 | """ |
| 108 | if isinstance(exception, other_retriable_errors): |
| 109 | return True |
| 110 | |
| 111 | if isinstance(exception, errors.HttpError): |
| 112 | if exception.code in retry_http_codes: |
| 113 | return True |
| 114 | else: |
| 115 | logger.debug("_ShouldRetry: Exception code %s not in %s: %s", |
| 116 | exception.code, retry_http_codes, str(exception)) |
| 117 | |
| 118 | logger.debug( |
| 119 | "_ShouldRetry: Exception %s is not one of %s: %s", type(exception), |
| 120 | list(other_retriable_errors) + [errors.HttpError], str(exception)) |
| 121 | return False |
| 122 | |
| 123 | def _TranslateError(self, exception): |
| 124 | """Translate the exception to a desired type. |
| 125 | |
| 126 | Args: |
| 127 | exception: An instance of Exception. |
| 128 | |
| 129 | Returns: |
| 130 | gerrors.HttpError will be translated to errors.HttpError. |
| 131 | If the error code is errors.HTTP_NOT_FOUND_CODE, it will |
| 132 | be translated to errors.ResourceNotFoundError. |
| 133 | Unrecognized error type will not be translated and will |
| 134 | be returned as is. |
| 135 | """ |
| 136 | if isinstance(exception, gerrors.HttpError): |
| 137 | exception = errors.HttpError.CreateFromHttpError(exception) |
| 138 | if exception.code == errors.HTTP_NOT_FOUND_CODE: |
| 139 | exception = errors.ResourceNotFoundError(exception.code, |
| 140 | str(exception)) |
| 141 | return exception |
| 142 | |
| 143 | def ExecuteOnce(self, api): |
| 144 | """Execute an api and parse the errors. |
| 145 | |
| 146 | Args: |
| 147 | api: An apiclient.http.HttpRequest, representing the api to execute. |
| 148 | |
| 149 | Returns: |
| 150 | Execution result of the api. |
| 151 | |
| 152 | Raises: |
| 153 | errors.ResourceNotFoundError: For 404 error. |
| 154 | errors.HttpError: For other types of http error. |
| 155 | """ |
| 156 | try: |
| 157 | return api.execute() |
| 158 | except gerrors.HttpError as e: |
| 159 | raise self._TranslateError(e) |
| 160 | |
| 161 | def Execute(self, |
| 162 | api, |
| 163 | retry_http_codes=None, |
| 164 | max_retry=None, |
| 165 | sleep=None, |
| 166 | backoff_factor=None, |
| 167 | other_retriable_errors=None): |
| 168 | """Execute an api with retry. |
| 169 | |
| 170 | Call ExecuteOnce and retry on http error with given codes. |
| 171 | |
| 172 | Args: |
| 173 | api: An apiclient.http.HttpRequest, representing the api to execute: |
| 174 | retry_http_codes: A list of http codes to retry. |
| 175 | max_retry: See utils.GenericRetry. |
| 176 | sleep: See utils.GenericRetry. |
| 177 | backoff_factor: See utils.GenericRetry. |
| 178 | other_retriable_errors: A tuple of error types that should be retried |
| 179 | other than errors.HttpError. |
| 180 | |
| 181 | Returns: |
| 182 | Execution result of the api. |
| 183 | |
| 184 | Raises: |
| 185 | See ExecuteOnce. |
| 186 | """ |
| 187 | retry_http_codes = (self.RETRY_HTTP_CODES if retry_http_codes is None |
| 188 | else retry_http_codes) |
| 189 | max_retry = (self.RETRY_COUNT if max_retry is None else max_retry) |
| 190 | sleep = (self.RETRY_SLEEP_MULTIPLIER if sleep is None else sleep) |
| 191 | backoff_factor = (self.RETRY_BACKOFF_FACTOR if backoff_factor is None |
| 192 | else backoff_factor) |
| 193 | other_retriable_errors = (self.RETRIABLE_ERRORS |
| 194 | if other_retriable_errors is None else |
| 195 | other_retriable_errors) |
| 196 | |
| 197 | def _Handler(exc): |
| 198 | """Check if |exc| is a retriable exception. |
| 199 | |
| 200 | Args: |
| 201 | exc: An exception. |
| 202 | |
| 203 | Returns: |
| 204 | True if exc is an errors.HttpError and code exists in |retry_http_codes| |
| 205 | False otherwise. |
| 206 | """ |
| 207 | if self._ShouldRetry(exc, retry_http_codes, |
| 208 | other_retriable_errors): |
| 209 | logger.debug("Will retry error: %s", str(exc)) |
| 210 | return True |
| 211 | return False |
| 212 | |
| 213 | return utils.GenericRetry(_Handler, |
| 214 | max_retry=max_retry, |
| 215 | functor=self.ExecuteOnce, |
| 216 | sleep=sleep, |
| 217 | backoff_factor=backoff_factor, |
| 218 | api=api) |
| 219 | |
| 220 | def BatchExecuteOnce(self, requests): |
| 221 | """Execute requests in a batch. |
| 222 | |
| 223 | Args: |
| 224 | requests: A dictionary where key is request id and value |
| 225 | is an http request. |
| 226 | |
| 227 | Returns: |
| 228 | results, a dictionary in the following format |
| 229 | {request_id: (response, exception)} |
| 230 | request_ids are those from requests; response |
| 231 | is the http response for the request or None on error; |
| 232 | exception is an instance of DriverError or None if no error. |
| 233 | """ |
| 234 | results = {} |
| 235 | |
| 236 | def _CallBack(request_id, response, exception): |
| 237 | results[request_id] = (response, self._TranslateError(exception)) |
| 238 | |
| 239 | batch = apiclient.http.BatchHttpRequest() |
| 240 | for request_id, request in requests.iteritems(): |
| 241 | batch.add(request=request, |
| 242 | callback=_CallBack, |
| 243 | request_id=request_id) |
| 244 | batch.execute() |
| 245 | return results |
| 246 | |
| 247 | def BatchExecute(self, |
| 248 | requests, |
| 249 | retry_http_codes=None, |
| 250 | max_retry=None, |
| 251 | sleep=None, |
| 252 | backoff_factor=None, |
| 253 | other_retriable_errors=None): |
| 254 | """Batch execute multiple requests with retry. |
| 255 | |
| 256 | Call BatchExecuteOnce and retry on http error with given codes. |
| 257 | |
| 258 | Args: |
| 259 | requests: A dictionary where key is request id picked by caller, |
| 260 | and value is a apiclient.http.HttpRequest. |
| 261 | retry_http_codes: A list of http codes to retry. |
| 262 | max_retry: See utils.GenericRetry. |
| 263 | sleep: See utils.GenericRetry. |
| 264 | backoff_factor: See utils.GenericRetry. |
| 265 | other_retriable_errors: A tuple of error types that should be retried |
| 266 | other than errors.HttpError. |
| 267 | |
| 268 | Returns: |
| 269 | results, a dictionary in the following format |
| 270 | {request_id: (response, exception)} |
| 271 | request_ids are those from requests; response |
| 272 | is the http response for the request or None on error; |
| 273 | exception is an instance of DriverError or None if no error. |
| 274 | """ |
| 275 | executor = utils.BatchHttpRequestExecutor( |
| 276 | self.BatchExecuteOnce, |
| 277 | requests=requests, |
| 278 | retry_http_codes=retry_http_codes or self.RETRY_HTTP_CODES, |
| 279 | max_retry=max_retry or self.RETRY_COUNT, |
| 280 | sleep=sleep or self.RETRY_SLEEP_MULTIPLIER, |
| 281 | backoff_factor=backoff_factor or self.RETRY_BACKOFF_FACTOR, |
| 282 | other_retriable_errors=other_retriable_errors or |
| 283 | self.RETRIABLE_ERRORS) |
| 284 | executor.Execute() |
| 285 | return executor.GetResults() |
| 286 | |
| 287 | def ListWithMultiPages(self, api_resource, *args, **kwargs): |
| 288 | """Call an api that list a type of resource. |
| 289 | |
| 290 | Multiple google services support listing a type of |
| 291 | resource (e.g list gce instances, list storage objects). |
| 292 | The querying pattern is similar -- |
| 293 | Step 1: execute the api and get a response object like, |
| 294 | { |
| 295 | "items": [..list of resource..], |
| 296 | # The continuation token that can be used |
| 297 | # to get the next page. |
| 298 | "nextPageToken": "A String", |
| 299 | } |
| 300 | Step 2: execute the api again with the nextPageToken to |
| 301 | retrieve more pages and get a response object. |
| 302 | |
| 303 | Step 3: Repeat Step 2 until no more page. |
| 304 | |
| 305 | This method encapsulates the generic logic of |
| 306 | calling such listing api. |
| 307 | |
| 308 | Args: |
| 309 | api_resource: An apiclient.discovery.Resource object |
| 310 | used to create an http request for the listing api. |
| 311 | *args: Arguments used to create the http request. |
| 312 | **kwargs: Keyword based arguments to create the http |
| 313 | request. |
| 314 | |
| 315 | Returns: |
| 316 | A list of items. |
| 317 | """ |
| 318 | items = [] |
| 319 | next_page_token = None |
| 320 | while True: |
| 321 | api = api_resource(pageToken=next_page_token, *args, **kwargs) |
| 322 | response = self.Execute(api) |
| 323 | items.extend(response.get("items", [])) |
| 324 | next_page_token = response.get("nextPageToken") |
| 325 | if not next_page_token: |
| 326 | break |
| 327 | return items |
| 328 | |
| 329 | @property |
| 330 | def service(self): |
| 331 | """Return self._service as a property.""" |
| 332 | return self._service |