Search in sources :

Example 1 with TenantEntity

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

the class AnalyticsServiceImpl method getTenantMetadata.

private Map<String, String> getTenantMetadata(String tenant) {
    Map<String, String> metadata = new HashMap<>();
    try {
        TenantEntity tenantEntity = tenantService.findByIdAndReference(tenant, GraviteeContext.getCurrentOrganization(), TenantReferenceType.ORGANIZATION);
        metadata.put(METADATA_NAME, tenantEntity.getName());
    } catch (TenantNotFoundException tnfe) {
        metadata.put(METADATA_DELETED, Boolean.TRUE.toString());
        metadata.put(METADATA_NAME, METADATA_DELETED_TENANT_NAME);
    }
    return metadata;
}
Also used : TenantEntity(io.gravitee.rest.api.model.TenantEntity)

Example 2 with TenantEntity

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

the class TenantServiceImpl method update.

@Override
public List<TenantEntity> update(final List<UpdateTenantEntity> tenantEntities, String referenceId, TenantReferenceType referenceType) {
    final List<TenantEntity> savedTenants = new ArrayList<>(tenantEntities.size());
    tenantEntities.forEach(tenantEntity -> {
        try {
            Tenant tenant = convert(tenantEntity);
            Optional<Tenant> tenantOptional = tenantRepository.findByIdAndReference(tenant.getId(), referenceId, repoTenantReferenceType(referenceType));
            if (tenantOptional.isPresent()) {
                Tenant existingTenant = tenantOptional.get();
                tenant.setReferenceId(existingTenant.getReferenceId());
                tenant.setReferenceType(existingTenant.getReferenceType());
                savedTenants.add(convert(tenantRepository.update(tenant)));
                auditService.createEnvironmentAuditLog(Collections.singletonMap(TENANT, tenant.getId()), TENANT_UPDATED, new Date(), tenantOptional.get(), tenant);
            }
        } catch (TechnicalException ex) {
            LOGGER.error("An error occurs while trying to update tenant {}", tenantEntity.getName(), ex);
            throw new TechnicalManagementException("An error occurs while trying to update tenant " + tenantEntity.getName(), ex);
        }
    });
    return savedTenants;
}
Also used : Tenant(io.gravitee.repository.management.model.Tenant) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) NewTenantEntity(io.gravitee.rest.api.model.NewTenantEntity) TenantEntity(io.gravitee.rest.api.model.TenantEntity) UpdateTenantEntity(io.gravitee.rest.api.model.UpdateTenantEntity) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 3 with TenantEntity

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

the class TenantServiceImpl method convert.

private TenantEntity convert(final Tenant tenant) {
    final TenantEntity tenantEntity = new TenantEntity();
    tenantEntity.setId(tenant.getId());
    tenantEntity.setName(tenant.getName());
    tenantEntity.setDescription(tenant.getDescription());
    return tenantEntity;
}
Also used : NewTenantEntity(io.gravitee.rest.api.model.NewTenantEntity) TenantEntity(io.gravitee.rest.api.model.TenantEntity) UpdateTenantEntity(io.gravitee.rest.api.model.UpdateTenantEntity)

Example 4 with TenantEntity

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

the class TenantServiceImpl method create.

