Search in sources :

Example 56 with Location

use of alien4cloud.model.orchestrators.locations.Location in project alien4cloud by alien4cloud.

the class PreconfiguredInputsModifier method process.

@Override
public void process(Topology topology, FlowExecutionContext context) {
    EnvironmentContext environmentContext = context.getEnvironmentContext().orElseThrow(() -> new IllegalArgumentException("Preconfigured input modifier requires an environment context."));
    ApplicationEnvironment environment = environmentContext.getEnvironment();
    Map<String, Location> locations = (Map<String, Location>) context.getExecutionCache().get(FlowExecutionContext.DEPLOYMENT_LOCATIONS_MAP_CACHE_KEY);
    AlienContextVariables alienContextVariables = new AlienContextVariables();
    alienContextVariables.setApplicationEnvironment(environment);
    alienContextVariables.setLocation(locations.values().stream().findFirst().get());
    alienContextVariables.setApplication(environmentContext.getApplication());
    // TODO: avoid reloading every time - find a way to know the last update on files (git hash ?)
    Properties appVarProps = quickFileStorageService.loadApplicationVariables(environmentContext.getApplication().getId());
    Properties envTypeVarProps = quickFileStorageService.loadEnvironmentTypeVariables(topology.getId(), environment.getEnvironmentType());
    Properties envVarProps = quickFileStorageService.loadEnvironmentVariables(topology.getId(), environment.getId());
    Map<String, Object> inputsMappingsMap = quickFileStorageService.loadInputsMappingFile(topology.getId());
    InputsMappingFileVariableResolver.InputsResolvingResult inputsResolvingResult = InputsMappingFileVariableResolver.configure(appVarProps, envTypeVarProps, envVarProps, alienContextVariables).resolve(inputsMappingsMap, topology.getInputs());
    if (CollectionUtils.isNotEmpty(inputsResolvingResult.getMissingVariables())) {
        context.log().error(new MissingVariablesTask(inputsResolvingResult.getMissingVariables()));
    }
    if (CollectionUtils.isNotEmpty(inputsResolvingResult.getUnresolved())) {
        context.log().error(new UnresolvablePredefinedInputsTask(inputsResolvingResult.getUnresolved()));
    }
    // checking constraints
    Map<String, ConstraintUtil.ConstraintInformation> violations = Maps.newHashMap();
    Map<String, ConstraintUtil.ConstraintInformation> typesViolations = Maps.newHashMap();
    for (Map.Entry<String, PropertyValue> entry : safe(inputsResolvingResult.getResolved()).entrySet()) {
        try {
            ConstraintPropertyService.checkPropertyConstraint(entry.getKey(), entry.getValue(), topology.getInputs().get(entry.getKey()));
        } catch (ConstraintViolationException e) {
            violations.put(entry.getKey(), getConstraintInformation(e.getMessage(), e.getConstraintInformation()));
        } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
            typesViolations.put(entry.getKey(), getConstraintInformation(e.getMessage(), e.getConstraintInformation()));
        }
    }
    if (MapUtils.isNotEmpty(violations)) {
        context.log().error(new PredefinedInputsConstraintViolationTask(violations, TaskCode.PREDEFINED_INPUTS_CONSTRAINT_VIOLATION));
    }
    if (MapUtils.isNotEmpty(typesViolations)) {
        context.log().error(new PredefinedInputsConstraintViolationTask(typesViolations, TaskCode.PREDEFINED_INPUTS_TYPE_VIOLATION));
    }
    PreconfiguredInputsConfiguration preconfiguredInputsConfiguration = new PreconfiguredInputsConfiguration(environment.getTopologyVersion(), environment.getId());
    preconfiguredInputsConfiguration.setInputs(inputsResolvingResult.getResolved());
    // add unresolved so that they are not considered as deployer input
    inputsResolvingResult.getUnresolved().forEach(unresolved -> preconfiguredInputsConfiguration.getInputs().put(unresolved, null));
    // TODO: improve me
    preconfiguredInputsConfiguration.setLastUpdateDate(new Date());
    preconfiguredInputsConfiguration.setCreationDate(new Date());
    context.saveConfiguration(preconfiguredInputsConfiguration);
}
Also used : ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) PredefinedInputsConstraintViolationTask(alien4cloud.topology.task.PredefinedInputsConstraintViolationTask) PreconfiguredInputsConfiguration(org.alien4cloud.alm.deployment.configuration.model.PreconfiguredInputsConfiguration) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) Properties(java.util.Properties) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) Date(java.util.Date) EnvironmentContext(org.alien4cloud.alm.deployment.configuration.flow.EnvironmentContext) AlienContextVariables(org.alien4cloud.tosca.variable.AlienContextVariables) InputsMappingFileVariableResolver(org.alien4cloud.tosca.variable.InputsMappingFileVariableResolver) UnresolvablePredefinedInputsTask(alien4cloud.topology.task.UnresolvablePredefinedInputsTask) MissingVariablesTask(alien4cloud.topology.task.MissingVariablesTask) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) Map(java.util.Map) Location(alien4cloud.model.orchestrators.locations.Location)

