Search in sources :

Example 1 with EnvironmentEntity

use of io.gravitee.rest.api.model.EnvironmentEntity in project gravitee-management-rest-api by gravitee-io.

the class EnvironmentResource method getEnvironmentPermissions.

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/permissions")
@ApiOperation(value = "Get permissions on environment")
@ApiResponses({ @ApiResponse(code = 200, message = "Current user permissions on environement", response = char[].class, responseContainer = "Map"), @ApiResponse(code = 500, message = "Internal server error") })
public Response getEnvironmentPermissions() {
    Map<String, char[]> permissions = new HashMap<>();
    if (isAuthenticated()) {
        final String username = getAuthenticatedUser();
        final EnvironmentEntity environmentEntity = environmentService.findById(envId);
        if (isAdmin()) {
            final char[] rights = new char[] { CREATE.getId(), READ.getId(), UPDATE.getId(), DELETE.getId() };
            for (EnvironmentPermission perm : EnvironmentPermission.values()) {
                permissions.put(perm.getName(), rights);
            }
        } else {
            permissions = membershipService.getUserMemberPermissions(environmentEntity, username);
        }
    }
    return Response.ok(permissions).build();
}
Also used : HashMap(java.util.HashMap) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) EnvironmentPermission(io.gravitee.rest.api.model.permissions.EnvironmentPermission)

Example 2 with EnvironmentEntity

use of io.gravitee.rest.api.model.EnvironmentEntity in project gravitee-management-rest-api by gravitee-io.

the class EnvironmentCommandHandler method handle.

@Override
public Single<EnvironmentReply> handle(EnvironmentCommand command) {
    EnvironmentPayload environmentPayload = command.getPayload();
    try {
        UpdateEnvironmentEntity newEnvironment = new UpdateEnvironmentEntity();
        newEnvironment.setCockpitId(environmentPayload.getCockpitId());
        newEnvironment.setHrids(environmentPayload.getHrids());
        newEnvironment.setName(environmentPayload.getName());
        newEnvironment.setDescription(environmentPayload.getDescription());
        newEnvironment.setDomainRestrictions(environmentPayload.getDomainRestrictions());
        final EnvironmentEntity environment = environmentService.createOrUpdate(environmentPayload.getOrganizationId(), environmentPayload.getId(), newEnvironment);
        logger.info("Environment [{}] handled with id [{}].", environment.getName(), environment.getId());
        return Single.just(new EnvironmentReply(command.getId(), CommandStatus.SUCCEEDED));
    } catch (Exception e) {
        logger.error("Error occurred when handling environment [{}] with id [{}].", environmentPayload.getName(), environmentPayload.getId(), e);
        return Single.just(new EnvironmentReply(command.getId(), CommandStatus.ERROR));
    }
}
Also used : UpdateEnvironmentEntity(io.gravitee.rest.api.model.UpdateEnvironmentEntity) EnvironmentReply(io.gravitee.cockpit.api.command.environment.EnvironmentReply) UpdateEnvironmentEntity(io.gravitee.rest.api.model.UpdateEnvironmentEntity) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) EnvironmentPayload(io.gravitee.cockpit.api.command.environment.EnvironmentPayload)

Example 3 with EnvironmentEntity

use of io.gravitee.rest.api.model.EnvironmentEntity in project gravitee-management-rest-api by gravitee-io.

the class ListEnvironmentOperationHandler method handle.

@Override
public Single<BridgeReply> handle(BridgeCommand bridgeCommand) {
    BridgeMultiReply multiReply = new BridgeMultiReply();
    multiReply.setCommandId(bridgeCommand.getId());
    try {
        final List<EnvironmentEntity> managedEnvironments = this.environmentService.findByOrganization(bridgeCommand.getOrganizationId());
        multiReply.setCommandStatus(CommandStatus.SUCCEEDED);
        multiReply.setReplies(managedEnvironments.stream().map(environmentEntity -> {
            BridgeSimpleReply simpleReply = new BridgeSimpleReply();
            simpleReply.setCommandId(bridgeCommand.getId());
            simpleReply.setCommandStatus(CommandStatus.SUCCEEDED);
            simpleReply.setOrganizationId(environmentEntity.getOrganizationId());
            simpleReply.setEnvironmentId(environmentEntity.getId());
            simpleReply.setInstallationId(installationService.get().getId());
            try {
                simpleReply.setPayload(objectMapper.writeValueAsString(environmentEntity));
            } catch (JsonProcessingException e) {
                logger.warn("Problem while serializing environment {}", environmentEntity.getId());
                simpleReply.setMessage("Problem while serializing environment: " + environmentEntity.getId());
                simpleReply.setCommandStatus(CommandStatus.ERROR);
            }
            return simpleReply;
        }).collect(Collectors.toList()));
    } catch (TechnicalManagementException ex) {
        multiReply.setCommandStatus(CommandStatus.ERROR);
        multiReply.setMessage("No environment available for organization: " + bridgeCommand.getOrganizationId());
    }
    return Single.just(multiReply);
}
Also used : BridgeMultiReply(io.gravitee.cockpit.api.command.bridge.BridgeMultiReply) BridgeSimpleReply(io.gravitee.cockpit.api.command.bridge.BridgeSimpleReply) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 4 with EnvironmentEntity

