Search in sources :

Example 16 with ImageStoreVO

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

the class ImageStoreHelper method createImageStore.

public ImageStoreVO createImageStore(Map<String, Object> params) {
    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.setName((String) params.get("name"));
    if (store.getName() == null) {
        store.setName(store.getUuid());
    }
    store.setUrl((String) params.get("url"));
    store.setRole((DataStoreRole) params.get("role"));
    store = imageStoreDao.persist(store);
    return store;
}
Also used : ImageStoreVO(org.apache.cloudstack.storage.datastore.db.ImageStoreVO)

Example 17 with ImageStoreVO

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

the class ImageStoreDetailsUtilTest method setup.

@Before
public void setup() throws Exception {
    Map<String, String> imgStoreDetails = new HashMap<String, String>();
    String nfsVersionKey = CapacityManager.ImageStoreNFSVersion.key();
    imgStoreDetails.put(nfsVersionKey, String.valueOf(NFS_VERSION));
    when(imgStoreDetailsDao.getDetails(STORE_ID)).thenReturn(imgStoreDetails);
    ImageStoreVO imgStoreVO = mock(ImageStoreVO.class);
    when(imgStoreVO.getId()).thenReturn(Long.valueOf(STORE_ID));
    when(imgStoreDao.findByUuid(STORE_UUID)).thenReturn(imgStoreVO);
    ConfigurationVO confVO = mock(ConfigurationVO.class);
    String defaultValue = (NFS_VERSION_DEFAULT == null ? null : String.valueOf(NFS_VERSION_DEFAULT));
    when(confVO.getValue()).thenReturn(defaultValue);
    when(configurationDao.findByName(nfsVersionKey)).thenReturn(confVO);
    imageStoreDetailsUtil.imageStoreDao = imgStoreDao;
    imageStoreDetailsUtil.imageStoreDetailsDao = imgStoreDetailsDao;
    imageStoreDetailsUtil.configurationDao = configurationDao;
}
Also used : ConfigurationVO(org.apache.cloudstack.framework.config.impl.ConfigurationVO) HashMap(java.util.HashMap) ImageStoreVO(org.apache.cloudstack.storage.datastore.db.ImageStoreVO) Before(org.junit.Before)

Example 18 with ImageStoreVO

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

the class ConfigurationManagerImpl method updateConfiguration.

