Search in sources :

Example 1 with CloudInstance

use of jetbrains.buildServer.clouds.CloudInstance in project teamcity-rest by JetBrains.

the class AgentFinder method getFilter.

@NotNull
@Override
public ItemFilter<SBuildAgent> getFilter(@NotNull final Locator locator) {
    final MultiCheckerFilter<SBuildAgent> result = new MultiCheckerFilter<SBuildAgent>();
    Long id = locator.getSingleDimensionValueAsLong(DIMENSION_ID);
    if (id != null) {
        result.add(item -> id == item.getId());
    }
    String name = locator.getSingleDimensionValue(NAME);
    if (name != null) {
        result.add(item -> name.equals(item.getName()));
    }
    final Boolean authorizedDimension = locator.getSingleDimensionValueAsBoolean(AUTHORIZED);
    if (authorizedDimension != null) {
        result.add(new FilterConditionChecker<SBuildAgent>() {

            public boolean isIncluded(@NotNull final SBuildAgent item) {
                return FilterUtil.isIncludedByBooleanFilter(authorizedDimension, item.isAuthorized());
            }
        });
    }
    final Boolean enabledDimension = locator.getSingleDimensionValueAsBoolean(ENABLED);
    if (enabledDimension != null) {
        result.add(new FilterConditionChecker<SBuildAgent>() {

            public boolean isIncluded(@NotNull final SBuildAgent item) {
                return FilterUtil.isIncludedByBooleanFilter(enabledDimension, item.isEnabled());
            }
        });
    }
    final Boolean connectedDimension = locator.getSingleDimensionValueAsBoolean(CONNECTED);
    if (connectedDimension != null) {
        result.add(new FilterConditionChecker<SBuildAgent>() {

            public boolean isIncluded(@NotNull final SBuildAgent item) {
                return FilterUtil.isIncludedByBooleanFilter(connectedDimension, item.isRegistered());
            }
        });
    }
    // see also AgentPoolsFinder.getPoolAgents()
    final String poolDimension = locator.getSingleDimensionValue(POOL);
    if (poolDimension != null) {
        AgentPoolFinder agentPoolFinder = myServiceLocator.getSingletonService(AgentPoolFinder.class);
        // get id here to support not existing pools?
        final AgentPool agentPool = agentPoolFinder.getItem(poolDimension);
        result.add(new FilterConditionChecker<SBuildAgent>() {

            public boolean isIncluded(@NotNull final SBuildAgent item) {
                // TeamCity API issue: cast
                return ((BuildAgentEx) item).getAgentType().getAgentPoolId() == agentPool.getAgentPoolId();
            }
        });
    }
    if (locator.isUnused(BUILD)) {
        final String buildDimension = locator.getSingleDimensionValue(BUILD);
        if (buildDimension != null) {
            Set<SBuildAgent> agents = getBuildRelatedAgents(buildDimension);
            result.add(new FilterConditionChecker<SBuildAgent>() {

                public boolean isIncluded(@NotNull final SBuildAgent item) {
                    return agents.contains(item);
                }
            });
        }
    }
    if (locator.isUnused(AGENT_TYPE_ID)) {
        final String agentTypeLocator = locator.getSingleDimensionValue(AGENT_TYPE_ID);
        if (agentTypeLocator != null) {
            int agentTypeId = getAgentType(agentTypeLocator, myServiceLocator.getSingletonService(AgentTypeFinder.class)).getAgentTypeId();
            result.add(new FilterConditionChecker<SBuildAgent>() {

                public boolean isIncluded(@NotNull final SBuildAgent item) {
                    return agentTypeId == item.getAgentTypeId();
                }
            });
        }
    }
    final String ipDimension = locator.getSingleDimensionValue(IP);
    if (ipDimension != null) {
        result.add(new FilterConditionChecker<SBuildAgent>() {

            public boolean isIncluded(@NotNull final SBuildAgent item) {
                // name of the field, not locator dimension
                return ipDimension.equals(Agent.getFieldValue(item, "ip", myServiceLocator));
            }
        });
    }
    final String protocolDimension = locator.getSingleDimensionValue(PROTOCOL);
    if (protocolDimension != null) {
        result.add(new FilterConditionChecker<SBuildAgent>() {

            public boolean isIncluded(@NotNull final SBuildAgent item) {
                // name of the field, not locator dimension
                return protocolDimension.equals(Agent.getFieldValue(item, "protocol", myServiceLocator));
            }
        });
    }
    final String parameterDimension = locator.getSingleDimensionValue(PARAMETER);
    if (parameterDimension != null) {
        final ParameterCondition parameterCondition = ParameterCondition.create(parameterDimension);
        result.add(new FilterConditionChecker<SBuildAgent>() {

            public boolean isIncluded(@NotNull final SBuildAgent item) {
                return parameterCondition.matches(new MapParametersProviderImpl(item.getAvailableParameters()));
            }
        });
    }
    if (locator.isUnused(CLOUD_INSTANCE)) {
        final String cloudInstanceLocator = locator.getSingleDimensionValue(CLOUD_INSTANCE);
        if (cloudInstanceLocator != null) {
            List<CloudInstance> instances = myServiceLocator.getSingletonService(CloudInstanceFinder.class).getItems(cloudInstanceLocator).myEntries.stream().map(CloudInstanceData::getInstance).collect(Collectors.toList());
            result.add(a -> instances.stream().anyMatch(i -> i.containsAgent(a)));
        /* CloudInstance might not have equals/hashcode, if it does, it would be better to use in a set like below
        Set<CloudInstance> instances = myServiceLocator.getSingletonService(CloudInstanceFinder.class).getItems(cloudInstanceLocator).myEntries.stream().map(i -> i.getInstance()).collect(
          Collectors.toSet());
        CloudManager cloudManager = myServiceLocator.getSingletonService(CloudManager.class);
        result.add(a -> Util.resolveNull(cloudManager.findInstanceByAgent(a), pair -> instances.contains(pair.getSecond()), false));
        */
        }
    }
    if (locator.isUnused(COMPATIBLE)) {
        // compatible with at least with one of the buildTypes
        final String compatible = locator.getSingleDimensionValue(COMPATIBLE);
        if (compatible != null) {
            final CompatibleLocatorParseResult compatibleData = getBuildTypesFromCompatibleDimension(compatible);
            if (compatibleData.buildTypes != null) {
                result.add(new FilterConditionChecker<SBuildAgent>() {

                    public boolean isIncluded(@NotNull final SBuildAgent item) {
                        return isCompatibleWithAny(item, compatibleData.buildTypes);
                    }
                });
            } else {
                assert compatibleData.buildPromotions != null;
                result.add(new FilterConditionChecker<SBuildAgent>() {

                    public boolean isIncluded(@NotNull final SBuildAgent item) {
                        return isCompatibleWithAnyBuild(item, compatibleData.buildPromotions);
                    }
                });
            }
        }
    }
    // incompatible with at least with one of the buildTypes
    final String incompatible = locator.getSingleDimensionValue(INCOMPATIBLE);
    if (incompatible != null) {
        final CompatibleLocatorParseResult compatibleData = getBuildTypesFromCompatibleDimension(incompatible);
        if (compatibleData.buildTypes != null) {
            result.add(new FilterConditionChecker<SBuildAgent>() {

                public boolean isIncluded(@NotNull final SBuildAgent item) {
                    return !isCompatibleWithAll(item, compatibleData.buildTypes);
                }
            });
        } else {
            result.add(new FilterConditionChecker<SBuildAgent>() {

                public boolean isIncluded(@NotNull final SBuildAgent item) {
                    assert compatibleData.buildPromotions != null;
                    return !isCompatibleWithAllBuild(item, compatibleData.buildPromotions);
                }
            });
        }
    }
    return result;
}
Also used : LocatorProcessException(jetbrains.buildServer.server.rest.errors.LocatorProcessException) java.util(java.util) LocatorName(jetbrains.buildServer.server.rest.swagger.constants.LocatorName) CloudInstance(jetbrains.buildServer.clouds.CloudInstance) DuplicateChecker(jetbrains.buildServer.server.rest.data.util.DuplicateChecker) LocatorResource(jetbrains.buildServer.server.rest.swagger.annotations.LocatorResource) Agent(jetbrains.buildServer.server.rest.model.agent.Agent) ComparatorDuplicateChecker(jetbrains.buildServer.server.rest.data.util.ComparatorDuplicateChecker) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) LocatorDimensionDataType(jetbrains.buildServer.server.rest.swagger.constants.LocatorDimensionDataType) AgentPool(jetbrains.buildServer.serverSide.agentPools.AgentPool) OperationException(jetbrains.buildServer.server.rest.errors.OperationException) AgentType(jetbrains.buildServer.serverSide.agentTypes.AgentType) SAgentType(jetbrains.buildServer.serverSide.agentTypes.SAgentType) Logger(com.intellij.openapi.diagnostic.Logger) PagerData(jetbrains.buildServer.server.rest.model.PagerData) Compatibility(jetbrains.buildServer.server.rest.model.agent.Compatibility) AgentTypeFinder(jetbrains.buildServer.serverSide.agentTypes.AgentTypeFinder) AgentRestrictor(jetbrains.buildServer.AgentRestrictor) ServiceLocator(jetbrains.buildServer.ServiceLocator) jetbrains.buildServer.serverSide(jetbrains.buildServer.serverSide) ComparisonChain(com.google.common.collect.ComparisonChain) Collectors(java.util.stream.Collectors) MapParametersProviderImpl(jetbrains.buildServer.parameters.impl.MapParametersProviderImpl) Nullable(org.jetbrains.annotations.Nullable) Stream(java.util.stream.Stream) LocatorDimension(jetbrains.buildServer.server.rest.swagger.annotations.LocatorDimension) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException) SingleAgentRestrictor(jetbrains.buildServer.serverSide.impl.buildDistribution.restrictors.SingleAgentRestrictor) NotNull(org.jetbrains.annotations.NotNull) CloudInstance(jetbrains.buildServer.clouds.CloudInstance) AgentPool(jetbrains.buildServer.serverSide.agentPools.AgentPool) MapParametersProviderImpl(jetbrains.buildServer.parameters.impl.MapParametersProviderImpl) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ComparisonChain (com.google.common.collect.ComparisonChain)1 Logger (com.intellij.openapi.diagnostic.Logger)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 AgentRestrictor (jetbrains.buildServer.AgentRestrictor)1 ServiceLocator (jetbrains.buildServer.ServiceLocator)1 CloudInstance (jetbrains.buildServer.clouds.CloudInstance)1 MapParametersProviderImpl (jetbrains.buildServer.parameters.impl.MapParametersProviderImpl)1 ComparatorDuplicateChecker (jetbrains.buildServer.server.rest.data.util.ComparatorDuplicateChecker)1 DuplicateChecker (jetbrains.buildServer.server.rest.data.util.DuplicateChecker)1 BadRequestException (jetbrains.buildServer.server.rest.errors.BadRequestException)1 LocatorProcessException (jetbrains.buildServer.server.rest.errors.LocatorProcessException)1 NotFoundException (jetbrains.buildServer.server.rest.errors.NotFoundException)1 OperationException (jetbrains.buildServer.server.rest.errors.OperationException)1 PagerData (jetbrains.buildServer.server.rest.model.PagerData)1 Agent (jetbrains.buildServer.server.rest.model.agent.Agent)1 Compatibility (jetbrains.buildServer.server.rest.model.agent.Compatibility)1 LocatorDimension (jetbrains.buildServer.server.rest.swagger.annotations.LocatorDimension)1 LocatorResource (jetbrains.buildServer.server.rest.swagger.annotations.LocatorResource)1