Search in sources :

Example 1 with CloudClientEx

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

the class AbstractAgentPoolResolver method cloudImages.

@NotNull
public AgentPoolCloudImagesConnection cloudImages(@NotNull AbstractAgentPool pool, @NotNull DataFetchingEnvironment env) {
    List<Pair<CloudProfile, CloudImage>> images = new ArrayList<>();
    for (SAgentType agentType : myAgentTypeFinder.getAgentTypesByPool(pool.getRealPool().getAgentPoolId())) {
        if (!agentType.isCloud())
            continue;
        AgentTypeKey targetAgentTypeKey = agentType.getAgentTypeKey();
        CloudProfile profile = myCloudManager.findProfileGloballyById(targetAgentTypeKey.getProfileId());
        if (profile == null)
            continue;
        CloudClientEx client = myCloudManager.getClient(profile.getProjectId(), profile.getProfileId());
        for (CloudImage image : client.getImages()) {
            SAgentType type = myCloudManager.getDescriptionFor(profile, image.getId());
            if (type == null)
                continue;
            if (targetAgentTypeKey.equals(type.getAgentTypeKey())) {
                images.add(new Pair<>(profile, image));
                break;
            }
        }
    }
    return new AgentPoolCloudImagesConnection(images, PaginationArguments.everything());
}
Also used : CloudProfile(jetbrains.buildServer.clouds.CloudProfile) SAgentType(jetbrains.buildServer.serverSide.agentTypes.SAgentType) CloudClientEx(jetbrains.buildServer.clouds.CloudClientEx) AgentPoolCloudImagesConnection(jetbrains.buildServer.server.graphql.model.connections.agentPool.AgentPoolCloudImagesConnection) AgentTypeKey(jetbrains.buildServer.serverSide.agentTypes.AgentTypeKey) ArrayList(java.util.ArrayList) CloudImage(jetbrains.buildServer.clouds.CloudImage) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with CloudClientEx

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

Example 3 with CloudClientEx

use of jetbrains.buildServer.clouds.CloudClientEx 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 4 with CloudClientEx

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

the class AgentPoolResolver method assignableCloudImages.

