def get_or_create_healthcheck(user, healthcheck_expect, healthcheck_type, healthcheck_request, healthcheck_destination, identifier=''): try: # Query HealthCheck table for one equal this if identifier == '': hc = Healthcheck.objects.get(healthcheck_expect=healthcheck_expect, healthcheck_type=healthcheck_type, healthcheck_request=healthcheck_request, destination=healthcheck_destination) else: hc = Healthcheck.objects.get(identifier=identifier, healthcheck_expect=healthcheck_expect, healthcheck_type=healthcheck_type, healthcheck_request=healthcheck_request, destination=healthcheck_destination) # Else, add a new one except ObjectDoesNotExist: hc = Healthcheck(identifier=identifier, healthcheck_type=healthcheck_type, healthcheck_request=healthcheck_request, healthcheck_expect=healthcheck_expect, destination=healthcheck_destination) hc.save() # Get the fisrt occureny and warn if redundant HCs are present except MultipleObjectsReturned: log.warning( "Multiple healthcheck entries found for the given parameters") if identifier == '': hc = Healthcheck.objects.filter(healthcheck_expect=healthcheck_expect, healthcheck_type=healthcheck_type, healthcheck_request=healthcheck_request, destination=healthcheck_destination).order_by('id')[0] else: hc = Healthcheck.objects.filter(identifier=identifier, healthcheck_expect=healthcheck_expect, healthcheck_type=healthcheck_type, healthcheck_request=healthcheck_request, destination=healthcheck_destination).order_by('id')[0] return hc
def get_or_create_healthcheck(user, healthcheck_expect, healthcheck_type, healthcheck_request, healthcheck_destination, identifier=''): try: # Query HealthCheck table for one equal this if identifier == '': hc = Healthcheck.objects.get( healthcheck_expect=healthcheck_expect, healthcheck_type=healthcheck_type, healthcheck_request=healthcheck_request, destination=healthcheck_destination) else: hc = Healthcheck.objects.get( identifier=identifier, healthcheck_expect=healthcheck_expect, healthcheck_type=healthcheck_type, healthcheck_request=healthcheck_request, destination=healthcheck_destination) # Else, add a new one except ObjectDoesNotExist: hc = Healthcheck(identifier=identifier, healthcheck_type=healthcheck_type, healthcheck_request=healthcheck_request, healthcheck_expect=healthcheck_expect, destination=healthcheck_destination) hc.save() # Get the fisrt occureny and warn if redundant HCs are present except MultipleObjectsReturned: log.warning( "Multiple healthcheck entries found for the given parameters") if identifier == '': hc = Healthcheck.objects.filter( healthcheck_expect=healthcheck_expect, healthcheck_type=healthcheck_type, healthcheck_request=healthcheck_request, destination=healthcheck_destination).order_by('id')[0] else: hc = Healthcheck.objects.filter( identifier=identifier, healthcheck_expect=healthcheck_expect, healthcheck_type=healthcheck_type, healthcheck_request=healthcheck_request, destination=healthcheck_destination).order_by('id')[0] return hc
def insert(request): try: # TODO: ADD VALIDATION healthcheck_type = request.DATA.get('healthcheck_type') healthcheck_request = request.DATA.get('healthcheck_request') healthcheck_expect = request.DATA.get('healthcheck_expect') old_healthcheck_id = request.DATA.get('old_healthcheck_id') try: # Query HealthCheck table for one equal this hc_check = Healthcheck.objects.get( healthcheck_expect=healthcheck_expect, healthcheck_type=healthcheck_type, healthcheck_request=healthcheck_request) # If a HealthCheck like this already exists, return it hc_serializer = HealthcheckSerializer(hc_check, many=False) # Else, add a new one except ObjectDoesNotExist: hc = Healthcheck( identifier='', healthcheck_type=healthcheck_type, healthcheck_request=healthcheck_request, healthcheck_expect=healthcheck_expect, destination='' ) hc.save(request.user) # Check if someone is using the old healthcheck # If not, delete it to keep the database clean if old_healthcheck_id is not None: pools_using_healthcheck = ServerPool.objects.filter( healthcheck=old_healthcheck_id).count() if pools_using_healthcheck == 0: Healthcheck.objects.filter(id=old_healthcheck_id).delete() hc_serializer = HealthcheckSerializer(hc, many=False) return Response(hc_serializer.data) except Exception, exception: log.error(exception) raise api_exceptions.NetworkAPIException()
def insert(request): try: # TODO: ADD VALIDATION healthcheck_type = request.DATA.get('healthcheck_type') healthcheck_request = request.DATA.get('healthcheck_request') healthcheck_expect = request.DATA.get('healthcheck_expect') old_healthcheck_id = request.DATA.get('old_healthcheck_id') try: #Query HealthCheck table for one equal this hc_check = Healthcheck.objects.get(healthcheck_expect=healthcheck_expect, healthcheck_type=healthcheck_type, healthcheck_request=healthcheck_request) #If a HealthCheck like this already exists, return it hc_serializer = HealthcheckSerializer(hc_check, many=False) #Else, add a new one except ObjectDoesNotExist: hc = Healthcheck( identifier='', healthcheck_type=healthcheck_type, healthcheck_request=healthcheck_request, healthcheck_expect=healthcheck_expect, destination='' ) hc.save(request.user) #Check if someone is using the old healthcheck #If not, delete it to keep the database clean if old_healthcheck_id is not None: pools_using_healthcheck = ServerPool.objects.filter(healthcheck=old_healthcheck_id).count() if pools_using_healthcheck == 0: Healthcheck.objects.filter(id=old_healthcheck_id).delete() hc_serializer = HealthcheckSerializer(hc, many=False) return Response(hc_serializer.data) except Exception, exception: log.error(exception) raise api_exceptions.NetworkAPIException()
def _get_healthcheck(healthcheck_obj): """Get or creates a healthcheck""" healthcheck = { 'identifier': healthcheck_obj['identifier'], 'healthcheck_expect': healthcheck_obj['healthcheck_expect'], 'healthcheck_type': healthcheck_obj['healthcheck_type'], 'healthcheck_request': healthcheck_obj['healthcheck_request'], 'destination': healthcheck_obj['destination'] } try: hc = Healthcheck.objects.get(**healthcheck) except ObjectDoesNotExist: hc = Healthcheck(**healthcheck) hc.save() return hc