Search in sources :

Example 6 with UnexpectedServerGraphQLError

use of jetbrains.buildServer.server.graphql.util.UnexpectedServerGraphQLError 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();
}
Also used : CloudProfile(jetbrains.buildServer.clouds.CloudProfile) CloudImage(jetbrains.buildServer.server.graphql.model.CloudImage) DataFetcherResult(graphql.execution.DataFetcherResult) jetbrains.buildServer.server.graphql.model.mutation.agentPool(jetbrains.buildServer.server.graphql.model.mutation.agentPool) Pair(com.intellij.openapi.util.Pair) CloudClientEx(jetbrains.buildServer.clouds.CloudClientEx) UnexpectedServerGraphQLError(jetbrains.buildServer.server.graphql.util.UnexpectedServerGraphQLError) OperationFailedGraphQLError(jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError) EntityNotFoundGraphQLError(jetbrains.buildServer.server.graphql.util.EntityNotFoundGraphQLError) Used(jetbrains.buildServer.Used) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

DataFetcherResult (graphql.execution.DataFetcherResult)6 Used (jetbrains.buildServer.Used)6 jetbrains.buildServer.server.graphql.model.mutation.agentPool (jetbrains.buildServer.server.graphql.model.mutation.agentPool)6 EntityNotFoundGraphQLError (jetbrains.buildServer.server.graphql.util.EntityNotFoundGraphQLError)6 OperationFailedGraphQLError (jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError)6 UnexpectedServerGraphQLError (jetbrains.buildServer.server.graphql.util.UnexpectedServerGraphQLError)6 NotNull (org.jetbrains.annotations.NotNull)6 CloudClientEx (jetbrains.buildServer.clouds.CloudClientEx)4 CloudProfile (jetbrains.buildServer.clouds.CloudProfile)4 CloudImage (jetbrains.buildServer.server.graphql.model.CloudImage)4 Pair (com.intellij.openapi.util.Pair)3 Project (jetbrains.buildServer.server.graphql.model.Project)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 CloudManagerBase (jetbrains.buildServer.clouds.server.CloudManagerBase)2 Agent (jetbrains.buildServer.server.graphql.model.Agent)2 jetbrains.buildServer.server.graphql.model.mutation (jetbrains.buildServer.server.graphql.model.mutation)2