Search in sources :

Example 6 with OperationFailedGraphQLError

use of jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError in project teamcity-rest by JetBrains.

the class AgentPoolMutation method bulkMoveAgentsToAgentPool.

@Used("graphql")
@NotNull
public DataFetcherResult<BulkMoveAgentToAgentsPoolPayload> bulkMoveAgentsToAgentPool(@NotNull BulkMoveAgentsToAgentPoolInput input) {
    DataFetcherResult.Builder<BulkMoveAgentToAgentsPoolPayload> result = DataFetcherResult.newResult();
    AgentPool targetPool = myAgentPoolManager.findAgentPoolById(input.getTargetAgentPoolRawId());
    if (targetPool == null) {
        return result.error(new EntityNotFoundGraphQLError("Target pool is not found.")).build();
    }
    if (targetPool.isProjectPool() || targetPool instanceof ReadOnlyAgentPool) {
        return result.error(new OperationFailedGraphQLError("Can't move agents to target pool.")).build();
    }
    if (!myAgentPoolActionsAccessChecker.canManageAgentsInPool(targetPool)) {
        return result.error(new OperationFailedGraphQLError("Can't move agents to target pool.")).build();
    }
    Set<String> projectsToCheck = new HashSet<>();
    Set<Integer> agentTypes = new HashSet<>();
    for (Integer agentId : input.getAgentRawIds()) {
        SBuildAgent agent = myBuildAgentManager.findAgentById(agentId, true);
        if (agent == null) {
            return result.error(new OperationFailedGraphQLError("One of the agents with given ids is not found.")).build();
        }
        agentTypes.add(agent.getAgentTypeId());
        projectsToCheck.addAll(agent.getAgentPool().getProjectIds());
    }
    AuthorityHolder authHolder = mySecurityContext.getAuthorityHolder();
    if (!AuthUtil.hasPermissionToManageAgentPoolsWithProjects(authHolder, projectsToCheck)) {
        return result.error(new OperationFailedGraphQLError("Not enough permissions on one of the agent pools.")).build();
    }
    try {
        myAgentPoolManager.moveAgentTypesToPool(input.getTargetAgentPoolRawId(), agentTypes);
    } catch (NoSuchAgentPoolException e) {
        return result.error(new EntityNotFoundGraphQLError("Target pool does not exist.")).build();
    } catch (PoolQuotaExceededException e) {
        LOG.debug(e.getMessage());
        return result.error(new OperationFailedGraphQLError("Target pool does not accept agents.")).build();
    } catch (AgentTypeCannotBeMovedException e) {
        LOG.debug(e.getMessage());
        return result.error(new OperationFailedGraphQLError("One of the selected agents can not be moved.")).build();
    }
    List<Agent> agents = new ArrayList<>();
    for (Integer agentId : input.getAgentRawIds()) {
        SBuildAgent agent = myBuildAgentManager.findAgentById(agentId, true);
        if (agent == null) {
            continue;
        }
        agents.add(new Agent(agent));
    }
    // should not be null at this stage
    AgentPool updatedTargetPool = myAgentPoolManager.findAgentPoolById(input.getTargetAgentPoolRawId());
    BulkMoveAgentToAgentsPoolPayload payload = new BulkMoveAgentToAgentsPoolPayload(agents, new jetbrains.buildServer.server.graphql.model.agentPool.AgentPool(updatedTargetPool));
    return result.data(payload).build();
}
Also used : Agent(jetbrains.buildServer.server.graphql.model.Agent) DataFetcherResult(graphql.execution.DataFetcherResult) OperationFailedGraphQLError(jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError) EntityNotFoundGraphQLError(jetbrains.buildServer.server.graphql.util.EntityNotFoundGraphQLError) jetbrains.buildServer.server.graphql.model.mutation.agentPool(jetbrains.buildServer.server.graphql.model.mutation.agentPool) AuthorityHolder(jetbrains.buildServer.serverSide.auth.AuthorityHolder) Used(jetbrains.buildServer.Used) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with OperationFailedGraphQLError

use of jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError in project teamcity-rest by JetBrains.

