use of com.thoughtworks.go.config.update.ModifyEnvironmentCommand in project gocd by gocd.
the class AgentConfigService method updateAgentAttributes.
public AgentConfig updateAgentAttributes(final String uuid, Username username, String hostname, String resources, String environments, TriState enable, AgentInstances agentInstances, HttpOperationResult result) {
final GoConfigDao.CompositeConfigCommand command = new GoConfigDao.CompositeConfigCommand();
if (!goConfigService.hasAgent(uuid) && enable.isTrue()) {
AgentInstance agentInstance = agentInstances.findAgent(uuid);
AgentConfig agentConfig = agentInstance.agentConfig();
command.addCommand(new AddAgentCommand(agentConfig));
}
if (enable.isTrue()) {
command.addCommand(new UpdateAgentApprovalStatus(uuid, false));
}
if (enable.isFalse()) {
command.addCommand(new UpdateAgentApprovalStatus(uuid, true));
}
if (hostname != null) {
command.addCommand(new UpdateAgentHostname(uuid, hostname, username.getUsername().toString()));
}
if (resources != null) {
command.addCommand(new UpdateResourcesCommand(uuid, new ResourceConfigs(resources)));
}
if (environments != null) {
Set<String> existingEnvironments = goConfigService.getCurrentConfig().getEnvironments().environmentsForAgent(uuid);
Set<String> newEnvironments = new HashSet<>(asList(environments.split(",")));
Set<String> environmentsToRemove = Sets.difference(existingEnvironments, newEnvironments);
Set<String> environmentsToAdd = Sets.difference(newEnvironments, existingEnvironments);
for (String environmentToRemove : environmentsToRemove) {
command.addCommand(new ModifyEnvironmentCommand(uuid, environmentToRemove, TriStateSelection.Action.remove));
}
for (String environmentToAdd : environmentsToAdd) {
command.addCommand(new ModifyEnvironmentCommand(uuid, environmentToAdd, TriStateSelection.Action.add));
}
}
return updateAgent(command, uuid, result, username);
}
Aggregations