Search in sources :

Example 1 with AbstractAgentPool

use of jetbrains.buildServer.server.graphql.model.agentPool.AbstractAgentPool in project teamcity-rest by JetBrains.

the class AbstractAgentPoolResolver method projects.

@NotNull
public AgentPoolProjectsConnection projects(@NotNull AbstractAgentPool pool, @NotNull ProjectsFilter filter, @NotNull DataFetchingEnvironment env) {
    jetbrains.buildServer.serverSide.agentPools.AgentPool realPool = pool.getRealPool();
    Collection<String> projectIds = realPool.getProjectIds();
    Stream<SProject> projects = myProjectManager.findProjects(projectIds).stream();
    if (filter.getArchived() != null) {
        projects = projects.filter(p -> p.isArchived() == filter.getArchived());
    }
    Integer excludedProjectsCount = null;
    if (env.getSelectionSet().contains("excludedCount")) {
        AuthorityHolder authHolder = mySecurityContext.getAuthorityHolder();
        excludedProjectsCount = (int) projectIds.stream().filter(projectId -> !authHolder.isPermissionGrantedForProject(projectId, Permission.VIEW_PROJECT)).count();
    }
    return new AgentPoolProjectsConnection(projects.collect(Collectors.toList()), excludedProjectsCount, PaginationArguments.everything());
}
Also used : AgentPool(jetbrains.buildServer.serverSide.agentPools.AgentPool) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) CloudClientEx(jetbrains.buildServer.clouds.CloudClientEx) AbstractAgentPool(jetbrains.buildServer.server.graphql.model.agentPool.AbstractAgentPool) CloudProfile(jetbrains.buildServer.clouds.CloudProfile) AuthorityHolder(jetbrains.buildServer.serverSide.auth.AuthorityHolder) AgentPoolProjectsConnection(jetbrains.buildServer.server.graphql.model.connections.agentPool.AgentPoolProjectsConnection) PaginationArguments(jetbrains.buildServer.server.graphql.model.connections.PaginationArguments) ArrayList(java.util.ArrayList) BooleanSupplier(java.util.function.BooleanSupplier) AgentPool(jetbrains.buildServer.serverSide.agentPools.AgentPool) CloudImage(jetbrains.buildServer.clouds.CloudImage) SAgentType(jetbrains.buildServer.serverSide.agentTypes.SAgentType) AgentTypeKey(jetbrains.buildServer.serverSide.agentTypes.AgentTypeKey) AgentPoolAgentsConnection(jetbrains.buildServer.server.graphql.model.connections.agentPool.AgentPoolAgentsConnection) AgentTypeFinder(jetbrains.buildServer.serverSide.agentTypes.AgentTypeFinder) CloudManager(jetbrains.buildServer.clouds.server.CloudManager) jetbrains.buildServer.serverSide(jetbrains.buildServer.serverSide) Collection(java.util.Collection) AgentPoolPermissions(jetbrains.buildServer.server.graphql.model.agentPool.AgentPoolPermissions) Collectors(java.util.stream.Collectors) List(java.util.List) Component(org.springframework.stereotype.Component) Stream(java.util.stream.Stream) ProjectsFilter(jetbrains.buildServer.server.graphql.model.filter.ProjectsFilter) Permission(jetbrains.buildServer.serverSide.auth.Permission) AgentPoolCloudImagesConnection(jetbrains.buildServer.server.graphql.model.connections.agentPool.AgentPoolCloudImagesConnection) Pair(com.intellij.openapi.util.Pair) AuthUtil(jetbrains.buildServer.serverSide.auth.AuthUtil) NotNull(org.jetbrains.annotations.NotNull) AgentPoolProjectsConnection(jetbrains.buildServer.server.graphql.model.connections.agentPool.AgentPoolProjectsConnection) AuthorityHolder(jetbrains.buildServer.serverSide.auth.AuthorityHolder) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with AbstractAgentPool

