Exemplo n.º 1
0
def api_status_write(request):
    """Process write requests for /apistatusesnode_groups route."""

    au = get_authenticated_user(request)

    try:
        payload = request.json_body
        status_name = payload['status_name']
        description = payload['description']

        log.debug('Searching for statuses status_name={0},description={1}'.format(status_name, description))
        try:
            s = DBSession.query(Status.status_name==status_name)
            s = s.one()
        except NoResultFound:
            try:
                log.info('Creating new status status_name={0},description={1}'.format(status_name, description))
                utcnow = datetime.utcnow()

                s = Status(status_name=status_name,
                           description=description,
                           updated_by=au['user_id'],
                           created=utcnow,
                           updated=utcnow)

                DBSession.add(s)
                DBSession.flush()
            except Exception, e:
                log.error('Error creating status status_name={0},description={1},exception={2}'.format(status_name, description, e))
                raise
        else:
Exemplo n.º 2
0
def api_node_schema(request):
    """Schema document for nodes API"""

    log.debug('schema requested')

    node = {
    }

    return node
Exemplo n.º 3
0
def api_node_groups_write(request):
    """Process write requests for /api/node_groups route."""

    au = get_authenticated_user(request)

    try:
        payload = request.json_body

        node_group_name = payload['node_group_name']
        node_group_owner = payload['node_group_owner']
        node_group_description = payload['node_group_description']

        log.debug('Searching for node_group node_group_name={0}'.format(node_group_name))

        try:
            ng = DBSession.query(NodeGroup)
            ng = ng.filter(NodeGroup.node_group_name==node_group_name)
            ng = ng.one()
        except NoResultFound:
            try:
                log.info('Creating new node_group node_group_name={0},node_group_owner={1},description={2}'.format(node_group_name, node_group_owner, node_group_description))
                utcnow = datetime.utcnow()

                ng = NodeGroup(node_group_name=node_group_name,
                               node_group_owner=node_group_owner,
                               description=node_group_description,
                               updated_by=au['user_id'],
                               created=utcnow,
                               updated=utcnow)

                DBSession.add(ng)
                DBSession.flush()
            except Exception as e:
                log.error('Error creating new node_group node_group_name={0},node_group_owner={1},description={2},exception={3}'.format(node_group_name, node_group_owner, node_group_description, e))
                raise
        else:
            try:
                log.info('Updating node_group node_group_name={0}'.format(node_group_name))

                ng.node_group_name = node_group_name
                ng.node_group_owner = node_group_owner
                ng.description = node_group_description
                ng.updated_by=au['user_id']

                DBSession.flush()
            except Exception as e:
                log.error('Error updating node_group node_group_name={0},node_group_owner={1},description={2},exception={3}'.format(node_group_name, node_group_owner, node_group_description, e))
                raise

        return ng

    except Exception as e:
        log.error('Error writing to node_groups API={0},exception={1}'.format(request.url, e))
        return Response(str(e), content_type='application/json', status_int=500)
Exemplo n.º 4
0
def api_node_write(request):
    """Process write requests for the /api/nodes route."""

    au = get_authenticated_user(request)

    try:
        payload = request.json_body

        # Manually created node via the client.
        try:
            node_name = payload['node_name']
            unique_id = payload['unique_id'].lower()
            status_id = payload['node_status_id']

            log.debug('Searching for node unique_id={0}'.format(unique_id))
            n = DBSession.query(Node)
            n = n.filter(Node.unique_id==unique_id)
            n = n.one()
        except NoResultFound:
            try:

                log.info('Manually creating new node node_name={0},unique_id={1}'.format(node_name, unique_id))
                utcnow = datetime.utcnow()

                n = Node(node_name=node_name,
                         unique_id=unique_id,
                         status_id=status_id,
                         updated_by=au['user_id'],
                         created=utcnow,
                         updated=utcnow)

                DBSession.add(n)
                DBSession.flush()
            except Exception as e:
                log.error('Error creating new node node_name={0}unique_id={1},status_id={2},exception={3}'.format(node_name, unique_id, status_id, e))
                raise
        else:
            try:
                log.info('Updating node node_name={0},unique_id={1}'.format(node_name, unique_id))

                n.node_name = node_name
                n.status_id = status_id
                n.updated_by=au['user_id']

                DBSession.flush()
            except Exception as e:
                log.error('Error updating node node_name={0},unique_id={1},exception={2}'.format(node_name, unique_id, e))
                raise

        return n

    except Exception as e:
        log.error('Error writing to nodes API={0},exception={1}'.format(request.url, e))
        return Response(str(e), content_type='application/json', status_int=500)
