use of jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError in project teamcity-rest by JetBrains.
the class AgentPoolMutation method bulkMoveCloudImagesToAgentPool.
@Used("graphql")
@NotNull
public DataFetcherResult<BulkMoveCloudImagesToAgentPoolPayload> bulkMoveCloudImagesToAgentPool(@NotNull BulkMoveCloudImagesToAgentPoolInput input) {
DataFetcherResult.Builder<BulkMoveCloudImagesToAgentPoolPayload> result = DataFetcherResult.newResult();
final int targetPoolId = input.getTargetAgentPoolRawId();
AgentPool targetPool = myAgentPoolManager.findAgentPoolById(targetPoolId);
if (targetPool == null) {
return result.error(new EntityNotFoundGraphQLError("Target agent pool is not found.")).build();
}
// agentTypeId -> CloudClient, AgentTypeKey
Map<Integer, Pair<CloudClientEx, AgentTypeKey>> cloudClientsAndTypeKeys = new HashMap<>();
input.getAgentTypeRawIds().forEach(agentTypeId -> {
SAgentType agentType = myAgentTypeFinder.findAgentType(agentTypeId);
if (agentType == null) {
result.error(new EntityNotFoundGraphQLError(String.format("Cloud image with agent type=%d does not exist.", agentTypeId)));
return;
}
if (!agentType.isCloud()) {
result.error(new OperationFailedGraphQLError(String.format("Agent type=%d does not correspond to a cloud agent.", agentTypeId)));
return;
}
final AgentTypeKey typeKey = agentType.getAgentTypeKey();
CloudProfile profile = myCloudManager.findProfileGloballyById(typeKey.getProfileId());
if (profile == null) {
result.error(new UnexpectedServerGraphQLError(String.format("Cloud profile with id=%s does not exist.", typeKey.getProfileId())));
return;
}
CloudClientEx client = myCloudManager.getClient(profile.getProjectId(), profile.getProfileId());
cloudClientsAndTypeKeys.put(agentTypeId, new Pair<>(client, typeKey));
});
try {
myAgentPoolManager.moveAgentTypesToPool(targetPoolId, cloudClientsAndTypeKeys.keySet());
} 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("Some of the images 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();
}
List<CloudImage> cloudImages = new ArrayList<>(cloudClientsAndTypeKeys.size());
cloudClientsAndTypeKeys.forEach((agentTypeId, clientAndTypeKey) -> {
CloudClientEx client = clientAndTypeKey.getFirst();
AgentTypeKey typeKey = clientAndTypeKey.getSecond();
jetbrains.buildServer.clouds.CloudImage image = client.findImageById(typeKey.getTypeId());
CloudProfile profile = myCloudManager.findProfileGloballyById(typeKey.getProfileId());
if (image != null && profile != null) {
cloudImages.add(new CloudImage(image, profile));
}
});
return result.data(new BulkMoveCloudImagesToAgentPoolPayload(cloudImages, new jetbrains.buildServer.server.graphql.model.agentPool.AgentPool(targetPool))).build();
}
use of jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError 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();
}
Aggregations