Search in sources :

Example 6 with ConfigContextEnum

use of com.synopsys.integration.alert.common.enumeration.ConfigContextEnum in project hub-alert by blackducksoftware.

the class ConfigurationFieldModelConverter method convertToConfigurationFieldModelMap.

public final Map<String, ConfigurationFieldModel> convertToConfigurationFieldModelMap(FieldModel fieldModel) {
    ConfigContextEnum context = EnumUtils.getEnum(ConfigContextEnum.class, fieldModel.getContext());
    String descriptorName = fieldModel.getDescriptorName();
    DescriptorKey descriptorKey = getDescriptorKey(descriptorName).orElseThrow(() -> new AlertRuntimeException("Could not find a Descriptor with the name: " + descriptorName));
    List<DefinedFieldModel> fieldsForContext = descriptorAccessor.getFieldsForDescriptor(descriptorKey, context);
    Map<String, ConfigurationFieldModel> configurationModels = new HashMap<>();
    for (DefinedFieldModel definedField : fieldsForContext) {
        fieldModel.getFieldValueModel(definedField.getKey()).flatMap(fieldValueModel -> convertFromDefinedFieldModel(definedField, fieldValueModel.getValues(), fieldValueModel.getIsSet())).ifPresent(configurationFieldModel -> configurationModels.put(configurationFieldModel.getFieldKey(), configurationFieldModel));
    }
    return configurationModels;
}
Also used : DefinedFieldModel(com.synopsys.integration.alert.common.persistence.model.DefinedFieldModel) DescriptorAccessor(com.synopsys.integration.alert.common.persistence.accessor.DescriptorAccessor) ConfigurationFieldModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel) DataStructureUtils(com.synopsys.integration.alert.common.util.DataStructureUtils) Collection(java.util.Collection) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) BooleanUtils(org.apache.commons.lang3.BooleanUtils) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum) StringUtils(org.apache.commons.lang3.StringUtils) ConfigurationModelMutable(com.synopsys.integration.alert.common.persistence.model.mutable.ConfigurationModelMutable) ConfigurationModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationModel) FieldUtility(com.synopsys.integration.alert.common.persistence.accessor.FieldUtility) RegisteredDescriptorModel(com.synopsys.integration.alert.common.persistence.model.RegisteredDescriptorModel) List(java.util.List) Component(org.springframework.stereotype.Component) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) EnumUtils(org.apache.commons.lang3.EnumUtils) Map(java.util.Map) AlertRuntimeException(com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException) Optional(java.util.Optional) EncryptionUtility(com.synopsys.integration.alert.common.security.EncryptionUtility) FieldModel(com.synopsys.integration.alert.common.rest.model.FieldModel) FieldValueModel(com.synopsys.integration.alert.common.rest.model.FieldValueModel) ConfigurationFieldModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel) DefinedFieldModel(com.synopsys.integration.alert.common.persistence.model.DefinedFieldModel) HashMap(java.util.HashMap) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) AlertRuntimeException(com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum)

Example 7 with ConfigContextEnum

use of com.synopsys.integration.alert.common.enumeration.ConfigContextEnum in project hub-alert by blackducksoftware.

the class AzureBoardsGlobalApiAction method beforeSaveAction.

@Override
public FieldModel beforeSaveAction(FieldModel fieldModel) throws AlertException {
    FieldModel updatedFieldModel = super.beforeSaveAction(fieldModel);
    Optional<DescriptorKey> descriptorKey = descriptorMap.getDescriptorKey(fieldModel.getDescriptorName());
    if (!descriptorKey.isPresent()) {
        return updatedFieldModel;
    }
    ConfigContextEnum context = ConfigContextEnum.valueOf(fieldModel.getContext());
    List<ConfigurationModel> existingConfig = configurationModelConfigurationAccessor.getConfigurationsByDescriptorKeyAndContext(descriptorKey.get(), context);
    Optional<ConfigurationModel> configurationModel = existingConfig.stream().findFirst();
    return configurationModel.map(config -> updateTokenFields(updatedFieldModel, config)).orElse(updatedFieldModel);
}
Also used : ApiAction(com.synopsys.integration.alert.common.action.ApiAction) OAuthRequestValidator(com.synopsys.integration.alert.channel.azure.boards.oauth.OAuthRequestValidator) ConfigurationModelConfigurationAccessor(com.synopsys.integration.alert.common.persistence.accessor.ConfigurationModelConfigurationAccessor) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) DescriptorMap(com.synopsys.integration.alert.common.descriptor.DescriptorMap) ConfigurationModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationModel) ConfigurationFieldModelConverter(com.synopsys.integration.alert.common.persistence.util.ConfigurationFieldModelConverter) List(java.util.List) Component(org.springframework.stereotype.Component) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) Map(java.util.Map) AzureBoardsDescriptor(com.synopsys.integration.alert.channel.azure.boards.descriptor.AzureBoardsDescriptor) Optional(java.util.Optional) FieldModel(com.synopsys.integration.alert.common.rest.model.FieldModel) FieldValueModel(com.synopsys.integration.alert.common.rest.model.FieldValueModel) ConfigurationModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationModel) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) FieldModel(com.synopsys.integration.alert.common.rest.model.FieldModel) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum)