use of io.gravitee.rest.api.model.EnvironmentEntity in project gravitee-management-rest-api by gravitee-io.

the class PromotionServiceImpl method listPromotionTargets.

@Override
public List<PromotionTargetEntity> listPromotionTargets(String organizationId, String environmentId) {
    EnvironmentEntity environmentEntity = environmentService.findById(environmentId);
    final CockpitReply<List<PromotionTargetEntity>> listCockpitReply = this.cockpitService.listPromotionTargets(organizationId);
    if (listCockpitReply.getStatus() == CockpitReplyStatus.SUCCEEDED) {
        return listCockpitReply.getReply().stream().filter(target -> !target.getId().equals(environmentEntity.getCockpitId())).collect(Collectors.toList());
    }
    throw new BridgeOperationException(BridgeOperation.LIST_ENVIRONMENT);
}
Also used : PROMOTION_CREATED(io.gravitee.repository.management.model.Promotion.AuditEvent.PROMOTION_CREATED) io.gravitee.rest.api.service(io.gravitee.rest.api.service) CockpitService(io.gravitee.rest.api.service.cockpit.services.CockpitService) Pageable(io.gravitee.rest.api.model.common.Pageable) Date(java.util.Date) Page(io.gravitee.common.data.domain.Page) Sortable(io.gravitee.rest.api.model.common.Sortable) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Order(io.gravitee.repository.management.api.search.Order) CockpitReplyStatus(io.gravitee.rest.api.service.cockpit.services.CockpitReplyStatus) LoggerFactory(org.slf4j.LoggerFactory) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) UPDATE(io.gravitee.rest.api.model.permissions.RolePermissionAction.UPDATE) Collections.singletonList(java.util.Collections.singletonList) SortableImpl(io.gravitee.rest.api.model.common.SortableImpl) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) AbstractService(io.gravitee.rest.api.service.impl.AbstractService) PromotionCriteria(io.gravitee.repository.management.api.search.PromotionCriteria) Collections.emptyMap(java.util.Collections.emptyMap) UuidString(io.gravitee.rest.api.service.common.UuidString) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Logger(org.slf4j.Logger) ENVIRONMENT_API(io.gravitee.rest.api.model.permissions.RolePermission.ENVIRONMENT_API) Promotion(io.gravitee.repository.management.model.Promotion) BridgeOperation(io.gravitee.rest.api.service.cockpit.command.bridge.operation.BridgeOperation) CockpitReply(io.gravitee.rest.api.service.cockpit.services.CockpitReply) PromotionService(io.gravitee.rest.api.service.promotion.PromotionService) Collectors(java.util.stream.Collectors) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) io.gravitee.rest.api.model.promotion(io.gravitee.rest.api.model.promotion) List(java.util.List) Component(org.springframework.stereotype.Component) PromotionStatus(io.gravitee.repository.management.model.PromotionStatus) SortableBuilder(io.gravitee.repository.management.api.search.builder.SortableBuilder) io.gravitee.rest.api.service.exceptions(io.gravitee.rest.api.service.exceptions) CollectionUtils(org.springframework.util.CollectionUtils) CREATE(io.gravitee.rest.api.model.permissions.RolePermissionAction.CREATE) PromotionRepository(io.gravitee.repository.management.api.PromotionRepository) Optional(java.util.Optional) PromotionAuthor(io.gravitee.repository.management.model.PromotionAuthor) ApiSerializer(io.gravitee.rest.api.service.jackson.ser.api.ApiSerializer) Collections(java.util.Collections) UserEntity(io.gravitee.rest.api.model.UserEntity) StringUtils(org.springframework.util.StringUtils) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List)

Example 5 with EnvironmentEntity

use of io.gravitee.rest.api.model.EnvironmentEntity in project gravitee-management-rest-api by gravitee-io.

the class CockpitServiceTest method shouldListEnvironmentsFromSuccessfulReplies.