the class AgentPoolMutation method unassignProjectFromAgentPool.

@Used("graphql")
@NotNull
public DataFetcherResult<UnassignProjectFromAgentPoolPayload> unassignProjectFromAgentPool(@NotNull UnassignProjectFromAgentPoolInput input) {
    DataFetcherResult.Builder<UnassignProjectFromAgentPoolPayload> result = DataFetcherResult.newResult();
    SProject project = myProjectManager.findProjectByExternalId(input.getProjectRawId());
    if (project == null) {
        return result.error(new EntityNotFoundGraphQLError("Project with given id does not exist.")).build();
    }
    AuthorityHolder authorityHolder = mySecurityContext.getAuthorityHolder();
    boolean canRemoveThisProject = AuthUtil.hasPermissionToManageAgentPoolsWithProject(authorityHolder, project.getProjectId());
    boolean thereAreOtherAssociatedPools = false;
    if (canRemoveThisProject) {
        // let's count other pools iff we are sure that we can potentially remove given project.
        thereAreOtherAssociatedPools = myAgentPoolManager.getAgentPoolsWithProject(project.getProjectId()).stream().map(poolId -> myAgentPoolManager.findAgentPoolById(poolId)).filter(Objects::nonNull).filter(pool -> !pool.isProjectPool()).count() > 1;
    }
    if (!canRemoveThisProject || !thereAreOtherAssociatedPools) {
        if (!canRemoveThisProject) {
            return result.error(new OperationFailedGraphQLError("Can't unassign project, not enough permissions.")).build();
        }
        return result.error(new OperationFailedGraphQLError("Can't unassign project, there are no other pools associated with this project.")).build();
    }
    Set<String> projectsToDisassociate;
    if (input.isRecursive()) {
        projectsToDisassociate = new HashSet<>();
        projectsToDisassociate.add(project.getProjectId());
        project.getProjects().stream().map(p -> p.getProjectId()).forEach(projectsToDisassociate::add);
    } else {
        projectsToDisassociate = Collections.singleton(project.getProjectId());
    }
    try {
        myAgentPoolManager.dissociateProjectsFromPool(input.getAgentPoolRawId(), projectsToDisassociate);
    } catch (NoSuchAgentPoolException e) {
        return result.error(new EntityNotFoundGraphQLError("Agent pool with given id does not exist.")).build();
    }
    AgentPool agentPool = myAgentPoolManager.findAgentPoolById(input.getAgentPoolRawId());
    if (agentPool == null) {
        LOG.warn(String.format("Agent pool with id=%d is missing after associating project id=%s", input.getAgentPoolRawId(), project.getProjectId()));
        return result.error(new UnexpectedServerGraphQLError("Agent pool with given id could not be found after operation.")).build();
    }
    return result.data(new UnassignProjectFromAgentPoolPayload(new Project(project), new jetbrains.buildServer.server.graphql.model.agentPool.AgentPool(agentPool))).build();
}
Also used : DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) CloudClientEx(jetbrains.buildServer.clouds.CloudClientEx) EntityNotFoundGraphQLError(jetbrains.buildServer.server.graphql.util.EntityNotFoundGraphQLError) UnexpectedServerGraphQLError(jetbrains.buildServer.server.graphql.util.UnexpectedServerGraphQLError) java.util(java.util) CloudProfile(jetbrains.buildServer.clouds.CloudProfile) AuthorityHolder(jetbrains.buildServer.serverSide.auth.AuthorityHolder) BooleanUtils(org.apache.commons.lang3.BooleanUtils) jetbrains.buildServer.serverSide.agentPools(jetbrains.buildServer.serverSide.agentPools) DataFetcherResult(graphql.execution.DataFetcherResult) jetbrains.buildServer.server.graphql.model.mutation.agentPool(jetbrains.buildServer.server.graphql.model.mutation.agentPool) SecurityContext(jetbrains.buildServer.serverSide.auth.SecurityContext) Logger(com.intellij.openapi.diagnostic.Logger) Project(jetbrains.buildServer.server.graphql.model.Project) Agent(jetbrains.buildServer.server.graphql.model.Agent) GraphQLMutationResolver(graphql.kickstart.tools.GraphQLMutationResolver) Used(jetbrains.buildServer.Used) jetbrains.buildServer.serverSide(jetbrains.buildServer.serverSide) jetbrains.buildServer.serverSide.agentTypes(jetbrains.buildServer.serverSide.agentTypes) Collectors(java.util.stream.Collectors) CloudManagerBase(jetbrains.buildServer.clouds.server.CloudManagerBase) jetbrains.buildServer.server.graphql.model.mutation(jetbrains.buildServer.server.graphql.model.mutation) OperationFailedGraphQLError(jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError) Component(org.springframework.stereotype.Component) Pair(com.intellij.openapi.util.Pair) AuthUtil(jetbrains.buildServer.serverSide.auth.AuthUtil) CloudImage(jetbrains.buildServer.server.graphql.model.CloudImage) NotNull(org.jetbrains.annotations.NotNull) UnexpectedServerGraphQLError(jetbrains.buildServer.server.graphql.util.UnexpectedServerGraphQLError) Project(jetbrains.buildServer.server.graphql.model.Project) DataFetcherResult(graphql.execution.DataFetcherResult) OperationFailedGraphQLError(jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError) EntityNotFoundGraphQLError(jetbrains.buildServer.server.graphql.util.EntityNotFoundGraphQLError) jetbrains.buildServer.server.graphql.model.mutation.agentPool(jetbrains.buildServer.server.graphql.model.mutation.agentPool) AuthorityHolder(jetbrains.buildServer.serverSide.auth.AuthorityHolder) Used(jetbrains.buildServer.Used) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with OperationFailedGraphQLError

