Search in sources :

Example 1 with GenericNotificationConfigEntity

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

the class ApplicationServiceImpl method createApplicationForEnvironment.

@NotNull
private ApplicationEntity createApplicationForEnvironment(String userId, Application application, String environmentId) {
    try {
        application.setEnvironmentId(environmentId);
        Application createdApplication = applicationRepository.create(application);
        // Audit
        auditService.createApplicationAuditLog(createdApplication.getId(), Collections.emptyMap(), APPLICATION_CREATED, createdApplication.getCreatedAt(), null, createdApplication);
        // Add the primary owner of the newly created Application
        membershipService.addRoleToMemberOnReference(new MembershipService.MembershipReference(MembershipReferenceType.APPLICATION, createdApplication.getId()), new MembershipService.MembershipMember(userId, null, MembershipMemberType.USER), new MembershipService.MembershipRole(RoleScope.APPLICATION, SystemRole.PRIMARY_OWNER.name()));
        // create the default mail notification
        UserEntity userEntity = userService.findById(userId);
        if (userEntity.getEmail() != null && !userEntity.getEmail().isEmpty()) {
            GenericNotificationConfigEntity notificationConfigEntity = new GenericNotificationConfigEntity();
            notificationConfigEntity.setName("Default Mail Notifications");
            notificationConfigEntity.setReferenceType(HookScope.APPLICATION.name());
            notificationConfigEntity.setReferenceId(createdApplication.getId());
            notificationConfigEntity.setHooks(Arrays.stream(ApplicationHook.values()).map(Enum::name).collect(toList()));
            notificationConfigEntity.setNotifier(NotifierServiceImpl.DEFAULT_EMAIL_NOTIFIER_ID);
            notificationConfigEntity.setConfig(userEntity.getEmail());
            genericNotificationConfigService.create(notificationConfigEntity);
        }
        return convert(createdApplication, userEntity);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to create {} for user {} in environment {}", application, userId, environmentId, ex);
        throw new TechnicalManagementException("An error occurs while trying create " + application + " for user " + userId + " in environment " + environmentId, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) GenericNotificationConfigEntity(io.gravitee.rest.api.model.notification.GenericNotificationConfigEntity) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with GenericNotificationConfigEntity

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

the class ApiServiceImpl method createWithApiDefinition.

@Override
public ApiEntity createWithApiDefinition(UpdateApiEntity api, String userId, JsonNode apiDefinition) throws ApiAlreadyExistsException {
    try {
        LOGGER.debug("Create {} for user {}", api, userId);
        String apiId = apiDefinition != null && apiDefinition.has("id") ? apiDefinition.get("id").asText() : null;
        String id = apiId != null && UUID.fromString(apiId) != null ? apiId : UuidString.generateRandom();
        Optional<Api> checkApi = apiRepository.findById(id);
        if (checkApi.isPresent()) {
            throw new ApiAlreadyExistsException(id);
        }
        // if user changes sharding tags, then check if he is allowed to do it
        checkShardingTags(api, null);
        // format context-path and check if context path is unique
        final Collection<VirtualHost> sanitizedVirtualHosts = virtualHostService.sanitizeAndValidate(api.getProxy().getVirtualHosts());
        api.getProxy().setVirtualHosts(new ArrayList<>(sanitizedVirtualHosts));
        // check endpoints name
        checkEndpointsName(api);
        // check HC inheritance
        checkHealthcheckInheritance(api);
        addLoggingMaxDuration(api.getProxy().getLogging());
        // check if there is regex errors in plaintext fields
        validateRegexfields(api);
        // check policy configurations.
        checkPolicyConfigurations(api);
        // check primary owner
        PrimaryOwnerEntity primaryOwner = findPrimaryOwner(apiDefinition, userId);
        if (apiDefinition != null) {
            apiDefinition = ((ObjectNode) apiDefinition).put("id", id);
        }
        Api repoApi = convert(id, api, apiDefinition != null ? apiDefinition.toString() : null);
        repoApi.setId(id);
        repoApi.setEnvironmentId(GraviteeContext.getCurrentEnvironment());
        // Set date fields
        repoApi.setCreatedAt(new Date());
        repoApi.setUpdatedAt(repoApi.getCreatedAt());
        // Be sure that lifecycle is set to STOPPED by default and visibility is private
        repoApi.setLifecycleState(LifecycleState.STOPPED);
        repoApi.setVisibility(api.getVisibility() == null ? Visibility.PRIVATE : Visibility.valueOf(api.getVisibility().toString()));
        // Add Default groups
        Set<String> defaultGroups = groupService.findByEvent(GroupEvent.API_CREATE).stream().map(GroupEntity::getId).collect(toSet());
        if (repoApi.getGroups() == null) {
            repoApi.setGroups(defaultGroups.isEmpty() ? null : defaultGroups);
        } else {
            repoApi.getGroups().addAll(defaultGroups);
        }
        // if po is a group, add it as a member of the API
        if (ApiPrimaryOwnerMode.GROUP.name().equals(primaryOwner.getType())) {
            if (repoApi.getGroups() == null) {
                repoApi.setGroups(new HashSet<>());
            }
            repoApi.getGroups().add(primaryOwner.getId());
        }
        repoApi.setApiLifecycleState(ApiLifecycleState.CREATED);
        if (parameterService.findAsBoolean(Key.API_REVIEW_ENABLED, ParameterReferenceType.ENVIRONMENT)) {
            workflowService.create(WorkflowReferenceType.API, id, REVIEW, userId, DRAFT, "");
        }
        Api createdApi = apiRepository.create(repoApi);
        // Audit
        auditService.createApiAuditLog(createdApi.getId(), Collections.emptyMap(), API_CREATED, createdApi.getCreatedAt(), null, createdApi);
        // Add the primary owner of the newly created API
        membershipService.addRoleToMemberOnReference(new MembershipService.MembershipReference(MembershipReferenceType.API, createdApi.getId()), new MembershipService.MembershipMember(primaryOwner.getId(), null, MembershipMemberType.valueOf(primaryOwner.getType())), new MembershipService.MembershipRole(RoleScope.API, SystemRole.PRIMARY_OWNER.name()));
        // create the default mail notification
        final String emailMetadataValue = "${(api.primaryOwner.email)!''}";
        GenericNotificationConfigEntity notificationConfigEntity = new GenericNotificationConfigEntity();
        notificationConfigEntity.setName("Default Mail Notifications");
        notificationConfigEntity.setReferenceType(HookScope.API.name());
        notificationConfigEntity.setReferenceId(createdApi.getId());
        notificationConfigEntity.setHooks(Arrays.stream(ApiHook.values()).map(Enum::name).collect(toList()));
        notificationConfigEntity.setNotifier(NotifierServiceImpl.DEFAULT_EMAIL_NOTIFIER_ID);
        notificationConfigEntity.setConfig(emailMetadataValue);
        genericNotificationConfigService.create(notificationConfigEntity);
        // create the default mail support metadata
        NewApiMetadataEntity newApiMetadataEntity = new NewApiMetadataEntity();
        newApiMetadataEntity.setFormat(MetadataFormat.MAIL);
        newApiMetadataEntity.setName(DefaultMetadataUpgrader.METADATA_EMAIL_SUPPORT_KEY);
        newApiMetadataEntity.setDefaultValue(emailMetadataValue);
        newApiMetadataEntity.setValue(emailMetadataValue);
        newApiMetadataEntity.setApiId(createdApi.getId());
        apiMetadataService.create(newApiMetadataEntity);
        // TODO add membership log
        ApiEntity apiEntity = convert(createdApi, primaryOwner, null);
        ApiEntity apiWithMetadata = fetchMetadataForApi(apiEntity);
        searchEngineService.index(apiWithMetadata, false);
        return apiEntity;
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to create {} for user {}", api, userId, ex);
        throw new TechnicalManagementException("An error occurs while trying create " + api + " for user " + userId, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) GenericNotificationConfigEntity(io.gravitee.rest.api.model.notification.GenericNotificationConfigEntity) UuidString(io.gravitee.rest.api.service.common.UuidString) Api(io.gravitee.repository.management.model.Api)

Example 3 with GenericNotificationConfigEntity

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

the class GenericNotificationConfigServiceImpl method convert.

private GenericNotificationConfigEntity convert(GenericNotificationConfig genericNotificationConfig) {
    GenericNotificationConfigEntity entity = new GenericNotificationConfigEntity();
    entity.setConfigType(NotificationConfigType.GENERIC);
    entity.setId(genericNotificationConfig.getId());
    entity.setName(genericNotificationConfig.getName());
    entity.setReferenceType(genericNotificationConfig.getReferenceType().name());
    entity.setReferenceId(genericNotificationConfig.getReferenceId());
    entity.setNotifier(genericNotificationConfig.getNotifier());
    entity.setConfig(genericNotificationConfig.getConfig());
    entity.setUseSystemProxy(genericNotificationConfig.isUseSystemProxy());
    entity.setHooks(genericNotificationConfig.getHooks());
    return entity;
}
Also used : GenericNotificationConfigEntity(io.gravitee.rest.api.model.notification.GenericNotificationConfigEntity)

Aggregations

GenericNotificationConfigEntity (io.gravitee.rest.api.model.notification.GenericNotificationConfigEntity)3 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 Api (io.gravitee.repository.management.model.Api)1 UuidString (io.gravitee.rest.api.service.common.UuidString)1 NotNull (org.jetbrains.annotations.NotNull)1