Skip to content

Commit d8c7ee7

Browse files
committed
fix tags restore
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent 9ea3364 commit d8c7ee7

2 files changed

Lines changed: 59 additions & 4 deletions

File tree

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

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.List;
2727
import java.util.Map;
2828
import java.util.Objects;
29+
import java.util.UUID;
2930
import java.util.concurrent.TimeUnit;
3031
import java.util.stream.Collectors;
3132

@@ -54,6 +55,7 @@
5455
import org.apache.cloudstack.api.command.user.job.ListAsyncJobsCmd;
5556
import org.apache.cloudstack.api.command.user.network.ListNetworksCmd;
5657
import org.apache.cloudstack.api.command.user.offering.ListServiceOfferingsCmd;
58+
import org.apache.cloudstack.api.command.user.tag.CreateTagsCmd;
5759
import org.apache.cloudstack.api.command.user.tag.ListTagsCmd;
5860
import org.apache.cloudstack.api.command.user.vm.AddNicToVMCmd;
5961
import org.apache.cloudstack.api.command.user.vm.DeployVMCmd;
@@ -164,6 +166,7 @@
164166
import com.cloud.projects.Project;
165167
import com.cloud.projects.ProjectManager;
166168
import com.cloud.server.ResourceTag;
169+
import com.cloud.server.TaggedResourceService;
167170
import com.cloud.service.ServiceOfferingVO;
168171
import com.cloud.service.dao.ServiceOfferingDao;
169172
import com.cloud.storage.Storage;
@@ -186,6 +189,7 @@
186189
import com.cloud.utils.EnumUtils;
187190
import com.cloud.utils.Pair;
188191
import com.cloud.utils.Ternary;
192+
import com.cloud.utils.UuidUtils;
189193
import com.cloud.utils.component.ComponentContext;
190194
import com.cloud.utils.component.ManagerBase;
191195
import com.cloud.utils.db.Filter;
@@ -207,7 +211,7 @@ public class ServerAdapter extends ManagerBase {
207211
Storage.StoragePoolType.NetworkFilesystem,
208212
Storage.StoragePoolType.SharedMountPoint
209213
);
210-
private static final String VM_TA_KEY = "veeam_tag";
214+
private static final String VM_TAG_KEY = "veeam_tag";
211215
private static final String WORKER_VM_GUEST_CPU_MODE = "host-passthrough";
212216
private static final String RESTORE_CONFIG = "restore.config";
213217