Example 57 with Location

use of alien4cloud.model.orchestrators.locations.Location in project alien4cloud by alien4cloud.

the class NodeMatchingCandidateModifier method getAvailableSubstitutions.

private Map<String, List<LocationResourceTemplate>> getAvailableSubstitutions(Topology topology, Map<String, NodeGroup> locationGroups, Map<String, Location> locationByIds, String environmentId) {
    // Fetch all node types for templates in the topology
    Map<String, NodeType> nodeTypes = getNodeTypes(topology);
    Map<String, List<LocationResourceTemplate>> availableSubstitutions = Maps.newHashMap();
    // Based on our model nodes may come from various locations actually.
    for (final Map.Entry<String, NodeGroup> locationGroupEntry : locationGroups.entrySet()) {
        String groupName = locationGroupEntry.getKey();
        final NodeGroup locationNodeGroup = locationGroupEntry.getValue();
        Map<String, NodeTemplate> nodesToMatch = Maps.newHashMap();
        if (MapUtils.isNotEmpty(topology.getNodeTemplates())) {
            if (AlienConstants.GROUP_ALL.equals(groupName)) {
                locationNodeGroup.setMembers(topology.getNodeTemplates().keySet());
                nodesToMatch = topology.getNodeTemplates();
            } else {
                nodesToMatch = Maps.filterEntries(topology.getNodeTemplates(), input -> locationNodeGroup.getMembers().contains(input.getKey()));
            }
        }
        availableSubstitutions.putAll(nodeMatcherService.match(nodeTypes, nodesToMatch, locationByIds.get(groupName), environmentId));
    }
    return availableSubstitutions;
}
Also used : MapUtils(org.apache.commons.collections4.MapUtils) NodeMatcherService(alien4cloud.deployment.matching.services.nodes.NodeMatcherService) NodeType(org.alien4cloud.tosca.model.types.NodeType) Maps(com.google.common.collect.Maps) AlienUtils.safe(alien4cloud.utils.AlienUtils.safe) Location(alien4cloud.model.orchestrators.locations.Location) Inject(javax.inject.Inject) AlienConstants(alien4cloud.utils.AlienConstants) List(java.util.List) Component(org.springframework.stereotype.Component) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) FlowExecutionContext(org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext) LocationPolicyTask(alien4cloud.topology.task.LocationPolicyTask) Map(java.util.Map) NodeGroup(org.alien4cloud.tosca.model.templates.NodeGroup) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) Optional(java.util.Optional) Topology(org.alien4cloud.tosca.model.templates.Topology) ITopologyModifier(org.alien4cloud.alm.deployment.configuration.flow.ITopologyModifier) LocationResourceTemplate(alien4cloud.model.orchestrators.locations.LocationResourceTemplate) ToscaContext(alien4cloud.tosca.context.ToscaContext) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) NodeType(org.alien4cloud.tosca.model.types.NodeType) List(java.util.List) Map(java.util.Map) NodeGroup(org.alien4cloud.tosca.model.templates.NodeGroup)

Example 58 with Location

use of alien4cloud.model.orchestrators.locations.Location in project alien4cloud by alien4cloud.

the class LocationMatchingModifier method process.