use of jetbrains.buildServer.server.graphql.model.agentPool.AbstractAgentPool 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)

Example 3 with AbstractAgentPool

use of jetbrains.buildServer.server.graphql.model.agentPool.AbstractAgentPool in project teamcity-rest by JetBrains.

the class Mutation method authorizeAgent.

@Used("graphql")
@NotNull
public DataFetcherResult<AuthorizeAgentPayload> authorizeAgent(@NotNull AuthorizeAgentInput input, @NotNull DataFetchingEnvironment dfe) {
    return runWithAgent(input.getAgentRawId(), agent -> {
        DataFetcherResult.Builder<AuthorizeAgentPayload> result = DataFetcherResult.newResult();
        GraphQLContext context = dfe.getContext();
        String authReason = input.getReason() == null ? "" : input.getReason();
        // Move agent to another pool first as we don't want some cheeky build to start while agent is in a wrong pool.
        if (input.getTargetAgentPoolRawId() != null) {
            try {
                myAgentPoolManager.moveAgentToPool(input.getTargetAgentPoolRawId(), agent);
            } catch (NoSuchAgentPoolException e) {
                return result.error(new EntityNotFoundGraphQLError(String.format("Agent pool with id=%d is not found.", input.getTargetAgentPoolRawId()))).build();
            } catch (PoolQuotaExceededException e) {
                LOG.debug(e.getMessage());
                return result.error(new OperationFailedGraphQLError(String.format("Agent pool with id=%d does not accept agents.", input.getTargetAgentPoolRawId()))).build();
            } catch (AgentTypeCannotBeMovedException e) {
                LOG.debug(e.getMessage());
                return result.error(new OperationFailedGraphQLError(String.format("Agent with id=%d can not be moved.", input.getAgentRawId()))).build();
            }
        }
        agent.setAuthorized(true, context.getUser(), authReason);
        Agent agentModel = new Agent(agent);
        AbstractAgentPool targetPoolModel = null;
        if (input.getTargetAgentPoolRawId() != null) {
            AgentPool realPool = myAgentPoolManager.findAgentPoolById(input.getTargetAgentPoolRawId());
            if (realPool != null) {
                targetPoolModel = myAgentPoolFactory.produce(realPool);
            }
        }
        return result.data(new AuthorizeAgentPayload(agentModel, targetPoolModel)).build();
    });
}
Also used : AbstractAgentPool(jetbrains.buildServer.server.graphql.model.agentPool.AbstractAgentPool) AbstractAgentPool(jetbrains.buildServer.server.graphql.model.agentPool.AbstractAgentPool) DataFetcherResult(graphql.execution.DataFetcherResult) OperationFailedGraphQLError(jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError) GraphQLContext(jetbrains.buildServer.server.graphql.GraphQLContext) EntityNotFoundGraphQLError(jetbrains.buildServer.server.graphql.util.EntityNotFoundGraphQLError) Used(jetbrains.buildServer.Used) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with AbstractAgentPool

use of jetbrains.buildServer.server.graphql.model.agentPool.AbstractAgentPool in project teamcity-rest by JetBrains.

the class Mutation method bulkAuthorizeAgents.