Exemplo n.º 5
0
def api_ec2_object_write(request):
    """Process write requests for /api/ec2_objects route."""

    au = get_authenticated_user(request)

    try:
        payload = request.json_body

        ec2_instance_id = payload['ec2_instance_id']
        ec2_ami_id = payload['ec2_ami_id']
        ec2_hostname = payload['ec2_hostname']
        ec2_public_hostname = payload['ec2_public_hostname']
        ec2_instance_type = payload['ec2_instance_type']
        ec2_security_groups = payload['ec2_security_groups']
        ec2_placement_availability_zone = payload['ec2_placement_availability_zone']

        log.debug('Checking for ec2_object ec2_instance_id={0}'.format(ec2_instance_id))

        try:
            ec2 = DBSession.query(Ec2)
            ec2 = ec2.filter(Ec2.ec2_instance_id==ec2_instance_id)
            ec2 = ec2.one()
        except NoResultFound:
            ec2 = create_ec2_object(ec2_instance_id,
                                    ec2_ami_id,
                                    ec2_hostname,
                                    ec2_public_hostname,
                                    ec2_instance_type,
                                    ec2_security_groups,
                                    ec2_placement_availability_zone,
                                    au['user_id'])
        else:
            try:
                log.info('Updating ec2_object ec2_instance_id={0}'.format(ec2_instance_id))

                ec2.ec2_ami_id = ec2_ami_id
                ec2.ec2_hostname = ec2_hostname
                ec2.ec2_public_hostname = ec2_public_hostname
                ec2.ec2_instance_type = ec2_instance_type
                ec2.ec2_security_groups = ec2_security_groups
                ec2.ec2_placement_availability_zone = ec2_placement_availability_zone
                ec2.updated_by=au['user_id']

                DBSession.flush()
            except Exception as e:
                log.error('Error updating ec2_object ec2_instance_id={0}'.format(ec2_instance_id, e))
                raise

        return ec2

    except Exception as e:
        log.error('Error writing to ec2_objects API={0},exception={1}'.format(request.url, e))
        return Response(str(e), content_type='text/plain', status_int=500)
Exemplo n.º 6
0
def api_node_group_assignments_write(request):
    """Process write requests for the /api/node_group_assignments route."""

    au = get_authenticated_user(request)

    try:
        payload = request.json_body

        node_id = payload['node_id']
        node_group_id = payload['node_group_id']

        log.debug('Checking for node_group_assignment node_id={0},node_group_id={1}'.format(node_id, node_group_id))

        try:
            nga = DBSession.query(NodeGroupAssignment)
            nga = nga.filter(NodeGroupAssignment.node_id==node_id)
            nga = nga.filter(NodeGroupAssignment.node_group_id==node_group_id)
            nga = nga.one()
            log.info('node_group_assignment already exists')
            return Response(content_type='application/json', status_int=409)
        except NoResultFound:
            try:
                log.debug('Creating new node_group_assignment for node_id={0},node_group_id={1}'.format(node_id, node_group_id))
                utcnow = datetime.utcnow()

                nga = NodeGroupAssignment(node_id=node_id,
                                          node_group_id=node_group_id,
                                          updated_by=au['user_id'],
                                          created=utcnow,
                                          updated=utcnow)

                DBSession.add(nga)
                DBSession.flush()
            except Exception as e:
                log.error('Error creating new node_group_assignment node_id={0},node_group_id={1},exception={2}'.format(node_id, node_group_id, e))
                raise

    except Exception as e:
        log.error('Error writing to node_group_assignment API={0},exception={1}'.format(request.url, e))
        return Response(str(e), content_type='application/json', status_int=500)
