def handle_create_vip_event(data): errors = validate_vip_event_data(data) if errors: msg = ('Error(s) detected in event data: %s. Ignoring received create ' 'event.') logger.error(msg, '; '.join(errors)) return # Check if VIP already exists. ip, ip_created = IPAddress.objects.get_or_create(address=data['ip']) protocol = VIPProtocol.from_name(data['protocol'].upper()) vip = get_vip(ip, data['port'], protocol) if vip: msg = ('VIP designated by IP address %s, port %s and protocol %s ' 'already exists. Ignoring received event.') logger.warning(msg, ip.address, data['port'], protocol.name) return # Create it. cluster_type, _ = ClusterType.objects.get_or_create( name=data['load_balancer_type']) cluster, _ = Cluster.objects.get_or_create( name=data['load_balancer'], type=cluster_type, ) if ip_created: eth = Ethernet.objects.create(base_object=cluster) ip.ethernet = eth ip.save() elif ip.dhcp_expose: logger.error( 'Trying to create VIP with IP %s, port %s and protocol %s ' 'failed because IP is exposed in dhcp', ip.address, data['port'], protocol.name) return try: service_env = ServiceEnvironment.objects.get( service__uid=data['service']['uid'], environment__name=data['environment'], ) except ServiceEnvironment.DoesNotExist: msg = ('ServiceEnvironment for service UID "%s" and environment "%s" ' 'does not exist. Ignoring received create event.') logger.error(msg, data['service']['uid'], data['environment']) return vip = VIP( name=data['name'], ip=ip, port=data['port'], protocol=protocol, parent=cluster, service_env=service_env, ) vip.save() logger.debug('VIP %s created successfully.', vip.name)
def handle_update_vip_event(data): errors = validate_vip_event_data(data) if errors: msg = ('Error(s) detected in event data: %s. Ignoring received update ' 'event.') logger.error(msg, '; '.join(errors)) return ip, _ = IPAddress.objects.get_or_create(address=data['ip']) protocol = VIPProtocol.from_name(data['protocol'].upper()) if ip.dhcp_expose: logger.error( 'Trying to update VIP with IP %s, port %s and protocol %s ' 'failed because IP is exposed in dhcp', ip.address, data['port'], protocol.name) return vip = get_vip(ip, data['port'], protocol) if vip is None: # VIP not found, should create new one. return handle_create_vip_event(data) # update cluster. cluster_type, _ = ClusterType.objects.get_or_create( name=data['load_balancer_type']) cluster, _ = Cluster.objects.get_or_create( name=data['load_balancer'], type=cluster_type, ) if (vip.parent != cluster or (ip.ethernet and ip.ethernet.base_object != cluster)): with transaction.atomic(): for migrated_vip in VIP.objects.select_for_update().filter(ip=ip): migrate_vip_to_cluster(migrated_vip, cluster, protocol) # update service/environment if changed. try: service_env = ServiceEnvironment.objects.get( service__uid=data['service']['uid'], environment__name=data['environment'], ) except ServiceEnvironment.DoesNotExist: msg = ('ServiceEnvironment for service UID "%s" and environment "%s" ' 'does not exist. Ignoring received update event.') logger.error(msg, data['service']['uid'], data['environment']) return if vip.service_env != service_env: vip.service_env = service_env vip.save() logger.debug('VIP %s changed service/env to %s.', vip.name, service_env) logger.debug('VIP %s update processed successfuly.', vip.name)
def handle_delete_vip_event(data): errors = validate_vip_event_data(data) if errors: msg = ('Error(s) detected in event data: %s. Ignoring received delete ' 'event.') logger.error(msg, '; '.join(errors)) return try: ip = IPAddress.objects.get(address=data['ip']) except IPAddress.DoesNotExist: msg = ( "IP address %s doesn't exist. Ignoring received delete VIP event.") logger.error(msg, data['ip']) return protocol = VIPProtocol.from_name(data['protocol'].upper()) vip = get_vip(ip, data['port'], protocol) if vip is None: msg = ("VIP designated by IP address %s, port %s and protocol %s " "doesn't exist. Ignoring received delete event.") logger.warning(msg, ip.address, data['port'], protocol.name) return vip.delete() logger.info('VIP %s deleted successfully.', vip.name) # Delete IP address associated with it (along with its Ethernet), but only # when this IP is not used anymore by other VIP(s). if not VIP.objects.filter(ip=ip).exists(): eth_deleted = False if ip.ethernet is not None: ip.ethernet.delete() eth_deleted = True ip.delete() if eth_deleted: msg = ( 'IP address %s has been deleted (along with Ethernet ' 'associated with it) since it is no longer being used by any ' 'VIP.') else: msg = ( 'IP address %s has been deleted since it is no longer being ' 'used by any VIP.') logger.info(msg, ip.address)
def handle_update_vip_event(data): # TODO(xor-xor): Since update event doesn't contain any changes yet, it # will be ignored for now. Remember to remove logger.info/return below when # this will get changed. logger.info( "Ignoring received update VIP event, since handling logic is not " "implemented yet.") return errors = validate_vip_event_data(data) if errors: msg = ('Error(s) detected in event data: %s. Ignoring received update ' 'event.') logger.error(msg, '; '.join(errors)) return ip, = IPAddress.objects.get_or_create(address=data['ip']) protocol = VIPProtocol.from_name(data['protocol'].upper()) vip = get_vip(ip.address, data['port'], protocol.name) if vip is None: msg = ("VIP designated by IP address %s, port %s and protocol %s " "doesn't exist. Ignoring received update event.") logger.warning(msg, ip.address, data['port'], protocol.name) return