2626import java .util .List ;
2727import java .util .Map ;
2828import java .util .Objects ;
29+ import java .util .UUID ;
2930import java .util .concurrent .TimeUnit ;
3031import java .util .stream .Collectors ;
3132
5455import org .apache .cloudstack .api .command .user .job .ListAsyncJobsCmd ;
5556import org .apache .cloudstack .api .command .user .network .ListNetworksCmd ;
5657import org .apache .cloudstack .api .command .user .offering .ListServiceOfferingsCmd ;
58+ import org .apache .cloudstack .api .command .user .tag .CreateTagsCmd ;
5759import org .apache .cloudstack .api .command .user .tag .ListTagsCmd ;
5860import org .apache .cloudstack .api .command .user .vm .AddNicToVMCmd ;
5961import org .apache .cloudstack .api .command .user .vm .DeployVMCmd ;
164166import com .cloud .projects .Project ;
165167import com .cloud .projects .ProjectManager ;
166168import com .cloud .server .ResourceTag ;
169+ import com .cloud .server .TaggedResourceService ;
167170import com .cloud .service .ServiceOfferingVO ;
168171import com .cloud .service .dao .ServiceOfferingDao ;
169172import com .cloud .storage .Storage ;
186189import com .cloud .utils .EnumUtils ;
187190import com .cloud .utils .Pair ;
188191import com .cloud .utils .Ternary ;
192+ import com .cloud .utils .UuidUtils ;
189193import com .cloud .utils .component .ComponentContext ;
190194import com .cloud .utils .component .ManagerBase ;
191195import 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}
0 commit comments