@Override
public List<TenantEntity> create(final List<NewTenantEntity> tenantEntities, String referenceId, TenantReferenceType referenceType) {
    // First we prevent the duplicate tenant name
    final List<String> tenantNames = tenantEntities.stream().map(NewTenantEntity::getName).collect(Collectors.toList());
    final Optional<TenantEntity> optionalTenant = findByReference(referenceId, referenceType).stream().filter(tenant -> tenantNames.contains(tenant.getName())).findAny();
    if (optionalTenant.isPresent()) {
        throw new DuplicateTenantNameException(optionalTenant.get().getName());
    }
    final List<TenantEntity> savedTenants = new ArrayList<>(tenantEntities.size());
    tenantEntities.forEach(tenantEntity -> {
        try {
            Tenant tenant = convert(tenantEntity, referenceId, referenceType);
            savedTenants.add(convert(tenantRepository.create(tenant)));
            auditService.createEnvironmentAuditLog(Collections.singletonMap(TENANT, tenant.getId()), TENANT_CREATED, new Date(), null, tenant);
        } catch (TechnicalException ex) {
            LOGGER.error("An error occurs while trying to create tenant {}", tenantEntity.getName(), ex);
            throw new TechnicalManagementException("An error occurs while trying to create tenant " + tenantEntity.getName(), ex);
        }
    });
    return savedTenants;
}
Also used : TenantService(io.gravitee.rest.api.service.TenantService) TENANT_UPDATED(io.gravitee.repository.management.model.Tenant.AuditEvent.TENANT_UPDATED) TagReferenceType(io.gravitee.rest.api.model.TagReferenceType) java.util(java.util) TENANT(io.gravitee.repository.management.model.Audit.AuditProperties.TENANT) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) TENANT_DELETED(io.gravitee.repository.management.model.Tenant.AuditEvent.TENANT_DELETED) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) IdGenerator(io.gravitee.common.utils.IdGenerator) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) TenantNotFoundException(io.gravitee.rest.api.service.exceptions.TenantNotFoundException) Logger(org.slf4j.Logger) TENANT_CREATED(io.gravitee.repository.management.model.Tenant.AuditEvent.TENANT_CREATED) TenantReferenceType(io.gravitee.rest.api.model.TenantReferenceType) NewTenantEntity(io.gravitee.rest.api.model.NewTenantEntity) AuditService(io.gravitee.rest.api.service.AuditService) TenantEntity(io.gravitee.rest.api.model.TenantEntity) Collectors(java.util.stream.Collectors) Component(org.springframework.stereotype.Component) TenantRepository(io.gravitee.repository.management.api.TenantRepository) Tenant(io.gravitee.repository.management.model.Tenant) UpdateTenantEntity(io.gravitee.rest.api.model.UpdateTenantEntity) DuplicateTenantNameException(io.gravitee.rest.api.service.exceptions.DuplicateTenantNameException) Tenant(io.gravitee.repository.management.model.Tenant) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) NewTenantEntity(io.gravitee.rest.api.model.NewTenantEntity) TenantEntity(io.gravitee.rest.api.model.TenantEntity) UpdateTenantEntity(io.gravitee.rest.api.model.UpdateTenantEntity) DuplicateTenantNameException(io.gravitee.rest.api.service.exceptions.DuplicateTenantNameException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Aggregations

TenantEntity (io.gravitee.rest.api.model.TenantEntity)4 NewTenantEntity (io.gravitee.rest.api.model.NewTenantEntity)3 UpdateTenantEntity (io.gravitee.rest.api.model.UpdateTenantEntity)3 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 Tenant (io.gravitee.repository.management.model.Tenant)2 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)2 IdGenerator (io.gravitee.common.utils.IdGenerator)1 TenantRepository (io.gravitee.repository.management.api.TenantRepository)1 TENANT (io.gravitee.repository.management.model.Audit.AuditProperties.TENANT)1 TENANT_CREATED (io.gravitee.repository.management.model.Tenant.AuditEvent.TENANT_CREATED)1 TENANT_DELETED (io.gravitee.repository.management.model.Tenant.AuditEvent.TENANT_DELETED)1 TENANT_UPDATED (io.gravitee.repository.management.model.Tenant.AuditEvent.TENANT_UPDATED)1 TagReferenceType (io.gravitee.rest.api.model.TagReferenceType)1 TenantReferenceType (io.gravitee.rest.api.model.TenantReferenceType)1 AuditService (io.gravitee.rest.api.service.AuditService)1 TenantService (io.gravitee.rest.api.service.TenantService)1 GraviteeContext (io.gravitee.rest.api.service.common.GraviteeContext)1 DuplicateTenantNameException (io.gravitee.rest.api.service.exceptions.DuplicateTenantNameException)1 TenantNotFoundException (io.gravitee.rest.api.service.exceptions.TenantNotFoundException)1 java.util (java.util)1