def show(self, request, environment_id, session_id): LOG.debug('Session:Show <SessionId: {id}>'.format(id=session_id)) unit = db_session.get_session() session = unit.query(models.Session).get(session_id) check_session(request, environment_id, session, session_id) user_id = request.context.user if session.user_id != user_id: msg = _('User <UserId {usr_id}> is not authorized to access ' 'session <SessionId {s_id}>.').format(usr_id=user_id, s_id=session_id) LOG.error(msg) raise exc.HTTPUnauthorized(explanation=msg) if not sessions.SessionServices.validate(session): msg = _('Session <SessionId {0}> is invalid: environment has been ' 'updated or updating right now with other session').format( session_id) LOG.error(msg) raise exc.HTTPForbidden(explanation=msg) return session.to_dict()
def show(self, request, environment_id): LOG.debug('Environments:Show <Id: {id}>'.format(id=environment_id)) target = {"environment_id": environment_id} policy.check('show_environment', request.context, target) session = db_session.get_session() environment = session.query(models.Environment).get(environment_id) env = environment.to_dict() env['status'] = envs.EnvironmentServices.get_status(env['id']) # if env is currently being deployed we can provide information about # the session right away env['acquired_by'] = None if env['status'] == states.EnvironmentStatus.DEPLOYING: session_list = session_services.SessionServices.get_sessions( environment_id, state=states.SessionState.DEPLOYING) if session_list: env['acquired_by'] = session_list[0].id session_id = None if hasattr(request, 'context') and request.context.session: session_id = request.context.session if session_id: env_session = session.query(models.Session).get(session_id) check_session(request, environment_id, env_session, session_id) # add services to env get_data = core_services.CoreServices.get_data env['services'] = get_data(environment_id, '/services', session_id) return env
def delete(self, request, environment_id, session_id): LOG.debug('Session:Delete <SessionId: {s_id}>'.format(s_id=session_id)) unit = db_session.get_session() session = unit.query(models.Session).get(session_id) check_session(request, environment_id, session, session_id) user_id = request.context.user if session.user_id != user_id: msg = _('User <UserId {usr_id}> is not authorized to access ' 'session <SessionId {s_id}>.').format(usr_id=user_id, s_id=session_id) LOG.error(msg) raise exc.HTTPUnauthorized(explanation=msg) if session.state == states.SessionState.DEPLOYING: msg = _('Session <SessionId: {s_id}> is in deploying state ' 'and could not be deleted').format(s_id=session_id) LOG.error(msg) raise exc.HTTPForbidden(explanation=msg) with unit.begin(): unit.delete(session) return None
def show(self, request, environment_id, session_id): LOG.debug('Session:Show <SessionId: {id}>'.format(id=session_id)) unit = db_session.get_session() session = unit.query(models.Session).get(session_id) check_session(request, environment_id, session, session_id) user_id = request.context.user if session.user_id != user_id: msg = _('User <UserId {usr_id}> is not authorized to access' 'session <SessionId {s_id}>.').format(usr_id=user_id, s_id=session_id) LOG.error(msg) raise exc.HTTPUnauthorized(explanation=msg) if not sessions.SessionServices.validate(session): msg = _('Session <SessionId {0}> is invalid: environment has been' ' updated or updating right now with other session' ).format(session_id) LOG.error(msg) raise exc.HTTPForbidden(explanation=msg) return session.to_dict()
def test_check_session_with_null_session(self): """Test check session with null session throws exception.""" expected_error_message = 'Session <SessionId {id}> is not found'\ .format(id=None) with self.assertRaisesRegex(exc.HTTPNotFound, expected_error_message): utils.check_session(None, None, None, None)
def test_check_session_with_mismatching_environment_id(self, _): """Test check session without matching env ids throws exception.""" mock_session = mock.MagicMock(session_id='session_id', environment_id='environment_id') environment_id = 'another_environment_id' expected_error_msg = 'Session <SessionId {session_id}> is not tied '\ 'with Environment <EnvId {environment_id}>'\ .format(session_id=mock_session.session_id, environment_id=environment_id) with self.assertRaisesRegexp(exc.HTTPBadRequest, expected_error_msg): utils.check_session(None, environment_id, mock_session, mock_session.session_id)
def deploy(self, request, environment_id, session_id): LOG.debug('Session:Deploy <SessionId: {0}>'.format(session_id)) unit = db_session.get_session() session = unit.query(models.Session).get(session_id) check_session(request, environment_id, session, session_id) if not sessions.SessionServices.validate(session): msg = _('Session <SessionId {0}> is invalid').format(session_id) LOG.error(msg) raise exc.HTTPForbidden(explanation=msg) if session.state != states.SessionState.OPENED: msg = _('Session <SessionId {0}> is already deployed or ' 'deployment is in progress').format(session_id) LOG.error(msg) raise exc.HTTPForbidden(explanation=msg) envs.EnvironmentServices.deploy(session, unit, request.context.auth_token)
def show(self, request, environment_id): LOG.debug('Environments:Show <Id: {0}>'.format(environment_id)) target = {"environment_id": environment_id} policy.check('show_environment', request.context, target) session = db_session.get_session() environment = session.query(models.Environment).get(environment_id) env = environment.to_dict() env['status'] = envs.EnvironmentServices.get_status(env['id']) session_id = None if hasattr(request, 'context') and request.context.session: session_id = request.context.session if session_id: env_session = session.query(models.Session).get(session_id) check_session(request, environment_id, env_session, session_id) # add services to env get_data = core_services.CoreServices.get_data env['services'] = get_data(environment_id, '/services', session_id) return env
def show(self, request, environment_id): LOG.debug("Environments:Show <Id: {0}>".format(environment_id)) target = {"environment_id": environment_id} policy.check("show_environment", request.context, target) session = db_session.get_session() environment = session.query(models.Environment).get(environment_id) env = environment.to_dict() env["status"] = envs.EnvironmentServices.get_status(env["id"]) session_id = None if hasattr(request, "context") and request.context.session: session_id = request.context.session if session_id: env_session = session.query(models.Session).get(session_id) check_session(request, environment_id, env_session, session_id) # add services to env get_data = core_services.CoreServices.get_data env["services"] = get_data(environment_id, "/services", session_id) return env
def show(self, request, environment_id): LOG.debug('Environments:Show <Id: {id}>'.format(id=environment_id)) target = {"environment_id": environment_id} policy.check('show_environment', request.context, target) session = db_session.get_session() environment = session.query(models.Environment).get(environment_id) env = environment.to_dict() env['status'] = envs.EnvironmentServices.get_status(env['id']) session_id = None if hasattr(request, 'context') and request.context.session: session_id = request.context.session if session_id: env_session = session.query(models.Session).get(session_id) check_session(request, environment_id, env_session, session_id) # add services to env get_data = core_services.CoreServices.get_data env['services'] = get_data(environment_id, '/services', session_id) return env