Search in sources :

Example 16 with EnvironmentEntity

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

the class VirtualHostServiceImpl method validateDomainRestrictions.

private void validateDomainRestrictions(Collection<VirtualHost> virtualHosts) {
    final EnvironmentEntity currentEnv = environmentService.findById(GraviteeContext.getCurrentEnvironment());
    final List<String> domainRestrictions = currentEnv.getDomainRestrictions();
    if (domainRestrictions != null && !domainRestrictions.isEmpty()) {
        for (VirtualHost vHost : virtualHosts) {
            String host = vHost.getHost();
            if (!StringUtils.isEmpty(host)) {
                String hostWithoutPort = host.split(":")[0];
                if (!isValidDomainOrSubDomain(hostWithoutPort, domainRestrictions)) {
                    throw new InvalidVirtualHostException(hostWithoutPort, domainRestrictions);
                }
            } else {
                vHost.setHost(domainRestrictions.get(0));
            }
        }
    }
}
Also used : EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) InvalidVirtualHostException(io.gravitee.rest.api.service.exceptions.InvalidVirtualHostException) VirtualHost(io.gravitee.definition.model.VirtualHost)

Example 17 with EnvironmentEntity

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

the class EnvironmentService_CreateTest method shouldCreateEnvironment.

@Test
public void shouldCreateEnvironment() throws TechnicalException {
    when(mockOrganizationService.findById(any())).thenReturn(null);
    when(mockEnvironmentRepository.findById(any())).thenReturn(Optional.empty());
    UpdateEnvironmentEntity env1 = new UpdateEnvironmentEntity();
    env1.setHrids(Arrays.asList("envhrid"));
    env1.setName("env_name");
    env1.setDescription("env_desc");
    List<String> domainRestrictions = Arrays.asList("domain", "restriction");
    env1.setDomainRestrictions(domainRestrictions);
    Environment createdEnv = new Environment();
    createdEnv.setId("env_id");
    when(mockEnvironmentRepository.create(any())).thenReturn(createdEnv);
    EnvironmentEntity environment = environmentService.createOrUpdate("DEFAULT", "env_id", env1);
    assertNotNull("result is null", environment);
    verify(mockEnvironmentRepository, times(1)).create(argThat(arg -> arg != null && arg.getHrids().equals(Arrays.asList("envhrid")) && arg.getName().equals("env_name") && arg.getDescription().equals("env_desc") && arg.getDomainRestrictions().equals(domainRestrictions) && arg.getOrganizationId().equals("DEFAULT")));
    verify(mockEnvironmentRepository, never()).update(any());
    verify(mockAPIHeaderService, times(1)).initialize("env_id");
    verify(mockPageService, times(1)).initialize("env_id");
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) InjectMocks(org.mockito.InjectMocks) Arrays(java.util.Arrays) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) UpdateEnvironmentEntity(io.gravitee.rest.api.model.UpdateEnvironmentEntity) Assert.assertNotNull(org.junit.Assert.assertNotNull) Mock(org.mockito.Mock) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) EnvironmentRepository(io.gravitee.repository.management.api.EnvironmentRepository) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) Mockito(org.mockito.Mockito) List(java.util.List) OrganizationNotFoundException(io.gravitee.rest.api.service.exceptions.OrganizationNotFoundException) BadOrganizationException(io.gravitee.rest.api.service.exceptions.BadOrganizationException) Environment(io.gravitee.repository.management.model.Environment) Optional(java.util.Optional) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) EnvironmentServiceImpl(io.gravitee.rest.api.service.impl.EnvironmentServiceImpl) UpdateEnvironmentEntity(io.gravitee.rest.api.model.UpdateEnvironmentEntity) UpdateEnvironmentEntity(io.gravitee.rest.api.model.UpdateEnvironmentEntity) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) Environment(io.gravitee.repository.management.model.Environment) Test(org.junit.Test)

Example 18 with EnvironmentEntity

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

the class EnvironmentService_CreateTest method shouldUpdateEnvironment.