@Override
public void process(Topology topology, FlowExecutionContext context) {
    // first process
    processLocationMatching(topology, context);
    Optional<DeploymentMatchingConfiguration> configurationOptional = context.getConfiguration(DeploymentMatchingConfiguration.class, LocationMatchingModifier.class.getSimpleName());
    // perform validation
    locationPolicyValidationService.validateLocationPolicies(configurationOptional.orElse(new DeploymentMatchingConfiguration())).forEach(locationPolicyTask -> context.log().error(locationPolicyTask));
    // No errors from validation let's inject location topology modifiers if any.
    if (context.log().isValid()) {
        Map<String, Location> selectedLocations = (Map<String, Location>) context.getExecutionCache().get(FlowExecutionContext.DEPLOYMENT_LOCATIONS_MAP_CACHE_KEY);
        List<Location> locationsWithVault = selectedLocations.values().stream().filter(location -> location.getSecretProviderConfiguration() != null && location.getSecretProviderConfiguration().getConfiguration() != null).collect(Collectors.toList());
        boolean needVaultCredential = locationsWithVault.size() > 0;
        if (needVaultCredential) {
            List<SecretCredentialInfo> secretCredentialInfos = new LinkedList<>();
            for (Location location : locationsWithVault) {
                try {
                    SecretCredentialInfo info = new SecretCredentialInfo();
                    String pluginName = location.getSecretProviderConfiguration().getPluginName();
                    Object rawSecretConfiguration = location.getSecretProviderConfiguration().getConfiguration();
                    secretCredentialInfos.add(secretProviderService.getSecretCredentialInfo(pluginName, rawSecretConfiguration));
                } catch (Exception e) {
                    log.error("Cannot process secret provider configuration", e);
                }
            }
            context.getExecutionCache().put(FlowExecutionContext.SECRET_CREDENTIAL, secretCredentialInfos);
        } else {
            context.getExecutionCache().remove(FlowExecutionContext.SECRET_CREDENTIAL);
        }
        for (LocationModifierReference modifierReference : safe(selectedLocations.values().iterator().next().getModifiers())) {
            if (pluginManager.getPluginOrFail(modifierReference.getPluginId()).isEnabled()) {
                injectLocationTopologyModfier(context, selectedLocations.values().iterator().next().getName(), modifierReference);
            } else {
                log.info("The plugin " + modifierReference.getPluginId() + " is not activated. Ignoring " + modifierReference.getBeanName() + ".");
            }
        }
    }
}
Also used : LocationModifierReference(alien4cloud.model.orchestrators.locations.LocationModifierReference) LocationService(alien4cloud.orchestrators.locations.services.LocationService) PluginManager(alien4cloud.plugin.PluginManager) MetaPropertiesService(alien4cloud.common.MetaPropertiesService) LocationPolicyValidationService(alien4cloud.topology.validation.LocationPolicyValidationService) AlienUtils.safe(alien4cloud.utils.AlienUtils.safe) Location(alien4cloud.model.orchestrators.locations.Location) Inject(javax.inject.Inject) SecretCredentialInfo(org.alien4cloud.alm.deployment.configuration.model.SecretCredentialInfo) LocationMatchingService(alien4cloud.deployment.matching.services.location.LocationMatchingService) Lists(com.google.common.collect.Lists) Map(java.util.Map) SecretProviderService(org.alien4cloud.secret.services.SecretProviderService) LinkedList(java.util.LinkedList) ITopologyModifier(org.alien4cloud.alm.deployment.configuration.flow.ITopologyModifier) ILocationMatch(alien4cloud.model.deployment.matching.ILocationMatch) ToscaContext(alien4cloud.tosca.context.ToscaContext) MapUtils(org.apache.commons.collections4.MapUtils) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) MissingPluginException(alien4cloud.plugin.exception.MissingPluginException) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) List(java.util.List) Component(org.springframework.stereotype.Component) Slf4j(lombok.extern.slf4j.Slf4j) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) FlowExecutionContext(org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext) Optional(java.util.Optional) Topology(org.alien4cloud.tosca.model.templates.Topology) SecretCredentialInfo(org.alien4cloud.alm.deployment.configuration.model.SecretCredentialInfo) LinkedList(java.util.LinkedList) MissingPluginException(alien4cloud.plugin.exception.MissingPluginException) LocationModifierReference(alien4cloud.model.orchestrators.locations.LocationModifierReference) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) Map(java.util.Map) Location(alien4cloud.model.orchestrators.locations.Location)

Example 59 with Location

use of alien4cloud.model.orchestrators.locations.Location in project alien4cloud by alien4cloud.

the class CsarService method getCsarRelatedResourceList.

/**
 * Get the list of resources that are using the given archive.
 *
 * @param csar The archive for which to get usage.
 * @return The list of usage of the archive.
 */
