Exemple #1
0
 def get(self, service_name):
     service = ServiceModel.find_by_name(service_name)
     try:
         if service:
             return service.json()
     except Exception as e:
         return {"message": "Record not found'{}'".format(e)}, 404
Exemple #2
0
    def post(self, service_name):
        db.create_all()
        db.session.commit()
        if ServiceModel.find_by_name(service_name):
            return {
                'message':
                "An Record with name '{}' already exists.".format(service_name)
            }, 400

        data = self.parser.parse_args()
        service = ServiceModel(
            service_name,
            **data,
        )
        service.created_by = 1
        service.created_on = datetime.now()
        service.modified_by = 0
        service.deleted_by = 0
        try:
            service.save_to_db()
        except Exception as e:
            return {
                "message":
                "An error occurred while inserting the Record.'{}'".format(e)
            }

        return service.json(), 201
Exemple #3
0
    def delete(cls, service_name):
        service = ServiceModel.find_by_name(service_name)
        if service:
            service.deleted_by = 1
            service.deleted_on = datetime.now()
            service.save_to_db()
            #service.delete_from_db()
            return {'message': 'Record deleted'}

        else:
            return {"Message": "Record Not FOUND"}
Exemple #4
0
    def put(self, service_name):
        data = self.parser.parse_args()
        service = ServiceModel.find_by_name(service_name)
        if service:
            service.modified_on = datetime.now()
            service.modified_by = 1
            service.logo = data['logo']
            service.brand_id = data['brand_id']
            service.category_id = data['category_id']
            service.save_to_db()

        else:
            return {"Message": "Record Not FOUND"}
        return service.json()
Exemple #5
0
  def post(self):
    """POST method to create a new service."""

    data = self.parser.parse_args()

    if ServiceModel.find_by_name(data['name']):
      return response(400, None, f"Service with name {data['name']} already exists.", None), 400

    args = self.check_args(data)
    if args['code'] is not 200:
      return response(args['code'], None, args['error'], None), args['code']

    volumes = ServiceModel.join_volume_string(data)
    env = ServiceModel.join_env_string(data)
    exposed_ports = ServiceModel.join_port_string(data['exposed_ports'])
    mapped_ports = ServiceModel.join_port_string(data['mapped_ports'])

    service = ServiceModel(name = data['name'],
                           blueprint_id = data['blueprint'],
                           description = data['description'],
                           exposed_ports = exposed_ports,
                           mapped_ports = mapped_ports,
                           volumes = volumes,
                           restart = data['restart'],
                           env = env)

    # Add stack to service table
    if data['stacks'] and data['stacks'] != [None]:
      for x in data['stacks']:
        stack = StackModel.find_by_id(x)
        service.stacks.append(stack)

    try:
      service.save_to_db()
      service.ip = service.get_ip(service.id)
      service.save_to_db()
    except:
      return response(500, None, f"An error occured while trying to update service {data['name']}.", None), 500

    return response(201, f"Service {data['name']} has been updated.", None, service.json()), 201
Exemple #6
0
    def post(self):
        data = self.parser.parse_args()
        service = ServiceModel(**data)
        service.created_by = 1
        service.created_on = datetime.now()
        service.modified_by = 0
        service.deleted_by = 0
        service.modified_on = None
        service.deleted_on = None

        try:
            validateObj = ServiceModel.validateData(data, request)

            if type(validateObj) is dict:
                return {"success": False, "errors": validateObj}, 400

            if ServiceModel.find_by_name(data['service_name']):
                return {
                    "success": False,
                    "message": "A service with that Record Name already exists"
                }, 400
            name = data['service_name']
            if name.strip():
                service.save_to_db()
                service.token = encodeID(service.service_id)
                service.save_to_db()
            else:
                return {
                    "success": False,
                    "message": "String Should not be empty"
                }
        except Exception as e:
            return {
                "message":
                "An error occurred creating the Record.'{}'".format(e)
            }, 500
        return service.json(), 201
Exemple #7
0
  def put(self):
    """PUT method to create or update a service."""    

    data = self.parser.parse_args()

    args = self.check_args(data)
    if args['code'] is not 200:
      return response(args['code'], None, args['error'], None), args['code']

    service = ServiceModel.find_by_name(data['name'])

    volumes = ServiceModel.join_volume_string(data)
    env = ServiceModel.join_env_string(data)
    exposed_ports = ServiceModel.join_port_string(data['exposed_ports'])
    mapped_ports = ServiceModel.join_port_string(data['mapped_ports'])
    
    if service:
      service.name = data['name']
      service.description = data['description']
      service.exposed_ports = exposed_ports
      service.mapped_ports = mapped_ports
      service.volumes = volumes
      service.env = env
      service.restart = data['restart']
      service.blueprint_id = data['blueprint']

      if data['stacks'] and data['stacks'] != [None]:
        # Get sets of stacks which need to be updated or deleted
        old = {x.id for x in service.stacks}
        new = set(data['stacks'])
        to_update = new - old
        to_delete = old - new

        # Remove old stacks
        for x in to_delete:
          service.stacks.remove(StackModel.find_by_id(x))

        # Add new services
        for x in to_update:
          service.stacks.append(StackModel.find_by_id(x))
      else:
        for x in [y.id for y in service.stacks]:
          service.stacks.remove(StackModel.find_by_id(x))

    else:
      service = ServiceModel(name = data['name'],
                             description = data['description'],
                             exposed_ports = exposed_ports,
                             mapped_ports = mapped_ports,
                             volumes = volumes,
                             env = env,
                             restart = data['restart'],
                             blueprint_id = data['blueprint'])

      if data['stacks'] and data['stacks'] != [None]:
        for x in data['stacks']:
          stack = StackModel.find_by_id(x)
          service.stacks.append(stack)

    try:
      service.save_to_db()
      service.ip = service.get_ip(service.id)
      service.save_to_db()
    except:
      return response(500, None, f"An error occured while trying to update service {data['name']}.", None), 500

    return response(201, f"Service {data['name']} has been updated.", None, service.json()), 201