@Test
public void shouldListEnvironmentsFromSuccessfulReplies() throws JsonProcessingException {
    // Given
    EnvironmentEntity envA = new EnvironmentEntity();
    envA.setId("my-env-A");
    envA.setOrganizationId(ORGANIZATION_ID);
    envA.setName("ENV A");
    BridgeSimpleReply envASimpleReply = new BridgeSimpleReply();
    envASimpleReply.setCommandStatus(CommandStatus.SUCCEEDED);
    envASimpleReply.setInstallationId(INSTALLATION_ID);
    envASimpleReply.setOrganizationId(ORGANIZATION_ID);
    envASimpleReply.setEnvironmentId(envA.getId());
    envASimpleReply.setPayload(objectMapper.writeValueAsString(envA));
    EnvironmentEntity envB = new EnvironmentEntity();
    envB.setId("my-env-B");
    envB.setOrganizationId(ORGANIZATION_ID);
    envB.setName("ENV B");
    BridgeSimpleReply envBSimpleReply = new BridgeSimpleReply();
    envBSimpleReply.setCommandStatus(CommandStatus.SUCCEEDED);
    envBSimpleReply.setInstallationId(INSTALLATION_ID);
    envBSimpleReply.setOrganizationId(ORGANIZATION_ID);
    envBSimpleReply.setEnvironmentId(envB.getId());
    envBSimpleReply.setPayload(objectMapper.writeValueAsString(envB));
    EnvironmentEntity envC_ERROR = new EnvironmentEntity();
    envC_ERROR.setId("my-env-C");
    envC_ERROR.setOrganizationId(ORGANIZATION_ID);
    envC_ERROR.setName("ENV C");
    BridgeSimpleReply envCSimpleReply = new BridgeSimpleReply();
    envCSimpleReply.setCommandStatus(CommandStatus.ERROR);
    envCSimpleReply.setInstallationId(INSTALLATION_ID);
    envCSimpleReply.setOrganizationId(ORGANIZATION_ID);
    envCSimpleReply.setEnvironmentId(envC_ERROR.getId());
    envCSimpleReply.setMessage("Problem while serializing environment: " + envC_ERROR.getId());
    BridgeMultiReply environmentsMultiReply = new BridgeMultiReply();
    environmentsMultiReply.setCommandStatus(CommandStatus.SUCCEEDED);
    environmentsMultiReply.setReplies(Arrays.asList(envASimpleReply, envBSimpleReply, envCSimpleReply));
    when(cockpitCommandService.send(any())).thenReturn(environmentsMultiReply);
    // When
    final CockpitReply<List<PromotionTargetEntity>> listCockpitReply = cockpitService.listPromotionTargets(ORGANIZATION_ID);
    // Then
    assertThat(listCockpitReply).isNotNull();
    assertThat(listCockpitReply.getStatus()).isEqualTo(CockpitReplyStatus.SUCCEEDED);
    final List<PromotionTargetEntity> environmentEntities = listCockpitReply.getReply();
    assertThat(environmentEntities).isNotNull();
    assertThat(environmentEntities).isNotEmpty();
    assertThat(environmentEntities.size()).isEqualTo(2);
}
Also used : BridgeMultiReply(io.gravitee.cockpit.api.command.bridge.BridgeMultiReply) BridgeSimpleReply(io.gravitee.cockpit.api.command.bridge.BridgeSimpleReply) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) List(java.util.List) PromotionTargetEntity(io.gravitee.rest.api.model.promotion.PromotionTargetEntity) Test(org.junit.Test)

Aggregations

EnvironmentEntity (io.gravitee.rest.api.model.EnvironmentEntity)33 Test (org.junit.Test)21 Page (io.gravitee.common.data.domain.Page)10 Promotion (io.gravitee.repository.management.model.Promotion)10 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)8 VirtualHost (io.gravitee.definition.model.VirtualHost)6 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)6 CockpitReply (io.gravitee.rest.api.service.cockpit.services.CockpitReply)6 List (java.util.List)6 Collections (java.util.Collections)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 BridgeMultiReply (io.gravitee.cockpit.api.command.bridge.BridgeMultiReply)4 UpdateEnvironmentEntity (io.gravitee.rest.api.model.UpdateEnvironmentEntity)4 RunWith (org.junit.runner.RunWith)4 Mock (org.mockito.Mock)4 MockitoJUnitRunner (org.mockito.junit.MockitoJUnitRunner)4 CommandStatus (io.gravitee.cockpit.api.command.CommandStatus)3 BridgeSimpleReply (io.gravitee.cockpit.api.command.bridge.BridgeSimpleReply)3 SortableImpl (io.gravitee.rest.api.model.common.SortableImpl)3 Arrays (java.util.Arrays)3