public List<Usage> getCsarRelatedResourceList(Csar csar) {
    if (csar == null) {
        log.debug("You have requested a resource list for a invalid csar object : <" + csar + ">");
        return Lists.newArrayList();
    }
    ArchiveUsageRequestEvent archiveUsageRequestEvent = new ArchiveUsageRequestEvent(this, csar.getName(), csar.getVersion());
    // Archive from applications are used by the application.
    if (Objects.equals(csar.getDelegateType(), ArchiveDelegateType.APPLICATION.toString())) {
        // The CSAR is from an application's topology
        Application application = applicationService.checkAndGetApplication(csar.getDelegateId());
        archiveUsageRequestEvent.addUsage(new Usage(application.getName(), Application.class.getSimpleName().toLowerCase(), csar.getDelegateId(), csar.getWorkspace()));
    }
    // a csar that is a dependency of another csar can not be deleted
    Csar[] relatedCsars = getDependantCsars(csar.getName(), csar.getVersion());
    if (ArrayUtils.isNotEmpty(relatedCsars)) {
        archiveUsageRequestEvent.addUsages(generateCsarsInfo(relatedCsars));
    }
    // check if some of the nodes are used in topologies.
    Topology[] topologies = getDependantTopologies(csar.getName(), csar.getVersion());
    if (topologies != null && topologies.length > 0) {
        archiveUsageRequestEvent.addUsages(generateTopologiesInfo(topologies));
    }
    // a csar that is a dependency of location can not be deleted
    Location[] relatedLocations = getDependantLocations(csar.getName(), csar.getVersion());
    if (relatedLocations != null && relatedLocations.length > 0) {
        archiveUsageRequestEvent.addUsages(generateLocationsInfo(relatedLocations));
    }
    publisher.publishEvent(archiveUsageRequestEvent);
    return archiveUsageRequestEvent.getUsages();
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) Usage(alien4cloud.model.common.Usage) ArchiveUsageRequestEvent(org.alien4cloud.tosca.catalog.events.ArchiveUsageRequestEvent) Topology(org.alien4cloud.tosca.model.templates.Topology) Application(alien4cloud.model.application.Application) Location(alien4cloud.model.orchestrators.locations.Location)

Example 60 with Location

use of alien4cloud.model.orchestrators.locations.Location in project alien4cloud by alien4cloud.

the class CsarService method generateLocationsInfo.

/**
 * Generate resources related to a locations list
 *
 * @param locations
 * @return
 */
public List<Usage> generateLocationsInfo(Location[] locations) {
    String resourceName;
    String resourceId;
    List<Usage> resourceList = Lists.newArrayList();
    for (Location location : locations) {
        resourceName = location.getName();
        resourceId = location.getId();
        Usage temp = new Usage(resourceName, Location.class.getSimpleName().toLowerCase(), resourceId, AlienConstants.GLOBAL_WORKSPACE_ID);
        resourceList.add(temp);
    }
    return resourceList;
}
Also used : Usage(alien4cloud.model.common.Usage) Location(alien4cloud.model.orchestrators.locations.Location)

Aggregations

Location (alien4cloud.model.orchestrators.locations.Location)80 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)31 ApiOperation (io.swagger.annotations.ApiOperation)30 List (java.util.List)28 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)28 Audit (alien4cloud.audit.annotation.Audit)21 ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)15 LocationService (alien4cloud.orchestrators.locations.services.LocationService)13 Set (java.util.Set)13 Collectors (java.util.stream.Collectors)13 Application (alien4cloud.model.application.Application)12 AbstractLocationResourceTemplate (alien4cloud.model.orchestrators.locations.AbstractLocationResourceTemplate)12 RestResponse (alien4cloud.rest.model.RestResponse)12 RestResponseBuilder (alien4cloud.rest.model.RestResponseBuilder)12 GroupDTO (alien4cloud.rest.orchestrator.model.GroupDTO)12 UserDTO (alien4cloud.rest.orchestrator.model.UserDTO)12 Resource (javax.annotation.Resource)12 ApplicationEnvironmentService (alien4cloud.application.ApplicationEnvironmentService)11 ResourcePermissionService (alien4cloud.authorization.ResourcePermissionService)11 ApplicationEnvironmentAuthorizationUpdateRequest (alien4cloud.rest.orchestrator.model.ApplicationEnvironmentAuthorizationUpdateRequest)11