private void guaranteeHasAllTargets ( TrafficSource trafficSource , TargetGroup targetGroup , Collection < TargetDescription > targets , Collection < BaragonAgentMetadata > baragonAgents ) { Collection < TargetDescription > targetDescriptions = new HashSet <> ( ) ; for ( BaragonAgentMetadata agent : baragonAgents ) { try { if ( ( trafficSource . getRegisterBy ( ) == RegisterBy . INSTANCE_ID && agent . getEc2 ( ) . getInstanceId ( ) . isPresent ( ) ) || ( trafficSource . getRegisterBy ( ) == RegisterBy . PRIVATE_IP && agent . getEc2 ( ) . getPrivateIp ( ) . isPresent ( ) ) ) { String id = trafficSource . getRegisterBy ( ) == RegisterBy . INSTANCE_ID ? agent . getEc2 ( ) . getInstanceId ( ) . get ( ) : agent . getEc2 ( ) . getPrivateIp ( ) . get ( ) ; if ( agentShouldRegisterInTargetGroup ( id , targets ) ) { targetDescriptions . add ( new TargetDescription ( ) . withId ( id ) ) ; LOG . info ( "Will register agent {} to target in group {}" , agent , targetGroup ) ; } else { LOG . debug ( "Agent {} is already registered" , agent ) ; } } } catch ( Exception exn ) { LOG . error ( "Could not create request to register agent {} due to error" , agent , exn ) ; exceptionNotifier . notify ( exn , ImmutableMap . of ( "agent" , agent . toString ( ) ) ) ; } } if ( targetDescriptions . isEmpty ( ) ) { LOG . debug ( "No new instances to register with target group" ) ; } else { try { RegisterTargetsRequest registerTargetsRequest = new RegisterTargetsRequest ( ) . withTargetGroupArn ( targetGroup . getTargetGroupArn ( ) ) . withTargets ( targetDescriptions ) ; elbClient . registerTargets ( registerTargetsRequest ) ; LOG . info ( "Registered targets {} onto target group {}" , targetDescriptions , targetGroup ) ; } catch ( AmazonClientException acexn ) { LOG . error ( "Failed to register targets {} onto target group {}" , targetDescriptions , targetGroup ) ; exceptionNotifier . notify ( acexn , ImmutableMap . of ( "targets" , targetDescriptions . toString ( ) , "targetGroup" , targetGroup . toString ( ) ) ) ; } } }
After this method completes every agent in baragonAgents should be associated with a target in the given target group .