Search in sources :

Example 1 with ConfigurationVO

use of org.apache.cloudstack.framework.config.impl.ConfigurationVO in project cloudstack by apache.

the class NuageVspManagerImpl method removeLegacyNuageVspDeviceCmsId.

@Deprecated
private void removeLegacyNuageVspDeviceCmsId(long deviceId) {
    ConfigurationVO cmsIdConfig = _configDao.findByName(CMSID_CONFIG_KEY);
    if (cmsIdConfig != null) {
        if (!cmsIdConfig.getValue().contains(";") && cmsIdConfig.getValue().startsWith(deviceId + ":")) {
            _configDao.update(CMSID_CONFIG_KEY, "Advanced", "");
        } else {
            String newValue = cmsIdConfig.getValue().replace(String.format("(^|;)%d:[0-9a-f\\-]+;?", deviceId), ";");
            _configDao.update(CMSID_CONFIG_KEY, "Advanced", newValue);
        }
    }
}
Also used : ConfigurationVO(org.apache.cloudstack.framework.config.impl.ConfigurationVO)

Example 2 with ConfigurationVO

use of org.apache.cloudstack.framework.config.impl.ConfigurationVO in project cloudstack by apache.

the class StorageAllocatorTest method setup.

@Before
@DB
public void setup() throws Exception {
    ConfigurationVO cfg = configDao.findByName(Config.VmAllocationAlgorithm.key());
    if (cfg == null) {
        ConfigurationVO configVO = new ConfigurationVO("test", "DEFAULT", "test", Config.VmAllocationAlgorithm.key(), "userdispersing", null);
        configDao.persist(configVO);
    }
    ComponentContext.initComponentsLifeCycle();
}
Also used : ConfigurationVO(org.apache.cloudstack.framework.config.impl.ConfigurationVO) Before(org.junit.Before) DB(com.cloud.utils.db.DB)

Example 3 with ConfigurationVO

use of org.apache.cloudstack.framework.config.impl.ConfigurationVO in project cloudstack by apache.

the class ConfigurationManagerImpl method validateConfigurationValue.

