def get_cas_service(): """Factory method that returns configured CASService instance. If the service is not configured, returns None. Also acts as a mocking point for unit tests. """ conf = config.cached() if not conf.cas_gs_path or not conf.cas_gs_temp: return None try: cloudstorage.validate_file_path(conf.cas_gs_path.rstrip('/')) cloudstorage.validate_file_path(conf.cas_gs_temp.rstrip('/')) except ValueError as err: logging.error("Invalid CAS config: %s", err) return None service_account_key = auth.ServiceAccountKey( client_email=conf.service_account_email, private_key=conf.service_account_pkey, private_key_id=conf.service_account_pkey_id) if utils.is_local_dev_server(): # pragma: no branch from . import hacks hacks.patch_cloudstorage_lib(service_account_key) return CASService( conf.cas_gs_path.rstrip('/'), conf.cas_gs_temp.rstrip('/'), service_account_key)
def __init__(self, gs_path, gs_temp, service_account_key=None): self._gs_path = gs_path.rstrip('/') self._gs_temp = gs_temp.rstrip('/') self._service_account_key = service_account_key self._retry_params = api_utils.RetryParams() cloudstorage.validate_file_path(self._gs_path) cloudstorage.validate_file_path(self._gs_temp)
def _gs_copy(self, src, dst, src_etag=None): # pragma: no cover """Copy |src| file to |dst| optionally checking src ETag. Raises cloudstorage.FatalError on precondition error. """ # See cloudstorage.cloudstorage_api._copy2. cloudstorage.validate_file_path(src) cloudstorage.validate_file_path(dst) headers = { 'x-goog-copy-source': src, 'x-goog-metadata-directive': 'COPY', } if src_etag is not None: headers['x-goog-copy-source-if-match'] = src_etag api = storage_api._get_storage_api(retry_params=self._retry_params) status, resp_headers, content = api.put_object( api_utils._quote_filename(dst), headers=headers) errors.check_status(status, [200], src, headers, resp_headers, body=content)
def gs_config(self, request): """Configures paths in Google Storage to use by CAS service.""" try: cloudstorage.validate_file_path(request.cas_gs_path.rstrip('/')) cloudstorage.validate_file_path(request.cas_gs_temp.rstrip('/')) except ValueError as err: raise endpoints.BadRequestException('Not a valid GS path: %s' % err) conf = config.GlobalConfig.fetch() if not conf: conf = config.GlobalConfig() changed = conf.modify(cas_gs_path=request.cas_gs_path.rstrip('/'), cas_gs_temp=request.cas_gs_temp.rstrip('/')) if changed: logging.warning('Updated Google Storage paths configuration') return message_types.VoidMessage()
def gs_config(self, request): """Configures paths in Google Storage to use by CAS service.""" try: cloudstorage.validate_file_path(request.cas_gs_path.rstrip('/')) cloudstorage.validate_file_path(request.cas_gs_temp.rstrip('/')) except ValueError as err: raise endpoints.BadRequestException('Not a valid GS path: %s' % err) conf = config.GlobalConfig.fetch() if not conf: conf = config.GlobalConfig() changed = conf.modify( cas_gs_path=request.cas_gs_path.rstrip('/'), cas_gs_temp=request.cas_gs_temp.rstrip('/')) if changed: logging.warning('Updated Google Storage paths configuration') return message_types.VoidMessage()
def get_cas_service(): """Factory method that returns configured CASService instance. If the service is not configured, returns None. Also acts as a mocking point for unit tests. """ conf = config.cached() if not conf.cas_gs_path or not conf.cas_gs_temp: return None try: cloudstorage.validate_file_path(conf.cas_gs_path.rstrip('/')) cloudstorage.validate_file_path(conf.cas_gs_temp.rstrip('/')) except ValueError as err: logging.error("Invalid CAS config: %s", err) return None service_account_key = auth.ServiceAccountKey( client_email=conf.service_account_email, private_key=conf.service_account_pkey, private_key_id=conf.service_account_pkey_id) if utils.is_local_dev_server(): # pragma: no branch from . import hacks hacks.patch_cloudstorage_lib(service_account_key) return CASService(conf.cas_gs_path.rstrip('/'), conf.cas_gs_temp.rstrip('/'), service_account_key)