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;
}
Aggregations