use of jetbrains.buildServer.serverSide.agentTypes.AgentTypeManager in project teamcity-rest by JetBrains.
the class CompatibilityPolicy method applyTo.
public void applyTo(@NotNull final SBuildAgent agent, @NotNull final ServiceLocator serviceLocator) {
if (!AuthUtil.canViewAgentDetails(serviceLocator.getSingletonService(SecurityContext.class).getAuthorityHolder(), agent)) {
// can get pool name i from the error message if we do not check this
throw new AuthorizationFailedException("No permission to view agent details");
}
final AgentTypeManager agentTypeManager = serviceLocator.getSingletonService(AgentTypeManager.class);
final int agentTypeId = agent.getAgentTypeId();
final String valueUp = policy.trim().toLowerCase();
if (POLICY_ANY.equals(valueUp)) {
agentTypeManager.setRunConfigurationPolicy(agentTypeId, BuildAgentManager.RunConfigurationPolicy.ALL_COMPATIBLE_CONFIGURATIONS);
} else if (POLICY_SELECTED.equals(valueUp)) {
if (buildTypes == null) {
buildTypes = new BuildTypes();
}
List<jetbrains.buildServer.BuildType> buildTypesFromPosted = buildTypes.getBuildTypesFromPosted(serviceLocator);
BuildAgentManager.RunConfigurationPolicy previousPolicy = agentTypeManager.getRunConfigurationPolicy(agentTypeId);
Set<String> previous_canRunConfigurations = agentTypeManager.getCanRunConfigurations(agentTypeId);
try {
agentTypeManager.setRunConfigurationPolicy(agentTypeId, BuildAgentManager.RunConfigurationPolicy.SELECTED_COMPATIBLE_CONFIGURATIONS);
agentTypeManager.excludeRunConfigurationsFromAllowed(agentTypeId, previous_canRunConfigurations.toArray(new String[0]));
agentTypeManager.includeRunConfigurationsToAllowed(agentTypeId, buildTypesFromPosted.stream().map(jetbrains.buildServer.BuildType::getBuildTypeId).toArray(String[]::new));
} catch (Exception e) {
agentTypeManager.setRunConfigurationPolicy(agentTypeId, previousPolicy);
agentTypeManager.excludeRunConfigurationsFromAllowed(agentTypeId, agentTypeManager.getCanRunConfigurations(agentTypeId).toArray(new String[0]));
agentTypeManager.includeRunConfigurationsToAllowed(agentTypeId, previous_canRunConfigurations.toArray(new String[0]));
throw e;
}
} else {
throw new BadRequestException("Unexpected policy '" + policy + "', expected '" + POLICY_ANY + "' or '" + POLICY_SELECTED + "'");
}
}
Aggregations