Exemplo n.º 7
0
def api_tag_node_assignments_write(request):
    """Process write requests for the /api/tag_node_assignments route."""

    au = get_authenticated_user(request)

    try:
        payload = request.json_body

        tag_id = payload['tag_id']
        node_id = payload['node_id']

        log.debug('Searching for tag_node_assignment tag_id={0}node_id={1}'.format(tag_id, node_id))
        try:
            tna = DBSession.query(TagNodeAssignment)
            tna = tna.filter(TagNodeAssignment.tag_id==tag_id)
            tna = tna.filter(TagNodeAssignment.node_id==node_id)
            tna = tna.one()
            log.info('tag_node_assignment already exists')
            return Response(content_type='application/json', status_int=409)
        except NoResultFound:
            try:
                log.info('Creating new tag_node_assignment tag_id={0},node_id={1}'.format(tag_id, node_id))
                utcnow = datetime.utcnow()

                ta = TagNodeAssignment(tag_id=tag_id,
                                       node_id=node_id,
                                       updated_by=au['user_id'],
                                       created=utcnow,
                                       updated=utcnow)

                DBSession.add(ta)
                DBSession.flush()
            except Exception as e:
                log.error('Error creating new tag_node_assignment tag_id={0},node_id={1},exception={2}'.format(tag_id, node_id, e))
                raise

    except Exception as e:
        log.error('Error writing to tag_node_assignments API={0},exception={1}'.format(request.url, e))
        return Response(str(e), content_type='application/json', status_int=500)
Exemplo n.º 8
0
def api_hardware_profile_write(request):
    """Process write requests for /api/hardware_profiles route."""

    au = get_authenticated_user(request)

    try:
        payload = request.json_body

        manufacturer = payload['manufacturer']
        model = payload['model']

        log.debug('Checking for hardware_profile manufacturer={0},model={1}'.format(manufacturer, model))

        try:
            hp = DBSession.query(HardwareProfile)
            hp = hp.filter(HardwareProfile.manufacturer==manufacturer)
            hp = hp.filter(HardwareProfile.model==model)
            hp = hp.one()
        except NoResultFound:
            hp = create_hardware_profile(manufacturer, model, au['user_id'])
        else:
            try:
                log.info('Updating hardware_profile manufacturer={0},model={1}'.format(manufacturer, model))

                hp.manufacturer = manufacturer
                hp.model = model
                hp.updated_by=au['user_id']

                DBSession.flush()
            except Exception as e:
                log.error('Error updating hardware_profile manufacturer={0},model={1},exception={2}'.format(manufacturer, model, e))
                raise

        return hp

    except Exception as e:
        log.error('Error writing to harware_profiles API={0},exception={1}'.format(request.url, e))
        return Response(str(e), content_type='text/plain', status_int=500)
Exemplo n.º 9
0
def api_read_by_id(request, model_type):
    """Process get requests for /api/{object_type}/{id} route match."""

    resource_id = request.matchdict['id']
    c = camel_to_underscore(model_type)
    c_id = c + '_id'

    try:
        log.debug('Displaying single {0} {1}={2}'.format(c, c_id, resource_id))

        # FIXME: Something better here than globals?
        q = DBSession.query(globals()[model_type])
        q = q.filter(getattr(globals()[model_type], c_id) == resource_id)
        q = q.one()

        return q

    except NoResultFound:
        return Response(content_type='application/json', status_int=404)

    except Exception as e:
        log.error('Error querying {0} url={1},exception={2}'.format(c, request.url, e))
        return Response(str(e), content_type='application/json', status_int=500)