@Override
@DB
public String updateConfiguration(final long userId, final String name, final String category, final String value, final String scope, final Long resourceId) {
    final String validationMsg = validateConfigurationValue(name, value, scope);
    if (validationMsg != null) {
        s_logger.error("Invalid configuration option, name: " + name + ", value:" + value);
        throw new InvalidParameterValueException(validationMsg);
    }
    // global parameter updation
    if (scope != null && !scope.isEmpty() && !ConfigKey.Scope.Global.toString().equalsIgnoreCase(scope)) {
        switch(ConfigKey.Scope.valueOf(scope)) {
            case Zone:
                final DataCenterVO zone = _zoneDao.findById(resourceId);
                if (zone == null) {
                    throw new InvalidParameterValueException("unable to find zone by id " + resourceId);
                }
                _dcDetailsDao.addDetail(resourceId, name, value, true);
                break;
            case Cluster:
                final ClusterVO cluster = _clusterDao.findById(resourceId);
                if (cluster == null) {
                    throw new InvalidParameterValueException("unable to find cluster by id " + resourceId);
                }
                ClusterDetailsVO clusterDetailsVO = _clusterDetailsDao.findDetail(resourceId, name);
                if (clusterDetailsVO == null) {
                    clusterDetailsVO = new ClusterDetailsVO(resourceId, name, value);
                    _clusterDetailsDao.persist(clusterDetailsVO);
                } else {
                    clusterDetailsVO.setValue(value);
                    _clusterDetailsDao.update(clusterDetailsVO.getId(), clusterDetailsVO);
                }
                break;
            case StoragePool:
                final StoragePoolVO pool = _storagePoolDao.findById(resourceId);
                if (pool == null) {
                    throw new InvalidParameterValueException("unable to find storage pool by id " + resourceId);
                }
                if (name.equals(CapacityManager.StorageOverprovisioningFactor.key())) {
                    if (!pool.getPoolType().supportsOverProvisioning()) {
                        throw new InvalidParameterValueException("Unable to update  storage pool with id " + resourceId + ". Overprovision not supported for " + pool.getPoolType());
                    }
                }
                _storagePoolDetailsDao.addDetail(resourceId, name, value, true);
                break;
            case Account:
                final AccountVO account = _accountDao.findById(resourceId);
                if (account == null) {
                    throw new InvalidParameterValueException("unable to find account by id " + resourceId);
                }
                AccountDetailVO accountDetailVO = _accountDetailsDao.findDetail(resourceId, name);
                if (accountDetailVO == null) {
                    accountDetailVO = new AccountDetailVO(resourceId, name, value);
                    _accountDetailsDao.persist(accountDetailVO);
                } else {
                    accountDetailVO.setValue(value);
                    _accountDetailsDao.update(accountDetailVO.getId(), accountDetailVO);
                }
                break;
            case ImageStore:
                final ImageStoreVO imgStore = _imageStoreDao.findById(resourceId);
                Preconditions.checkState(imgStore != null);
                _imageStoreDetailsDao.addDetail(resourceId, name, value, true);
                break;
            default:
                throw new InvalidParameterValueException("Scope provided is invalid");
        }
        return value;
    }
    // Execute all updates in a single transaction
    final TransactionLegacy txn = TransactionLegacy.currentTxn();
    txn.start();
    if (!_configDao.update(name, category, value)) {
        s_logger.error("Failed to update configuration option, name: " + name + ", value:" + value);
        throw new CloudRuntimeException("Failed to update configuration value. Please contact Cloud Support.");
    }
    PreparedStatement pstmt = null;
    if (Config.XenServerGuestNetwork.key().equalsIgnoreCase(name)) {
        final String sql = "update host_details set value=? where name=?";
        try {
            pstmt = txn.prepareAutoCloseStatement(sql);
            pstmt.setString(1, value);
            pstmt.setString(2, "guest.network.device");
            pstmt.executeUpdate();
        } catch (final Throwable e) {
            throw new CloudRuntimeException("Failed to update guest.network.device in host_details due to exception ", e);
        }
    } else if (Config.XenServerPrivateNetwork.key().equalsIgnoreCase(name)) {
        final String sql = "update host_details set value=? where name=?";
        try {
            pstmt = txn.prepareAutoCloseStatement(sql);
            pstmt.setString(1, value);
            pstmt.setString(2, "private.network.device");
            pstmt.executeUpdate();
        } catch (final Throwable e) {
            throw new CloudRuntimeException("Failed to update private.network.device in host_details due to exception ", e);
        }
    } else if (Config.XenServerPublicNetwork.key().equalsIgnoreCase(name)) {
        final String sql = "update host_details set value=? where name=?";
        try {
            pstmt = txn.prepareAutoCloseStatement(sql);
            pstmt.setString(1, value);
            pstmt.setString(2, "public.network.device");
            pstmt.executeUpdate();
        } catch (final Throwable e) {
            throw new CloudRuntimeException("Failed to update public.network.device in host_details due to exception ", e);
        }
    } else if (Config.XenServerStorageNetwork1.key().equalsIgnoreCase(name)) {
        final String sql = "update host_details set value=? where name=?";
        try {
            pstmt = txn.prepareAutoCloseStatement(sql);
            pstmt.setString(1, value);
            pstmt.setString(2, "storage.network.device1");
            pstmt.executeUpdate();
        } catch (final Throwable e) {
            throw new CloudRuntimeException("Failed to update storage.network.device1 in host_details due to exception ", e);
        }
    } else if (Config.XenServerStorageNetwork2.key().equals(name)) {
        final String sql = "update host_details set value=? where name=?";
        try {
            pstmt = txn.prepareAutoCloseStatement(sql);
            pstmt.setString(1, value);
            pstmt.setString(2, "storage.network.device2");
            pstmt.executeUpdate();
        } catch (final Throwable e) {
            throw new CloudRuntimeException("Failed to update storage.network.device2 in host_details due to exception ", e);
        }
    } else if (Config.SecStorageSecureCopyCert.key().equalsIgnoreCase(name)) {
        //FIXME - Ideally there should be a listener model to listen to global config changes and be able to take action gracefully.
        //Expire the download urls
        final String sqlTemplate = "update template_store_ref set download_url_created=?";
        final String sqlVolume = "update volume_store_ref set download_url_created=?";
        try {
            // Change for templates
            pstmt = txn.prepareAutoCloseStatement(sqlTemplate);
            // Set the time before the epoch time.
            pstmt.setDate(1, new Date(-1l));
            pstmt.executeUpdate();
            // Change for volumes
            pstmt = txn.prepareAutoCloseStatement(sqlVolume);
            // Set the time before the epoch time.
            pstmt.setDate(1, new Date(-1l));
            pstmt.executeUpdate();
            // Cleanup the download urls
            _storageManager.cleanupDownloadUrls();
        } catch (final Throwable e) {
            throw new CloudRuntimeException("Failed to clean up download URLs in template_store_ref or volume_store_ref due to exception ", e);
        }
    }
    txn.commit();
    return _configDao.getValue(name);
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) ClusterVO(com.cloud.dc.ClusterVO) PreparedStatement(java.sql.PreparedStatement) AccountVO(com.cloud.user.AccountVO) Date(java.sql.Date) TransactionLegacy(com.cloud.utils.db.TransactionLegacy) AccountDetailVO(com.cloud.user.AccountDetailVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) ImageStoreVO(org.apache.cloudstack.storage.datastore.db.ImageStoreVO) ClusterDetailsVO(com.cloud.dc.ClusterDetailsVO) DB(com.cloud.utils.db.DB)