@NotNull
public AgentPoolCloudImagesConnection assignableCloudImages(@NotNull AgentPool pool, @NotNull DataFetchingEnvironment env) {
    AuthorityHolder authHolder = mySecurityContext.getAuthorityHolder();
    jetbrains.buildServer.serverSide.agentPools.AgentPool defaultPool = myPoolManager.findAgentPoolById(jetbrains.buildServer.serverSide.agentPools.AgentPool.DEFAULT_POOL_ID);
    if (!AuthUtil.hasGlobalOrPoolProjectsPermission(authHolder, defaultPool, Permission.MANAGE_AGENT_POOLS, Permission.MANAGE_AGENT_POOLS_FOR_PROJECT)) {
        return AgentPoolCloudImagesConnection.empty();
    }
    if (!myPoolActionsAccessChecker.canManageAgentsInPool(pool.getRealPool())) {
        return AgentPoolCloudImagesConnection.empty();
    }
    final Set<String> profileIdsInRootProject = myProjectManager.getRootProject().getOwnFeaturesOfType(CloudConstants.CLOUD_PROFILE_FEATURE_TYPE).stream().map(SProjectFeatureDescriptor::getId).collect(Collectors.toSet());
    List<Pair<CloudProfile, CloudImage>> images = new ArrayList<>();
    profileIdsInRootProject.forEach(profileId -> {
        CloudClientEx client = myCloudManager.getClientIfExists(BuildProject.ROOT_PROJECT_ID, profileId);
        CloudProfile profile = myCloudManager.findProfileById(BuildProject.ROOT_PROJECT_ID, profileId);
        if (client == null || profile == null)
            return;
        client.getImages().stream().filter(image -> {
            AgentTypeKey key = new AgentTypeKey(profile.getCloudCode(), profileId, image.getId());
            AgentType type = myAgentTypeManager.findAgentTypeByKey(key);
            return type != null && pool.getRealPool().getAgentPoolId() != type.getAgentPoolId();
        }).forEach(image -> {
            images.add(new Pair<>(profile, image));
        });
    });
    return new AgentPoolCloudImagesConnection(images, PaginationArguments.everything());
}
Also used : DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) CloudClientEx(jetbrains.buildServer.clouds.CloudClientEx) AgentTypeManager(jetbrains.buildServer.serverSide.agentTypes.AgentTypeManager) CloudProfile(jetbrains.buildServer.clouds.CloudProfile) AgentPool(jetbrains.buildServer.server.graphql.model.agentPool.AgentPool) AuthorityHolder(jetbrains.buildServer.serverSide.auth.AuthorityHolder) AgentPoolProjectsConnection(jetbrains.buildServer.server.graphql.model.connections.agentPool.AgentPoolProjectsConnection) PaginationArguments(jetbrains.buildServer.server.graphql.model.connections.PaginationArguments) ArrayList(java.util.ArrayList) AgentType(jetbrains.buildServer.serverSide.agentTypes.AgentType) CloudImage(jetbrains.buildServer.clouds.CloudImage) AgentTypeKey(jetbrains.buildServer.serverSide.agentTypes.AgentTypeKey) SecurityContext(jetbrains.buildServer.serverSide.auth.SecurityContext) AgentPoolAgentsConnection(jetbrains.buildServer.server.graphql.model.connections.agentPool.AgentPoolAgentsConnection) ProjectsConnection(jetbrains.buildServer.server.graphql.model.connections.ProjectsConnection) jetbrains.buildServer.serverSide(jetbrains.buildServer.serverSide) Set(java.util.Set) AgentPoolPermissions(jetbrains.buildServer.server.graphql.model.agentPool.AgentPoolPermissions) ModelResolver(jetbrains.buildServer.server.graphql.util.ModelResolver) Collectors(java.util.stream.Collectors) CloudManagerBase(jetbrains.buildServer.clouds.server.CloudManagerBase) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Component(org.springframework.stereotype.Component) ProjectsFilter(jetbrains.buildServer.server.graphql.model.filter.ProjectsFilter) Permission(jetbrains.buildServer.serverSide.auth.Permission) jetbrains.buildServer.server.graphql.model.agentPool.actions(jetbrains.buildServer.server.graphql.model.agentPool.actions) AgentPoolCloudImagesConnection(jetbrains.buildServer.server.graphql.model.connections.agentPool.AgentPoolCloudImagesConnection) Pair(com.intellij.openapi.util.Pair) AuthUtil(jetbrains.buildServer.serverSide.auth.AuthUtil) CloudConstants(jetbrains.buildServer.clouds.CloudConstants) AgentPoolManager(jetbrains.buildServer.serverSide.agentPools.AgentPoolManager) NotNull(org.jetbrains.annotations.NotNull) BuildProject(jetbrains.buildServer.BuildProject) CloudProfile(jetbrains.buildServer.clouds.CloudProfile) CloudClientEx(jetbrains.buildServer.clouds.CloudClientEx) AgentTypeKey(jetbrains.buildServer.serverSide.agentTypes.AgentTypeKey) ArrayList(java.util.ArrayList) AgentType(jetbrains.buildServer.serverSide.agentTypes.AgentType) AgentPoolCloudImagesConnection(jetbrains.buildServer.server.graphql.model.connections.agentPool.AgentPoolCloudImagesConnection) AuthorityHolder(jetbrains.buildServer.serverSide.auth.AuthorityHolder) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

CloudClientEx (jetbrains.buildServer.clouds.CloudClientEx)4 CloudProfile (jetbrains.buildServer.clouds.CloudProfile)4 NotNull (org.jetbrains.annotations.NotNull)4 Pair (com.intellij.openapi.util.Pair)3 DataFetcherResult (graphql.execution.DataFetcherResult)2 ArrayList (java.util.ArrayList)2 Used (jetbrains.buildServer.Used)2 CloudImage (jetbrains.buildServer.clouds.CloudImage)2 CloudImage (jetbrains.buildServer.server.graphql.model.CloudImage)2 AgentPoolCloudImagesConnection (jetbrains.buildServer.server.graphql.model.connections.agentPool.AgentPoolCloudImagesConnection)2 jetbrains.buildServer.server.graphql.model.mutation.agentPool (jetbrains.buildServer.server.graphql.model.mutation.agentPool)2 EntityNotFoundGraphQLError (jetbrains.buildServer.server.graphql.util.EntityNotFoundGraphQLError)2 OperationFailedGraphQLError (jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError)2 UnexpectedServerGraphQLError (jetbrains.buildServer.server.graphql.util.UnexpectedServerGraphQLError)2 AgentTypeKey (jetbrains.buildServer.serverSide.agentTypes.AgentTypeKey)2 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 BuildProject (jetbrains.buildServer.BuildProject)1