Exemplo n.º 10
0
def get_api_attribute(request, model_type):
    """Get single attribute request for /api/{object_type}/{id}/{attribute}
       route match."""

    c = camel_to_underscore(model_type)
    c_id = c + '_id'

    resource_id = request.matchdict['id']
    resource = request.matchdict['resource']
    log.debug('Querying for attribute={0},url={1}'.format(resource, request.url))

    try:
        # FIXME: Something better here than globals?
        q = DBSession.query(globals()[model_type])
        q = q.filter(getattr(globals()[model_type], c_id) == resource_id)
        q = q.one()
        return { resource: getattr(q, resource) }

    except (NoResultFound, AttributeError):
        return Response(content_type='application/json', status_int=404)
    except Exception as e:
        log.error('Error querying node_group={0},exception={1}'.format(request.url, e))
        raise
Exemplo n.º 11
0
def api_delete_by_id(request, model_type):
    """Process delete requests for /api/{object_type}/{id} route match."""

    # FIXME: Will be used for auditing eventually. Would be nice to use 
    # request.authenticated_userid, but I think this gets ugly when it's
    # an AD user. Need to test.
    au = get_authenticated_user(request)

    resource_id = request.matchdict['id']
    c = camel_to_underscore(model_type)
    c_id = c + '_id'
    c_name = c + '_name'

    try:
        log.debug('Checking for {0}={1}'.format(c_id, resource_id))

        # FIXME: Something better here than globals?
        q = DBSession.query(globals()[model_type])
        q = q.filter(getattr(globals()[model_type], c_id) == resource_id)
        q = q.one()

        object_name = getattr(q, c_name)

        # FIXME: Need auditing
        # FIXME: What about orphaned assigments? Should we clean them up?
        log.info('Deleting {0}={1},{2}={3}'.format(c_name, object_name, c_id, resource_id))
        DBSession.delete(q)
        DBSession.flush()

        return True

    except NoResultFound:
        return Response(content_type='application/json', status_int=404)

    except Exception as e:
        log.error('Error deleting {0}={1},exception={2}'.format(c_id, resource_id, e))
        return Response(str(e), content_type='application/json', status_int=500)
Exemplo n.º 12
0
def api_delete_by_params(request, model_type):
    """Process delete requests for /api/{object_type} route match. Iterates
       over passed parameters."""

    # FIXME: Should we enforce required parameters here?

    # Will be used for auditing
    au = get_authenticated_user(request)

    # FIXME: Should we allow this to be set on the client, or hard code it to true, requiring an # exact match? Might make sense since there is no confirmation, it just deletes.
    exact_get = True
    c = camel_to_underscore(model_type)

    try:
        payload = request.json_body

        s = ''
        q = DBSession.query(globals()[model_type])

        for k,v in payload.items():
            # FIXME: This is sub-par. Need a better way to distinguish
            # meta params from search params without having to
            # pre-define everything.
            if k == 'exact_get':
                continue

            s+='{0}={1},'.format(k, v)
            if exact_get:
                log.debug('Exact filtering on {0}={1}'.format(k, v))
                q = q.filter(getattr(globals()[model_type] ,k)==v)
            else:
                log.debug('Loose filtering on {0}={1}'.format(k, v))
                q = q.filter(getattr(globals()[model_type] ,k).like('%{0}%'.format(v)))
        log.debug('Searching for {0} with params: {1}'.format(c, s.rstrip(',')))

        q = q.one()

        # FIXME: Need auditing
        log.info('Deleting {0} with params: {1}'.format(c, s.rstrip(',')))
        DBSession.delete(q)
        DBSession.flush()

        return True

    except NoResultFound:
        return Response(content_type='application/json', status_int=404)

    except Exception as e:
        log.error('Error deleting {0} with params: {1} exception: {2}'.format(c, s.rstrip(','), e))
        return Response(str(e), content_type='application/json', status_int=500)
