Search in sources :

Example 1 with AgentTypeManager

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 + "'");
    }
}
Also used : Set(java.util.Set) BuildTypes(jetbrains.buildServer.server.rest.model.buildType.BuildTypes) AuthorizationFailedException(jetbrains.buildServer.server.rest.errors.AuthorizationFailedException) SecurityContext(jetbrains.buildServer.serverSide.auth.SecurityContext) AgentTypeManager(jetbrains.buildServer.serverSide.agentTypes.AgentTypeManager) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) List(java.util.List) AuthorizationFailedException(jetbrains.buildServer.server.rest.errors.AuthorizationFailedException) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException)

Aggregations

List (java.util.List)1 Set (java.util.Set)1 AuthorizationFailedException (jetbrains.buildServer.server.rest.errors.AuthorizationFailedException)1 BadRequestException (jetbrains.buildServer.server.rest.errors.BadRequestException)1 BuildTypes (jetbrains.buildServer.server.rest.model.buildType.BuildTypes)1 AgentTypeManager (jetbrains.buildServer.serverSide.agentTypes.AgentTypeManager)1 SecurityContext (jetbrains.buildServer.serverSide.auth.SecurityContext)1