Search in sources :

Example 11 with Used

use of jetbrains.buildServer.Used in project teamcity-rest by JetBrains.

the class AgentPoolMutation method moveAgentToAgentPool.

@Used("graphql")
@NotNull
public DataFetcherResult<MoveAgentToAgentPoolPayload> moveAgentToAgentPool(@NotNull MoveAgentToAgentPoolInput input, @NotNull DataFetchingEnvironment env) {
    DataFetcherResult.Builder<MoveAgentToAgentPoolPayload> result = DataFetcherResult.newResult();
    int agentId = input.getAgentRawId();
    int targetPoolId = input.getTargetAgentPoolRawId();
    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();
    }
    int sourcePoolId = agent.getAgentPoolId();
    try {
        myAgentPoolManager.moveAgentToPool(targetPoolId, agent);
    } 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("Agent can't be moved.")).build();
    } catch (PoolQuotaExceededException e) {
        LOG.debug(e.getMessage());
        return result.error(new OperationFailedGraphQLError("Agent can't be moved, target agent pool is full.")).build();
    }
    AgentPool sourcePool = myAgentPoolManager.findAgentPoolById(sourcePoolId);
    AgentPool targetPool = myAgentPoolManager.findAgentPoolById(targetPoolId);
    // Strictly speaking, the same is true for an agent, but let's not bother.
    return result.data(new MoveAgentToAgentPoolPayload(new Agent(agent), sourcePool == null ? null : new jetbrains.buildServer.server.graphql.model.agentPool.AgentPool(sourcePool), targetPool == null ? null : new jetbrains.buildServer.server.graphql.model.agentPool.AgentPool(targetPool))).build();
}
Also used : Agent(jetbrains.buildServer.server.graphql.model.Agent) DataFetcherResult(graphql.execution.DataFetcherResult) OperationFailedGraphQLError(jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError) EntityNotFoundGraphQLError(jetbrains.buildServer.server.graphql.util.EntityNotFoundGraphQLError) Used(jetbrains.buildServer.Used) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with Used

use of jetbrains.buildServer.Used 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)

Example 13 with Used

use of jetbrains.buildServer.Used in project teamcity-rest by JetBrains.

the class Mutation method unassignBuildTypeFromAgent.

@Used("graphql")
@NotNull
public DataFetcherResult<UnassignBuildTypeFromAgentPayload> unassignBuildTypeFromAgent(@NotNull UnassignBuildTypeFromAgentInput input) {
    return runWithAgent(input.getAgentRawId(), agent -> {
        DataFetcherResult.Builder<UnassignBuildTypeFromAgentPayload> result = DataFetcherResult.newResult();
        SBuildType bt = myBuildTypeFinder.getItem("id:" + input.getBuildTypeRawId()).getBuildType();
        if (bt == null) {
            final String errorMessage = String.format("Build type with id=%s is not found.", input.getBuildTypeRawId());
            return result.error(new EntityNotFoundGraphQLError(errorMessage)).build();
        }
        myAgentTypeManager.excludeRunConfigurationsFromAllowed(agent.getAgentTypeId(), new String[] { bt.getInternalId() });
        return result.data(new UnassignBuildTypeFromAgentPayload(new Agent(agent), new BuildType(bt))).build();
    });
}
Also used : DataFetcherResult(graphql.execution.DataFetcherResult) BuildType(jetbrains.buildServer.server.graphql.model.buildType.BuildType) EntityNotFoundGraphQLError(jetbrains.buildServer.server.graphql.util.EntityNotFoundGraphQLError) Used(jetbrains.buildServer.Used) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with Used

use of jetbrains.buildServer.Used 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 15 with Used

use of jetbrains.buildServer.Used in project teamcity-aws-core-plugin by JetBrains.

the class S3Util method createDefaultExecutorService.

@Used("code-deploy-plugin")
public static ExecutorService createDefaultExecutorService(final int nThreads) {
    final ThreadFactory threadFactory = new ThreadFactory() {

        private final AtomicInteger threadCount = new AtomicInteger(1);

        public Thread newThread(@NotNull Runnable r) {
            Thread thread = new Thread(r);
            thread.setName("amazon-util-s3-transfer-manager-worker-" + threadCount.getAndIncrement());
            thread.setContextClassLoader(getClass().getClassLoader());
            return thread;
        }
    };
    return Executors.newFixedThreadPool(nThreads, threadFactory);
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NotNull(org.jetbrains.annotations.NotNull) Used(jetbrains.buildServer.Used)

Aggregations

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