Search in sources :

Example 1 with GraphQLContext

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

the class Mutation method unauthorizeAgent.

@Used("graphql")
@NotNull
public DataFetcherResult<UnauthorizeAgentPayload> unauthorizeAgent(@NotNull UnauthorizeAgentInput input, @NotNull DataFetchingEnvironment dfe) {
    return runWithAgent(input.getAgentRawId(), agent -> {
        DataFetcherResult.Builder<UnauthorizeAgentPayload> result = DataFetcherResult.newResult();
        GraphQLContext context = dfe.getContext();
        String authReason = input.getReason() == null ? "" : input.getReason();
        try {
            agent.setAuthorized(false, context.getUser(), authReason);
        } catch (LicenseNotGrantedException e) {
            return result.error(new OperationFailedGraphQLError(e.getMessage())).build();
        }
        Agent agentModel = new Agent(agent);
        return result.data(new UnauthorizeAgentPayload(agentModel)).build();
    });
}
Also used : DataFetcherResult(graphql.execution.DataFetcherResult) LicenseNotGrantedException(jetbrains.buildServer.LicenseNotGrantedException) OperationFailedGraphQLError(jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError) GraphQLContext(jetbrains.buildServer.server.graphql.GraphQLContext) Used(jetbrains.buildServer.Used) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with GraphQLContext

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

the class Query method globalPermissions.

@NotNull
public GlobalPermissions globalPermissions(@NotNull DataFetchingEnvironment env) {
    GraphQLContext ctx = env.getContext();
    SUser user = ctx.getUser();
    if (user == null) {
        return new GlobalPermissions(false);
    }
    return new GlobalPermissions(user.getGlobalPermissions().contains(Permission.MANAGE_AGENT_POOLS));
}
Also used : GlobalPermissions(jetbrains.buildServer.server.graphql.model.GlobalPermissions) SUser(jetbrains.buildServer.users.SUser) GraphQLContext(jetbrains.buildServer.server.graphql.GraphQLContext) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GraphQLContext

use of jetbrains.buildServer.server.graphql.GraphQLContext 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 GraphQLContext

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

Example 5 with GraphQLContext

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

the class ProjectResolver method permissions.

@NotNull
public ProjectPermissions permissions(@NotNull Project source, @NotNull DataFetchingEnvironment env) {
    GraphQLContext ctx = env.getContext();
    SUser user = ctx.getUser();
    if (user == null) {
        return new ProjectPermissions(false);
    }
    SProject self = source.getRealProject();
    return new ProjectPermissions(AuthUtil.hasPermissionToManageAgentPoolsWithProject(user, self.getProjectId()));
}
Also used : SUser(jetbrains.buildServer.users.SUser) ProjectPermissions(jetbrains.buildServer.server.graphql.model.ProjectPermissions) GraphQLContext(jetbrains.buildServer.server.graphql.GraphQLContext) SProject(jetbrains.buildServer.serverSide.SProject) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GraphQLContext (jetbrains.buildServer.server.graphql.GraphQLContext)5 NotNull (org.jetbrains.annotations.NotNull)5 DataFetcherResult (graphql.execution.DataFetcherResult)3 Used (jetbrains.buildServer.Used)3 OperationFailedGraphQLError (jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError)3 LicenseNotGrantedException (jetbrains.buildServer.LicenseNotGrantedException)2 AbstractAgentPool (jetbrains.buildServer.server.graphql.model.agentPool.AbstractAgentPool)2 EntityNotFoundGraphQLError (jetbrains.buildServer.server.graphql.util.EntityNotFoundGraphQLError)2 SUser (jetbrains.buildServer.users.SUser)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 GlobalPermissions (jetbrains.buildServer.server.graphql.model.GlobalPermissions)1 ProjectPermissions (jetbrains.buildServer.server.graphql.model.ProjectPermissions)1 SProject (jetbrains.buildServer.serverSide.SProject)1