def create_ec2_object(ec2_instance_id, ec2_ami_id, ec2_hostname, ec2_public_hostname, ec2_instance_type, ec2_security_groups, ec2_placement_availability_zone, user_id): """ Create a new ec2 object. """ try: log.info('Creating new ec2 ec2_instance_id={0}'.format(ec2_instance_id)) utcnow = datetime.utcnow() ec2 = Ec2(ec2_instance_id = ec2_instance_id, ec2_ami_id = ec2_ami_id, ec2_hostname = ec2_hostname, ec2_public_hostname = ec2_public_hostname, ec2_instance_type = ec2_instance_type, ec2_security_groups = ec2_security_groups, ec2_placement_availability_zone = ec2_placement_availability_zone, updated_by = user_id, created = utcnow, updated = utcnow) DBSession.add(ec2) DBSession.flush() return ec2 except Exception as e: log.error('Error creating new ec2 ec2_instance_id={0},exception={1}'.format(ec2_instance_id,e)) raise
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:
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)
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)
def api_hypervisor_vm_assignments_write(request): """Process write requests for the /api/hypervisor_vm_assignments route.""" au = get_authenticated_user(request) try: payload = request.json_body parent_node_id = payload['parent_node_id'] child_node_id = payload['child_node_id'] log.info('Checking for hypervisor_vm_assignment child_node_id={0}'.format(child_node_id)) try: hva = DBSession.query(HypervisorVmAssignment) hva = hva.filter(HypervisorVmAssignment.child_node_id==child_node_id) hva = hva.one() except NoResultFound: try: log.info('Creating new hypervisor_vm_assignment parent_node_id={0},child_node_id={1}'.format(parent_node_id, child_node_id)) utcnow = datetime.utcnow() hva = HypervisorVmAssignment(parent_node_id=parent_node_id, child_node_id=child_node_id, updated_by=au['user_id'], created=utcnow, updated=utcnow) DBSession.add(hva) DBSession.flush() except Exception as e: log.error('Error creating new hypervisor_vm_assignment parent_node_id={0},child_node_id={1},exception={2}'.format(parent_node_id, child_node_id, e)) raise else: try: log.info('Updating hypervisor_vm_assignment parent_node_id={0},child_node_id={1}'.format(parent_node_id, child_node_id)) hva.parent_node_id = parent_node_id hva.child_node_id = child_node_id hva.updated_by=au['user_id'] DBSession.flush() except Exception as e: log.error('Error updating hypervisor_vm_assignment parent_node_id={0},child_node_id={1},exception={2}'.format(parent_node_id, child_node_id, e)) raise except Exception as e: log.error('Error writing to hypervisor_vm_assignment API={0},exception={1}'.format(request.url, e)) return Response(str(e), content_type='application/json', status_int=500)
def api_node_tags_write(request): """Process write requests for the /api/tags route.""" au = get_authenticated_user(request) try: payload = request.json_body tag_name = payload['tag_name'] tag_value = payload['tag_value'] log.info('Searching for tag tag_name={0}'.format(tag_name)) try: t = DBSession.query(Tag) t = t.filter(Tag.tag_name==tag_name) t = t.filter(Tag.tag_value==tag_value) t = t.one() except NoResultFound: try: log.info('Creating new tag tag_name={0},tag_value={1}'.format(tag_name, tag_value)) utcnow = datetime.utcnow() t = Tag(tag_name=tag_name, tag_value=tag_value, updated_by=au['user_id'], created=utcnow, updated=utcnow) DBSession.add(t) DBSession.flush() except Exception as e: log.error('Error creating new tag tag_name={0},tag_value={1},exception={2}'.format(tag_name, tag_value, e)) raise # Since there are no fields to update other than the two that # constitue a unqiue tag we return a 409 when an update would # have otherwise happened and handle it in client/UI. else: return Response(content_type='application/json', status_int=409) return t except Exception as e: log.error('Error writing to tags API={0},exception={1}'.format(request.url, e)) return Response(str(e), content_type='application/json', status_int=500)
def create_hardware_profile(manufacturer, model, user_id): """Create a new hardware_profile.""" try: log.info('Creating new hardware_profile manufacturer={0},model={1}'.format(manufacturer, model)) utcnow = datetime.utcnow() hp = HardwareProfile(manufacturer = manufacturer, model = model, updated_by = user_id, created = utcnow, updated = utcnow) DBSession.add(hp) DBSession.flush() return hp except Exception as e: log.error('Error creating new harware_profile manufacturer={0},model={1},exception={2}'.format(manufacturer, model, e)) raise
def create_operating_system(variant, version_number, architecture, description, user_id): """ """ try: log.info('Creating new operating_system variant={0},version_number={1},architecture={2},description={3}'.format(variant, version_number, architecture, description)) utcnow = datetime.utcnow() os = OperatingSystem(variant=variant, version_number=version_number, architecture=architecture, description=description, updated_by=user_id, created=utcnow, updated=utcnow) DBSession.add(os) DBSession.flush() return os except Exception as e: log.error('Error creating new operating_system variant={0},version_number={1},architecture={2},description={3},exception={4}'.format(variant, version_number, architecture, description, e)) raise
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)
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)
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)