Skip to content

Commit c6278dc

Browse files
committed
Update AgentGrpcService.java
Adding getCustomHeaders() method to avoid DRY violation, agent verification before perform the update, reformatting agent delete and handling exceptions
1 parent ec0b033 commit c6278dc

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

backend/src/main/java/com/park/utmstack/service/agent_manager/AgentGrpcService.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,29 @@ public AuthResponseDTO updateAgentAttributes(AgentRequestVM agentRequestVM) thro
9393
final String ctx = CLASSNAME + ".updateAgentAttributes";
9494
try {
9595
AgentRequest req = agentRequestVM.getAgentRequest();
96-
Metadata customHeaders = new Metadata();
97-
customHeaders.put(Metadata.Key.of("key", Metadata.ASCII_STRING_MARSHALLER), agentRequestVM.getAgentKey());
98-
customHeaders.put(Metadata.Key.of("id", Metadata.ASCII_STRING_MARSHALLER), String.valueOf(agentRequestVM.getId()));
96+
97+
// Validating the existence of the agent.
98+
String currentUser = SecurityUtils.getCurrentUserLogin().orElseThrow(() -> new RuntimeException("No current user login"));
99+
Agent agent = null;
100+
String hostname = agentRequestVM.getHostname();
101+
102+
try {
103+
agent = blockingStub.getAgentByHostname(Hostname.newBuilder().setHostname(hostname).build());
104+
if (agent == null) {
105+
String msg = String.format("%1$s: Agent %2$s could not be updated because no information was obtained from the agent", ctx, hostname);
106+
log.error(msg);
107+
throw new Exception(msg);
108+
}
109+
} catch (StatusRuntimeException e) {
110+
if (e.getStatus().getCode() == Status.Code.NOT_FOUND) {
111+
String msg = String.format("%1$s: Agent %2$s could not be updated because was not found", ctx, hostname);
112+
log.error(msg);
113+
throw new Exception(msg);
114+
}
115+
}
116+
117+
assert agent != null;
118+
Metadata customHeaders = getCustomHeaders(agent);
99119

100120
Channel intercept = ClientInterceptors.intercept(grpcManagedChannel, MetadataUtils.newAttachHeadersInterceptor(customHeaders));
101121
AgentServiceGrpc.AgentServiceBlockingStub newStub = AgentServiceGrpc.newBlockingStub(intercept);
@@ -163,9 +183,8 @@ public void deleteAgent(String hostname) {
163183

164184
AgentDelete request = AgentDelete.newBuilder().setDeletedBy(currentUser).build();
165185

166-
Metadata customHeaders = new Metadata();
167-
customHeaders.put(Metadata.Key.of("key", Metadata.ASCII_STRING_MARSHALLER), agent.getAgentKey());
168-
customHeaders.put(Metadata.Key.of("id", Metadata.ASCII_STRING_MARSHALLER), String.valueOf(agent.getId()));
186+
assert agent != null;
187+
Metadata customHeaders = getCustomHeaders(agent);
169188

170189
Channel intercept = ClientInterceptors.intercept(grpcManagedChannel, MetadataUtils.newAttachHeadersInterceptor(customHeaders));
171190
AgentServiceGrpc.AgentServiceBlockingStub newStub = AgentServiceGrpc.newBlockingStub(intercept);
@@ -177,4 +196,11 @@ public void deleteAgent(String hostname) {
177196
throw new RuntimeException(msg);
178197
}
179198
}
199+
200+
private Metadata getCustomHeaders (Agent agent) {
201+
Metadata customHeaders = new Metadata();
202+
customHeaders.put(Metadata.Key.of("key", Metadata.ASCII_STRING_MARSHALLER), agent.getAgentKey());
203+
customHeaders.put(Metadata.Key.of("id", Metadata.ASCII_STRING_MARSHALLER), String.valueOf(agent.getId()));
204+
return customHeaders;
205+
}
180206
}

0 commit comments

Comments
 (0)