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));
}
}
}
}
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");
}
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");
}
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);
}
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;
}
Aggregations