def update_vim(self, context, vim_id, vim): self._validate_default_vim(context, vim, vim_id=vim_id) with context.session.begin(subtransactions=True): vim_cred = vim['auth_cred'] vim_project = vim['vim_project'] vim_db = self._get_resource(context, meo_db.Vim, vim_id) try: if 'name' in vim: vim_db.update({'name': vim.get('name')}) if 'description' in vim: vim_db.update({'description': vim.get('description')}) if 'is_default' in vim: vim_db.update({'is_default': vim.get('is_default')}) if 'placement_attr' in vim: vim_db.update( {'placement_attr': vim.get('placement_attr')}) vim_auth_db = (self._model_query( context, meo_db.VimAuth).filter( meo_db.VimAuth.vim_id == vim_id).with_lockmode( 'update').one()) except orm_exc.NoResultFound: raise meo.VimNotFoundException(vim_id=vim_id) vim_auth_db.update({'auth_cred': vim_cred, 'password': vim_cred.pop('password'), 'vim_project': vim_project}) vim_db.update({'updated_at': timeutils.utcnow()}) self._cos_db_plg.create_event( context, res_id=vim_db['id'], res_type=constants.RES_TYPE_VIM, res_state=vim_db['status'], evt_type=constants.RES_EVT_UPDATE, tstamp=vim_db[constants.RES_EVT_UPDATED_FLD]) return self.get_vim(context, vim_id)
def get_vim(self, context, vim_id=None, region_name=None): """Get Vim information for provided VIM id Initiate the MEO plugin, request VIM information for the provided VIM id and validate region """ meo_plugin = manager.ApmecManager.get_service_plugins().get( constants.MEO) if not vim_id: LOG.debug('VIM id not provided. Attempting to find default ' 'VIM information') try: vim_info = meo_plugin.get_default_vim(context) except Exception as ex: LOG.debug('Fail to get default vim due to %s', ex) raise meo.VimDefaultNotDefined() else: try: vim_info = meo_plugin.get_vim(context, vim_id, mask_password=False) except Exception: raise meo.VimNotFoundException(vim_id=vim_id) LOG.debug('VIM info found for vim id %s', vim_id) if region_name and not self.region_valid(vim_info['placement_attr'] ['regions'], region_name): raise meo.VimRegionNotFoundException(region_name=region_name) vim_auth = self._build_vim_auth(context, vim_info) vim_res = {'vim_auth': vim_auth, 'vim_id': vim_info['id'], 'vim_name': vim_info.get('name', vim_info['id']), 'vim_type': vim_info['type']} return vim_res
def _get_resource(self, context, model, id): try: return self._get_by_id(context, model, id) except orm_exc.NoResultFound: if issubclass(model, meo_db.Vim): raise meo.VimNotFoundException(vim_id=id) else: raise
def update_vim_status(self, context, vim_id, status): with context.session.begin(subtransactions=True): try: vim_db = (self._model_query(context, meo_db.Vim).filter( meo_db.Vim.id == vim_id).with_lockmode('update').one()) except orm_exc.NoResultFound: raise meo.VimNotFoundException(vim_id=vim_id) vim_db.update({'status': status, 'updated_at': timeutils.utcnow()}) return self._make_vim_dict(vim_db)
def _decode_vim_auth(self, context, vim_id, auth): """Decode Vim credentials Decrypt VIM cred, get fernet Key from local_file_system or barbican. """ cred = auth['password'].encode('utf-8') if auth.get('key_type') == 'barbican_key': keystone_conf = CONF.keystone_authtoken secret_uuid = auth['secret_uuid'] keymgr_api = KEYMGR_API(keystone_conf.auth_url) secret_obj = keymgr_api.get(context, secret_uuid) vim_key = secret_obj.payload else: vim_key = self._find_vim_key(vim_id) f = fernet.Fernet(vim_key) if not f: LOG.warning('Unable to decode VIM auth') raise meo.VimNotFoundException('Unable to decode VIM auth key') return f.decrypt(cred)
def update_vim(self, context, vim_id, status): t_admin_context = t_context.get_admin_context() update_time = timeutils.utcnow() with t_admin_context.session.begin(subtransactions=True): try: query = t_admin_context.session.query(meo_db.Vim) query.filter(meo_db.Vim.id == vim_id).update({ 'status': status, 'updated_at': update_time }) except orm_exc.NoResultFound: raise meo.VimNotFoundException(vim_id=vim_id) event_db = common_services_db.Event( resource_id=vim_id, resource_type=constants.RES_TYPE_VIM, resource_state=status, event_details="", event_type=constants.RES_EVT_MONITOR, timestamp=update_time) t_admin_context.session.add(event_db) return status