@Test
public void shouldUpdateEnvironment() throws TechnicalException {
    when(mockOrganizationService.findById(any())).thenReturn(null);
    when(mockEnvironmentRepository.findById(any())).thenReturn(Optional.of(new Environment()));
    UpdateEnvironmentEntity env1 = new UpdateEnvironmentEntity();
    env1.setHrids(Arrays.asList("envhrid"));
    env1.setName("env_name");
    env1.setDescription("env_desc");
    List<String> domainRestrictions = Arrays.asList("domain", "restriction");
    env1.setDomainRestrictions(domainRestrictions);
    Environment updatedEnv = new Environment();
    when(mockEnvironmentRepository.update(any())).thenReturn(updatedEnv);
    EnvironmentEntity environment = environmentService.createOrUpdate("DEFAULT", "env_id", env1);
    assertNotNull("result is null", environment);
    verify(mockEnvironmentRepository, times(1)).update(argThat(arg -> arg != null && arg.getHrids().equals(Arrays.asList("envhrid")) && arg.getName().equals("env_name") && arg.getDescription().equals("env_desc") && arg.getDomainRestrictions().equals(domainRestrictions) && arg.getOrganizationId().equals("DEFAULT")));
    verify(mockEnvironmentRepository, never()).create(any());
    verify(mockAPIHeaderService, never()).initialize("env_id");
    verify(mockPageService, never()).initialize("env_id");
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) InjectMocks(org.mockito.InjectMocks) Arrays(java.util.Arrays) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) UpdateEnvironmentEntity(io.gravitee.rest.api.model.UpdateEnvironmentEntity) Assert.assertNotNull(org.junit.Assert.assertNotNull) Mock(org.mockito.Mock) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) EnvironmentRepository(io.gravitee.repository.management.api.EnvironmentRepository) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) Mockito(org.mockito.Mockito) List(java.util.List) OrganizationNotFoundException(io.gravitee.rest.api.service.exceptions.OrganizationNotFoundException) BadOrganizationException(io.gravitee.rest.api.service.exceptions.BadOrganizationException) Environment(io.gravitee.repository.management.model.Environment) Optional(java.util.Optional) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) EnvironmentServiceImpl(io.gravitee.rest.api.service.impl.EnvironmentServiceImpl) UpdateEnvironmentEntity(io.gravitee.rest.api.model.UpdateEnvironmentEntity) UpdateEnvironmentEntity(io.gravitee.rest.api.model.UpdateEnvironmentEntity) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) Environment(io.gravitee.repository.management.model.Environment) Test(org.junit.Test)

Example 19 with EnvironmentEntity

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

the class CockpitServiceImpl method listPromotionTargets.

@Override
public CockpitReply<List<PromotionTargetEntity>> listPromotionTargets(String organizationId) {
    final BridgeCommand listEnvironmentCommand = this.bridgeCommandFactory.createListEnvironmentCommand();
    BridgeReply bridgeReply = cockpitCommandService.send(listEnvironmentCommand);
    if (bridgeReply.getCommandStatus() != CommandStatus.SUCCEEDED) {
        logger.warn("Problem while listing promotion targets through cockpit. \n {}", bridgeReply.getMessage());
        return new CockpitReply<>(Collections.emptyList(), CockpitReplyStatus.ERROR);
    }
    final List<PromotionTargetEntity> environmentEntities = ((BridgeMultiReply) bridgeReply).getReplies().stream().filter(simpleReply -> CommandStatus.SUCCEEDED == simpleReply.getCommandStatus()).map(simpleReply -> {
        try {
            final EnvironmentEntity environmentEntity = this.objectMapper.readValue(simpleReply.getPayload(), EnvironmentEntity.class);
            // because cockpit has updated them to handle the case were id is "DEFAULT"
            return new PromotionTargetEntity(environmentEntity, simpleReply.getOrganizationId(), simpleReply.getEnvironmentId(), simpleReply.getInstallationId());
        } catch (JsonProcessingException e) {
            logger.warn("Problem while deserializing environment {} with payload {}", simpleReply.getEnvironmentId(), simpleReply.getPayload());
            return null;
        }
    }).filter(Objects::nonNull).collect(Collectors.toList());
    return new CockpitReply<>(environmentEntities, CockpitReplyStatus.SUCCEEDED);
}
Also used : Logger(org.slf4j.Logger) BridgeCommandFactory(io.gravitee.rest.api.service.cockpit.command.bridge.BridgeCommandFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) LoggerFactory(org.slf4j.LoggerFactory) BridgeMultiReply(io.gravitee.cockpit.api.command.bridge.BridgeMultiReply) PromotionEntity(io.gravitee.rest.api.model.promotion.PromotionEntity) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Collectors(java.util.stream.Collectors) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) Objects(java.util.Objects) BridgeCommand(io.gravitee.cockpit.api.command.bridge.BridgeCommand) List(java.util.List) Component(org.springframework.stereotype.Component) CommandStatus(io.gravitee.cockpit.api.command.CommandStatus) BridgeReply(io.gravitee.cockpit.api.command.bridge.BridgeReply) CockpitCommandService(io.gravitee.rest.api.service.cockpit.command.CockpitCommandService) Collections(java.util.Collections) PromotionTargetEntity(io.gravitee.rest.api.model.promotion.PromotionTargetEntity) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) PromotionTargetEntity(io.gravitee.rest.api.model.promotion.PromotionTargetEntity) BridgeCommand(io.gravitee.cockpit.api.command.bridge.BridgeCommand) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) BridgeReply(io.gravitee.cockpit.api.command.bridge.BridgeReply)

