def post(self, *args, **kwargs): """ Receives a request for a new Instance """ (args, token) = (self.params.post, kwargs.get('token')) try: db.set_autocommit(False) with db.transaction(): instance = Instance.create( instance_id=Instance.generate_instance_id(token), state=INSTANCE_STATES['REQUESTED'], token=token) instance.resources = [] for resource, value in args.items(): if value not in (None, ''): instance.resources.append( InstanceResourceModel.create( kind=resource, value=value, instance=instance)) except Exception as ex: logger.error('Cannot create new resources' ', exception: %s' % ex.message) db.rollback() return abort(500) else: db.commit() finally: db.set_autocommit(True) return marshal(instance, self.params.fields)
def delete(self, *args, **kwargs): """ Receives a request for destroy a running/requested instance """ (args, token) = (self.params.delete, kwargs.get('token')) instances = Instance.update(state=INSTANCE_STATES['DELETED']).where( Instance.token == kwargs.get('token')) if args['state']: instances = instances.where(Instance.state == INSTANCE_STATES[args['state']]) instance_ids = args.get('instance_ids', None) if instance_ids: instance_filters = [] for instance_id in instance_ids: instance_filters.append(Instance.instance_id == instance_id) import operator instances = instances.where(reduce(operator.or_, instance_filters)) affected = instances.execute() return affected
def get(self, *args, **kwargs): """ List all instances """ (args, token) = (self.params.get, kwargs.get('token')) instances = Instance.select() if args['state']: instances = instances.where(Instance.state == INSTANCE_STATES[args['state']]) if not args['all']: instances = instances.where( Instance.token == kwargs.get('token')) m_instances = [] for instance in instances: resource_filters = args.get('resource_filter', None) if resource_filters: if instance.has_resources(resource_filters) == True: m_instances.append(instance) else: m_instances.append(instance) m_instances = map(lambda i: i.hydrate(marshal, self.params.fields, self.params.resource.fields), m_instances) instances = { 'count' : len(m_instances), 'instances': m_instances } return instances
def get(self, public_key): """ List all instances """ instances = Instance.select().where( Instance.public_key == public_key) if self.params.get['state'] is not None: instances = instances.where( Instance.state == self.params.get['state']) m_instances = [] for instance in instances: resource_filters = self.params.get.get('resource_filter', None) if resource_filters: if instance.has_resources(resource_filters): m_instances.append(instance) else: m_instances.append(instance) m_instances = map(lambda i: i.hydrate(marshal, self.params.fields, self.params.resource.fields), m_instances) return marshal_and_count('instances', m_instances)
def post(self, public_key): """ Receives a request for a new Instance """ try: instance = Instance.create_from_args(self.params.post, public_key) job_id = InstanceTask().start(instance.instance_id) except Exception as ex: logger.error(ex.message) return abort(500) return marshal_and_count('instances', instance, f=self.params.fields, job_id=job_id)