Skip to content

Commit 568c1aa

Browse files
committed
minor cleanup and tests
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent 574f0ea commit 568c1aa

46 files changed

Lines changed: 5383 additions & 217 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

plugins/integrations/veeam-control-service/src/main/java/org/apache/cloudstack/veeam/VeeamControlServlet.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020

2121
import java.io.IOException;
22+
import java.util.HashMap;
2223
import java.util.List;
2324
import java.util.Map;
2425

@@ -59,7 +60,7 @@ public Mapper getMapper() {
5960
@Override
6061
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
6162
String method = req.getMethod();
62-
String path = normalize(req.getPathInfo());
63+
String path = normalize(req);
6364
Negotiation.OutFormat outFormat = Negotiation.responseFormat(req);
6465

6566
LOGGER.info("Received {} request for {} with out format: {}", method, path, outFormat);
@@ -107,23 +108,29 @@ private static void logRequest(HttpServletRequest req, String method, String pat
107108
}
108109
}
109110

110-
private String normalize(String pathInfo) {
111-
if (pathInfo == null || pathInfo.isBlank()) return "/";
112-
return pathInfo;
111+
private String normalize(HttpServletRequest req) {
112+
String path = req.getPathInfo();
113+
if (path == null || path.isBlank()) {
114+
path = req.getRequestURI();
115+
}
116+
if (path == null || path.isBlank()) {
117+
return "/";
118+
}
119+
return path;
113120
}
114121

115122
protected void handleRoot(HttpServletRequest req, HttpServletResponse resp, Negotiation.OutFormat outFormat)
116123
throws IOException {
117124

118125
String method = req.getMethod();
119126
if (!"GET".equals(method) && !"POST".equals(method)) {
120-
// You didn’t list 405; keep it simple with 400
121127
throw Error.badRequest("Unsupported method for root: " + method);
122128
}
129+
Map<String, String> responseData = new HashMap<>();
130+
responseData.put("name", VeeamControlService.PLUGIN_NAME);
131+
responseData.put("pluginVersion", this.getClass().getPackage().getImplementationVersion());
123132

124-
writer.write(resp, 200, Map.of(
125-
"name", VeeamControlService.PLUGIN_NAME,
126-
"pluginVersion", this.getClass().getPackage().getImplementationVersion()), outFormat);
133+
writer.write(resp, 200, responseData, outFormat);
127134
}
128135

129136
public void methodNotAllowed(final HttpServletResponse resp, final String allow, final Negotiation.OutFormat outFormat) throws IOException {

plugins/integrations/veeam-control-service/src/main/java/org/apache/cloudstack/veeam/adapter/ServerAdapter.java

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,11 @@
2626
import java.util.List;
2727
import java.util.Map;
2828
import java.util.Objects;
29-
import java.util.UUID;
3029
import java.util.concurrent.TimeUnit;
3130
import java.util.stream.Collectors;
3231

3332
import javax.inject.Inject;
3433

35-
import org.apache.cloudstack.acl.Role;
36-
import org.apache.cloudstack.acl.RolePermissionEntity;
37-
import org.apache.cloudstack.acl.RoleService;
38-
import org.apache.cloudstack.acl.RoleType;
39-
import org.apache.cloudstack.acl.Rule;
4034
import org.apache.cloudstack.acl.SecurityChecker;
4135
import org.apache.cloudstack.affinity.AffinityGroupVO;
4236
import org.apache.cloudstack.affinity.dao.AffinityGroupDao;
@@ -58,7 +52,6 @@
5852
import org.apache.cloudstack.api.command.admin.vm.DeployVMCmdByAdmin;
5953
import org.apache.cloudstack.api.command.user.backup.ListBackupsCmd;
6054
import org.apache.cloudstack.api.command.user.job.ListAsyncJobsCmd;
61-
import org.apache.cloudstack.api.command.user.job.QueryAsyncJobResultCmd;
6255
import org.apache.cloudstack.api.command.user.network.ListNetworksCmd;
6356
import org.apache.cloudstack.api.command.user.offering.ListServiceOfferingsCmd;
6457
import org.apache.cloudstack.api.command.user.tag.ListTagsCmd;
@@ -77,11 +70,8 @@
7770
import org.apache.cloudstack.api.command.user.volume.AssignVolumeCmd;
7871
import org.apache.cloudstack.api.command.user.volume.AttachVolumeCmd;
7972
import org.apache.cloudstack.api.command.user.volume.CreateVolumeCmd;
80-
import org.apache.cloudstack.api.command.user.volume.DeleteVolumeCmd;
8173
import org.apache.cloudstack.api.command.user.volume.DestroyVolumeCmd;
82-
import org.apache.cloudstack.api.command.user.volume.DetachVolumeCmd;
8374
import org.apache.cloudstack.api.command.user.volume.ListVolumesCmd;
84-
import org.apache.cloudstack.api.command.user.volume.ResizeVolumeCmd;
8575
import org.apache.cloudstack.api.command.user.volume.UpdateVolumeCmd;
8676
import org.apache.cloudstack.api.command.user.zone.ListZonesCmd;
8777
import org.apache.cloudstack.api.response.ListResponse;
@@ -100,7 +90,6 @@
10090
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
10191
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
10292
import org.apache.cloudstack.veeam.VeeamControlService;
103-
import org.apache.cloudstack.veeam.api.TagsRouteHandler;
10493
import org.apache.cloudstack.veeam.api.converter.AsyncJobJoinVOToJobConverter;
10594
import org.apache.cloudstack.veeam.api.converter.BackupVOToBackupConverter;
10695
import org.apache.cloudstack.veeam.api.converter.ClusterVOToClusterConverter;
@@ -190,7 +179,6 @@
190179
import com.cloud.user.AccountService;
191180
import com.cloud.user.DomainService;
192181
import com.cloud.user.User;
193-
import com.cloud.user.UserAccount;
194182
import com.cloud.user.UserDataVO;
195183
import com.cloud.user.dao.UserDataDao;
196184
import com.cloud.uservm.UserVm;
@@ -211,28 +199,7 @@
211199
import com.cloud.vm.snapshot.VMSnapshotVO;
212200
import com.cloud.vm.snapshot.dao.VMSnapshotDao;
213201

214-
// ToDo: check access for list APIs when not ROOT admin
215-
216202
public class ServerAdapter extends ManagerBase {
217-
private static final String SERVICE_ACCOUNT_NAME = "veemserviceuser";
218-
private static final String SERVICE_ACCOUNT_ROLE_NAME = "Veeam Service Role";
219-
private static final String SERVICE_ACCOUNT_FIRST_NAME = "Veeam";
220-
private static final String SERVICE_ACCOUNT_LAST_NAME = "Service User";
221-
private static final List<Class<?>> SERVICE_ACCOUNT_ROLE_ALLOWED_APIS = Arrays.asList(
222-
QueryAsyncJobResultCmd.class,
223-
ListVMsCmd.class,
224-
DeployVMCmd.class,
225-
StartVMCmd.class,
226-
StopVMCmd.class,
227-
DestroyVMCmd.class,
228-
ListVolumesCmd.class,
229-
CreateVolumeCmd.class,
230-
DeleteVolumeCmd.class,
231-
AttachVolumeCmd.class,
232-
DetachVolumeCmd.class,
233-
ResizeVolumeCmd.class,
234-
ListNetworksCmd.class
235-
);
236203
private static final List<Storage.StoragePoolType> SUPPORTED_STORAGE_TYPES = Arrays.asList(
237204
Storage.StoragePoolType.Filesystem,
238205
Storage.StoragePoolType.NetworkFilesystem,
@@ -241,9 +208,6 @@ public class ServerAdapter extends ManagerBase {
241208
private static final String VM_TA_KEY = "veeam_tag";
242209
private static final String WORKER_VM_GUEST_CPU_MODE = "host-passthrough";
243210

244-
@Inject
245-
RoleService roleService;
246-
247211
@Inject
248212
AccountService accountService;
249213

@@ -346,74 +310,13 @@ public class ServerAdapter extends ManagerBase {
346310
@Inject
347311
DomainDao domainDao;
348312

349-
protected static Tag getDummyTagByName(String name) {
350-
Tag tag = new Tag();
351-
String id = UUID.nameUUIDFromBytes(String.format("veeam:%s", name.toLowerCase()).getBytes()).toString();
352-
tag.setId(id);
353-
tag.setName(name);
354-
tag.setDescription(String.format("Default %s tag", name.toLowerCase()));
355-
tag.setHref(VeeamControlService.ContextPath.value() + TagsRouteHandler.BASE_ROUTE + "/" + id);
356-
tag.setParent(ResourceTagVOToTagConverter.getRootTagRef());
357-
return tag;
358-
}
359-
360313
protected static Map<String, Tag> getDummyTags() {
361314
Map<String, Tag> tags = new HashMap<>();
362315
Tag rootTag = ResourceTagVOToTagConverter.getRootTag();
363316
tags.put(rootTag.getId(), rootTag);
364317
return tags;
365318
}
366319

367-
protected Role createServiceAccountRole() {
368-
Role role = roleService.createRole(SERVICE_ACCOUNT_ROLE_NAME, RoleType.User,
369-
SERVICE_ACCOUNT_ROLE_NAME, false);
370-
for (Class<?> allowedApi : SERVICE_ACCOUNT_ROLE_ALLOWED_APIS) {
371-
final String apiName = BaseCmd.getCommandNameByClass(allowedApi);
372-
roleService.createRolePermission(role, new Rule(apiName), RolePermissionEntity.Permission.ALLOW,
373-
String.format("Allow %s", apiName));
374-
}
375-
roleService.createRolePermission(role, new Rule("*"), RolePermissionEntity.Permission.DENY,
376-
"Deny all");
377-
logger.debug("Created default role for Veeam service account in projects: {}", role);
378-
return role;
379-
}
380-
381-
protected Role getServiceAccountRole() {
382-
List<Role> roles = roleService.findRolesByName(SERVICE_ACCOUNT_ROLE_NAME);
383-
if (CollectionUtils.isNotEmpty(roles)) {
384-
Role role = roles.get(0);
385-
logger.debug("Found default role for Veeam service account in projects: {}", role);
386-
return role;
387-
}
388-
return createServiceAccountRole();
389-
}
390-
391-
protected UserAccount createServiceAccount() {
392-
CallContext.register(User.UID_SYSTEM, Account.ACCOUNT_ID_SYSTEM);
393-
try {
394-
Role role = getServiceAccountRole();
395-
UserAccount userAccount = accountService.createUserAccount(SERVICE_ACCOUNT_NAME,
396-
UUID.randomUUID().toString(), SERVICE_ACCOUNT_FIRST_NAME,
397-
SERVICE_ACCOUNT_LAST_NAME, null, null, SERVICE_ACCOUNT_NAME, Account.Type.NORMAL, role.getId(),
398-
1L, null, null, null, null, User.Source.NATIVE);
399-
logger.debug("Created Veeam service account: {}", userAccount);
400-
return userAccount;
401-
} finally {
402-
CallContext.unregister();
403-
}
404-
}
405-
406-
protected Pair<User, Account> getDefaultServiceAccount() {
407-
UserAccount userAccount = accountService.getActiveUserAccount(SERVICE_ACCOUNT_NAME, 1L);
408-
if (userAccount == null) {
409-
userAccount = createServiceAccount();
410-
} else {
411-
logger.debug("Veeam service user account found: {}", userAccount);
412-
}
413-
return new Pair<>(accountService.getActiveUser(userAccount.getId()),
414-
accountService.getActiveAccountById(userAccount.getAccountId()));
415-
}
416-
417320
protected void waitForJobCompletion(long jobId) {
418321
long timeoutNanos = TimeUnit.MINUTES.toNanos(5);
419322
final long deadline = System.nanoTime() + timeoutNanos;

plugins/integrations/veeam-control-service/src/main/java/org/apache/cloudstack/veeam/filter/BasicAuthFilter.java

Lines changed: 0 additions & 110 deletions
This file was deleted.

0 commit comments

Comments
 (0)