Search in sources :

Example 1 with EntityNotFoundGraphQLError

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

the class AgentPoolMutation method bulkAssignProjectWithAgentPool.

@Used("graphql")
@NotNull
public DataFetcherResult<BulkAssignProjectWithAgentPoolPayload> bulkAssignProjectWithAgentPool(@NotNull BulkAssignProjectWithAgentPoolInput input) {
    DataFetcherResult.Builder<BulkAssignProjectWithAgentPoolPayload> result = DataFetcherResult.newResult();
    if (!myAgentPoolActionsAccessChecker.canManageProjectsInPool(input.getAgentPoolRawId())) {
        return result.error(new OperationFailedGraphQLError("Can't assign projects, not enough permissions to manage projects in target pool.")).build();
    }
    Set<String> projectIds = myProjectManager.findProjectsByExternalIds(input.getProjectRawIds()).stream().map(p -> p.getProjectId()).collect(Collectors.toSet());
    try {
        myAgentPoolManager.associateProjectsWithPool(input.getAgentPoolRawId(), projectIds);
    } catch (NoSuchAgentPoolException e) {
        return result.error(new EntityNotFoundGraphQLError("Agent pool with given id does not exist.")).build();
    }
    if (input.getExclusively()) {
        myAgentPoolManager.dissociateProjectsFromOtherPools(input.getAgentPoolRawId(), projectIds);
    }
    AgentPool agentPool = myAgentPoolManager.findAgentPoolById(input.getAgentPoolRawId());
    if (agentPool == null) {
        LOG.warn(String.format("Agent pool with id=%d is missing after bulk association request", input.getAgentPoolRawId()));
        return result.error(new UnexpectedServerGraphQLError("Agent pool with given id could not be found after operation.")).build();
    }
    return result.data(new BulkAssignProjectWithAgentPoolPayload(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) 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 2 with EntityNotFoundGraphQLError

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

the class AgentPoolMutation method updateAgentPool.

@Used("graphql")
@NotNull
public DataFetcherResult<UpdateAgentPoolPayload> updateAgentPool(@NotNull UpdateAgentPoolInput input) {
    DataFetcherResult.Builder<UpdateAgentPoolPayload> result = DataFetcherResult.newResult();
    int poolId = input.getRawId();
    AgentPool poolOfInterest = myAgentPoolManager.findAgentPoolById(poolId);
    if (poolOfInterest == null) {
        return result.error(new EntityNotFoundGraphQLError("Pool with given id does not exist.")).build();
    }
    int maxAgents = input.getMaxAgentsNumber() == null ? poolOfInterest.getMaxAgents() : input.getMaxAgentsNumber();
    String name = input.getName() == null ? poolOfInterest.getName() : input.getName();
    try {
        myAgentPoolManager.updateAgentPool(poolId, name, new AgentPoolLimitsImpl(AgentPoolLimits.DEFAULT.getMinAgents(), maxAgents));
    } catch (AgentPoolCannotBeRenamedException e) {
        LOG.debug(e);
        return result.data(new UpdateAgentPoolPayload(new jetbrains.buildServer.server.graphql.model.agentPool.AgentPool(poolOfInterest))).error(new OperationFailedGraphQLError(e.getMessage())).build();
    } catch (NoSuchAgentPoolException e) {
        return result.error(new EntityNotFoundGraphQLError("Pool with given id does not exist.")).build();
    }
    AgentPool updatedPool = myAgentPoolManager.findAgentPoolById(poolId);
    if (updatedPool == null) {
        LOG.warn(String.format("Agent pool with id=%d is missing right after update operation.", poolId));
        return result.error(new UnexpectedServerGraphQLError("Pool is missing after update.")).build();
    }
    result.data(new UpdateAgentPoolPayload(new jetbrains.buildServer.server.graphql.model.agentPool.AgentPool(updatedPool)));
    return result.build();
}
Also used : UnexpectedServerGraphQLError(jetbrains.buildServer.server.graphql.util.UnexpectedServerGraphQLError) 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 3 with EntityNotFoundGraphQLError

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

the class AgentPoolMutation method removeAgentPool.

@Used("graphql")
@NotNull
public DataFetcherResult<RemoveAgentPoolPayload> removeAgentPool(@NotNull RemoveAgentPoolInput input) {
    DataFetcherResult.Builder<RemoveAgentPoolPayload> result = DataFetcherResult.newResult();
    int poolId = input.getAgentPoolRawId();
    try {
        AgentPool removedPool = myAgentPoolManager.deleteAgentPool(poolId);
        return result.data(new RemoveAgentPoolPayload(new ShallowAgentPool(poolId, removedPool.getName()))).build();
    } catch (NoSuchAgentPoolException e) {
        return result.error(new EntityNotFoundGraphQLError("Pool with given id does not exist.")).build();
    } catch (AgentPoolCannotBeDeletedException e) {
        LOG.debug(e.getMessage());
        return result.error(new OperationFailedGraphQLError(e.getMessage())).build();
    }
}
Also used : 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)

Example 4 with EntityNotFoundGraphQLError

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

the class AgentPoolMutation method moveCloudImageToAgentPool.

@Used("graphql")
@NotNull
public DataFetcherResult<MoveCloudImageToAgentPoolPayload> moveCloudImageToAgentPool(@NotNull MoveCloudImageToAgentPoolInput input) {
    DataFetcherResult.Builder<MoveCloudImageToAgentPoolPayload> result = DataFetcherResult.newResult();
    final int targetPoolId = input.getTargetAgentPoolRawId();
    SAgentType agentType = myAgentTypeFinder.findAgentType(input.getAgentTypeRawId());
    if (agentType == null) {
        return result.error(new EntityNotFoundGraphQLError(String.format("Cloud image with agent type=%d does not exist.", input.getAgentTypeRawId()))).build();
    }
    final int sourcePoolId = agentType.getAgentPoolId();
    if (!agentType.isCloud()) {
        return result.error(new OperationFailedGraphQLError(String.format("Agent type=%d does not correspond to a cloud agent.", input.getAgentTypeRawId()))).build();
    }
    final AgentTypeKey typeKey = agentType.getAgentTypeKey();
    CloudProfile profile = myCloudManager.findProfileGloballyById(typeKey.getProfileId());
    if (profile == null) {
        return result.error(new UnexpectedServerGraphQLError(String.format("Cloud profile with id=%s does not exist.", typeKey.getProfileId()))).build();
    }
    CloudClientEx client = myCloudManager.getClient(profile.getProjectId(), profile.getProfileId());
    try {
        myAgentPoolManager.moveAgentTypesToPool(targetPoolId, Collections.singleton(agentType.getAgentTypeId()));
    } 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("Image can't be moved.")).build();
    } catch (PoolQuotaExceededException e) {
        LOG.debug(e.getMessage());
        return result.error(new OperationFailedGraphQLError("Image can't be moved, target agent pool is full.")).build();
    }
    AgentPool sourcePool = myAgentPoolManager.findAgentPoolById(sourcePoolId);
    AgentPool targetPool = myAgentPoolManager.findAgentPoolById(targetPoolId);
    jetbrains.buildServer.clouds.CloudImage image = client.findImageById(typeKey.getTypeId());
    return result.data(new MoveCloudImageToAgentPoolPayload(new CloudImage(image, profile), new jetbrains.buildServer.server.graphql.model.agentPool.AgentPool(sourcePool), new jetbrains.buildServer.server.graphql.model.agentPool.AgentPool(targetPool))).build();
}
Also used : CloudProfile(jetbrains.buildServer.clouds.CloudProfile) CloudClientEx(jetbrains.buildServer.clouds.CloudClientEx) UnexpectedServerGraphQLError(jetbrains.buildServer.server.graphql.util.UnexpectedServerGraphQLError) CloudImage(jetbrains.buildServer.server.graphql.model.CloudImage) 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 5 with EntityNotFoundGraphQLError

use of jetbrains.buildServer.server.graphql.util.EntityNotFoundGraphQLError 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)

Aggregations

DataFetcherResult (graphql.execution.DataFetcherResult)16 EntityNotFoundGraphQLError (jetbrains.buildServer.server.graphql.util.EntityNotFoundGraphQLError)16 NotNull (org.jetbrains.annotations.NotNull)16 Used (jetbrains.buildServer.Used)13 OperationFailedGraphQLError (jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError)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 Project (jetbrains.buildServer.server.graphql.model.Project)3 AbstractAgentPool (jetbrains.buildServer.server.graphql.model.agentPool.AbstractAgentPool)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