use of jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError in project teamcity-rest by JetBrains.

the class AgentPoolMutation method assignProjectWithAgentPool.

@Used("graphql")
@NotNull
public DataFetcherResult<AssignProjectWithAgentPoolPayload> assignProjectWithAgentPool(@NotNull AssignProjectWithAgentPoolInput input) {
    DataFetcherResult.Builder<AssignProjectWithAgentPoolPayload> result = DataFetcherResult.newResult();
    if (!myAgentPoolActionsAccessChecker.canManageProjectsInPool(input.getAgentPoolRawId())) {
        return result.error(new OperationFailedGraphQLError("Can't assign project.")).build();
    }
    SProject project = myProjectManager.findProjectByExternalId(input.getProjectRawId());
    if (project == null) {
        return result.error(new EntityNotFoundGraphQLError("Project with given id does not exist.")).build();
    }
    try {
        myAgentPoolManager.associateProjectsWithPool(input.getAgentPoolRawId(), Collections.singleton(project.getProjectId()));
    } catch (NoSuchAgentPoolException e) {
        return result.error(new EntityNotFoundGraphQLError("Agent pool with given id does not exist.")).build();
    }
    if (BooleanUtils.isTrue(input.getExclusively())) {
        myAgentPoolManager.dissociateProjectsFromOtherPools(input.getAgentPoolRawId(), Collections.singleton(project.getProjectId()));
    }
    AgentPool agentPool = myAgentPoolManager.findAgentPoolById(input.getAgentPoolRawId());
    if (agentPool == null) {
        LOG.warn(String.format("Agent pool with id=%d is missing after associating project id=%s", input.getAgentPoolRawId(), project.getProjectId()));
        return result.error(new UnexpectedServerGraphQLError("Agent pool with given id could not be found after operation.")).build();
    }
    return result.data(new AssignProjectWithAgentPoolPayload(new Project(project), new jetbrains.buildServer.server.graphql.model.agentPool.AgentPool(agentPool))).build();
}
Also used : UnexpectedServerGraphQLError(jetbrains.buildServer.server.graphql.util.UnexpectedServerGraphQLError) Project(jetbrains.buildServer.server.graphql.model.Project) DataFetcherResult(graphql.execution.DataFetcherResult) OperationFailedGraphQLError(jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError) EntityNotFoundGraphQLError(jetbrains.buildServer.server.graphql.util.EntityNotFoundGraphQLError) jetbrains.buildServer.server.graphql.model.mutation.agentPool(jetbrains.buildServer.server.graphql.model.mutation.agentPool) Used(jetbrains.buildServer.Used) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with OperationFailedGraphQLError

