Esempio n. 1
0
def create_session():
    try:
        data = json.loads(request.data.decode('utf-8'))
    except ValueError as e:
        return (error('Base', 'MalformedJSON'), 400)

    if 'UserName' in data.keys() and 'Password' in data.keys():
        result = g.session.create(
                username=data['UserName'],
                password=data['Password']
        )

        if isinstance(result, tuple):
            return (error(result[0], result[1]), result[2])

        if result:
            response = make_response(
                    render_template('sessions/Session.1.0.0.json',
                            id=str(result['ID']),
                            username=result['USERNAME']
                    ), 201,
            )
            response.headers['Location'] = URL['SESSIONS'] + '/Sessions/' +\
                    str(result['ID'])
            response.headers['X-Auth-Token'] = result['X-AUTH']
            return response
    else:
        return (error('Base', 'MalformedJSON'), 500)

    return (error('BluebirdServer', 'UnauthorizedLoginAttempt'), 401)
Esempio n. 2
0
def create_session():
    try:
        data = json.loads(request.data.decode('utf-8'))
    except ValueError as e:
        return (error('Base', 'MalformedJSON'), 400)

    if 'UserName' in data.keys() and 'Password' in data.keys():
        result = g.session.create(
                username=data['UserName'],
                password=data['Password']
        )

        if isinstance(result, tuple):
            return (error(result[0], result[1]), result[2])

        if result:
            response = make_response(
                    render_template('sessions/Session.1.0.0.json',
                            id=str(result['ID']),
                            username=result['USERNAME']
                    ), 201,
            )
            response.headers['Location'] = URL['SESSIONS'] + '/Sessions/' +\
                    str(result['ID'])
            response.headers['X-Auth-Token'] = result['X-AUTH']
            return response
    else:
        return (error('Base', 'MalformedJSON'), 500)

    return (error('BluebirdServer', 'UnauthorizedLoginAttempt'), 401)
Esempio n. 3
0
def create_session():
    try:
        data = json.loads(request.data.decode('utf-8'))
        assert 'UserName' and 'Password' in data

        result = g.session.create(username=data['UserName'],
                                  password=data['Password'])

        response = make_response(
            render_template('sessions/Session.1.0.0.json',
                            id=str(result['ID']),
                            username=result['USERNAME']),
            201,
        )

        response.headers['Location'] = URL['SESSIONS'] + '/' + str(
            result['ID'])

        response.headers['X-Auth-Token'] = result['X-AUTH']
        return response

    except (ValueError, TypeError, KeyError, AssertionError):
        return (error('Base', 'UnrecognizedRequestBody'), 400)

    except (NoAuthModule, NoValidSession, SessionLimitExceeded) as e:
        return e.response()
Esempio n. 4
0
def check_content_type():
    """Check if incoming POST request is JSON"""
    if request.method == 'POST':
        content_type = request.headers.get('Content-Type')
        if content_type == 'application/json':
            return
        else:
            return (error('Base', 'MalformedJSON'), 500)
Esempio n. 5
0
def show_domains(domain):
    domain = g.libvirt.get_domain(domain)
    if domain:
        return render_template('ComputerSystem.1.0.1.json',
                               domain=domain,
                               server=Server())
    else:
        return (error('Base', 'ResourceMissingAtURI'), 404)
Esempio n. 6
0
def check_content_type():
    """Check if incoming POST request is JSON"""
    if request.method == 'POST':
        content_type = request.headers.get('Content-Type')
        if content_type == 'application/json':
            return
        else:
            return (error('Base', 'MalformedJSON'), 500)
Esempio n. 7
0
def show_domains(domain):
    domain = g.libvirt.get_domain(domain)
    if domain:
        return render_template('ComputerSystem.1.0.1.json',
                domain=domain,
                server=Server()
        )
    else:
        return (error('Base', 'ResourceMissingAtURI'), 404)
Esempio n. 8
0
def reset(domain):
    dom = g.libvirt.get_domain(domain)

    if not dom:
        return (error('Base', 'ResourceMissingAtURI'), 404)

    try:
        data = json.loads(request.data.decode('utf-8'))
    except ValueError as e:
        return (error('Base', 'MalformedJSON'), 400)

    if 'ResetType' in data.keys():
        action = g.libvirt.valid_action(data['ResetType'])
        if action:
            credentials = (g.login['USERNAME'], g.login['PASSWORD'])
            execute = getattr(dom, action)
            credentials = (credentials)
            manager = LibvirtManage(credentials)
    return (error('Base', 'ActionNotSupported'), 500)
