Example #1
0
    def __get_config(self, service_id, user_id, app_id, service_type="", vpn=None):
        """Add manager configuration"""
        if not config_parser.has_section("manager"):
            config_parser.add_section("manager")

        if config_parser.has_option('conpaas', 'DEPLOYMENT_NAME'):
            conpaas_deployment_name = config_parser.get('conpaas', 'DEPLOYMENT_NAME')
        else:
            conpaas_deployment_name = 'conpaas'

        config_parser.set("manager", "DEPLOYMENT_NAME", conpaas_deployment_name)
        config_parser.set("manager", "SERVICE_ID", service_id)
        config_parser.set("manager", "USER_ID", user_id)
        config_parser.set("manager", "APP_ID", app_id)
        config_parser.set("manager", "CREDIT_URL",
                        config_parser.get('director',
                                            'DIRECTOR_URL') + "/credit")
        config_parser.set("manager", "TERMINATE_URL",
                        config_parser.get('director',
                                            'DIRECTOR_URL') + "/terminate")
        config_parser.set("manager", "CA_URL",
                        config_parser.get('director',
                                            'DIRECTOR_URL') + "/ca")
        config_parser.set("manager", "TYPE", service_type)

        if vpn:
            config_parser.set("manager", "IPOP_SUBNET", vpn)

        return config_parser
Example #2
0
def available_clouds():
    """GET /available_clouds"""
    clouds = ['default']
    if config_parser.has_option('iaas','OTHER_CLOUDS'):
        clouds.extend([cloud_name for cloud_name
            in config_parser.get('iaas', 'OTHER_CLOUDS').split(',')
            if config_parser.has_section(cloud_name)])
    return build_response(simplejson.dumps(clouds))
Example #3
0
def available_clouds():
    """GET /available_clouds"""
    clouds = ['default']
    if config_parser.has_option('iaas','OTHER_CLOUDS'):
        clouds.extend([cloud_name for cloud_name
            in config_parser.get('iaas', 'OTHER_CLOUDS').split(',')
            if config_parser.has_section(cloud_name)])
    return simplejson.dumps(clouds)
Example #4
0
def new_user():
    values = {}
    required_fields = ('username', 'fname', 'lname', 'email',
                       'affiliation', 'password', 'credit', 'uuid', 'openid')

    log('New user "%s <%s>" creation attempt' % (
        request.values.get('username'), request.values.get('email')))

    # check for presence of mandatory fields
    for field in required_fields:
        values[field] = request.values.get(field)

        if not values[field]:
            log('Missing required field: %s' % field)

            return build_response(jsonify({
                'error': True, 'msg': '%s is a required field' % field,
            }))
        if field == 'uuid' and values[field] == '<none>':
            values[field] = ''

    # check if the provided username already exists
    if User.query.filter_by(username=values['username']).first():
        log('User %s already exists' % values['username'])

        return build_response(jsonify({
            'error': True,
            'msg': 'Username "%s" already taken' % values['username'],
        }))

    # check if the provided email already exists
    if False and User.query.filter_by(email=values['email']).first(): # for debugging purposes allow duplicate email address
        log('Duplicate email: %s' % values['email'])

        return build_response(jsonify({
            'error': True,
            'msg': 'E-mail "%s" already registered' % values['email'],
        }))

    # check that the requested credit does not exceed the maximum
    if config_parser.has_option('conpaas', 'MAX_CREDIT'):
        max_credit = config_parser.get('conpaas', 'MAX_CREDIT')
        try:
            max_credit = int(max_credit)
        except ValueError:
            log('ERROR: Parameter MAX_CREDIT "%s" is not a valid integer.'
                ' Defaulting to maximum credit %s.' % (max_credit, default_max_credit))
            max_credit = default_max_credit
        if max_credit < 0:
            log('ERROR: Parameter MAX_CREDIT "%s" cannot be a negative number.'
                ' Defaulting to maximum credit %s.' % (max_credit, default_max_credit))
            max_credit = default_max_credit
    else:
        max_credit = default_max_credit
    try:
        req_credit = int(values['credit'])
    except ValueError:
        return build_response(jsonify({
            'error': True,
            'msg': 'Required credit "%s" is not a valid integer.' % values['credit']}))
    if req_credit < 0:
        return build_response(jsonify({
            'error': True,
            'msg': 'Required credit "%s" cannot be a negative integer.' % values['credit']}))
    if req_credit > max_credit:
        return build_response(jsonify({
            'error': True,
            'msg': 'Cannot allocate %s credit for a new user (max credit %s).' % (values['credit'], max_credit)}))

    try:
        user = create_user(**values)
        # successful creation
        log('User %s created successfully' % user.username)
        return build_response(simplejson.dumps(user.to_dict()))
    except Exception, err:
        # something went wrong
        error_msg = 'Error upon user creation: %s -> %s' % (type(err), err)
        log(error_msg)
        return build_response(jsonify({'error': True, 'msg': error_msg}))
