use of graphql.execution.DataFetcherResult 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();
}
use of graphql.execution.DataFetcherResult 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();
}
use of graphql.execution.DataFetcherResult 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();
}
use of graphql.execution.DataFetcherResult 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();
}
use of graphql.execution.DataFetcherResult in project teamcity-rest by JetBrains.
the class CloudImageResolver method project.
@NotNull
public DataFetcherResult<Project> project(@NotNull CloudImage image, @NotNull DataFetchingEnvironment env) {
String projectId = image.getRealProfile().getProjectId();
SProject project;
if (SProject.ROOT_PROJECT_ID.equals(projectId)) {
project = myProjectManager.getRootProject();
} else {
project = myProjectManager.findProjectById(projectId);
}
DataFetcherResult.Builder<Project> result = new DataFetcherResult.Builder<>();
if (project == null) {
result.error(new EntityNotFoundGraphQLError(String.format("Could not find project for instance id=%s", image.getRawId())));
} else {
result.data(new Project(project)).localContext(project);
}
return result.build();
}
Aggregations