def _check_swift(self, container, object): # Auth needs to be enabled because qinling needs to check swift # object using user's credential. if not CONF.pecan.auth_enable: raise exc.InputException('Swift object not supported.') if not swift_util.check_object(container, object): raise exc.InputException('Object does not exist in Swift.')
def post(self, **kwargs): LOG.info("Creating %s, params: %s", self.type, kwargs) # When using image to create function, runtime_id is not a required # param. if not POST_REQUIRED.issubset(set(kwargs.keys())): raise exc.InputException( 'Required param is missing. Required: %s' % POST_REQUIRED) values = { 'name': kwargs['name'], 'description': kwargs.get('description'), 'runtime_id': kwargs.get('runtime_id'), 'code': json.loads(kwargs['code']), 'entry': kwargs.get('entry', 'main.main'), } source = values['code'].get('source') if not source or source not in CODE_SOURCE: raise exc.InputException( 'Invalid code source specified, available sources: %s' % ', '.join(CODE_SOURCE)) if source != 'image': if not kwargs.get('runtime_id'): raise exc.InputException('"runtime_id" must be specified.') runtime = db_api.get_runtime(kwargs['runtime_id']) if runtime.status != 'available': raise exc.InputException('Runtime %s is not available.' % kwargs['runtime_id']) store = False if values['code']['source'] == 'package': store = True data = kwargs['package'].file.read() elif values['code']['source'] == 'swift': # Auth needs to be enabled because qinling needs to check swift # object using user's credential. if not CONF.pecan.auth_enable: raise exc.InputException('Swift object not supported.') container = values['code']['swift'].get('container') object = values['code']['swift'].get('object') if not swift_util.check_object(container, object): raise exc.InputException('Object does not exist in Swift.') with db_api.transaction(): func_db = db_api.create_function(values) if store: ctx = context.get_ctx() self.storage_provider.store(ctx.projectid, func_db.id, data) pecan.response.status = 201 return resources.Function.from_dict(func_db.to_dict()).to_dict()
def test_check_object_invalid_length(self, mock_sclient): length = constants.MAX_PACKAGE_SIZE + 1 mock_sclient.return_value.head_object.return_value = { "content-length": length } ret = swift.check_object("fake_container", "fake_object") self.assertFalse(ret)
def test_check_object_other_exception(self, mock_sclient): mock_sclient.return_value.head_object.side_effect = Exception ret = swift.check_object("fake_container", "fake_object") self.assertFalse(ret)