Example 20 with EnvironmentEntity

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

the class SyncManager method convert.

private ApiEntity convert(Api api) {
    ApiEntity apiEntity = new ApiEntity();
    apiEntity.setId(api.getId());
    apiEntity.setName(api.getName());
    apiEntity.setDeployedAt(api.getDeployedAt());
    apiEntity.setCreatedAt(api.getCreatedAt());
    if (api.getDefinition() != null) {
        try {
            io.gravitee.definition.model.Api apiDefinition = objectMapper.readValue(api.getDefinition(), io.gravitee.definition.model.Api.class);
            apiEntity.setProxy(apiDefinition.getProxy());
            apiEntity.setPaths(apiDefinition.getPaths());
            apiEntity.setServices(apiDefinition.getServices());
            apiEntity.setResources(apiDefinition.getResources());
            apiEntity.setProperties(apiDefinition.getProperties());
            apiEntity.setTags(apiDefinition.getTags());
            if (apiDefinition.getPathMappings() != null) {
                apiEntity.setPathMappings(new HashSet<>(apiDefinition.getPathMappings().keySet()));
            }
        } catch (IOException ioe) {
            logger.error("Unexpected error while generating API definition", ioe);
        }
    }
    apiEntity.setUpdatedAt(api.getUpdatedAt());
    apiEntity.setVersion(api.getVersion());
    apiEntity.setDescription(api.getDescription());
    apiEntity.setPicture(api.getPicture());
    apiEntity.setBackground(api.getBackground());
    apiEntity.setCategories(api.getCategories());
    final LifecycleState lifecycleState = api.getLifecycleState();
    if (lifecycleState != null) {
        apiEntity.setState(Lifecycle.State.valueOf(lifecycleState.name()));
    }
    final ApiLifecycleState apiLifecycleState = api.getApiLifecycleState();
    if (apiLifecycleState != null) {
        apiEntity.setLifecycleState(io.gravitee.rest.api.model.api.ApiLifecycleState.valueOf(apiLifecycleState.name()));
    }
    if (api.getVisibility() != null) {
        apiEntity.setVisibility(io.gravitee.rest.api.model.Visibility.valueOf(api.getVisibility().toString()));
    }
    // FIXME: Find a way to avoid this context override needed because the same thread synchronize all the apis
    // (and they can be related to different organizations)
    EnvironmentEntity environmentEntity = this.environmentService.findById(api.getEnvironmentId());
    GraviteeContext.setCurrentOrganization(environmentEntity.getOrganizationId());
    apiEntity.setPrimaryOwner(apiService.getPrimaryOwner(api.getId()));
    return apiEntity;
}
Also used : EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) io.gravitee.repository.management.model(io.gravitee.repository.management.model) IOException(java.io.IOException)

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