Search in sources :

Example 1 with CreateSnapshotCmd

use of org.apache.cloudstack.api.command.user.snapshot.CreateSnapshotCmd in project cloudstack by apache.

the class CreateSnapshotCmdTest method testParsingTags.

@Test
public void testParsingTags() {
    final CreateSnapshotCmd createSnapshotCmd = new CreateSnapshotCmd();
    final Map<String, String> tag1 = new HashMap<>();
    tag1.put("key", "key1");
    tag1.put("value", "value1");
    final Map<String, String> tag2 = new HashMap<>();
    tag2.put("key", "key2");
    tag2.put("value", "value2");
    final Map<String, String> expectedTags = new HashMap<>();
    expectedTags.put("key1", "value1");
    expectedTags.put("key2", "value2");
    final Map<String, Map<String, String>> tagsParams = new HashMap<>();
    tagsParams.put("0", tag1);
    tagsParams.put("1", tag2);
    ReflectionTestUtils.setField(createSnapshotCmd, "tags", tagsParams);
    Assert.assertEquals(createSnapshotCmd.getTags(), expectedTags);
}
Also used : HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) CreateSnapshotCmd(org.apache.cloudstack.api.command.user.snapshot.CreateSnapshotCmd) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with CreateSnapshotCmd

use of org.apache.cloudstack.api.command.user.snapshot.CreateSnapshotCmd in project cloudstack by apache.

the class SnapshotSchedulerImpl method scheduleSnapshots.

@DB
protected void scheduleSnapshots() {
    String displayTime = DateUtil.displayDateInTimezone(DateUtil.GMT_TIMEZONE, _currentTimestamp);
    s_logger.debug("Snapshot scheduler.poll is being called at " + displayTime);
    final List<SnapshotScheduleVO> snapshotsToBeExecuted = _snapshotScheduleDao.getSchedulesToExecute(_currentTimestamp);
    s_logger.debug("Got " + snapshotsToBeExecuted.size() + " snapshots to be executed at " + displayTime);
    for (final SnapshotScheduleVO snapshotToBeExecuted : snapshotsToBeExecuted) {
        SnapshotScheduleVO tmpSnapshotScheduleVO = null;
        final long snapshotScheId = snapshotToBeExecuted.getId();
        final long policyId = snapshotToBeExecuted.getPolicyId();
        final long volumeId = snapshotToBeExecuted.getVolumeId();
        try {
            final VolumeVO volume = _volsDao.findById(volumeId);
            if (volume.getPoolId() == null) {
                // this volume is not attached
                continue;
            }
            Account volAcct = _acctDao.findById(volume.getAccountId());
            if (volAcct == null || volAcct.getState() == Account.State.disabled) {
                // this account has been removed, so don't trigger recurring snapshot
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Skip snapshot for volume " + volume.getUuid() + " since its account has been removed or disabled");
                }
                continue;
            }
            if (_snapshotPolicyDao.findById(policyId) == null) {
                _snapshotScheduleDao.remove(snapshotToBeExecuted.getId());
            }
            if (s_logger.isDebugEnabled()) {
                final Date scheduledTimestamp = snapshotToBeExecuted.getScheduledTimestamp();
                displayTime = DateUtil.displayDateInTimezone(DateUtil.GMT_TIMEZONE, scheduledTimestamp);
                s_logger.debug("Scheduling 1 snapshot for volume id " + volumeId + " (volume name:" + volume.getName() + ") for schedule id: " + snapshotToBeExecuted.getId() + " at " + displayTime);
            }
            tmpSnapshotScheduleVO = _snapshotScheduleDao.acquireInLockTable(snapshotScheId);
            final Long eventId = ActionEventUtils.onScheduledActionEvent(User.UID_SYSTEM, volume.getAccountId(), EventTypes.EVENT_SNAPSHOT_CREATE, "creating snapshot for volume Id:" + volume.getUuid(), true, 0);
            final Map<String, String> params = new HashMap<String, String>();
            params.put(ApiConstants.VOLUME_ID, "" + volumeId);
            params.put(ApiConstants.POLICY_ID, "" + policyId);
            params.put("ctxUserId", "1");
            params.put("ctxAccountId", "" + volume.getAccountId());
            params.put("ctxStartEventId", String.valueOf(eventId));
            List<? extends ResourceTag> resourceTags = taggedResourceService.listByResourceTypeAndId(ResourceTag.ResourceObjectType.SnapshotPolicy, policyId);
            if (resourceTags != null && !resourceTags.isEmpty()) {
                int tagNumber = 0;
                for (ResourceTag resourceTag : resourceTags) {
                    params.put("tags[" + tagNumber + "].key", resourceTag.getKey());
                    params.put("tags[" + tagNumber + "].value", resourceTag.getValue());
                    tagNumber++;
                }
            }
            final CreateSnapshotCmd cmd = new CreateSnapshotCmd();
            ComponentContext.inject(cmd);
            _dispatcher.dispatchCreateCmd(cmd, params);
            params.put("id", "" + cmd.getEntityId());
            params.put("ctxStartEventId", "1");
            AsyncJobVO job = new AsyncJobVO("", User.UID_SYSTEM, volume.getAccountId(), CreateSnapshotCmd.class.getName(), ApiGsonHelper.getBuilder().create().toJson(params), cmd.getEntityId(), cmd.getInstanceType() != null ? cmd.getInstanceType().toString() : null, null);
            job.setDispatcher(_asyncDispatcher.getName());
            final long jobId = _asyncMgr.submitAsyncJob(job);
            tmpSnapshotScheduleVO.setAsyncJobId(jobId);
            _snapshotScheduleDao.update(snapshotScheId, tmpSnapshotScheduleVO);
        } catch (final Exception e) {
            // TODO Logging this exception is enough?
            s_logger.warn("Scheduling snapshot failed due to " + e.toString());
        } finally {
            if (tmpSnapshotScheduleVO != null) {
                _snapshotScheduleDao.releaseFromLockTable(snapshotScheId);
            }
        }
    }
}
Also used : Account(com.cloud.user.Account) HashMap(java.util.HashMap) CreateSnapshotCmd(org.apache.cloudstack.api.command.user.snapshot.CreateSnapshotCmd) Date(java.util.Date) ConfigurationException(javax.naming.ConfigurationException) AsyncJobVO(org.apache.cloudstack.framework.jobs.impl.AsyncJobVO) ResourceTag(com.cloud.server.ResourceTag) VolumeVO(com.cloud.storage.VolumeVO) SnapshotScheduleVO(com.cloud.storage.SnapshotScheduleVO) DB(com.cloud.utils.db.DB)

Aggregations

HashMap (java.util.HashMap)2 CreateSnapshotCmd (org.apache.cloudstack.api.command.user.snapshot.CreateSnapshotCmd)2 ResourceTag (com.cloud.server.ResourceTag)1 SnapshotScheduleVO (com.cloud.storage.SnapshotScheduleVO)1 VolumeVO (com.cloud.storage.VolumeVO)1 Account (com.cloud.user.Account)1 DB (com.cloud.utils.db.DB)1 Date (java.util.Date)1 Map (java.util.Map)1 ConfigurationException (javax.naming.ConfigurationException)1 AsyncJobVO (org.apache.cloudstack.framework.jobs.impl.AsyncJobVO)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1