Chromium Code Reviews| 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 = [ |
|
dnj (Google)
2016/07/08 17:37:29
nit: might as well make this a set()
nodir
2016/07/08 17:42:54
Done.
| |
| 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) |
| 44 baseUrl = 'https://%s/%sapi/buildbucket/v1/' % ( | |
| 45 hostname, | |
| 46 '' if master.buildbucket_bucket in BYPASS_ENDPOINTS_WHITELIST else '_ah/', | |
| 47 ) | |
| 36 | 48 |
| 37 cred_factory = deferred_resource.CredentialFactory( | 49 cred_factory = deferred_resource.CredentialFactory( |
| 38 lambda: auth.create_credentials_for_master(master), | 50 lambda: auth.create_credentials_for_master(master), |
| 39 ttl=datetime.timedelta(minutes=5), | 51 ttl=datetime.timedelta(minutes=5), |
| 40 ) | 52 ) |
| 41 | 53 |
| 42 return deferred_resource.DeferredResource.build( | 54 with open(DISCOVERY_DOC_PATH) as f: |
| 43 'buildbucket', | 55 discovery_doc = json.load(f) |
| 44 'v1', | 56 |
| 57 resource = apiclient.discovery.Resource( | |
|
dnj (Google)
2016/07/08 17:37:29
nit: Comment that this is copied from the source l
nodir
2016/07/08 17:42:53
Done.
| |
| 58 http=httplib2.Http(), | |
| 59 baseUrl=baseUrl, | |
| 60 model=apiclient.model.JsonModel(False), | |
| 61 requestBuilder=apiclient.http.HttpRequest, | |
| 62 developerKey=None, | |
| 63 resourceDesc=discovery_doc, | |
| 64 rootDesc=discovery_doc, | |
| 65 schema=apiclient.schema.Schemas(discovery_doc)) | |
| 66 return deferred_resource.DeferredResource( | |
| 67 resource, | |
| 45 credentials=cred_factory, | 68 credentials=cred_factory, |
| 46 max_concurrent_requests=10, | 69 max_concurrent_requests=10, |
| 47 discoveryServiceUrl=buildbucket_api_discovery_url(hostname), | |
| 48 verbose=verbose or False, | 70 verbose=verbose or False, |
| 49 log_prefix=common.LOG_PREFIX, | 71 log_prefix=common.LOG_PREFIX, |
| 50 timeout=60, | 72 timeout=60, |
| 51 ) | 73 ) |
| OLD | NEW |