Example 19 with ImageStoreVO

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

the class S3ImageStoreLifeCycleImpl method initialize.

@SuppressWarnings("unchecked")
@Override
public DataStore initialize(Map<String, Object> dsInfos) {
    String url = (String) dsInfos.get("url");
    String name = (String) dsInfos.get("name");
    String providerName = (String) dsInfos.get("providerName");
    ScopeType scope = (ScopeType) dsInfos.get("scope");
    DataStoreRole role = (DataStoreRole) dsInfos.get("role");
    Map<String, String> details = (Map<String, String>) dsInfos.get("details");
    s_logger.info("Trying to add a S3 store with endpoint: " + details.get(ApiConstants.S3_END_POINT));
    Map<String, Object> imageStoreParameters = new HashMap();
    imageStoreParameters.put("name", name);
    imageStoreParameters.put("url", url);
    String protocol = "http";
    String useHttps = details.get(ApiConstants.S3_HTTPS_FLAG);
    if (useHttps != null && Boolean.parseBoolean(useHttps)) {
        protocol = "https";
    }
    imageStoreParameters.put("protocol", protocol);
    if (scope != null) {
        imageStoreParameters.put("scope", scope);
    } else {
        imageStoreParameters.put("scope", ScopeType.REGION);
    }
    imageStoreParameters.put("providerName", providerName);
    imageStoreParameters.put("role", role);
    ImageStoreVO ids = imageStoreHelper.createImageStore(imageStoreParameters, details);
    return imageStoreMgr.getImageStore(ids.getId());
}
Also used : ScopeType(com.cloud.storage.ScopeType) DataStoreRole(com.cloud.storage.DataStoreRole) HashMap(java.util.HashMap) ImageStoreVO(org.apache.cloudstack.storage.datastore.db.ImageStoreVO) HashMap(java.util.HashMap) Map(java.util.Map)

Example 20 with ImageStoreVO

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

the class TemplateManagerImplTest method testCreatePrivateTemplateRecordForRegionStore.

