Search in sources :

Example 1 with AgentPool

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;
}
Also used : java.util(java.util) AuthorizationFailedException(jetbrains.buildServer.server.rest.errors.AuthorizationFailedException) LocatorName(jetbrains.buildServer.server.rest.swagger.constants.LocatorName) LocatorResource(jetbrains.buildServer.server.rest.swagger.annotations.LocatorResource) BuildTypeOrTemplate(jetbrains.buildServer.server.rest.util.BuildTypeOrTemplate) PropEntityProjectFeature(jetbrains.buildServer.server.rest.model.project.PropEntityProjectFeature) LocatorDimensionDataType(jetbrains.buildServer.server.rest.swagger.constants.LocatorDimensionDataType) AgentPool(jetbrains.buildServer.serverSide.agentPools.AgentPool) AccessDeniedException(jetbrains.buildServer.serverSide.auth.AccessDeniedException) StringUtil(jetbrains.buildServer.util.StringUtil) Logger(com.intellij.openapi.diagnostic.Logger) PagerData(jetbrains.buildServer.server.rest.model.PagerData) SVcsRoot(jetbrains.buildServer.vcs.SVcsRoot) ServiceLocator(jetbrains.buildServer.ServiceLocator) jetbrains.buildServer.serverSide(jetbrains.buildServer.serverSide) Collectors(java.util.stream.Collectors) APIController(jetbrains.buildServer.server.rest.APIController) Project(jetbrains.buildServer.server.rest.model.project.Project) Nullable(org.jetbrains.annotations.Nullable) LocatorDimension(jetbrains.buildServer.server.rest.swagger.annotations.LocatorDimension) Permission(jetbrains.buildServer.serverSide.auth.Permission) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException) NotNull(org.jetbrains.annotations.NotNull) SUser(jetbrains.buildServer.users.SUser) BuildProject(jetbrains.buildServer.BuildProject) UserEx(jetbrains.buildServer.users.impl.UserEx) AgentPool(jetbrains.buildServer.serverSide.agentPools.AgentPool) PropEntityProjectFeature(jetbrains.buildServer.server.rest.model.project.PropEntityProjectFeature) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with AgentPool

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;
}
Also used : AgentPool(jetbrains.buildServer.serverSide.agentPools.AgentPool)

Example 3 with AgentPool

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

Example 4 with AgentPool

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);
}
Also used : ProjectAgentPoolImpl(jetbrains.buildServer.serverSide.agentPools.ProjectAgentPoolImpl) SProject(jetbrains.buildServer.serverSide.SProject) Project(jetbrains.buildServer.server.graphql.model.Project) SProject(jetbrains.buildServer.serverSide.SProject) AgentPool(jetbrains.buildServer.serverSide.agentPools.AgentPool) ProjectAgentPool(jetbrains.buildServer.server.graphql.model.agentPool.ProjectAgentPool) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with AgentPool

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();
}
Also used : AgentType(jetbrains.buildServer.serverSide.agentTypes.AgentType) DataFetcherResult(graphql.execution.DataFetcherResult) ProjectAgentPool(jetbrains.buildServer.server.graphql.model.agentPool.ProjectAgentPool) AbstractAgentPool(jetbrains.buildServer.server.graphql.model.agentPool.AbstractAgentPool) EntityNotFoundGraphQLError(jetbrains.buildServer.server.graphql.util.EntityNotFoundGraphQLError) AbstractAgentPool(jetbrains.buildServer.server.graphql.model.agentPool.AbstractAgentPool) AgentPool(jetbrains.buildServer.serverSide.agentPools.AgentPool) ProjectAgentPool(jetbrains.buildServer.server.graphql.model.agentPool.ProjectAgentPool) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

AgentPool (jetbrains.buildServer.serverSide.agentPools.AgentPool)7 NotNull (org.jetbrains.annotations.NotNull)4 ProjectAgentPool (jetbrains.buildServer.server.graphql.model.agentPool.ProjectAgentPool)3 Nullable (org.jetbrains.annotations.Nullable)3 Logger (com.intellij.openapi.diagnostic.Logger)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 AgentRestrictor (jetbrains.buildServer.AgentRestrictor)2 ServiceLocator (jetbrains.buildServer.ServiceLocator)2 NotFoundException (jetbrains.buildServer.server.rest.errors.NotFoundException)2 PagerData (jetbrains.buildServer.server.rest.model.PagerData)2 Agent (jetbrains.buildServer.server.rest.model.agent.Agent)2 LocatorDimension (jetbrains.buildServer.server.rest.swagger.annotations.LocatorDimension)2 LocatorResource (jetbrains.buildServer.server.rest.swagger.annotations.LocatorResource)2 LocatorDimensionDataType (jetbrains.buildServer.server.rest.swagger.constants.LocatorDimensionDataType)2 LocatorName (jetbrains.buildServer.server.rest.swagger.constants.LocatorName)2 jetbrains.buildServer.serverSide (jetbrains.buildServer.serverSide)2 SProject (jetbrains.buildServer.serverSide.SProject)2 AgentType (jetbrains.buildServer.serverSide.agentTypes.AgentType)2 AgentTypeFinder (jetbrains.buildServer.serverSide.agentTypes.AgentTypeFinder)2