Search in sources :

Example 1 with ImageStoreDetailVO

use of org.apache.cloudstack.storage.datastore.db.ImageStoreDetailVO in project cloudstack by apache.

the class ImageStoreHelper method createImageStore.

public ImageStoreVO createImageStore(Map<String, Object> params, Map<String, String> details) {
    ImageStoreVO store = imageStoreDao.findByName((String) params.get("name"));
    if (store != null) {
        return store;
    }
    store = new ImageStoreVO();
    store.setProtocol((String) params.get("protocol"));
    store.setProviderName((String) params.get("providerName"));
    store.setScope((ScopeType) params.get("scope"));
    store.setDataCenterId((Long) params.get("zoneId"));
    String uuid = (String) params.get("uuid");
    if (uuid != null) {
        store.setUuid(uuid);
    } else {
        store.setUuid(UUID.randomUUID().toString());
    }
    store.setUrl((String) params.get("url"));
    store.setName((String) params.get("name"));
    if (store.getName() == null) {
        store.setName(store.getUuid());
    }
    store.setRole((DataStoreRole) params.get("role"));
    if ("cifs".equalsIgnoreCase((String) params.get("protocol")) && details != null) {
        String user = details.get("user");
        String password = details.get("password");
        String domain = details.get("domain");
        String updatedPath = (String) params.get("url");
        if (user == null || password == null) {
            String errMsg = "Missing cifs user and password details. Add them as details parameter.";
            throw new InvalidParameterValueException(errMsg);
        } else {
            try {
                password = DBEncryptionUtil.encrypt(URLEncoder.encode(password, "UTF-8"));
                details.put("password", password);
                updatedPath += "?user=" + user + "&password=" + password + "&domain=" + domain;
            } catch (UnsupportedEncodingException e) {
                throw new CloudRuntimeException("Error while generating the cifs url. " + e.getMessage());
            }
            store.setUrl(updatedPath);
        }
    }
    store = imageStoreDao.persist(store);
    // persist details
    if (details != null) {
        Iterator<String> keyIter = details.keySet().iterator();
        while (keyIter.hasNext()) {
            String key = keyIter.next().toString();
            String value = details.get(key);
            // encrypt swift key or s3 secret key
            if (key.equals(ApiConstants.KEY) || key.equals(ApiConstants.S3_SECRET_KEY)) {
                value = DBEncryptionUtil.encrypt(value);
            }
            ImageStoreDetailVO detail = new ImageStoreDetailVO(store.getId(), key, value, true);
            imageStoreDetailsDao.persist(detail);
        }
    }
    return store;
}
Also used : ImageStoreDetailVO(org.apache.cloudstack.storage.datastore.db.ImageStoreDetailVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ImageStoreVO(org.apache.cloudstack.storage.datastore.db.ImageStoreVO)

Aggregations

InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ImageStoreDetailVO (org.apache.cloudstack.storage.datastore.db.ImageStoreDetailVO)1 ImageStoreVO (org.apache.cloudstack.storage.datastore.db.ImageStoreVO)1