@Test
public void testCreatePrivateTemplateRecordForRegionStore() throws ResourceAllocationException {
    CreateTemplateCmd mockCreateCmd = mock(CreateTemplateCmd.class);
    when(mockCreateCmd.getTemplateName()).thenReturn("test");
    when(mockCreateCmd.getTemplateTag()).thenReturn(null);
    when(mockCreateCmd.getBits()).thenReturn(64);
    when(mockCreateCmd.getRequiresHvm()).thenReturn(true);
    when(mockCreateCmd.isPasswordEnabled()).thenReturn(false);
    when(mockCreateCmd.isPublic()).thenReturn(false);
    when(mockCreateCmd.isFeatured()).thenReturn(false);
    when(mockCreateCmd.isDynamicallyScalable()).thenReturn(false);
    when(mockCreateCmd.getVolumeId()).thenReturn(null);
    when(mockCreateCmd.getSnapshotId()).thenReturn(1L);
    when(mockCreateCmd.getOsTypeId()).thenReturn(1L);
    when(mockCreateCmd.getEventDescription()).thenReturn("test");
    when(mockCreateCmd.getDetails()).thenReturn(null);
    Account mockTemplateOwner = mock(Account.class);
    SnapshotVO mockSnapshot = mock(SnapshotVO.class);
    when(snapshotDao.findById(anyLong())).thenReturn(mockSnapshot);
    when(mockSnapshot.getVolumeId()).thenReturn(1L);
    when(mockSnapshot.getState()).thenReturn(Snapshot.State.BackedUp);
    when(mockSnapshot.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.XenServer);
    doNothing().when(resourceLimitMgr).checkResourceLimit(any(Account.class), eq(Resource.ResourceType.template));
    doNothing().when(resourceLimitMgr).checkResourceLimit(any(Account.class), eq(Resource.ResourceType.secondary_storage), anyLong());
    GuestOSVO mockGuestOS = mock(GuestOSVO.class);
    when(guestOSDao.findById(anyLong())).thenReturn(mockGuestOS);
    when(tmpltDao.getNextInSequence(eq(Long.class), eq("id"))).thenReturn(1L);
    List<ImageStoreVO> mockRegionStores = new ArrayList<>();
    ImageStoreVO mockRegionStore = mock(ImageStoreVO.class);
    mockRegionStores.add(mockRegionStore);
    when(imgStoreDao.findRegionImageStores()).thenReturn(mockRegionStores);
    when(tmpltDao.persist(any(VMTemplateVO.class))).thenAnswer(new Answer<VMTemplateVO>() {

        @Override
        public VMTemplateVO answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            return (VMTemplateVO) args[0];
        }
    });
    VMTemplateVO template = templateManager.createPrivateTemplateRecord(mockCreateCmd, mockTemplateOwner);
    assertTrue("Template in a region store should have cross zones set", template.isCrossZones());
}
Also used : Account(com.cloud.user.Account) ArrayList(java.util.ArrayList) VMTemplateVO(com.cloud.storage.VMTemplateVO) GuestOSVO(com.cloud.storage.GuestOSVO) SnapshotVO(com.cloud.storage.SnapshotVO) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Matchers.anyLong(org.mockito.Matchers.anyLong) CreateTemplateCmd(org.apache.cloudstack.api.command.user.template.CreateTemplateCmd) ImageStoreVO(org.apache.cloudstack.storage.datastore.db.ImageStoreVO) Test(org.junit.Test)

Aggregations

ImageStoreVO (org.apache.cloudstack.storage.datastore.db.ImageStoreVO)38 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)14 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)12 Account (com.cloud.user.Account)10 DataCenterVO (com.cloud.dc.DataCenterVO)9 HashMap (java.util.HashMap)9 VMTemplateVO (com.cloud.storage.VMTemplateVO)8 ArrayList (java.util.ArrayList)8 ClusterVO (com.cloud.dc.ClusterVO)6 HostPodVO (com.cloud.dc.HostPodVO)5 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)5 Map (java.util.Map)5 HostVO (com.cloud.host.HostVO)4 DataStoreRole (com.cloud.storage.DataStoreRole)4 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)3 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)3 URISyntaxException (java.net.URISyntaxException)3 DataStoreProvider (org.apache.cloudstack.engine.subsystem.api.storage.DataStoreProvider)3 Test (org.testng.annotations.Test)3 ClusterDetailsVO (com.cloud.dc.ClusterDetailsVO)2