Example #5
0
def new_user():
    values = {}
    all_fields = ('username', 'fname', 'lname', 'email',
                  'affiliation', 'password', 'credit', 'uuid', 'openid')
    required_fields = ('username', 'email', 'password')

    log('New user "%s <%s>" creation attempt' % (
        request.values.get('username'), request.values.get('email')))

    # check for presence of mandatory fields
    for field in all_fields:
        values[field] = request.values.get(field)
        if field == 'uuid' and values[field] == '<none>':
            values[field] = ''

    for field in required_fields:
        if not values[field]:
            msg = "Missing required field: '%s'" % field
            log_error(msg)
            return build_response(jsonify(error_response(msg)))

    # check if the provided username already exists
    if User.query.filter_by(username=values['username']).first():
        msg = "Username '%s' is already taken" % values['username']
        log_error(msg)
        return build_response(jsonify(error_response(msg)))

    # check if the provided email already exists
    if User.query.filter_by(email=values['email']).first():
        msg = "E-mail '%s' is already registered" % values['email']
        log_error(msg)
        return build_response(jsonify(error_response(msg)))

    # check that the requested credit does not exceed the maximum
    if config_parser.has_option('conpaas', 'MAX_CREDIT'):
        max_credit = config_parser.get('conpaas', 'MAX_CREDIT')
        try:
            max_credit = int(max_credit)
        except ValueError:
            log_error("Parameter MAX_CREDIT '%s' is not a valid integer."
                " Defaulting to maximum credit %s." % (max_credit, default_max_credit))
            max_credit = default_max_credit
        if max_credit < 0:
            log_error("Parameter MAX_CREDIT '%s' cannot be a negative number."
                " Defaulting to maximum credit %s." % (max_credit, default_max_credit))
            max_credit = default_max_credit
    else:
        max_credit = default_max_credit
    try:
        req_credit = int(values['credit'])
    except ValueError:
        msg = "Required credit '%s' is not a valid integer." % values['credit']
        log_error(msg)
        return build_response(jsonify(error_response(msg)))
    if req_credit < 0:
        msg = "Required credit %s cannot be a negative integer." % values['credit']
        log_error(msg)
        return build_response(jsonify(error_response(msg)))
    if req_credit > max_credit:
        msg = "Cannot allocate %s credit for a new user (max credit %s)." % (values['credit'], max_credit)
        log_error(msg)
        return build_response(jsonify(error_response(msg)))

    try:
        user = create_user(**values)
        # successful creation
        log("User '%s' created successfully" % user.username)
        return build_response(simplejson.dumps(user.to_dict()))
    except Exception, err:
        # something went wrong
        msg = 'Error upon user creation: %s -> %s' % (type(err), err)
        log_error(msg)
        return build_response(jsonify(error_response(msg)))