Exemplo n.º 13
0
def api_read_by_params(request, model_type):
    """Process get requests for /api/{object_type} route match."""

    c = camel_to_underscore(model_type)
    (perpage, offset) = get_pag_params(request)

    try:
        exact_get =  request.GET.get("exact_get", None)

        if request.params:
            s = ''
            # Filter on all the passed in terms
            q = DBSession.query(globals()[model_type])

            for k,v in request.GET.items():
                # Force unique_id to lower since mac addrs can be represented both ways.
                if k == 'unique_id':
                    v = v.lower()
                # FIXME: This is sub-par. Need a better way to distinguish
                # meta params from search params without having to
                # pre-define everything.
                if k == 'exact_get':
                    continue
                if k == 'start':
                    continue

                s+='{0}={1},'.format(k, v)
                if exact_get:
                    log.debug('Exact filtering on {0}={1}'.format(k, v))
                    if ',' in v:
                        q = q.filter(or_(getattr(globals()[model_type] ,k) == t for t in v.split(',')))
                    else:
                        q = q.filter(getattr(globals()[model_type] ,k)==v)
                else:
                    log.debug('Loose filtering on {0}={1}'.format(k, v))
                    if ',' in v:
                        log.info('Multiple values for key {0}={1}'.format(k, v))
                        multior = []
                        for t in v.split(','):
                            multior.append(((getattr(globals()[model_type] ,k).like(('%{0}%'.format(t))))))
                        q = q.filter(or_(*multior))
                    else:
                        q = q.filter(getattr(globals()[model_type] ,k).like('%{0}%'.format(v)))

            log.debug('Searching for {0} {1}'.format(c, s.rstrip(',')))

            total = q.count()
            myob = q.limit(perpage).offset(offset).all()
            results = {'meta': {'total': total}, 'results': myob}
            return results
        else:

            log.info('Displaying all {0}'.format(c))

            q = DBSession.query(globals()[model_type])
            total = q.count()
            myob =  q.limit(perpage).offset(offset).all()
            results = {'meta': {'total': total}, 'results': myob}
            return results

    # FIXME: Should AttributeError return something different?
    except (NoResultFound, AttributeError):
        return Response(content_type='application/json', status_int=404)

    except Exception, e:
        log.error('Error reading from {0} API={1},exception={2}'.format(c, request.url, e))
        return Response(str(e), content_type='application/json', status_int=500)