@Used("graphql")
@NotNull
public DataFetcherResult<BulkAuthorizeAgentsPayload> bulkAuthorizeAgents(@NotNull BulkAuthorizeAgentsInput input, @NotNull DataFetchingEnvironment dfe) {
    DataFetcherResult.Builder<BulkAuthorizeAgentsPayload> result = DataFetcherResult.newResult();
    GraphQLContext context = dfe.getContext();
    String authReason = input.getReason() == null ? "" : input.getReason();
    Set<Integer> agentTypeIds = new HashSet<>(input.getAgentRawIds().size());
    List<BuildAgentEx> agents = new ArrayList<>();
    for (int agentId : input.getAgentRawIds()) {
        BuildAgentEx agent = myBuildAgentManager.findAgentById(agentId, true);
        if (agent == null) {
            return result.error(new EntityNotFoundGraphQLError(String.format("Agent with id=%d is not found.", agentId))).build();
        }
        agentTypeIds.add(agent.getAgentTypeId());
        agents.add(agent);
    }
    if (input.getTargetAgentPoolRawId() != null) {
        try {
            myAgentPoolManager.moveAgentTypesToPool(input.getTargetAgentPoolRawId(), agentTypeIds);
        } catch (NoSuchAgentPoolException e) {
            return result.error(new EntityNotFoundGraphQLError("Agent pool is not found.")).build();
        } catch (AgentTypeCannotBeMovedException e) {
            LOG.debug(e.getMessage());
            return result.error(new OperationFailedGraphQLError("One of the given agents can't be moved.")).build();
        } catch (PoolQuotaExceededException e) {
            LOG.debug(e.getMessage());
            return result.error(new OperationFailedGraphQLError(String.format("Agent pool can't accept %d agents.", agentTypeIds.size()))).build();
        }
    }
    AbstractAgentPool poolModel = null;
    if (input.getTargetAgentPoolRawId() != null) {
        AgentPool targetRealPool = myAgentPoolManager.findAgentPoolById(input.getTargetAgentPoolRawId());
        if (targetRealPool != null) {
            poolModel = myAgentPoolFactory.produce(targetRealPool);
        } else {
            result.error(new EntityNotFoundGraphQLError("Agent pool is not found after successfully moving agents to it. Possibly it was deleted already."));
        }
    }
    List<Agent> agentModels = new ArrayList<>();
    for (BuildAgentEx agent : agents) {
        try {
            agent.setAuthorized(true, context.getUser(), authReason);
        } catch (LicenseNotGrantedException e) {
            result.error(new OperationFailedGraphQLError(e.getMessage()));
        }
        agentModels.add(new Agent(agent));
    }
    return result.data(new BulkAuthorizeAgentsPayload(agentModels, poolModel)).build();
}
Also used : ArrayList(java.util.ArrayList) AbstractAgentPool(jetbrains.buildServer.server.graphql.model.agentPool.AbstractAgentPool) AbstractAgentPool(jetbrains.buildServer.server.graphql.model.agentPool.AbstractAgentPool) DataFetcherResult(graphql.execution.DataFetcherResult) LicenseNotGrantedException(jetbrains.buildServer.LicenseNotGrantedException) OperationFailedGraphQLError(jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError) GraphQLContext(jetbrains.buildServer.server.graphql.GraphQLContext) EntityNotFoundGraphQLError(jetbrains.buildServer.server.graphql.util.EntityNotFoundGraphQLError) HashSet(java.util.HashSet) Used(jetbrains.buildServer.Used) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

AbstractAgentPool (jetbrains.buildServer.server.graphql.model.agentPool.AbstractAgentPool)4 NotNull (org.jetbrains.annotations.NotNull)4 DataFetcherResult (graphql.execution.DataFetcherResult)3 EntityNotFoundGraphQLError (jetbrains.buildServer.server.graphql.util.EntityNotFoundGraphQLError)3 ArrayList (java.util.ArrayList)2 Used (jetbrains.buildServer.Used)2 GraphQLContext (jetbrains.buildServer.server.graphql.GraphQLContext)2 OperationFailedGraphQLError (jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError)2 AgentPool (jetbrains.buildServer.serverSide.agentPools.AgentPool)2 Pair (com.intellij.openapi.util.Pair)1 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 List (java.util.List)1 BooleanSupplier (java.util.function.BooleanSupplier)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 LicenseNotGrantedException (jetbrains.buildServer.LicenseNotGrantedException)1 CloudClientEx (jetbrains.buildServer.clouds.CloudClientEx)1 CloudImage (jetbrains.buildServer.clouds.CloudImage)1