Example #1
0
    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)