Example 8 with ConfigContextEnum

use of com.synopsys.integration.alert.common.enumeration.ConfigContextEnum in project hub-alert by blackducksoftware.

the class DescriptorMetadataActions method getDescriptorsByPermissions.

public ActionResponse<DescriptorsResponseModel> getDescriptorsByPermissions(@Nullable String name, @Nullable String type, @Nullable String context) {
    Predicate<Descriptor> descriptorFilter = ignored -> true;
    Set<ConfigContextEnum> requestedContexts = Set.of(ConfigContextEnum.GLOBAL, ConfigContextEnum.DISTRIBUTION);
    if (StringUtils.isNotBlank(name)) {
        descriptorFilter = descriptorFilter.and(descriptor -> name.equals(descriptor.getDescriptorKey().getUniversalKey()));
    }
    if (StringUtils.isNotBlank(type)) {
        descriptorFilter = descriptorFilter.and(descriptor -> type.equals(descriptor.getType().name()));
    }
    if (StringUtils.isNotBlank(context)) {
        ConfigContextEnum requestedContext = EnumUtils.getEnum(ConfigContextEnum.class, context);
        if (null != requestedContext) {
            requestedContexts = Set.of(requestedContext);
        } else {
            requestedContexts = Set.of();
        }
    }
    return createDescriptorResponse(descriptorFilter, requestedContexts);
}
Also used : Descriptor(com.synopsys.integration.alert.common.descriptor.Descriptor) AuthorizationManager(com.synopsys.integration.alert.common.security.authorization.AuthorizationManager) DescriptorMetadata(com.synopsys.integration.alert.common.descriptor.config.ui.DescriptorMetadata) Predicate(java.util.function.Predicate) AccessOperation(com.synopsys.integration.alert.common.enumeration.AccessOperation) Collection(java.util.Collection) Set(java.util.Set) Autowired(org.springframework.beans.factory.annotation.Autowired) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable) HttpStatus(org.springframework.http.HttpStatus) Component(org.springframework.stereotype.Component) ActionResponse(com.synopsys.integration.alert.common.action.ActionResponse) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) EnumUtils(org.apache.commons.lang3.EnumUtils) Optional(java.util.Optional) DescriptorType(com.synopsys.integration.alert.common.enumeration.DescriptorType) DescriptorsResponseModel(com.synopsys.integration.alert.web.api.metadata.model.DescriptorsResponseModel) Descriptor(com.synopsys.integration.alert.common.descriptor.Descriptor) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum)

Example 9 with ConfigContextEnum

use of com.synopsys.integration.alert.common.enumeration.ConfigContextEnum in project hub-alert by blackducksoftware.

the class DescriptorMocker method addFieldToDescriptor.

public void addFieldToDescriptor(String descriptorName, String fieldKey, Collection<ConfigContextEnum> contexts, Boolean sensitive) {
    Long descriptorId = registeredDescriptorRepository.findFirstByName(descriptorName).map(RegisteredDescriptorEntity::getId).orElseThrow();
    Long fieldId = definedFieldRepository.findFirstByKey(fieldKey).map(DefinedFieldEntity::getId).orElseGet(() -> definedFieldRepository.save(new DefinedFieldEntity(fieldKey, sensitive)).getId());
    try {
        descriptorFieldRepository.save(new DescriptorFieldRelation(descriptorId, fieldId));
    } catch (Exception e) {
    // Ignore exception, relation already registered
    }
    for (ConfigContextEnum context : contexts) {
        String contextName = context.name();
        Long contextId = configContextRepository.findFirstByContext(contextName).map(ConfigContextEntity::getId).orElseGet(() -> configContextRepository.save(new ConfigContextEntity(contextName)).getId());
        try {
            fieldContextRepository.save(new FieldContextRelation(fieldId, contextId));
        } catch (Exception e) {
        // Ignore exception, relation already registered
        }
    }
}
Also used : FieldContextRelation(com.synopsys.integration.alert.database.configuration.FieldContextRelation) DescriptorFieldRelation(com.synopsys.integration.alert.database.configuration.DescriptorFieldRelation) DefinedFieldEntity(com.synopsys.integration.alert.database.configuration.DefinedFieldEntity) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum) ConfigContextEntity(com.synopsys.integration.alert.database.configuration.ConfigContextEntity)

Example 10 with ConfigContextEnum

use of com.synopsys.integration.alert.common.enumeration.ConfigContextEnum in project hub-alert by blackducksoftware.

