Exemplo n.º 1
0
  def check_args(self, data):
    """Helper method to check various passed payload arguments

    Note:
      This does not check if the proxy_port is part of the published ports ports

    Args:
      data (:obj:`dict`): Request payload with parsed arguments. Arguments to be checked:
        data['name'] (str): Name of the stack.
        data['description'] (str): Description for the stack.
        data['subdomain'] (str): Subdomain of the stack.
        data['email'] (str): The email needed for Caddy to grab a TLS certificate.
        data['proxy_port'] (int): The main port Caddy will forward requests to for this stack.

    Returns:
      dict: If all checks pass, dict of type {'code': 200}. 
        If one check fails, dict of type {'code': <error code>, 'error' <error message>}, where the code and
        message will be directly fed into a response.

    """ 

    # Regex check for valid name
    if not StackModel.valid_name(data['name']):
      return {'code': 400, 'error': f"Invalid stack name {data['name']}."}

    # Regex check for valid proxy_port
    if data['proxy_port'] and not ServiceModel.valid_ports([data['proxy_port']]):
      return {'code': 400, 'error': f"Invalid proxy_port {data['proxy_port']}"}

    # Regex check for valid subdomain
    if data['subdomain'] and not StackModel.valid_subdomain(data['subdomain']):
      return {'code': 400, 'error': f"Invalid subdomain {data['subdomain']}."}

    # Regex check for valid email
    if data['email'] and not StackModel.valid_email(data['email']):
      return {'code': 400, 'error': f"Email {data['email']} is invalid."}

    return {'code': 200}
Exemplo n.º 2
0
  def check_args(self, data):
    """Helper method to check various passed payload arguments

    Args:
      data (:obj:`dict`): Request payload with parsed arguments.
        data['blueprint'] (int): Blueprint ID which service will be derived from.
        data['description'] (string): The service description.
        data['name'] (str): Name of service.
        data['exposed_ports'] (list of int): Ports to be exposed.
        data['mapped_ports'] (list of str): Ports to be mapped between host and container.
        data['volumes'](list of str): Volumes to be mapped between host and container.
        data['env'] (list of str): Environment variables for container.
        data['restart'] (str): The restart flag for the container.
        data['stacks'] (list of int): Stack this service should be a part of.

    Returns:
      dict: If all checks pass, dict of type {'code': 200}. 
        If one check fails, dict of type {'code': <error code>, 'error' <error message>}, where the code and
        message will be directly fed into a response.

    """
    # Check if blueprint exists
    if not BlueprintModel.find_by_id(data['blueprint']):
      return {'code': 400, 'error': f"Blueprint with ID {data['blueprint']} hasn't been found."}

    # Regex check for name
    if not StackModel.valid_name(data['name']):
      return {'code': 400, 'error': f"Invalid service name {data['name']}."}
    
    if data['exposed_ports']:
      # Regex check if passed exposed_ports are correct
      if not ServiceModel.valid_ports(data['exposed_ports']):
        return {'code': 400, 'error': f"Invalid exposed_ports."}

    if data['mapped_ports']:
      # Regex check if mapped_ports are correct, those are passed as string like '80:80,8080:8080'
      if not ServiceModel.valid_mapped_ports(data['mapped_ports']):
        return {'code': 400, 'error': f"Invalid mapped_ports."}

    # Regex check if volumes are correct
    if data['volumes']:
      if not ServiceModel.valid_volumes(data['volumes']):
        return {'code': 400, 'error': f"Invalid volumes"} 

    # Regex check if environment variables are correct
    if data['env']:
      if not ServiceModel.valid_env(data['env']):
        return {'code': 400, 'error': f"Invalid environment variables."}

    # Check if restart flag is part of allowed options:
    if data['restart']:
      if data['restart'].lower() not in ["no", "always"]:
        return {'code': 400, 'error': f"Invalid restart flag."}

    # Check if passed stack exists.
    if data['stacks'] and data['stacks'] != [None]:
      for x in data['stacks']:
        if not StackModel.find_by_id(x):
          return {'code': 400, 'error': f"Stack with ID {x} cannot be found."}   

    return {'code': 200}