use of jetbrains.buildServer.serverSide.agentPools.AgentPool in project teamcity-rest by JetBrains.
the class ProjectFinder method getFilter.
@NotNull
@Override
public ItemFilter<SProject> getFilter(@NotNull final Locator locator) {
final MultiCheckerFilter<SProject> result = new MultiCheckerFilter<SProject>();
final String id = locator.getSingleDimensionValue(DIMENSION_ID);
if (id != null) {
if (TeamCityProperties.getBoolean(APIController.REST_COMPATIBILITY_ALLOW_EXTERNAL_ID_AS_INTERNAL)) {
result.add(item -> id.equalsIgnoreCase(item.getExternalId()) || id.equalsIgnoreCase(item.getProjectId()));
} else {
result.add(item -> id.equalsIgnoreCase(item.getExternalId()));
}
}
final String name = locator.getSingleDimensionValue(DIMENSION_NAME);
if (name != null) {
result.add(new FilterConditionChecker<SProject>() {
public boolean isIncluded(@NotNull final SProject item) {
return name.equals(item.getName());
}
});
}
final Boolean archived = locator.getSingleDimensionValueAsBoolean(DIMENSION_ARCHIVED);
if (archived != null) {
result.add(new FilterConditionChecker<SProject>() {
public boolean isIncluded(@NotNull final SProject item) {
return FilterUtil.isIncludedByBooleanFilter(archived, item.isArchived());
}
});
}
final Boolean readOnlyUI = locator.getSingleDimensionValueAsBoolean(DIMENSION_READ_ONLY_UI);
if (readOnlyUI != null) {
result.add(new FilterConditionChecker<SProject>() {
public boolean isIncluded(@NotNull final SProject item) {
return FilterUtil.isIncludedByBooleanFilter(readOnlyUI, item.isReadOnly());
}
});
}
final String parameterDimension = locator.getSingleDimensionValue(DIMENSION_PARAMETER);
if (parameterDimension != null) {
final ParameterCondition parameterCondition = ParameterCondition.create(parameterDimension);
result.add(new FilterConditionChecker<SProject>() {
public boolean isIncluded(@NotNull final SProject item) {
final boolean canView = !Project.shouldRestrictSettingsViewing(item, myPermissionChecker);
if (!canView) {
LOG.debug("While filtering projects by " + DIMENSION_PARAMETER + " user does not have enough permissions to see settings. Excluding project: " + item.describe(false));
return false;
}
return parameterCondition.matches(item);
}
});
}
if (locator.isUnused(DIMENSION_PROJECT)) {
final String directParentLocator = locator.getSingleDimensionValue(DIMENSION_PROJECT);
if (directParentLocator != null) {
final SProject directParent = getItem(directParentLocator);
result.add(new FilterConditionChecker<SProject>() {
public boolean isIncluded(@NotNull final SProject item) {
return directParent.getProjectId().equals(item.getParentProjectId());
}
});
}
}
if (locator.isUnused(DIMENSION_AFFECTED_PROJECT)) {
final SProject parentProject = getParentProject(locator);
if (parentProject != null) {
result.add(new FilterConditionChecker<SProject>() {
public boolean isIncluded(@NotNull final SProject item) {
return isSameOrParent(parentProject, item);
}
});
}
}
final String featureDimension = locator.getSingleDimensionValue(FEATURE);
if (featureDimension != null) {
result.add(new FilterConditionChecker<SProject>() {
public boolean isIncluded(@NotNull final SProject item) {
final boolean canView = !Project.shouldRestrictSettingsViewing(item, myPermissionChecker);
if (!canView) {
LOG.debug("While filtering projects by " + DIMENSION_PARAMETER + " user does not have enough permissions to see settings. Excluding project: " + item.describe(false));
return false;
}
return new PropEntityProjectFeature.ProjectFeatureFinder(item).getItems(featureDimension).myEntries.size() > 0;
}
});
}
final String defaultTemplateDimension = locator.getSingleDimensionValue(DEFAULT_TEMPLATE);
if (defaultTemplateDimension != null) {
Set<String> defaultTemplateIds = myServiceLocator.getSingletonService(BuildTypeFinder.class).getItems(defaultTemplateDimension).myEntries.stream().map(bt -> bt.getInternalId()).collect(Collectors.toSet());
result.add(item -> {
final boolean canView = !Project.shouldRestrictSettingsViewing(item, myPermissionChecker);
if (!canView) {
LOG.debug("While filtering projects by " + DIMENSION_PARAMETER + " user does not have enough permissions to see settings. Excluding project: " + item.describe(false));
return false;
}
String defaultTemplateId = item.getDefaultTemplateId();
return defaultTemplateId != null && defaultTemplateIds.contains(defaultTemplateId);
});
}
final String userPermissionDimension = locator.getSingleDimensionValue(USER_PERMISSION);
if (userPermissionDimension != null) {
result.add(new PermissionCheck().matches(userPermissionDimension));
}
final List<String> poolDimensions = locator.getDimensionValue(AGENT_POOL);
if (!poolDimensions.isEmpty()) {
AgentPoolFinder agentPoolFinder = myServiceLocator.getSingletonService(AgentPoolFinder.class);
for (String poolDimension : poolDimensions) {
List<AgentPool> pools = agentPoolFinder.getItems(poolDimension).myEntries;
Set<String> filterProjectInternalIds = new HashSet<>();
for (AgentPool pool : pools) {
filterProjectInternalIds.addAll(pool.getProjectIds());
}
result.add(item -> filterProjectInternalIds.contains(item.getProjectId()));
}
}
return result;
}
use of jetbrains.buildServer.serverSide.agentPools.AgentPool in project teamcity-rest by JetBrains.
the class AgentPoolActionsAccessCheckerImpl method canManageProjectsInPool.
@Override
public boolean canManageProjectsInPool(int agentPoolId) {
AgentPool pool = myAgentPoolManager.findAgentPoolById(agentPoolId);
if (pool == null || pool.isProjectPool()) {
return false;
}
AuthorityHolder authHolder = mySecurityContext.getAuthorityHolder();
return getRestrictingProjectsInPoolUnsafe(authHolder, agentPoolId).count() == 0;
}
use of jetbrains.buildServer.serverSide.agentPools.AgentPool 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;
}
use of jetbrains.buildServer.serverSide.agentPools.AgentPool in project teamcity-rest by JetBrains.
the class ProjectAgentPoolResolver method project.
@NotNull
public Project project(@NotNull ProjectAgentPool pool, @NotNull DataFetchingEnvironment env) {
AgentPool realPool = pool.getRealPool();
if (!realPool.isProjectPool()) {
throw new RuntimeException(String.format("Pool id=%d is not a project pool.", realPool.getAgentPoolId()));
}
String projectId = ((ProjectAgentPoolImpl) realPool).getProjectId();
SProject project = myProjectManager.findProjectById(projectId);
if (project == null) {
throw new RuntimeException(String.format("ProjectAgentPool id=%d does not have a project.", realPool.getAgentPoolId()));
}
return new Project(project);
}
use of jetbrains.buildServer.serverSide.agentPools.AgentPool in project teamcity-rest by JetBrains.
the class CloudImageResolver method agentPool.
@NotNull
public DataFetcherResult<AbstractAgentPool> agentPool(@NotNull CloudImage image, @NotNull DataFetchingEnvironment env) {
DataFetcherResult.Builder<AbstractAgentPool> result = new DataFetcherResult.Builder<>();
AgentType agentType = findAgentType(image);
AgentPool pool = agentType != null ? myAgentPoolManager.findAgentPoolById(agentType.getAgentPoolId()) : null;
if (agentType == null || pool == null) {
result.error(new EntityNotFoundGraphQLError(String.format("Could not find agent pool for image id=%s in profile id=%s", image.getRawId(), image.getProfileId())));
return result.build();
}
return result.data(new ProjectAgentPool(pool)).localContext(pool).build();
}
Aggregations