use of jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError 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 10 with OperationFailedGraphQLError

use of jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError in project teamcity-rest by JetBrains.

the class AgentPoolMutation method moveAgentToAgentPool.

@Used("graphql")
@NotNull
public DataFetcherResult<MoveAgentToAgentPoolPayload> moveAgentToAgentPool(@NotNull MoveAgentToAgentPoolInput input, @NotNull DataFetchingEnvironment env) {
    DataFetcherResult.Builder<MoveAgentToAgentPoolPayload> result = DataFetcherResult.newResult();
    int agentId = input.getAgentRawId();
    int targetPoolId = input.getTargetAgentPoolRawId();
    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();
    }
    int sourcePoolId = agent.getAgentPoolId();
    try {
        myAgentPoolManager.moveAgentToPool(targetPoolId, agent);
    } catch (NoSuchAgentPoolException e) {
        return result.error(new EntityNotFoundGraphQLError(String.format("Agent pool with id=%d is not found.", targetPoolId))).build();
    } catch (AgentTypeCannotBeMovedException e) {
        LOG.debug(e.getMessage());
        return result.error(new OperationFailedGraphQLError("Agent can't be moved.")).build();
    } catch (PoolQuotaExceededException e) {
        LOG.debug(e.getMessage());
        return result.error(new OperationFailedGraphQLError("Agent can't be moved, target agent pool is full.")).build();
    }
    AgentPool sourcePool = myAgentPoolManager.findAgentPoolById(sourcePoolId);
    AgentPool targetPool = myAgentPoolManager.findAgentPoolById(targetPoolId);
    // Strictly speaking, the same is true for an agent, but let's not bother.
    return result.data(new MoveAgentToAgentPoolPayload(new Agent(agent), sourcePool == null ? null : new jetbrains.buildServer.server.graphql.model.agentPool.AgentPool(sourcePool), targetPool == null ? null : new jetbrains.buildServer.server.graphql.model.agentPool.AgentPool(targetPool))).build();
}
Also used : Agent(jetbrains.buildServer.server.graphql.model.Agent) DataFetcherResult(graphql.execution.DataFetcherResult) OperationFailedGraphQLError(jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError) EntityNotFoundGraphQLError(jetbrains.buildServer.server.graphql.util.EntityNotFoundGraphQLError) Used(jetbrains.buildServer.Used) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

DataFetcherResult (graphql.execution.DataFetcherResult)12 Used (jetbrains.buildServer.Used)12 OperationFailedGraphQLError (jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError)12 NotNull (org.jetbrains.annotations.NotNull)12 EntityNotFoundGraphQLError (jetbrains.buildServer.server.graphql.util.EntityNotFoundGraphQLError)11 jetbrains.buildServer.server.graphql.model.mutation.agentPool (jetbrains.buildServer.server.graphql.model.mutation.agentPool)7 UnexpectedServerGraphQLError (jetbrains.buildServer.server.graphql.util.UnexpectedServerGraphQLError)6 CloudClientEx (jetbrains.buildServer.clouds.CloudClientEx)4 CloudProfile (jetbrains.buildServer.clouds.CloudProfile)4 Agent (jetbrains.buildServer.server.graphql.model.Agent)4 CloudImage (jetbrains.buildServer.server.graphql.model.CloudImage)4 Pair (com.intellij.openapi.util.Pair)3 GraphQLContext (jetbrains.buildServer.server.graphql.GraphQLContext)3 Project (jetbrains.buildServer.server.graphql.model.Project)3 AuthorityHolder (jetbrains.buildServer.serverSide.auth.AuthorityHolder)3 Logger (com.intellij.openapi.diagnostic.Logger)2 GraphQLMutationResolver (graphql.kickstart.tools.GraphQLMutationResolver)2 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2