use of jetbrains.buildServer.server.graphql.util.UnexpectedServerGraphQLError 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();
}
use of jetbrains.buildServer.server.graphql.util.UnexpectedServerGraphQLError 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();
}
use of jetbrains.buildServer.server.graphql.util.UnexpectedServerGraphQLError 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();
}
use of jetbrains.buildServer.server.graphql.util.UnexpectedServerGraphQLError 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 jetbrains.buildServer.server.graphql.util.UnexpectedServerGraphQLError 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();
}
Aggregations