private String validateConfigurationValue(final String name, String value, final String scope) {
    final ConfigurationVO cfg = _configDao.findByName(name);
    if (cfg == null) {
        s_logger.error("Missing configuration variable " + name + " in configuration table");
        return "Invalid configuration variable.";
    }
    final String configScope = cfg.getScope();
    if (scope != null) {
        if (!configScope.contains(scope) && !(ENABLE_ACCOUNT_SETTINGS_FOR_DOMAIN.value() && configScope.contains(ConfigKey.Scope.Account.toString()) && scope.equals(ConfigKey.Scope.Domain.toString()))) {
            s_logger.error("Invalid scope id provided for the parameter " + name);
            return "Invalid scope id provided for the parameter " + name;
        }
    }
    Class<?> type = null;
    final Config c = Config.getConfig(name);
    if (c == null) {
        s_logger.warn("Did not find configuration " + name + " in Config.java. Perhaps moved to ConfigDepot");
        final ConfigKey<?> configKey = _configDepot.get(name);
        if (configKey == null) {
            s_logger.warn("Did not find configuration " + name + " in ConfigDepot too.");
            return null;
        }
        type = configKey.type();
    } else {
        type = c.getType();
    }
    // no need to validate further if a
    // config can have null value.
    String errMsg = null;
    try {
        if (type.equals(Integer.class)) {
            errMsg = "There was error in trying to parse value: " + value + ". Please enter a valid integer value for parameter " + name;
            Integer.parseInt(value);
        } else if (type.equals(Float.class)) {
            errMsg = "There was error in trying to parse value: " + value + ". Please enter a valid float value for parameter " + name;
            Float.parseFloat(value);
        } else if (type.equals(Long.class)) {
            errMsg = "There was error in trying to parse value: " + value + ". Please enter a valid long value for parameter " + name;
            Long.parseLong(value);
        }
    } catch (final Exception e) {
        // catching generic exception as some throws NullPointerException and some throws NumberFormatExcpeion
        s_logger.error(errMsg);
        return errMsg;
    }
    if (value == null) {
        if (type.equals(Boolean.class)) {
            return "Please enter either 'true' or 'false'.";
        }
        if (overprovisioningFactorsForValidation.contains(name)) {
            final String msg = "value cannot be null for the parameter " + name;
            s_logger.error(msg);
            return msg;
        }
        return null;
    }
    value = value.trim();
    try {
        if (overprovisioningFactorsForValidation.contains(name) && Float.parseFloat(value) < 1f) {
            final String msg = name + " should be greater than or equal to 1";
            s_logger.error(msg);
            throw new InvalidParameterValueException(msg);
        }
    } catch (final NumberFormatException e) {
        final String msg = "There was an error trying to parse the float value for: " + name;
        s_logger.error(msg);
        throw new InvalidParameterValueException(msg);
    }
    if (type.equals(Boolean.class)) {
        if (!(value.equals("true") || value.equals("false"))) {
            s_logger.error("Configuration variable " + name + " is expecting true or false instead of " + value);
            return "Please enter either 'true' or 'false'.";
        }
        return null;
    }
    if (type.equals(Integer.class) && NetworkModel.MACIdentifier.key().equalsIgnoreCase(name)) {
        try {
            final int val = Integer.parseInt(value);
            // 0 value is considered as disable.
            if (val < 0 || val > 255) {
                throw new InvalidParameterValueException(name + " value should be between 0 and 255. 0 value will disable this feature");
            }
        } catch (final NumberFormatException e) {
            s_logger.error("There was an error trying to parse the integer value for:" + name);
            throw new InvalidParameterValueException("There was an error trying to parse the integer value for:" + name);
        }
    }
    if (type.equals(Integer.class) && configValuesForValidation.contains(name)) {
        try {
            final int val = Integer.parseInt(value);
            if (val <= 0) {
                throw new InvalidParameterValueException("Please enter a positive value for the configuration parameter:" + name);
            }
            if ("vm.password.length".equalsIgnoreCase(name) && val < 6) {
                throw new InvalidParameterValueException("Please enter a value greater than 5 for the configuration parameter:" + name);
            }
            if ("remote.access.vpn.psk.length".equalsIgnoreCase(name)) {
                if (val < 8) {
                    throw new InvalidParameterValueException("Please enter a value greater than 7 for the configuration parameter:" + name);
                }
                if (val > 256) {
                    throw new InvalidParameterValueException("Please enter a value less than 257 for the configuration parameter:" + name);
                }
            }
            if (VM_USERDATA_MAX_LENGTH_STRING.equalsIgnoreCase(name)) {
                if (val > 1048576) {
                    throw new InvalidParameterValueException("Please enter a value less than 1048576 for the configuration parameter:" + name);
                }
            }
        } catch (final NumberFormatException e) {
            s_logger.error("There was an error trying to parse the integer value for:" + name);
            throw new InvalidParameterValueException("There was an error trying to parse the integer value for:" + name);
        }
    }
    if (type.equals(Float.class)) {
        try {
            final Float val = Float.parseFloat(value);
            if (weightBasedParametersForValidation.contains(name) && (val < 0f || val > 1f)) {
                throw new InvalidParameterValueException("Please enter a value between 0 and 1 for the configuration parameter: " + name);
            }
        } catch (final NumberFormatException e) {
            s_logger.error("There was an error trying to parse the float value for:" + name);
            throw new InvalidParameterValueException("There was an error trying to parse the float value for:" + name);
        }
    }
    if (c == null) {
        // return in case of Configkey parameters
        return null;
    }
    final String range = c.getRange();
    if (range == null) {
        return null;
    }
    if (type.equals(String.class)) {
        if (range.equals("privateip")) {
            try {
                if (!NetUtils.isSiteLocalAddress(value)) {
                    s_logger.error("privateip range " + value + " is not a site local address for configuration variable " + name);
                    return "Please enter a site local IP address.";
                }
            } catch (final NullPointerException e) {
                s_logger.error("Error parsing ip address for " + name);
                throw new InvalidParameterValueException("Error parsing ip address");
            }
        } else if (range.equals("netmask")) {
            if (!NetUtils.isValidIp4Netmask(value)) {
                s_logger.error("netmask " + value + " is not a valid net mask for configuration variable " + name);
                return "Please enter a valid netmask.";
            }
        } else if (range.equals("hypervisorList")) {
            final String[] hypervisors = value.split(",");
            if (hypervisors == null) {
                return "Please enter hypervisor list, separated by comma";
            }
            for (final String hypervisor : hypervisors) {
                if (HypervisorType.getType(hypervisor) == HypervisorType.Any || HypervisorType.getType(hypervisor) == HypervisorType.None) {
                    return "Please enter a valid hypervisor type";
                }
            }
        } else if (range.equalsIgnoreCase("instanceName")) {
            if (!NetUtils.verifyInstanceName(value)) {
                return "Instance name can not contain hyphen, space or plus sign";
            }
        } else if (range.equalsIgnoreCase("domainName")) {
            String domainName = value;
            if (value.startsWith("*")) {
                // skip the "*."
                domainName = value.substring(2);
            }
            // max length for FQDN is 253 + 2, code adds xxx-xxx-xxx-xxx to domain name when creating URL
            if (domainName.length() >= 238 || !domainName.matches(DOMAIN_NAME_PATTERN)) {
                return "Please enter a valid string for domain name, prefixed with '*.' if applicable";
            }
        } else if (range.equals("routes")) {
            final String[] routes = value.split(",");
            for (final String route : routes) {
                if (route != null) {
                    final String routeToVerify = route.trim();
                    if (!NetUtils.isValidIp4Cidr(routeToVerify)) {
                        throw new InvalidParameterValueException("Invalid value for route: " + route + " in deny list. Valid format is list" + " of cidrs separated by coma. Example: 10.1.1.0/24,192.168.0.0/24");
                    }
                }
            }
        } else {
            final String[] options = range.split(",");
            for (final String option : options) {
                if (option.trim().equalsIgnoreCase(value)) {
                    return null;
                }
            }
            s_logger.error("configuration value for " + name + " is invalid");
            return "Please enter : " + range;
        }
    } else if (type.equals(Integer.class)) {
        final String[] options = range.split("-");
        if (options.length != 2) {
            final String msg = "configuration range " + range + " for " + name + " is invalid";
            s_logger.error(msg);
            return msg;
        }
        final int min = Integer.parseInt(options[0]);
        final int max = Integer.parseInt(options[1]);
        final int val = Integer.parseInt(value);
        if (val < min || val > max) {
            s_logger.error("configuration value for " + name + " is invalid");
            return "Please enter : " + range;
        }
    }
    return null;
}
Also used : IPRangeConfig(com.cloud.test.IPRangeConfig) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) URISyntaxException(java.net.URISyntaxException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException) ConfigurationVO(org.apache.cloudstack.framework.config.impl.ConfigurationVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException)