Esempio n. 9
0
def check_content_type():
    """Check if incoming POST request is JSON"""
    if request.method == 'POST':
        content_type = request.headers.get('Content-Type')
        if content_type == 'application/json':
            try:
                json.loads(request.data.decode('utf-8'))
                return
            except (ValueError, TypeError, KeyError):
                pass

        return (error('Base', 'MalformedJSON'), 400)
Esempio n. 10
0
def reset(domain):
    dom = g.libvirt.get_domain(domain)

    if not dom:
        return (error('Base', 'ResourceMissingAtURI'), 404)

    try:
        data = json.loads(request.data.decode('utf-8'))
    except ValueError as e:
        return (error('Base', 'MalformedJSON'), 400)

    if 'ResetType' in data.keys():
        username = g.login['USERNAME']
        action = g.libvirt.valid_action(data['ResetType'])
        if action:
            execute = getattr(dom, action)
            if execute(username):
                ("", 200)
            else:
                return (error('BluebirdServer',
                              'UnauthorizedResetAction'), 401)
    return (error('Base', 'ActionNotSupported'), 500)
Esempio n. 11
0
def reset(domain):
    if not g.libvirt.get_domain(domain):
        return (error('Base', 'ResourceMissingAtURI'), 404)

    try:
        data = json.loads(request.data.decode('utf-8'))
        manager = LibvirtManage((g.login['USERNAME'], g.login['PASSWORD']))
        manager.reset(domain, data['ResetType'])
        return ('', 200)
    except Exception as e:
        return (str(e), 400)
        pass

    return ('', 400)
Esempio n. 12
0
    def decorated_function(*args, **kwargs):
        """
        Return Not Implemented (501) if query parameters are given.

        When the resource addressed is a collection, the client can use the
        following paging query options to specify that a subset of the members
        be returned. These paging query options apply to the Members property
        of a collection resource.

        Query options:

        * http://collection?$skip=5
        * http://collection?$top=30
        """
        for parameter in request.args:
            if parameter.startswith('$'):
                return (error('Base', 'QueryNotSupported'), 501)
        return f(*args, **kwargs)
Esempio n. 13
0
    def decorated_function(*args, **kwargs):
        """
        Return Not Implemented (501) if query parameters are given.

        When the resource addressed is a collection, the client can use the
        following paging query options to specify that a subset of the members
        be returned. These paging query options apply to the Members property
        of a collection resource.

        Query options:

        * http://collection?$skip=5
        * http://collection?$top=30
        """
        for parameter in request.args:
            if parameter.startswith('$'):
                return (error('Base', 'QueryNotSupported'), 501)
        return f(*args, **kwargs)
Esempio n. 14
0
 def decorated_function(*args, **kwargs):
     """X-Auth-Token and basic authentication decorator"""
     if xauth() or basic_authentication():
         return f(*args, **kwargs)
     else:
         return (error('Base', 'NoValidSession'), 401)
Esempio n. 15
0
 def response(self):
     return (error(self.__registry, self.__message), 403)
Esempio n. 16
0
 def decorated_function(*args, **kwargs):
     """Basic authentication decorator"""
     if basic_authentication():
         return f(*args, **kwargs)
     else:
         return (error('Base', 'NoValidSession'), 401)
Esempio n. 17
0
 def decorated_function(*args, **kwargs):
     """X-Auth-Token and basic authentication decorator"""
     if xauth() or basic_authentication():
         return f(*args, **kwargs)
     else:
         return (error('Base', 'NoValidSession'), 401)
Esempio n. 18
0
 def decorated_function(*args, **kwargs):
     """X-Auth-Token header decorator"""
     if xauth():
         return f(*args, **kwargs)
     else:
         return (error('Base', 'NoValidSession'), 401)
Esempio n. 19
0
 def decorated_function(*args, **kwargs):
     """Basic authentication decorator"""
     if basic_authentication():
         return f(*args, **kwargs)
     else:
         return (error('Base', 'NoValidSession'), 401)
Esempio n. 20
0
 def decorated_function(*args, **kwargs):
     """X-Auth-Token header decorator"""
     if xauth():
         return f(*args, **kwargs)
     else:
         return (error('Base', 'NoValidSession'), 401)