Exemplo n.º 14
0
def api_node_register(request):
    """Process registration requests for the /api/register route."""

    au = get_authenticated_user(request)

    log.info('Registering new node')
    try:
        payload = request.json_body

        # Get the hardware_profile_id or create if it doesn't exist.
        try:
            manufacturer = payload['hardware_profile']['manufacturer']
            model = payload['hardware_profile']['model']

            # FIXME: remove the http call
            uri = '/api/hardware_profiles'
            data = {'manufacturer': manufacturer,
                    'model': model
            }
            hardware_profile = _api_get(request, uri, data)

            try:
                hardware_profile_id = hardware_profile['results'][0]['hardware_profile_id']

            except IndexError:

                log.debug('hardware_profile not found, creating')

                hardware_profile = create_hardware_profile(manufacturer, model, au['user_id'])
                hardware_profile_id = hardware_profile.hardware_profile_id

            log.debug('hardware_profile is: {0}'.format(hardware_profile))

        except Exception as e:
            log.error('Unable to determine hardware_profile manufacturer={0},model={1},exception={2}'.format(manufacturer, model, e))
            raise

        # Get the operating_system_id or create if it doesn't exist.
        try:
            variant = payload['operating_system']['variant']
            version_number = payload['operating_system']['version_number']
            architecture = payload['operating_system']['architecture']
            description = payload['operating_system']['description']

            # FIXME: remove the http call
            uri = '/api/operating_systems'
            data = {'variant': variant,
                    'version_number': version_number,
                    'architecture': architecture,
                    'description': description
            }
            operating_system = _api_get(request, uri, data)

            try:
                operating_system_id = operating_system['results'][0]['operating_system_id']

            except IndexError:

                log.debug('operating_system not found, attempting to create')

                operating_system = create_operating_system(variant, version_number, architecture, description, au['user_id'])
                operating_system_id = operating_system.operating_system_id
            log.debug('operating_system is: {0}'.format(operating_system))

        except Exception as e:
            log.error('Unable to determine operating_system variant={0},version_number={1},architecture={2},description={3},exception={4}'.format(variant, version_number, architecture, description, e))
            raise

        # if sent, Get the ec2_object or create if it doesn't exist.
        ec2_id = None
        if payload['ec2']:
            try:
                ec2_instance_id = payload['ec2']['ec2_instance_id']
                ec2_ami_id = payload['ec2']['ec2_ami_id']
                ec2_hostname = payload['ec2']['ec2_hostname']
                ec2_public_hostname = payload['ec2']['ec2_public_hostname']
                ec2_instance_type = payload['ec2']['ec2_instance_type']
                ec2_security_groups = payload['ec2']['ec2_security_groups']
                ec2_placement_availability_zone = payload['ec2']['ec2_placement_availability_zone']
    
                # FIXME: remove the http call
                uri = '/api/ec2_objects'
                data = {'ec2_instance_id': ec2_instance_id,
                        'exact_get': True,
                }
                ec2 = _api_get(request, uri, data)

                try:
                    ec2_id = ec2['results'][0]['ec2_id']

                except IndexError:

                    log.debug('ec2_object not found, attempting to create')

                    ec2 = create_ec2_object(ec2_instance_id,
                                            ec2_ami_id,
                                            ec2_hostname,
                                            ec2_public_hostname,
                                            ec2_instance_type,
                                            ec2_security_groups,
                                            ec2_placement_availability_zone,
                                            au['user_id'])
                    ec2_id = ec2.ec2_id
                log.debug('ec2_object is: {0}'.format(ec2))

            except Exception as e:
                log.error('Unable to determine ec2_object ec2_instance_id={0},exception={1}'.format(payload['ec2']['ec2_instance_id'], e))
                raise

        try:
            unique_id = payload['unique_id'].lower()
            node_name = payload['node_name']
            uptime = payload['uptime']

            log.debug('Searching for node unique_id={0}'.format(unique_id))
            n = DBSession.query(Node)
            n = n.filter(Node.unique_id==unique_id)
            n = n.one()
        except NoResultFound:
            try:
                log.info('Creating new node node_name={0},unique_id={1}'.format(node_name, unique_id))
                utcnow = datetime.utcnow()

                n = Node(unique_id=unique_id,
                         node_name=node_name,
                         hardware_profile_id=hardware_profile_id,
                         operating_system_id=operating_system_id,
                         uptime=uptime,
                         status_id=2,
                         ec2_id=ec2_id,
                         updated_by=au['user_id'],
                         created=utcnow,
                         updated=utcnow)

                DBSession.add(n)
                DBSession.flush()
            except Exception as e:
                log.error('Error creating new node node_name={0},unique_id={1},exception={2}'.format(node_name, unique_id, e))
                raise
        else:
            try:
                log.info('Updating node: {0}'.format(unique_id))

                n.node_name = node_name
                n.hardware_profile_id = hardware_profile_id
                n.operating_system_id = operating_system_id
                n.ec2_id = ec2_id
                n.uptime = uptime
                n.updated_by=au['user_id']

                DBSession.flush()
            except Exception as e:
                log.error('Error updating node node_name={0},unique_id={1},exception={2}'.format(node_name, unique_id, e))
                raise

        return n

    except Exception as e:
        log.error('Error registering new node API={0},exception={1}'.format(request.url, e))
        return Response(str(e), content_type='application/json', status_int=500)