Example 4 with ConfigurationVO

use of org.apache.cloudstack.framework.config.impl.ConfigurationVO in project cloudstack by apache.

the class ConfigurationManagerImpl method updateConfiguration.

@Override
@ActionEvent(eventType = EventTypes.EVENT_CONFIGURATION_VALUE_EDIT, eventDescription = "updating configuration")
public Configuration updateConfiguration(final UpdateCfgCmd cmd) throws InvalidParameterValueException {
    final Long userId = CallContext.current().getCallingUserId();
    final String name = cmd.getCfgName();
    String value = cmd.getValue();
    final Long zoneId = cmd.getZoneId();
    final Long clusterId = cmd.getClusterId();
    final Long storagepoolId = cmd.getStoragepoolId();
    final Long imageStoreId = cmd.getImageStoreId();
    Long accountId = cmd.getAccountId();
    Long domainId = cmd.getDomainId();
    CallContext.current().setEventDetails(" Name: " + name + " New Value: " + (name.toLowerCase().contains("password") ? "*****" : value == null ? "" : value));
    // check if config value exists
    final ConfigurationVO config = _configDao.findByName(name);
    String catergory = null;
    final Account caller = CallContext.current().getCallingAccount();
    if (_accountMgr.isDomainAdmin(caller.getId())) {
        if (accountId == null && domainId == null) {
            domainId = caller.getDomainId();
        }
    } else if (_accountMgr.isNormalUser(caller.getId())) {
        if (accountId == null) {
            accountId = caller.getAccountId();
        }
    }
    // FIX ME - All configuration parameters are not moved from config.java to configKey
    if (config == null) {
        if (_configDepot.get(name) == null) {
            s_logger.warn("Probably the component manager where configuration variable " + name + " is defined needs to implement Configurable interface");
            throw new InvalidParameterValueException("Config parameter with name " + name + " doesn't exist");
        }
        catergory = _configDepot.get(name).category();
    } else {
        catergory = config.getCategory();
    }
    if (value == null) {
        return _configDao.findByName(name);
    }
    String scope = null;
    Long id = null;
    int paramCountCheck = 0;
    if (zoneId != null) {
        scope = ConfigKey.Scope.Zone.toString();
        id = zoneId;
        paramCountCheck++;
    }
    if (clusterId != null) {
        scope = ConfigKey.Scope.Cluster.toString();
        id = clusterId;
        paramCountCheck++;
    }
    if (accountId != null) {
        Account account = _accountMgr.getAccount(accountId);
        _accountMgr.checkAccess(caller, null, false, account);
        scope = ConfigKey.Scope.Account.toString();
        id = accountId;
        paramCountCheck++;
    }
    if (domainId != null) {
        _accountMgr.checkAccess(caller, _domainDao.findById(domainId));
        scope = ConfigKey.Scope.Domain.toString();
        id = domainId;
        paramCountCheck++;
    }
    if (storagepoolId != null) {
        scope = ConfigKey.Scope.StoragePool.toString();
        id = storagepoolId;
        paramCountCheck++;
    }
    if (imageStoreId != null) {
        scope = ConfigKey.Scope.ImageStore.toString();
        id = imageStoreId;
        paramCountCheck++;
    }
    if (paramCountCheck > 1) {
        throw new InvalidParameterValueException("cannot handle multiple IDs, provide only one ID corresponding to the scope");
    }
    value = value.trim();
    if (value.isEmpty() || value.equals("null")) {
        value = (id == null) ? null : "";
    }
    final String updatedValue = updateConfiguration(userId, name, catergory, value, scope, id);
    if (value == null && updatedValue == null || updatedValue.equalsIgnoreCase(value)) {
        return _configDao.findByName(name);
    } else {
        throw new CloudRuntimeException("Unable to update configuration parameter " + name);
    }
}
Also used : Account(com.cloud.user.Account) ConfigurationVO(org.apache.cloudstack.framework.config.impl.ConfigurationVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ActionEvent(com.cloud.event.ActionEvent)

Example 5 with ConfigurationVO

use of org.apache.cloudstack.framework.config.impl.ConfigurationVO in project cloudstack by apache.

the class ConfigKey method value.

public T value() {
    if (_value == null || isDynamic()) {
        ConfigurationVO vo = s_depot != null ? s_depot.global().findById(key()) : null;
        final String value = (vo != null && vo.getValue() != null) ? vo.getValue() : defaultValue();
        _value = ((value == null) ? (T) defaultValue() : valueOf(value));
    }
    return _value;
}
Also used : ConfigurationVO(org.apache.cloudstack.framework.config.impl.ConfigurationVO)

Aggregations

ConfigurationVO (org.apache.cloudstack.framework.config.impl.ConfigurationVO)19 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)4 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)4 ArrayList (java.util.ArrayList)3 ConfigurationException (javax.naming.ConfigurationException)3 Before (org.junit.Before)3 IPRangeConfig (com.cloud.test.IPRangeConfig)2 Account (com.cloud.user.Account)2 DB (com.cloud.utils.db.DB)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 HashMap (java.util.HashMap)2 NuageTest (com.cloud.NuageTest)1 SyncNuageVspCmsIdAnswer (com.cloud.agent.api.sync.SyncNuageVspCmsIdAnswer)1 DeleteNuageVspDeviceCmd (com.cloud.api.commands.DeleteNuageVspDeviceCmd)1 Config (com.cloud.configuration.Config)1 VlanVO (com.cloud.dc.VlanVO)1 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)1 ActionEvent (com.cloud.event.ActionEvent)1 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)1 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)1