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