@@ -313,6 +317,9 @@ public class ServerAdapter extends ManagerBase {
313317
@Inject
314318
DomainDao domainDao;
315319

320+
@Inject
321+
TaggedResourceService taggedResourceService;
322+
316323
protected static Map<String, Tag> getDummyTags() {
317324
Map<String, Tag> tags = new HashMap<>();
318325
Tag rootTag = ResourceTagVOToTagConverter.getRootTag();
@@ -1154,7 +1161,7 @@ public VmAction shutdownInstance(String uuid, boolean async) {
11541161
@ApiAccess(command = ListTagsCmd.class)
11551162
protected List<Tag> listTagsByInstanceId(final long instanceId) {
11561163
List<ResourceTagVO> tags = resourceTagDao.listByResourceTypeIdAndKeyPrefix(
1157-
ResourceTag.ResourceObjectType.UserVm, instanceId, VM_TA_KEY);
1164+
ResourceTag.ResourceObjectType.UserVm, instanceId, VM_TAG_KEY);
11581165
return ResourceTagVOToTagConverter.toTags(tags);
11591166
}
11601167

@@ -1725,7 +1732,7 @@ public List<Tag> listAllTags(final Long offset, final Long limit) {
17251732
Filter filter = new Filter(ResourceTagVO.class, "id", true, offset, limit);
17261733
Pair<List<Long>, List<Long>> ownerDetails = getResourceOwnerFiltersWithDomainIds();
17271734
List<String> vmResourceTags = resourceTagDao.listByResourceTypeKeyPrefixAndOwners(
1728-
ResourceTag.ResourceObjectType.UserVm, VM_TA_KEY, ownerDetails.first(), ownerDetails.second(), filter);
1735+
ResourceTag.ResourceObjectType.UserVm, VM_TAG_KEY, ownerDetails.first(), ownerDetails.second(), filter);
17291736
if (CollectionUtils.isNotEmpty(vmResourceTags)) {
17301737
tags.addAll(ResourceTagVOToTagConverter.toTagsFromValues(vmResourceTags));
17311738
}
@@ -1740,7 +1747,7 @@ public Tag getTag(String uuid) {
17401747
Tag tag = getDummyTags().get(uuid);
17411748
if (tag == null) {
17421749
ResourceTagVO resourceTagVO = resourceTagDao.findByResourceTypeKeyPrefixAndValue(
1743-
ResourceTag.ResourceObjectType.UserVm, VM_TA_KEY, uuid);
1750+
ResourceTag.ResourceObjectType.UserVm, VM_TAG_KEY, uuid);
17441751
accountService.checkAccess(CallContext.current().getCallingAccount(), null, false,
17451752
resourceTagVO);
17461753
if (resourceTagVO != null) {
@@ -1752,4 +1759,31 @@ public Tag getTag(String uuid) {
17521759
}
17531760
return tag;
17541761
}
1762+
1763+
@ApiAccess(command = CreateTagsCmd.class)
1764+
public Tag createInstanceTag(final String vmUuid, final Tag request) {
1765+
UserVmVO vmVo = userVmDao.findByUuid(vmUuid);
1766+
if (vmVo == null) {
1767+
throw new InvalidParameterValueException("VM with ID " + vmUuid + " not found");
1768+
}
1769+
accountService.checkAccess(CallContext.current().getCallingAccount(), SecurityChecker.AccessType.OperateEntry,
1770+
false, vmVo);
1771+
Map<String, String> tags = new HashMap<>();
1772+
String name = request.getId();
1773+
tags.put(String.format( "%s%s", VM_TAG_KEY, UuidUtils.first(UUID.randomUUID().toString())), name);
1774+
try {
1775+
List<ResourceTag> resourceTags = taggedResourceService.createTags(Collections.singletonList(vmUuid),
1776+
ResourceTag.ResourceObjectType.UserVm, tags, null);
1777+
ResourceTagVO tag = null;
1778+
if (CollectionUtils.isNotEmpty(resourceTags)) {
1779+
tag = resourceTagDao.findById(resourceTags.get(0).getId());
1780+
}
1781+
if (tag == null) {
1782+
throw new CloudRuntimeException("Unknown error");
1783+
}
1784+
return ResourceTagVOToTagConverter.toTag(tag);
1785+
} catch (Exception e) {
1786+
throw new CloudRuntimeException(String.format("Failed to create tag for %s: %s", name, e.getMessage()), e);
1787+
}
1788+
}
17551789
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.apache.cloudstack.veeam.api.dto.Nic;
3636
import org.apache.cloudstack.veeam.api.dto.ResourceAction;
3737
import org.apache.cloudstack.veeam.api.dto.Snapshot;
38+
import org.apache.cloudstack.veeam.api.dto.Tag;
3839
import org.apache.cloudstack.veeam.api.dto.Vm;
3940
import org.apache.cloudstack.veeam.api.dto.VmAction;
4041
import org.apache.cloudstack.veeam.api.request.ListQuery;
@@ -162,6 +163,13 @@ public void handle(HttpServletRequest req, HttpServletResponse resp, String path
162163
io.methodNotAllowed(resp, "GET, POST", outFormat);
163164
}
164165
return;
166+
} else if ("tags".equals(subPath)) {
167+
if ("POST".equalsIgnoreCase(method)) {
168+
handlePostTagForVmId(id, req, resp, outFormat, io);
169+
} else {
170+
io.methodNotAllowed(resp, "POST", outFormat);
171+
}
172+
return;
165173
}
166174
} else if (idAndSubPath.size() == 3) {
167175
String subPath = idAndSubPath.get(1);
@@ -529,4 +537,17 @@ protected void handleDeleteCheckpoint(final String vmId, final String checkpoint
529537
io.badRequest(resp, e.getMessage(), outFormat);
530538
}
531539
}
540+
541+
protected void handlePostTagForVmId(final String id, final HttpServletRequest req,
542+
final HttpServletResponse resp, final Negotiation.OutFormat outFormat, final VeeamControlServlet io)
543+
throws IOException {
544+
String data = RouteHandler.getRequestData(req, logger);
545+
try {
546+
Tag request = io.getMapper().jsonMapper().readValue(data, Tag.class);
547+
Tag response = serverAdapter.createInstanceTag(id, request);
548+
io.getWriter().write(resp, HttpServletResponse.SC_OK, response, outFormat);
549+
} catch (JsonProcessingException | CloudRuntimeException e) {
550+
io.badRequest(resp, e.getMessage(), outFormat);
551+
}
552+
}
532553
}

0 commit comments

Comments
 (0)