def __inner(self, request, *args, **kwargs): if hasattr(request, 'context') and not request.context.session: LOG.info(_('Session is required for this call')) raise exc.HTTPForbidden() session_id = request.context.session unit = db_session.get_session() session = unit.query(models.Session).get(session_id) if session is None: LOG.info(_('Session <SessionId {0}> ' 'is not found').format(session_id)) raise exc.HTTPForbidden() if not sessions.SessionServices.validate(session): LOG.info(_('Session <SessionId {0}> ' 'is invalid').format(session_id)) raise exc.HTTPForbidden() if session.state == sessions.SessionState.deploying: LOG.info(_('Session <SessionId {0}> is already in ' 'deployment state').format(session_id)) raise exc.HTTPForbidden() return func(self, request, *args, **kwargs)
def show(self, request, environment_id): LOG.debug(_('Environments:Show <Id: {0}>').format(environment_id)) session = db_session.get_session() environment = session.query(models.Environment).get(environment_id) if environment is None: LOG.info( _('Environment <EnvId {0}> is not found').format( environment_id)) raise exc.HTTPNotFound if environment.tenant_id != request.context.tenant: LOG.info( _('User is not authorized to access ' 'this tenant resources.')) raise exc.HTTPUnauthorized 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 #add services to env get_data = core_services.CoreServices.get_data env['services'] = get_data(environment_id, '/services', session_id) return env
def destroy_instance(instance_id, environment_id): unit = db_session.get_session() instance = unit.query(models.Instance).get( (environment_id, instance_id)) if instance and not instance.destroyed: instance.destroyed = timeutils.utcnow_ts() instance.save(unit)
def category_get_names(): session = db_session.get_session() categories = [] for row in session.query(models.Category.name).all(): for name in row: categories.append(name) return categories
def create(environment_id, user_id): """ Creates session object for specific environment for specified user. :param environment_id: Environment Id :param user_id: User Id :return: Created session """ unit = db_session.get_session() environment = unit.query(models.Environment).get(environment_id) session = models.Session() session.environment_id = environment.id session.user_id = user_id session.state = SessionState.open # used for checking if other sessions was deployed before this one session.version = environment.version # all changes to environment is stored here, and translated to # environment only after deployment completed session.description = environment.description with unit.begin(): unit.add(session) return session
def validate(session): """ Session is valid only if no other session for same environment was already deployed on in deploying state, :param session: Session for validation """ #if other session is deploying now current session is invalid unit = db_session.get_session() #if environment version is higher then version on which current session #is created then other session was already deployed current_env = unit.query(models.Environment).\ get(session.environment_id) if current_env.version > session.version: return False #if other session is deploying now current session is invalid other_is_deploying = unit.query(models.Session).filter_by( environment_id=session.environment_id, state=SessionState.deploying ).count() > 0 if session.state == SessionState.open and other_is_deploying: return False return True
def configure(self, request, environment_id): LOG.debug(_('Session:Configure <EnvId: {0}>').format(environment_id)) unit = db_session.get_session() environment = unit.query(models.Environment).get(environment_id) if environment is None: LOG.info( _('Environment <EnvId {0}> ' 'is not found').format(environment_id)) raise exc.HTTPNotFound if environment.tenant_id != request.context.tenant: LOG.info( _('User is not authorized to access ' 'this tenant resources.')) raise exc.HTTPUnauthorized # no new session can be opened if environment has deploying status env_status = envs.EnvironmentServices.get_status(environment_id) if env_status == envs.EnvironmentStatus.deploying: LOG.info( _('Could not open session for environment <EnvId: {0}>,' 'environment has deploying ' 'status.').format(environment_id)) raise exc.HTTPForbidden() user_id = request.context.user session = sessions.SessionServices.create(environment_id, user_id) return session.to_dict()
def show(self, request, environment_id, session_id): LOG.debug(_('Session:Show <SessionId: {0}>').format(session_id)) unit = db_session.get_session() session = unit.query(models.Session).get(session_id) if session is None: LOG.error( _('Session <SessionId {0}> ' 'is not found').format(session_id)) raise exc.HTTPNotFound() if session.environment_id != environment_id: LOG.error( _('Session <SessionId {0}> is not tied with Environment ' '<EnvId {1}>').format(session_id, environment_id)) raise exc.HTTPNotFound() user_id = request.context.user if session.user_id != user_id: LOG.error( _('User <UserId {0}> is not authorized to access session' '<SessionId {1}>.').format(user_id, session_id)) raise exc.HTTPUnauthorized() if not sessions.SessionServices.validate(session): LOG.error( _('Session <SessionId {0}> ' 'is invalid').format(session_id)) raise exc.HTTPForbidden() return session.to_dict()
def show(self, request, environment_id): LOG.debug(_('Environments:Show <Id: {0}>').format(environment_id)) session = db_session.get_session() environment = session.query(models.Environment).get(environment_id) if environment is None: LOG.info(_('Environment <EnvId {0}> is not found').format( environment_id)) raise exc.HTTPNotFound if environment.tenant_id != request.context.tenant: LOG.info(_('User is not authorized to access ' 'this tenant resources.')) raise exc.HTTPUnauthorized 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 #add services to env get_data = core_services.CoreServices.get_data env['services'] = get_data(environment_id, '/services', session_id) return env
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) if session is None: LOG.error( _('Session <SessionId {0}> ' 'is not found').format(session_id)) raise exc.HTTPNotFound() if session.environment_id != environment_id: LOG.error( _('Session <SessionId {0}> is not tied with Environment ' '<EnvId {1}>').format(session_id, environment_id)) raise exc.HTTPNotFound() if not sessions.SessionServices.validate(session): LOG.error( _('Session <SessionId {0}> ' 'is invalid').format(session_id)) raise exc.HTTPForbidden() if session.state != sessions.SessionState.open: LOG.error( _('Session <SessionId {0}> is already deployed or ' 'deployment is in progress').format(session_id)) raise exc.HTTPForbidden() sessions.SessionServices.deploy(session, unit, request.context.auth_token)
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) if session is None: LOG.error(_('Session <SessionId {0}> ' 'is not found').format(session_id)) raise exc.HTTPNotFound() if session.environment_id != environment_id: LOG.error(_('Session <SessionId {0}> is not tied with Environment ' '<EnvId {1}>').format(session_id, environment_id)) raise exc.HTTPNotFound() if not sessions.SessionServices.validate(session): LOG.error(_('Session <SessionId {0}> ' 'is invalid').format(session_id)) raise exc.HTTPForbidden() if session.state != sessions.SessionState.open: LOG.error(_('Session <SessionId {0}> is already deployed or ' 'deployment is in progress').format(session_id)) raise exc.HTTPForbidden() sessions.SessionServices.deploy(session, unit, request.context.auth_token)
def create(environment_params, tenant_id): #tagging environment by tenant_id for later checks """ Creates environment with specified params, in particular - name :param environment_params: Dict, e.g. {'name': 'env-name'} :param tenant_id: Tenant Id :return: Created Environment """ objects = {'?': { 'id': uuidutils.generate_uuid(), }} objects.update(environment_params) objects.update( EnvironmentServices.generate_default_networks(objects['name'])) objects['?']['type'] = 'io.murano.Environment' environment_params['tenant_id'] = tenant_id data = { 'Objects': objects, 'Attributes': [] } environment = models.Environment() environment.update(environment_params) unit = db_session.get_session() with unit.begin(): unit.add(environment) #saving environment as Json to itself environment.update({'description': data}) environment.save(unit) return environment
def delete(self, request, environment_id, session_id): LOG.debug(_('Session:Delete <SessionId: {0}>').format(session_id)) unit = db_session.get_session() session = unit.query(models.Session).get(session_id) if session is None: LOG.error(_('Session <SessionId {0}> ' 'is not found').format(session_id)) raise exc.HTTPNotFound() if session.environment_id != environment_id: LOG.error(_('Session <SessionId {0}> is not tied with Environment ' '<EnvId {1}>').format(session_id, environment_id)) raise exc.HTTPNotFound() user_id = request.context.user if session.user_id != user_id: LOG.error(_('User <UserId {0}> is not authorized to access session' '<SessionId {1}>.').format(user_id, session_id)) raise exc.HTTPUnauthorized() if session.state == sessions.SessionState.deploying: LOG.error(_('Session <SessionId: {0}> is in deploying state and ' 'could not be deleted').format(session_id)) raise exc.HTTPForbidden() with unit.begin(): unit.delete(session) return None
def track_instance(instance_id, environment_id, instance_type, type_name, type_title=None, unit_count=None): unit = db_session.get_session() try: with unit.begin(): env = unit.query(models.Environment).get(environment_id) instance = models.Instance() instance.instance_id = instance_id instance.environment_id = environment_id instance.tenant_id = env.tenant_id instance.instance_type = instance_type instance.created = timeutils.utcnow_ts() instance.destroyed = None instance.type_name = type_name instance.type_title = type_title instance.unit_count = unit_count unit.add(instance) except exception.DBDuplicateEntry: unit.execute( sqlalchemy.update(models.Instance).where( models.Instance.instance_id == instance_id and models.Instance.environment_id == environment_id).values( unit_count=unit_count))
def configure(self, request, environment_id): LOG.debug(_('Session:Configure <EnvId: {0}>').format(environment_id)) unit = db_session.get_session() environment = unit.query(models.Environment).get(environment_id) if environment is None: LOG.info(_('Environment <EnvId {0}> ' 'is not found').format(environment_id)) raise exc.HTTPNotFound if environment.tenant_id != request.context.tenant: LOG.info(_('User is not authorized to access ' 'this tenant resources.')) raise exc.HTTPUnauthorized # no new session can be opened if environment has deploying status env_status = envs.EnvironmentServices.get_status(environment_id) if env_status == envs.EnvironmentStatus.deploying: LOG.info(_('Could not open session for environment <EnvId: {0}>,' 'environment has deploying ' 'status.').format(environment_id)) raise exc.HTTPForbidden() user_id = request.context.user session = sessions.SessionServices.create(environment_id, user_id) return session.to_dict()
def show(self, request, environment_id, session_id): LOG.debug(_('Session:Show <SessionId: {0}>').format(session_id)) unit = db_session.get_session() session = unit.query(models.Session).get(session_id) if session is None: LOG.error(_('Session <SessionId {0}> ' 'is not found').format(session_id)) raise exc.HTTPNotFound() if session.environment_id != environment_id: LOG.error(_('Session <SessionId {0}> is not tied with Environment ' '<EnvId {1}>').format(session_id, environment_id)) raise exc.HTTPNotFound() user_id = request.context.user if session.user_id != user_id: LOG.error(_('User <UserId {0}> is not authorized to access session' '<SessionId {1}>.').format(user_id, session_id)) raise exc.HTTPUnauthorized() if not sessions.SessionServices.validate(session): LOG.error(_('Session <SessionId {0}> ' 'is invalid').format(session_id)) raise exc.HTTPForbidden() return session.to_dict()
def delete(self, request, environment_id, session_id): LOG.debug(_('Session:Delete <SessionId: {0}>').format(session_id)) unit = db_session.get_session() session = unit.query(models.Session).get(session_id) if session is None: LOG.error( _('Session <SessionId {0}> ' 'is not found').format(session_id)) raise exc.HTTPNotFound() if session.environment_id != environment_id: LOG.error( _('Session <SessionId {0}> is not tied with Environment ' '<EnvId {1}>').format(session_id, environment_id)) raise exc.HTTPNotFound() user_id = request.context.user if session.user_id != user_id: LOG.error( _('User <UserId {0}> is not authorized to access session' '<SessionId {1}>.').format(user_id, session_id)) raise exc.HTTPUnauthorized() if session.state == sessions.SessionState.deploying: LOG.error( _('Session <SessionId: {0}> is in deploying state and ' 'could not be deleted').format(session_id)) raise exc.HTTPForbidden() with unit.begin(): unit.delete(session) return None
def category_add(category_name): session = db_session.get_session() category = models.Category() with session.begin(): category.update({'name': category_name}) category.save(session) return category
def get_draft(environment_id=None, session_id=None): unit = db_session.get_session() #TODO: When session is deployed should be returned env.description if session_id: session = unit.query(models.Session).get(session_id) return session.description else: environment = unit.query(models.Environment).get(environment_id) return environment.description
def process_result(context, result): secure_result = token_sanitizer.TokenSanitizer().sanitize(result) LOG.debug(_('Got result from orchestration ' 'engine:\n{0}').format(secure_result)) result_id = result['Objects']['?']['id'] if 'deleted' in result: LOG.debug(_('Result for environment {0} is dropped. Environment ' 'is deleted').format(result_id)) return unit = session.get_session() environment = unit.query(models.Environment).get(result_id) if not environment: LOG.warning(_('Environment result could not be handled, specified ' 'environment was not found in database')) return environment.description = result environment.description['Objects']['services'] = \ environment.description['Objects'].get('applications', []) del environment.description['Objects']['applications'] environment.networking = result.get('networking', {}) environment.version += 1 environment.save(unit) #close session conf_session = unit.query(models.Session).filter_by( **{'environment_id': environment.id, 'state': 'deploying'}).first() conf_session.state = 'deployed' conf_session.save(unit) #close deployment deployment = get_last_deployment(unit, environment.id) deployment.finished = timeutils.utcnow() num_errors = unit.query(models.Status)\ .filter_by(level='error', deployment_id=deployment.id).count() num_warnings = unit.query(models.Status)\ .filter_by(level='warning', deployment_id=deployment.id).count() final_status_text = "Deployment finished" if num_errors: final_status_text += " with errors" elif num_warnings: final_status_text += " with warnings" status = models.Status() status.deployment_id = deployment.id status.text = final_status_text status.level = 'info' deployment.statuses.append(status) deployment.save(unit)
def package_get(package_id, context): """ Return package details :param package_id: ID of a package, string :returns: detailed information about package, dict """ session = db_session.get_session() package = _package_get(package_id, session) _authorize_package(package, context, allow_public=True) return package
def index(self, request, environment_id): unit = db_session.get_session() verify_and_get_env(unit, environment_id, request) query = unit.query(models.Deployment) \ .filter_by(environment_id=environment_id) \ .order_by(desc(models.Deployment.created)) result = query.all() deployments = [ set_dep_state(deployment, unit).to_dict() for deployment in result ] return {'deployments': deployments}
def package_delete(package_id, context): """ Delete package information from the system ID of a package, string parameters to update """ session = db_session.get_session() with session.begin(): package = session.query(models.Package).get(package_id) _authorize_package(package, context) session.delete(package)
def get_environments_by(filters): """ Returns list of environments :param filters: property filters :return: Returns list of environments """ unit = db_session.get_session() environments = unit.query(models.Environment).\ filter_by(**filters).all() for env in environments: env['status'] = EnvironmentServices.get_status(env['id']) return environments
def __inner(self, request, environment_id, *args, **kwargs): unit = db_session.get_session() environment = unit.query(models.Environment).get(environment_id) if environment is None: LOG.info(_("Environment with id '{0}'" " not found").format(environment_id)) raise exc.HTTPNotFound() if hasattr(request, 'context'): if environment.tenant_id != request.context.tenant: LOG.info(_('User is not authorized to access' ' this tenant resources')) raise exc.HTTPUnauthorized() return func(self, request, environment_id, *args, **kwargs)
def create(host, request_count, error_count, average_response_time, request_per_tenant, cpu_count, cpu_percent): stats = m.ApiStats() stats.host = host stats.request_count = request_count stats.error_count = error_count stats.average_response_time = average_response_time stats.request_per_tenant = request_per_tenant stats.request_per_second = 0.0 stats.errors_per_second = 0.0 stats.cpu_count = cpu_count stats.cpu_percent = cpu_percent db = db_session.get_session() stats.save(db)
def get_aggregated_stats(environment_id): unit = db_session.get_session() now = timeutils.utcnow_ts() query = unit.query(models.Instance.instance_type, func.sum( func.coalesce(models.Instance.destroyed, now) - models.Instance.created), func.count()).filter( models.Instance.environment_id == environment_id) res = query.group_by(models.Instance.instance_type).all() return [{ 'type': int(record[0]), 'duration': int(record[1]), 'count': int(record[2]) } for record in res]
def _get_tags(tag_names, session=None): """ Return existing tags object or create new ones :param tag_names: name of tags to associate with package, list :returns: list of Tag objects to associate with package, list """ if session is None: session = db_session.get_session() tags = [] for tag_name in tag_names: tag_obj = session.query(models.Tag).filter_by(name=tag_name).first() if tag_obj: tags.append(tag_obj) else: tag_record = models.Tag(name=tag_name) tags.append(tag_record) return tags
def report_notification(report): LOG.debug( _('Got report from orchestration ' 'engine:\n{0}').format(report)) report['entity_id'] = report['id'] del report['id'] status = models.Status() status.update(report) unit = session.get_session() #connect with deployment with unit.begin(): running_deployment = get_last_deployment(unit, status.environment_id) status.deployment_id = running_deployment.id unit.add(status)
def report_notification(report): LOG.debug(_('Got report from orchestration ' 'engine:\n{0}').format(report)) report['entity_id'] = report['id'] del report['id'] status = models.Status() status.update(report) unit = session.get_session() #connect with deployment with unit.begin(): running_deployment = get_last_deployment(unit, status.environment_id) status.deployment_id = running_deployment.id unit.add(status)
def delete(self, request, environment_id): LOG.debug(_('Environments:Delete <Id: {0}>').format(environment_id)) unit = db_session.get_session() environment = unit.query(models.Environment).get(environment_id) if environment is None: LOG.info(_('Environment <EnvId {0}> ' 'is not found').format(environment_id)) raise exc.HTTPNotFound if environment.tenant_id != request.context.tenant: LOG.info(_('User is not authorized to access ' 'this tenant resources.')) raise exc.HTTPUnauthorized envs.EnvironmentServices.delete(environment_id, request.context.auth_token)
def save_environment_description(session_id, environment, inner=True): """ Saves environment description to specified session :param session_id: Session Id :param environment: Environment Description :param inner: save modifications to only content of environment rather than whole Object Model structure """ unit = db_session.get_session() session = unit.query(models.Session).get(session_id) if inner: data = session.description.copy() data['Objects'] = environment session.description = data else: session.description = environment session.save(unit)
def package_update(pkg_id, changes, context): """ Update package information :param changes: parameters to update :returns: detailed information about new package, dict """ operation_methods = {'add': _do_add, 'replace': _do_replace, 'remove': _do_remove} session = db_session.get_session() with session.begin(): pkg = _package_get(pkg_id, session) _authorize_package(pkg, context) for change in changes: pkg = operation_methods[change['op']](pkg, change) session.add(pkg) return pkg
def delete(self, request, environment_id): LOG.debug(_('Environments:Delete <Id: {0}>').format(environment_id)) unit = db_session.get_session() environment = unit.query(models.Environment).get(environment_id) if environment is None: LOG.info( _('Environment <EnvId {0}> ' 'is not found').format(environment_id)) raise exc.HTTPNotFound if environment.tenant_id != request.context.tenant: LOG.info( _('User is not authorized to access ' 'this tenant resources.')) raise exc.HTTPUnauthorized envs.EnvironmentServices.delete(environment_id, request.context.auth_token)
def last(self, request, environment_id): session_id = None if hasattr(request, 'context') and request.context.session: session_id = request.context.session services = core_services.CoreServices.get_data(environment_id, '/services', session_id) session = db_session.get_session() result = {} for service in services: service_id = service['?']['id'] entity_ids = utils.build_entity_map(service).keys() last_status = session.query(models.Status). \ filter(models.Status.entity_id.in_(entity_ids)). \ order_by(desc(models.Status.created)). \ first() if last_status: result[service_id] = last_status.to_dict() else: result[service_id] = None return {'lastStatuses': result}
def _get_categories(category_names, session=None): """ Return existing category objects or raise an exception :param category_names: name of categories to associate with package, list :returns: list of Category objects to associate with package, list """ if session is None: session = db_session.get_session() categories = [] for ctg_name in category_names: ctg_obj = session.query(models.Category).filter_by( name=ctg_name).first() if not ctg_obj: msg = _("Category '{name}' doesn't exist").format(name=ctg_name) LOG.error(msg) # it's not allowed to specify non-existent categories raise exc.HTTPBadRequest(msg) categories.append(ctg_obj) return categories
def get_raw_environment_stats(environment_id, instance_id=None): unit = db_session.get_session() now = timeutils.utcnow_ts() query = unit.query(models.Instance).filter( models.Instance.environment_id == environment_id) if instance_id: query = query.filter(models.Instance.instance_id == instance_id) res = query.all() return [{ 'type': record.instance_type, 'duration': (record.destroyed or now) - record.created, 'type_name': record.type_name, 'unit_count': record.unit_count, 'instance_id': record.instance_id, 'type_title': record.type_title, 'active': True if not record.destroyed else False } for record in res]
def update(self, request, environment_id, body): LOG.debug(_('Environments:Update <Id: {0}, ' 'Body: {1}>').format(environment_id, body)) session = db_session.get_session() environment = session.query(models.Environment).get(environment_id) if environment is None: LOG.info(_('Environment <EnvId {0}> is not ' 'found').format(environment_id)) raise exc.HTTPNotFound if environment.tenant_id != request.context.tenant: LOG.info(_('User is not authorized to access ' 'this tenant resources.')) raise exc.HTTPUnauthorized environment.update(body) environment.save(session) return environment.to_dict()
def get_service_status(environment_id, session_id, service): status = 'draft' unit = db_session.get_session() session_state = unit.query(models.Session).get(session_id).state entities = [u['id'] for u in service['units']] reports_count = unit.query(models.Status).filter( models.Status.environment_id == environment_id and models.Status.session_id == session_id and models.Status.entity_id.in_(entities)).count() if session_state == 'deployed': status = 'finished' if session_state == 'deploying' and reports_count == 0: status = 'pending' if session_state == 'deploying' and reports_count > 0: status = 'inprogress' return status
def statuses(self, request, environment_id, deployment_id): unit = db_session.get_session() query = unit.query(models.Status) \ .filter_by(deployment_id=deployment_id) \ .order_by(models.Status.created) deployment = verify_and_get_deployment(unit, environment_id, deployment_id) if 'service_id' in request.GET: service_id_set = set(request.GET.getall('service_id')) environment = deployment.description entity_ids = [] for service in environment.get('services', []): if service['?']['id'] in service_id_set: id_map = utils.build_entity_map(service) entity_ids = entity_ids + id_map.keys() if entity_ids: query = query.filter(models.Status.entity_id.in_(entity_ids)) else: return {'reports': []} result = query.all() return {'reports': [status.to_dict() for status in result]}
def update(self, request, environment_id, body): LOG.debug( _('Environments:Update <Id: {0}, ' 'Body: {1}>').format(environment_id, body)) session = db_session.get_session() environment = session.query(models.Environment).get(environment_id) if environment is None: LOG.info( _('Environment <EnvId {0}> is not ' 'found').format(environment_id)) raise exc.HTTPNotFound if environment.tenant_id != request.context.tenant: LOG.info( _('User is not authorized to access ' 'this tenant resources.')) raise exc.HTTPUnauthorized environment.update(body) environment.save(session) return environment.to_dict()
def get_sessions(environment_id, state=None): """ Get list of sessions for specified environment :param environment_id: Environment Id :param state: glazierapi.db.services.environments.EnvironmentStatus :return: Sessions for specified Environment, if SessionState is not defined all sessions for specified environment is returned. """ unit = db_session.get_session() # Here we duplicate logic for reducing calls to database # Checks for validation is same as in validate. environment = unit.query(models.Environment).get(environment_id) return unit.query(models.Session).filter( #Get all session for this environment models.Session.environment_id == environment_id, #in this state, if state is not specified return in all states models.Session.state.in_(SessionState if state is None else [state]), #Only sessions with same version as current env version are valid models.Session.version == environment.version ).all()
def delete(environment_id, token): """ Deletes environment and notify orchestration engine about deletion :param environment_id: Environment that is going to be deleted :param token: OpenStack auth token """ unit = db_session.get_session() environment = unit.query(models.Environment).get(environment_id) #preparing data for removal from conductor env = environment.description env['Objects'] = None data = { 'model': env, 'token': token, 'tenant_id': environment.tenant_id } rpc.engine().handle_task(data) with unit.begin(): unit.delete(environment)
def get_environment_description(environment_id, session_id=None, inner=True): """ Returns environment description for specified environment. If session is specified and not in deploying state function returns modified environment description, otherwise returns actual environment desc. :param environment_id: Environment Id :param session_id: Session Id :param inner: return contents of environment rather than whole Object Model structure :return: Environment Description Object """ unit = db_session.get_session() if session_id: session = unit.query(models.Session).get(session_id) if sessions.SessionServices.validate(session): if session.state != sessions.SessionState.deployed: env_description = session.description else: env = unit.query(models.Environment)\ .get(session.environment_id) env_description = env.description else: env = unit.query(models.Environment)\ .get(session.environment_id) env_description = env.description else: env = (unit.query(models.Environment).get(environment_id)) env_description = env.description if not inner: return env_description else: return env_description['Objects']
def update(host, stats): db = db_session.get_session() stats.save(db)
def save(self, session=None): """Save this object""" session = session or db_session.get_session() session.add(self) session.flush()