@@ -725,8 +725,9 @@ private Set<Tag> loadEndpointTags(Endpoint endpoint) {
725725 private Agent updateExistingEndpointAndManageAgent (Endpoint endpoint , AgentRegisterInput input ) {
726726 setUpdatedEndpointAttributes (endpoint , input );
727727 addSourceTagToEndpoint (endpoint , input );
728+ Agent agent = createOrUpdateAgent (endpoint , input );
728729 updateEndpoint (endpoint );
729- return createOrUpdateAgent ( endpoint , input ) ;
730+ return agent ;
730731 }
731732
732733 private Agent updateExistingAgent (Agent agent , AgentRegisterInput input ) {
@@ -736,25 +737,24 @@ private Agent updateExistingAgent(Agent agent, AgentRegisterInput input) {
736737
737738 setUpdatedEndpointAttributes (endpoint , input );
738739 addSourceTagToEndpoint (endpoint , input );
739- updateEndpoint (endpoint );
740740 setUpdatedAgentAttributes (agent , input , endpoint );
741- Agent saved = agentService . createOrUpdateAgent ( agent );
741+ updateEndpoint ( endpoint );
742742
743743 // Suppress audit logging for heartbeat-only updates (no significant endpoint change)
744- Endpoint after = (Endpoint ) saved .getAsset ();
745- suppressAuditIfUnchanged (before , after .significantState (objectMapper ));
744+ suppressAuditIfUnchanged (before , endpoint .significantState (objectMapper ));
746745
747- return saved ;
746+ return agent ;
748747 }
749748
750749 private Agent updateExistingEndpointAndCreateAgent (Endpoint endpoint , AgentRegisterInput input ) {
751750 setUpdatedEndpointAttributes (endpoint , input );
752751 addSourceTagToEndpoint (endpoint , input );
753- updateEndpoint (endpoint );
754752 Agent agent = new Agent ();
755753 setNewAgentAttributes (input , agent );
756754 setUpdatedAgentAttributes (agent , input , endpoint );
757- return agentService .createOrUpdateAgent (agent );
755+ endpoint .getAgents ().add (agent );
756+ updateEndpoint (endpoint );
757+ return agent ;
758758 }
759759
760760 private Agent createOrUpdateAgent (Endpoint endpoint , AgentRegisterInput input ) {
@@ -777,7 +777,10 @@ private Agent createOrUpdateAgent(Endpoint endpoint, AgentRegisterInput input) {
777777 setNewAgentAttributes (input , agent );
778778 }
779779 setUpdatedAgentAttributes (agent , input , endpoint );
780- return agentService .createOrUpdateAgent (agent );
780+ if (!endpoint .getAgents ().contains (agent )) {
781+ endpoint .getAgents ().add (agent );
782+ }
783+ return agent ;
781784 }
782785
783786 private void setUpdatedEndpointAttributes (Endpoint endpoint , AgentRegisterInput input ) {
@@ -813,12 +816,13 @@ private Agent createNewEndpointAndAgent(AgentRegisterInput input) {
813816 endpoint .setSeenIp (input .getSeenIp ());
814817 endpoint .setMacAddresses (input .getMacAddresses ());
815818 endpoint .setTenant (input .getExecutor ().getTenant ());
816- createEndpoint (endpoint );
817- addSourceTagToEndpoint (endpoint , input );
818819 Agent agent = new Agent ();
819- setUpdatedAgentAttributes (agent , input , endpoint );
820820 setNewAgentAttributes (input , agent );
821- return agentService .createOrUpdateAgent (agent );
821+ setUpdatedAgentAttributes (agent , input , endpoint );
822+ endpoint .getAgents ().add (agent );
823+ createEndpoint (endpoint );
824+ addSourceTagToEndpoint (endpoint , input );
825+ return agent ;
822826 }
823827
824828 private void setNewAgentAttributes (AgentRegisterInput input , Agent agent ) {
0 commit comments