| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """This file contains buildbucket service client.""" | 5 """This file contains buildbucket service client.""" |
| 6 | 6 |
| 7 import datetime | 7 import datetime |
| 8 import json |
| 9 import os |
| 10 import urlparse |
| 8 | 11 |
| 9 from master import auth | 12 from master import auth |
| 10 from master import deferred_resource | 13 from master import deferred_resource |
| 11 from master.buildbucket import common | 14 from master.buildbucket import common |
| 15 import apiclient |
| 16 import httplib2 |
| 12 | 17 |
| 13 BUILDBUCKET_HOSTNAME_PRODUCTION = 'cr-buildbucket.appspot.com' | 18 BUILDBUCKET_HOSTNAME_PRODUCTION = 'cr-buildbucket.appspot.com' |
| 14 BUILDBUCKET_HOSTNAME_TESTING = 'cr-buildbucket-test.appspot.com' | 19 BUILDBUCKET_HOSTNAME_TESTING = 'cr-buildbucket-test.appspot.com' |
| 15 | 20 |
| 21 THIS_DIR = os.path.dirname(os.path.abspath(os.path.realpath(__file__))) |
| 22 DISCOVERY_DOC_PATH = os.path.join(THIS_DIR, 'discovery_doc.json') |
| 16 | 23 |
| 17 def buildbucket_api_discovery_url(hostname=None): | 24 |
| 18 return ( | 25 # A list of buckets for which we bypass Cloud Endpoints proxy. |
| 19 'https://%s/_ah/api/discovery/v1/apis/{api}/{apiVersion}/rest' % hostname) | 26 BYPASS_ENDPOINTS_WHITELIST = { |
| 27 'master.tryserver.infra', |
| 28 } |
| 20 | 29 |
| 21 | 30 |
| 22 def get_default_buildbucket_hostname(master): | 31 def get_default_buildbucket_hostname(master): |
| 23 return ( | 32 return ( |
| 24 BUILDBUCKET_HOSTNAME_PRODUCTION if master.is_production_host | 33 BUILDBUCKET_HOSTNAME_PRODUCTION if master.is_production_host |
| 25 else BUILDBUCKET_HOSTNAME_TESTING) | 34 else BUILDBUCKET_HOSTNAME_TESTING) |
| 26 | 35 |
| 27 | 36 |
| 28 def create_buildbucket_service( | 37 def create_buildbucket_service(master, hostname=None, verbose=None): |
| 29 master, hostname=None, verbose=None): | |
| 30 """Asynchronously creates buildbucket API resource. | 38 """Asynchronously creates buildbucket API resource. |
| 31 | 39 |
| 32 Returns: | 40 Returns: |
| 33 A DeferredResource as Deferred. | 41 A DeferredResource as Deferred. |
| 34 """ | 42 """ |
| 35 hostname = hostname or get_default_buildbucket_hostname(master) | 43 hostname = hostname or get_default_buildbucket_hostname(master) |
| 36 | 44 |
| 37 cred_factory = deferred_resource.CredentialFactory( | 45 cred_factory = deferred_resource.CredentialFactory( |
| 38 lambda: auth.create_credentials_for_master(master), | 46 lambda: auth.create_credentials_for_master(master), |
| 39 ttl=datetime.timedelta(minutes=5), | 47 ttl=datetime.timedelta(minutes=5), |
| 40 ) | 48 ) |
| 41 | 49 |
| 42 return deferred_resource.DeferredResource.build( | 50 with open(DISCOVERY_DOC_PATH) as f: |
| 43 'buildbucket', | 51 discovery_doc = json.load(f) |
| 44 'v1', | 52 |
| 53 # This block of code is adapted from |
| 54 # https://chromium.googlesource.com/chromium/tools/build/+/08b404f/third_party
/google_api_python_client/googleapiclient/discovery.py#146 |
| 55 # https://chromium.googlesource.com/chromium/tools/build/+/08b404f/third_party
/google_api_python_client/googleapiclient/discovery.py#218 |
| 56 baseUrl = 'https://%s/%sapi/buildbucket/v1/' % ( |
| 57 hostname, |
| 58 '' if master.buildbucket_bucket in BYPASS_ENDPOINTS_WHITELIST else '_ah/', |
| 59 ) |
| 60 resource = apiclient.discovery.Resource( |
| 61 http=httplib2.Http(), |
| 62 baseUrl=baseUrl, |
| 63 model=apiclient.model.JsonModel(False), |
| 64 requestBuilder=apiclient.http.HttpRequest, |
| 65 developerKey=None, |
| 66 resourceDesc=discovery_doc, |
| 67 rootDesc=discovery_doc, |
| 68 schema=apiclient.schema.Schemas(discovery_doc)) |
| 69 |
| 70 return deferred_resource.DeferredResource( |
| 71 resource, |
| 45 credentials=cred_factory, | 72 credentials=cred_factory, |
| 46 max_concurrent_requests=10, | 73 max_concurrent_requests=10, |
| 47 discoveryServiceUrl=buildbucket_api_discovery_url(hostname), | |
| 48 verbose=verbose or False, | 74 verbose=verbose or False, |
| 49 log_prefix=common.LOG_PREFIX, | 75 log_prefix=common.LOG_PREFIX, |
| 50 timeout=60, | 76 timeout=60, |
| 51 ) | 77 ) |
| OLD | NEW |