Skip to content
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8f806ff
API: Add support to list all snapshot policies & backup schedules
Pearl1594 Sep 5, 2025
619f01e
Add support for backup policy listing without tying it to the vmid
Pearl1594 Sep 8, 2025
0a6ac67
add tests for snapshot policy listing
Pearl1594 Sep 8, 2025
2e2c12d
update tests for listbackupschedules
Pearl1594 Sep 9, 2025
7ec4f5d
remove trailing spaces and fix lint failure
Pearl1594 Sep 9, 2025
1e91364
Merge branch 'main' of https://github.com/apache/cloudstack into supp…
Pearl1594 Sep 10, 2025
452b0e6
Add upgrade test
Pearl1594 Sep 10, 2025
a759acc
remove unused import
Pearl1594 Sep 10, 2025
7b9a1b4
add create policy - snap/backup in the list view with resource (volum…
Pearl1594 Sep 10, 2025
a0af6e7
add translations
Pearl1594 Sep 10, 2025
c31f278
refresh parent list
Pearl1594 Sep 11, 2025
e420927
remove unnecessary alert info
Pearl1594 Sep 11, 2025
7367a41
fix checks for UI backup schedule list view
Pearl1594 Sep 11, 2025
dbb2955
fix checks for UI backup schedule list view
Pearl1594 Sep 11, 2025
6dee31f
Merge branch 'support-list-policies' of https://github.com/apache/clo…
Pearl1594 Sep 12, 2025
376ac43
Merge branch 'main' of https://github.com/apache/cloudstack into supp…
Pearl1594 Sep 12, 2025
88d298f
add back access checks
Pearl1594 Sep 12, 2025
fd36bcb
add since param
Pearl1594 Sep 12, 2025
3882d33
fix failing test
Pearl1594 Sep 12, 2025
074d466
update snapshot policy and backup schedule ownership when VM is moved
Pearl1594 Sep 19, 2025
b71e323
fix issue with showing vm selection
Pearl1594 Sep 19, 2025
568178d
fix unit test failure
Pearl1594 Sep 19, 2025
82a2ef3
Update list snappolicy & backup schedule logic to list only those tha…
Pearl1594 Sep 24, 2025
5b8e568
Merge branch 'main' of https://github.com/apache/cloudstack into supp…
Pearl1594 Sep 24, 2025
def6dad
fix test
Pearl1594 Sep 24, 2025
b68b727
support snap / backup policy search using keyword
Pearl1594 Sep 26, 2025
7a73ebe
Merge branch 'main' of https://github.com/apache/cloudstack into supp…
Pearl1594 Sep 26, 2025
e29b752
fix tests
Pearl1594 Sep 26, 2025
24b2f73
Merge branch 'main' of https://github.com/apache/cloudstack into supp…
Pearl1594 Oct 7, 2025
016b608
Merge branch 'main' of https://github.com/apache/cloudstack into supp…
Pearl1594 Oct 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public interface SnapshotApiService {
* the command that specifies the volume criteria
* @return list of snapshot policies
*/
Pair<List<? extends SnapshotPolicy>, Integer> listPoliciesforVolume(ListSnapshotPoliciesCmd cmd);
Pair<List<? extends SnapshotPolicy>, Integer> listSnapshotPolicies(ListSnapshotPoliciesCmd cmd);

boolean deleteSnapshotPolicies(DeleteSnapshotPoliciesCmd cmd);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
// under the License.
package com.cloud.storage.snapshot;

import org.apache.cloudstack.acl.ControlledEntity;
import org.apache.cloudstack.api.Displayable;
import org.apache.cloudstack.api.Identity;
import org.apache.cloudstack.api.InternalIdentity;

public interface SnapshotPolicy extends Identity, InternalIdentity, Displayable {
public interface SnapshotPolicy extends ControlledEntity, Identity, InternalIdentity, Displayable {

long getVolumeId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.ApiErrorCode;
import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.BaseListDomainResourcesCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.BackupScheduleResponse;
Expand All @@ -39,7 +39,6 @@
import com.cloud.exception.NetworkRuleConflictException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.utils.exception.CloudRuntimeException;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -48,10 +47,10 @@
description = "List backup schedule of a VM",
responseObject = BackupScheduleResponse.class, since = "4.14.0",
authorized = {RoleType.Admin, RoleType.ResourceAdmin, RoleType.DomainAdmin, RoleType.User})
public class ListBackupScheduleCmd extends BaseCmd {
public class ListBackupScheduleCmd extends BaseListDomainResourcesCmd {

@Inject
private BackupManager backupManager;
BackupManager backupManager;

/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
Expand All @@ -60,10 +59,16 @@ public class ListBackupScheduleCmd extends BaseCmd {
@Parameter(name = ApiConstants.VIRTUAL_MACHINE_ID,
type = CommandType.UUID,
entityType = UserVmResponse.class,
required = true,
description = "ID of the VM")
private Long vmId;

@Parameter(name = ApiConstants.ID,
type = CommandType.UUID,
entityType = BackupScheduleResponse.class,
description = "the ID of the backup schedule",
since = "4.22.0")
private Long id;

/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
Expand All @@ -72,26 +77,29 @@ public Long getVmId() {
return vmId;
}

public Long getId() {
return id;
}

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////

@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException {
try{
List<BackupSchedule> schedules = backupManager.listBackupSchedule(getVmId());
List<BackupSchedule> schedules = backupManager.listBackupSchedules(this);
ListResponse<BackupScheduleResponse> response = new ListResponse<>();
List<BackupScheduleResponse> scheduleResponses = new ArrayList<>();

if (!CollectionUtils.isNullOrEmpty(schedules)) {
for (BackupSchedule schedule : schedules) {
scheduleResponses.add(_responseGenerator.createBackupScheduleResponse(schedule));
}
response.setResponses(scheduleResponses, schedules.size());
response.setResponseName(getCommandName());
setResponseObject(response);
} else {
throw new CloudRuntimeException("No backup schedule exists for the VM");
}
response.setResponses(scheduleResponses, schedules.size());
response.setResponseName(getCommandName());
setResponseObject(response);
} catch (Exception e) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseListCmd;
import org.apache.cloudstack.api.BaseListDomainResourcesCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.api.response.SnapshotPolicyResponse;
Expand All @@ -34,7 +34,7 @@

@APICommand(name = "listSnapshotPolicies", description = "Lists snapshot policies.", responseObject = SnapshotPolicyResponse.class,
requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
public class ListSnapshotPoliciesCmd extends BaseListCmd {
public class ListSnapshotPoliciesCmd extends BaseListDomainResourcesCmd {


/////////////////////////////////////////////////////
Expand Down Expand Up @@ -69,13 +69,14 @@ public boolean isDisplay() {
public Long getId() {
return id;
}

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////

@Override
public void execute() {
Pair<List<? extends SnapshotPolicy>, Integer> result = _snapshotService.listPoliciesforVolume(this);
Pair<List<? extends SnapshotPolicy>, Integer> result = _snapshotService.listSnapshotPolicies(this);
ListResponse<SnapshotPolicyResponse> response = new ListResponse<SnapshotPolicyResponse>();
List<SnapshotPolicyResponse> policyResponses = new ArrayList<SnapshotPolicyResponse>();
for (SnapshotPolicy policy : result.first()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public class SnapshotPolicyResponse extends BaseResponseWithTagInformation {
@Param(description = "the ID of the disk volume")
private String volumeId;

@SerializedName("volumename")
@Param(description = "the name of the disk volume")
private String volumeName;

@SerializedName("schedule")
@Param(description = "time the snapshot is scheduled to be taken.")
private String schedule;
Expand Down Expand Up @@ -87,6 +91,10 @@ public void setVolumeId(String volumeId) {
this.volumeId = volumeId;
}

public void setVolumeName(String volumeName) {
this.volumeName = volumeName;
}

public String getSchedule() {
return schedule;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.cloudstack.api.command.user.backup.CreateBackupScheduleCmd;
import org.apache.cloudstack.api.command.user.backup.DeleteBackupScheduleCmd;
import org.apache.cloudstack.api.command.user.backup.ListBackupOfferingsCmd;
import org.apache.cloudstack.api.command.user.backup.ListBackupScheduleCmd;
import org.apache.cloudstack.api.command.user.backup.ListBackupsCmd;
import org.apache.cloudstack.api.response.BackupResponse;
import org.apache.cloudstack.framework.config.ConfigKey;
Expand Down Expand Up @@ -174,7 +175,7 @@ public interface BackupManager extends BackupService, Configurable, PluggableSer
* @param vmId
* @return
*/
List<BackupSchedule> listBackupSchedule(Long vmId);
List<BackupSchedule> listBackupSchedules(ListBackupScheduleCmd cmd);

/**
* Deletes VM backup schedule for a VM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@

import java.util.Date;

import org.apache.cloudstack.acl.ControlledEntity;
import org.apache.cloudstack.api.InternalIdentity;

import com.cloud.utils.DateUtil;

public interface BackupSchedule extends InternalIdentity {
public interface BackupSchedule extends ControlledEntity, InternalIdentity {
Long getVmId();
DateUtil.IntervalType getScheduleType();
String getSchedule();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.user.backup;

import com.cloud.exception.InsufficientCapacityException;
import com.cloud.exception.NetworkRuleConflictException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.user.Account;
import org.apache.cloudstack.api.ResponseGenerator;
import org.apache.cloudstack.api.response.BackupScheduleResponse;
import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.backup.BackupManager;
import org.apache.cloudstack.backup.BackupSchedule;
import org.apache.cloudstack.context.CallContext;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.ArrayList;
import java.util.List;

@RunWith(MockitoJUnitRunner.class)
public class ListBackupScheduleCmdTest {

@Mock
private BackupManager backupManager;

@Mock
private ResponseGenerator responseGenerator;

private ListBackupScheduleCmd cmd;

@Before
public void setUp() {
cmd = new ListBackupScheduleCmd();
cmd.backupManager = backupManager;
cmd._responseGenerator = responseGenerator;
}

@Test
public void testExecuteWithSchedules() throws ResourceUnavailableException, InsufficientCapacityException, ResourceAllocationException, NetworkRuleConflictException {
BackupSchedule schedule = Mockito.mock(BackupSchedule.class);
BackupScheduleResponse scheduleResponse = Mockito.mock(BackupScheduleResponse.class);
List<BackupSchedule> schedules = new ArrayList<>();
schedules.add(schedule);

Mockito.when(backupManager.listBackupSchedules(cmd)).thenReturn(schedules);
Mockito.when(responseGenerator.createBackupScheduleResponse(schedule)).thenReturn(scheduleResponse);

Account mockAccount = Mockito.mock(Account.class);
CallContext callContext = Mockito.mock(CallContext.class);
try (org.mockito.MockedStatic<CallContext> mocked = Mockito.mockStatic(CallContext.class)) {
cmd.execute();
}

ListResponse<?> response = (ListResponse<?>) cmd.getResponseObject();
Assert.assertNotNull(response);
Assert.assertEquals(1, response.getResponses().size());
Assert.assertEquals(scheduleResponse, response.getResponses().get(0));
}

@Test
public void testExecuteWithNoSchedules() {
Mockito.when(backupManager.listBackupSchedules(cmd)).thenReturn(new ArrayList<>());
CallContext callContext = Mockito.mock(CallContext.class);

try (org.mockito.MockedStatic<CallContext> mocked = Mockito.mockStatic(CallContext.class)) {
mocked.when(CallContext::current).thenReturn(callContext);
cmd.execute();
} catch (ResourceUnavailableException | InsufficientCapacityException | ResourceAllocationException |
NetworkRuleConflictException e) {
throw new RuntimeException(e);
}

ListResponse<?> response = (ListResponse<?>) cmd.getResponseObject();
Assert.assertNotNull(response);
Assert.assertEquals(0, response.getResponses().size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.user.snapshot;

import com.cloud.storage.snapshot.SnapshotApiService;
import com.cloud.storage.snapshot.SnapshotPolicy;
import com.cloud.utils.Pair;
import org.apache.cloudstack.api.ResponseGenerator;
import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.api.response.SnapshotPolicyResponse;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import java.util.ArrayList;
import java.util.List;

public class ListSnapshotPoliciesCmdTest {
private ListSnapshotPoliciesCmd cmd;
private SnapshotApiService snapshotService;
private ResponseGenerator responseGenerator;

@Before
public void setUp() {
cmd = new ListSnapshotPoliciesCmd();
snapshotService = Mockito.mock(SnapshotApiService.class);
responseGenerator = Mockito.mock(ResponseGenerator.class);

cmd._snapshotService = snapshotService;
cmd._responseGenerator = responseGenerator;
}

@Test
public void testExecuteWithPolicies() {
SnapshotPolicy policy = Mockito.mock(SnapshotPolicy.class);
SnapshotPolicyResponse policyResponse = Mockito.mock(SnapshotPolicyResponse.class);
List<SnapshotPolicy> policies = new ArrayList<>();
policies.add(policy);

Mockito.when(snapshotService.listSnapshotPolicies(cmd))
.thenReturn(new Pair<>(policies, 1));
Mockito.when(responseGenerator.createSnapshotPolicyResponse(policy))
.thenReturn(policyResponse);

cmd.execute();

ListResponse<?> response = (ListResponse<?>) cmd.getResponseObject();
Assert.assertNotNull(response);
Assert.assertEquals(1, response.getResponses().size());
Assert.assertEquals(policyResponse, response.getResponses().get(0));
}

@Test
public void testExecuteWithNoPolicies() {
Mockito.when(snapshotService.listSnapshotPolicies(cmd))
.thenReturn(new Pair<>(new ArrayList<>(), 0));

cmd.execute();

ListResponse<?> response = (ListResponse<?>) cmd.getResponseObject();
Assert.assertNotNull(response);
Assert.assertTrue(response.getResponses().isEmpty());
}
}
Loading
Loading