Skip to content

Commit f1614aa

Browse files
Anurag Awasthiyadvr
authored andcommitted
api: allow copy tags from template/iso image to VM via deployVM API (#3297)
Support copy tags from template/iso image to VM from deploy vm command. Allow creation of tags from the source template/iso image to vm when deploy vm command creates virtual machine. Fixes: #3048 Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
1 parent c93630f commit f1614aa

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

api/src/main/java/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class ApiConstants {
5050
public static final String CERTIFICATE_CHAIN = "certchain";
5151
public static final String CERTIFICATE_FINGERPRINT = "fingerprint";
5252
public static final String CERTIFICATE_ID = "certid";
53+
public static final String COPY_IMAGE_TAGS = "copyimagetags";
5354
public static final String CSR = "csr";
5455
public static final String PRIVATE_KEY = "privatekey";
5556
public static final String DOMAIN_SUFFIX = "domainsuffix";

api/src/main/java/org/apache/cloudstack/api/command/user/vm/DeployVMCmd.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ public class DeployVMCmd extends BaseAsyncCreateCustomIdCmd implements SecurityG
203203
@Parameter(name = ApiConstants.EXTRA_CONFIG, type = CommandType.STRING, since = "4.12", description = "an optional URL encoded string that can be passed to the virtual machine upon successful deployment", length = 5120)
204204
private String extraConfig;
205205

206+
@Parameter(name = ApiConstants.COPY_IMAGE_TAGS, type = CommandType.BOOLEAN, since = "4.13", description = "if true the image tags (if any) will be copied to the VM, default value is false")
207+
private Boolean copyImageTags;
208+
206209
/////////////////////////////////////////////////////
207210
/////////////////// Accessors ///////////////////////
208211
/////////////////////////////////////////////////////
@@ -489,6 +492,10 @@ public String getExtraConfig() {
489492
return extraConfig;
490493
}
491494

495+
public boolean getCopyImageTags() {
496+
return copyImageTags == null ? false : copyImageTags;
497+
}
498+
492499
/////////////////////////////////////////////////////
493500
/////////////// API Implementation///////////////////
494501
/////////////////////////////////////////////////////

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@
223223
import com.cloud.resource.ResourceManager;
224224
import com.cloud.resource.ResourceState;
225225
import com.cloud.server.ManagementService;
226+
import com.cloud.server.ResourceTag;
226227
import com.cloud.service.ServiceOfferingVO;
227228
import com.cloud.service.dao.ServiceOfferingDao;
228229
import com.cloud.service.dao.ServiceOfferingDetailsDao;
@@ -251,6 +252,8 @@
251252
import com.cloud.storage.dao.VMTemplateDao;
252253
import com.cloud.storage.dao.VMTemplateZoneDao;
253254
import com.cloud.storage.dao.VolumeDao;
255+
import com.cloud.tags.ResourceTagVO;
256+
import com.cloud.tags.dao.ResourceTagDao;
254257
import com.cloud.template.TemplateApiService;
255258
import com.cloud.template.TemplateManager;
256259
import com.cloud.template.VirtualMachineTemplate;
@@ -477,6 +480,8 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
477480
private ConfigurationDao _configDao;
478481
@Inject
479482
private DpdkHelper dpdkHelper;
483+
@Inject
484+
private ResourceTagDao resourceTagDao;
480485

481486
private ScheduledExecutorService _executor = null;
482487
private ScheduledExecutorService _vmIpFetchExecutor = null;
@@ -4958,6 +4963,18 @@ public UserVm createVirtualMachine(DeployVMCmd cmd) throws InsufficientCapacityE
49584963
addExtraConfig(vm, caller, extraConfig);
49594964
}
49604965

4966+
if (cmd.getCopyImageTags()) {
4967+
VMTemplateVO templateOrIso = _templateDao.findById(templateId);
4968+
if (templateOrIso != null) {
4969+
final ResourceTag.ResourceObjectType templateType = (templateOrIso.getFormat() == ImageFormat.ISO) ? ResourceTag.ResourceObjectType.ISO : ResourceTag.ResourceObjectType.Template;
4970+
final List<? extends ResourceTag> resourceTags = resourceTagDao.listBy(templateId, templateType);
4971+
for (ResourceTag resourceTag : resourceTags) {
4972+
final ResourceTagVO copyTag = new ResourceTagVO(resourceTag.getKey(), resourceTag.getValue(), resourceTag.getAccountId(), resourceTag.getDomainId(), vm.getId(), ResourceTag.ResourceObjectType.UserVm, resourceTag.getCustomer(), vm.getUuid());
4973+
resourceTagDao.persist(copyTag);
4974+
}
4975+
}
4976+
}
4977+
49614978
return vm;
49624979
}
49634980

0 commit comments

Comments
 (0)