the class DescriptorMetadataActionsTest method getDescriptorsWithNameAndContextTest.

@Test
public void getDescriptorsWithNameAndContextTest() {
    String componentName1 = getNamePrefix(DescriptorType.CHANNEL) + "_4";
    ConfigContextEnum context1 = ConfigContextEnum.GLOBAL;
    ActionResponse<DescriptorsResponseModel> response1 = actions.getDescriptorsByPermissions(componentName1, null, context1.name());
    assertTrue(response1.isSuccessful());
    assertTrue(response1.hasContent());
    Set<DescriptorMetadata> descriptorMetadata1 = response1.getContent().get().getDescriptors();
    assertEquals(1, descriptorMetadata1.size());
    String componentName2 = getNamePrefix(DescriptorType.COMPONENT) + "_4";
    ConfigContextEnum context2 = ConfigContextEnum.GLOBAL;
    ActionResponse<DescriptorsResponseModel> response2 = actions.getDescriptorsByPermissions(componentName2, null, context2.name());
    assertTrue(response2.isSuccessful());
    assertTrue(response2.hasContent());
    Set<DescriptorMetadata> descriptorMetadata2 = response2.getContent().get().getDescriptors();
    assertEquals(1, descriptorMetadata2.size());
    String componentName3 = getNamePrefix(DescriptorType.PROVIDER) + "_4";
    ConfigContextEnum context3 = ConfigContextEnum.GLOBAL;
    ActionResponse<DescriptorsResponseModel> response3 = actions.getDescriptorsByPermissions(componentName3, null, context3.name());
    assertTrue(response3.isSuccessful());
    assertTrue(response3.hasContent());
    Set<DescriptorMetadata> descriptorMetadata3 = response3.getContent().get().getDescriptors();
    assertEquals(1, descriptorMetadata3.size());
    ConfigContextEnum context4 = ConfigContextEnum.DISTRIBUTION;
    ActionResponse<DescriptorsResponseModel> response4 = actions.getDescriptorsByPermissions(componentName1, null, context4.name());
    assertTrue(response4.isSuccessful());
    assertTrue(response4.hasContent());
    Set<DescriptorMetadata> descriptorMetadata4 = response4.getContent().get().getDescriptors();
    assertEquals(1, descriptorMetadata4.size());
    ConfigContextEnum context5 = ConfigContextEnum.DISTRIBUTION;
    ActionResponse<DescriptorsResponseModel> response5 = actions.getDescriptorsByPermissions(componentName2, null, context5.name());
    assertTrue(response5.isSuccessful());
    assertTrue(response5.hasContent());
    Set<DescriptorMetadata> descriptorMetadata5 = response5.getContent().get().getDescriptors();
    assertEquals(1, descriptorMetadata5.size());
    ConfigContextEnum context6 = ConfigContextEnum.DISTRIBUTION;
    ActionResponse<DescriptorsResponseModel> response6 = actions.getDescriptorsByPermissions(componentName3, null, context6.name());
    assertTrue(response6.isSuccessful());
    assertTrue(response6.hasContent());
    Set<DescriptorMetadata> descriptorMetadata6 = response6.getContent().get().getDescriptors();
    assertEquals(1, descriptorMetadata6.size());
}
Also used : DescriptorMetadata(com.synopsys.integration.alert.common.descriptor.config.ui.DescriptorMetadata) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum) DescriptorsResponseModel(com.synopsys.integration.alert.web.api.metadata.model.DescriptorsResponseModel) Test(org.junit.jupiter.api.Test)

Aggregations

ConfigContextEnum (com.synopsys.integration.alert.common.enumeration.ConfigContextEnum)12 DescriptorKey (com.synopsys.integration.alert.descriptor.api.model.DescriptorKey)7 Test (org.junit.jupiter.api.Test)5 DescriptorMetadata (com.synopsys.integration.alert.common.descriptor.config.ui.DescriptorMetadata)4 DescriptorsResponseModel (com.synopsys.integration.alert.web.api.metadata.model.DescriptorsResponseModel)4 Optional (java.util.Optional)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 Component (org.springframework.stereotype.Component)4 DescriptorType (com.synopsys.integration.alert.common.enumeration.DescriptorType)3 ConfigurationModel (com.synopsys.integration.alert.common.persistence.model.ConfigurationModel)3 DefinedFieldModel (com.synopsys.integration.alert.common.persistence.model.DefinedFieldModel)3 FieldModel (com.synopsys.integration.alert.common.rest.model.FieldModel)3 ConfigContextEntity (com.synopsys.integration.alert.database.configuration.ConfigContextEntity)3 DefinedFieldEntity (com.synopsys.integration.alert.database.configuration.DefinedFieldEntity)3 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 StringUtils (org.apache.commons.lang3.StringUtils)3 AlertRuntimeException (com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException)2 ActionResponse (com.synopsys.integration.alert.common.action.ActionResponse)2