Resource tags: CS-15473 - fixed delete/list by resourceId when UUID is specified as a value

Conflicts:

	server/src/com/cloud/network/dao/FirewallRulesDaoImpl.java
	server/src/com/cloud/network/vpc/Dao/StaticRouteDaoImpl.java
	server/src/com/cloud/network/vpc/Dao/VpcDaoImpl.java
	server/src/com/cloud/projects/dao/ProjectDaoImpl.java
This commit is contained in:
Alena Prokharchyk 2012-07-06 10:31:20 -07:00
parent 710fa36bfd
commit 85677b71fe
16 changed files with 67 additions and 23 deletions

View File

@ -64,4 +64,9 @@ public interface ResourceTag extends ControlledEntity{
*/ */
String getCustomer(); String getCustomer();
/**
* @return
*/
String getResourceUuid();
} }

View File

@ -280,11 +280,11 @@ public class FirewallRulesDaoImpl extends GenericDaoBase<FirewallRuleVO, Long> i
FirewallRuleVO entry = findById(id); FirewallRuleVO entry = findById(id);
if (entry != null) { if (entry != null) {
if (entry.getPurpose() == Purpose.LoadBalancing) { if (entry.getPurpose() == Purpose.LoadBalancing) {
_tagsDao.removeBy(id, TaggedResourceType.LoadBalancer); _tagsDao.removeByIdAndType(id, TaggedResourceType.LoadBalancer);
} else if (entry.getPurpose() == Purpose.PortForwarding) { } else if (entry.getPurpose() == Purpose.PortForwarding) {
_tagsDao.removeBy(id, TaggedResourceType.PortForwardingRule); _tagsDao.removeByIdAndType(id, TaggedResourceType.PortForwardingRule);
} else if (entry.getPurpose() == Purpose.Firewall) { } else if (entry.getPurpose() == Purpose.Firewall) {
_tagsDao.removeBy(id, TaggedResourceType.FirewallRule); _tagsDao.removeByIdAndType(id, TaggedResourceType.FirewallRule);
} }
} }
boolean result = super.remove(id); boolean result = super.remove(id);

View File

@ -323,7 +323,7 @@ public class IPAddressDaoImpl extends GenericDaoBase<IPAddressVO, Long> implemen
txn.start(); txn.start();
IPAddressVO entry = findById(id); IPAddressVO entry = findById(id);
if (entry != null) { if (entry != null) {
_tagsDao.removeBy(id, TaggedResourceType.SecurityGroup); _tagsDao.removeByIdAndType(id, TaggedResourceType.SecurityGroup);
} }
boolean result = super.remove(id); boolean result = super.remove(id);
txn.commit(); txn.commit();

View File

@ -495,7 +495,7 @@ public class NetworkDaoImpl extends GenericDaoBase<NetworkVO, Long> implements N
txn.start(); txn.start();
NetworkVO entry = findById(id); NetworkVO entry = findById(id);
if (entry != null) { if (entry != null) {
_tagsDao.removeBy(id, TaggedResourceType.Network); _tagsDao.removeByIdAndType(id, TaggedResourceType.Network);
} }
boolean result = super.remove(id); boolean result = super.remove(id);
txn.commit(); txn.commit();

View File

@ -108,7 +108,7 @@ public class SecurityGroupDaoImpl extends GenericDaoBase<SecurityGroupVO, Long>
txn.start(); txn.start();
SecurityGroupVO entry = findById(id); SecurityGroupVO entry = findById(id);
if (entry != null) { if (entry != null) {
_tagsDao.removeBy(id, TaggedResourceType.SecurityGroup); _tagsDao.removeByIdAndType(id, TaggedResourceType.SecurityGroup);
} }
boolean result = super.remove(id); boolean result = super.remove(id);
txn.commit(); txn.commit();

View File

@ -73,8 +73,6 @@ public class ProjectDaoImpl extends GenericDaoBase<ProjectVO, Long> implements P
if (!update(projectId, projectToRemove)) { if (!update(projectId, projectToRemove)) {
s_logger.warn("Failed to reset name for the project id=" + projectId + " as a part of project remove"); s_logger.warn("Failed to reset name for the project id=" + projectId + " as a part of project remove");
return false; return false;
} else {
} }
result = super.remove(projectId); result = super.remove(projectId);
txn.commit(); txn.commit();

View File

@ -301,7 +301,7 @@ public class SnapshotDaoImpl extends GenericDaoBase<SnapshotVO, Long> implements
txn.start(); txn.start();
SnapshotVO entry = findById(id); SnapshotVO entry = findById(id);
if (entry != null) { if (entry != null) {
_tagsDao.removeBy(id, TaggedResourceType.Snapshot); _tagsDao.removeByIdAndType(id, TaggedResourceType.Snapshot);
} }
boolean result = super.remove(id); boolean result = super.remove(id);
txn.commit(); txn.commit();

View File

@ -805,9 +805,9 @@ public class VMTemplateDaoImpl extends GenericDaoBase<VMTemplateVO, Long> implem
template.setRemoved(new Date()); template.setRemoved(new Date());
if (template != null) { if (template != null) {
if (template.getFormat() == ImageFormat.ISO) { if (template.getFormat() == ImageFormat.ISO) {
_tagsDao.removeBy(id, TaggedResourceType.ISO); _tagsDao.removeByIdAndType(id, TaggedResourceType.ISO);
} else { } else {
_tagsDao.removeBy(id, TaggedResourceType.Template); _tagsDao.removeByIdAndType(id, TaggedResourceType.Template);
} }
} }

View File

@ -407,7 +407,7 @@ public class VolumeDaoImpl extends GenericDaoBase<VolumeVO, Long> implements Vol
txn.start(); txn.start();
VolumeVO entry = findById(id); VolumeVO entry = findById(id);
if (entry != null) { if (entry != null) {
_tagsDao.removeBy(id, TaggedResourceType.Volume); _tagsDao.removeByIdAndType(id, TaggedResourceType.Volume);
} }
boolean result = super.remove(id); boolean result = super.remove(id);
txn.commit(); txn.commit();

View File

@ -61,6 +61,9 @@ public class ResourceTagVO implements Identity, ResourceTag{
@Column(name="resource_id") @Column(name="resource_id")
long resourceId; long resourceId;
@Column(name="resource_uuid")
private String resourceUuid;
@Column(name="resource_type") @Column(name="resource_type")
@Enumerated(value=EnumType.STRING) @Enumerated(value=EnumType.STRING)
private TaggedResourceType resourceType; private TaggedResourceType resourceType;
@ -81,9 +84,10 @@ public class ResourceTagVO implements Identity, ResourceTag{
* @param resourceId * @param resourceId
* @param resourceType * @param resourceType
* @param customer TODO * @param customer TODO
* @param resourceUuid TODO
*/ */
public ResourceTagVO(String key, String value, long accountId, long domainId, long resourceId, public ResourceTagVO(String key, String value, long accountId, long domainId, long resourceId,
TaggedResourceType resourceType, String customer) { TaggedResourceType resourceType, String customer, String resourceUuid) {
super(); super();
this.key = key; this.key = key;
this.value = value; this.value = value;
@ -93,6 +97,7 @@ public class ResourceTagVO implements Identity, ResourceTag{
this.resourceType = resourceType; this.resourceType = resourceType;
this.uuid = UUID.randomUUID().toString(); this.uuid = UUID.randomUUID().toString();
this.customer = customer; this.customer = customer;
this.resourceUuid = resourceUuid;
} }
@ -149,4 +154,9 @@ public class ResourceTagVO implements Identity, ResourceTag{
public String getCustomer() { public String getCustomer() {
return customer; return customer;
} }
@Override
public String getResourceUuid() {
return resourceUuid;
}
} }

View File

@ -236,10 +236,12 @@ public class TaggedResourceManagerImpl implements TaggedResourceService, Manager
for (String tag : tags.keySet()) { for (String tag : tags.keySet()) {
for (String resourceId : resourceIds) { for (String resourceId : resourceIds) {
Long id = getResourceId(resourceId, resourceType); Long id = getResourceId(resourceId, resourceType);
String resourceUuid = getUuid(resourceId, resourceType);
//check if object exists //check if object exists
if (_daoMap.get(resourceType).findById(id) == null) { if (_daoMap.get(resourceType).findById(id) == null) {
throw new InvalidParameterValueException("Unable to find resource by id " + resourceId + " and type " + resourceType); throw new InvalidParameterValueException("Unable to find resource by id " + resourceId +
" and type " + resourceType);
} }
Pair<Long, Long> accountDomainPair = getAccountDomain(id, resourceType); Pair<Long, Long> accountDomainPair = getAccountDomain(id, resourceType);
@ -257,10 +259,9 @@ public class TaggedResourceManagerImpl implements TaggedResourceService, Manager
ResourceTagVO resourceTag = new ResourceTagVO(tag, tags.get(tag), accountDomainPair.first(), ResourceTagVO resourceTag = new ResourceTagVO(tag, tags.get(tag), accountDomainPair.first(),
accountDomainPair.second(), accountDomainPair.second(),
id, resourceType, customer); id, resourceType, customer, resourceUuid);
resourceTag = _resourceTagDao.persist(resourceTag); resourceTag = _resourceTagDao.persist(resourceTag);
resourceTags.add(resourceTag); resourceTags.add(resourceTag);
} }
} }
@ -324,7 +325,9 @@ public class TaggedResourceManagerImpl implements TaggedResourceService, Manager
sb.and("key", sb.entity().getKey(), SearchCriteria.Op.EQ); sb.and("key", sb.entity().getKey(), SearchCriteria.Op.EQ);
sb.and("value", sb.entity().getValue(), SearchCriteria.Op.EQ); sb.and("value", sb.entity().getValue(), SearchCriteria.Op.EQ);
sb.and("resourceId", sb.entity().getResourceId(), SearchCriteria.Op.EQ); sb.and().op("resourceId", sb.entity().getResourceId(), SearchCriteria.Op.EQ);
sb.or("resourceUuid", sb.entity().getResourceUuid(), SearchCriteria.Op.EQ);
sb.cp();
sb.and("resourceType", sb.entity().getResourceType(), SearchCriteria.Op.EQ); sb.and("resourceType", sb.entity().getResourceType(), SearchCriteria.Op.EQ);
sb.and("customer", sb.entity().getCustomer(), SearchCriteria.Op.EQ); sb.and("customer", sb.entity().getCustomer(), SearchCriteria.Op.EQ);
@ -342,6 +345,7 @@ public class TaggedResourceManagerImpl implements TaggedResourceService, Manager
if (resourceId != null) { if (resourceId != null) {
sc.setParameters("resourceId", resourceId); sc.setParameters("resourceId", resourceId);
sc.setParameters("resourceUuid", resourceId);
} }
if (resourceType != null) { if (resourceType != null) {
@ -362,10 +366,14 @@ public class TaggedResourceManagerImpl implements TaggedResourceService, Manager
Account caller = UserContext.current().getCaller(); Account caller = UserContext.current().getCaller();
SearchBuilder<ResourceTagVO> sb = _resourceTagDao.createSearchBuilder(); SearchBuilder<ResourceTagVO> sb = _resourceTagDao.createSearchBuilder();
sb.and("resourceId", sb.entity().getResourceId(), SearchCriteria.Op.IN); sb.and().op("resourceId", sb.entity().getResourceId(), SearchCriteria.Op.IN);
sb.or("resourceUuid", sb.entity().getResourceUuid(), SearchCriteria.Op.IN);
sb.cp();
sb.and("resourceType", sb.entity().getResourceType(), SearchCriteria.Op.EQ); sb.and("resourceType", sb.entity().getResourceType(), SearchCriteria.Op.EQ);
SearchCriteria<ResourceTagVO> sc = sb.create(); SearchCriteria<ResourceTagVO> sc = sb.create();
sc.setParameters("resourceId", resourceIds.toArray()); sc.setParameters("resourceId", resourceIds.toArray());
sc.setParameters("resourceUuid", resourceIds.toArray());
sc.setParameters("resourceType", resourceType); sc.setParameters("resourceType", resourceType);
List<? extends ResourceTag> resourceTags = _resourceTagDao.search(sc, null);; List<? extends ResourceTag> resourceTags = _resourceTagDao.search(sc, null);;
@ -400,6 +408,10 @@ public class TaggedResourceManagerImpl implements TaggedResourceService, Manager
} }
} }
if (tagsToRemove.isEmpty()) {
throw new InvalidParameterValueException("Unable to find tags by parameters specified");
}
//Remove the tags //Remove the tags
Transaction txn = Transaction.currentTxn(); Transaction txn = Transaction.currentTxn();
txn.start(); txn.start();

View File

@ -33,8 +33,15 @@ public interface ResourceTagDao extends GenericDao<ResourceTagVO, Long>{
* @param resourceType * @param resourceType
* @return * @return
*/ */
boolean removeBy(long resourceId, TaggedResourceType resourceType); boolean removeByIdAndType(long resourceId, TaggedResourceType resourceType);
List<? extends ResourceTag> listBy(long resourceId, TaggedResourceType resourceType); List<? extends ResourceTag> listBy(long resourceId, TaggedResourceType resourceType);
// /**
// * @param resourceUuId
// * @param resourceType
// * @return
// */
// ResourceTag findByUuid(String resourceUuId, TaggedResourceType resourceType);
} }

View File

@ -39,12 +39,13 @@ public class ResourceTagsDaoImpl extends GenericDaoBase<ResourceTagVO, Long> imp
protected ResourceTagsDaoImpl() { protected ResourceTagsDaoImpl() {
AllFieldsSearch = createSearchBuilder(); AllFieldsSearch = createSearchBuilder();
AllFieldsSearch.and("resourceId", AllFieldsSearch.entity().getResourceId(), Op.EQ); AllFieldsSearch.and("resourceId", AllFieldsSearch.entity().getResourceId(), Op.EQ);
AllFieldsSearch.and("uuid", AllFieldsSearch.entity().getResourceUuid(), Op.EQ);
AllFieldsSearch.and("resourceType", AllFieldsSearch.entity().getResourceType(), Op.EQ); AllFieldsSearch.and("resourceType", AllFieldsSearch.entity().getResourceType(), Op.EQ);
AllFieldsSearch.done(); AllFieldsSearch.done();
} }
@Override @Override
public boolean removeBy(long resourceId, ResourceTag.TaggedResourceType resourceType) { public boolean removeByIdAndType(long resourceId, ResourceTag.TaggedResourceType resourceType) {
SearchCriteria<ResourceTagVO> sc = AllFieldsSearch.create(); SearchCriteria<ResourceTagVO> sc = AllFieldsSearch.create();
sc.setParameters("resourceId", resourceId); sc.setParameters("resourceId", resourceId);
sc.setParameters("resourceType", resourceType); sc.setParameters("resourceType", resourceType);
@ -52,6 +53,14 @@ public class ResourceTagsDaoImpl extends GenericDaoBase<ResourceTagVO, Long> imp
return true; return true;
} }
// @Override
// public ResourceTag findByUuid(String resourceUuId, ResourceTag.TaggedResourceType resourceType) {
// SearchCriteria<ResourceTagVO> sc = AllFieldsSearch.create();
// sc.setParameters("uuid", resourceUuId);
// sc.setParameters("resourceType", resourceType);
// return findOneBy(sc);
// }
@Override @Override
public List<? extends ResourceTag> listBy(long resourceId, TaggedResourceType resourceType) { public List<? extends ResourceTag> listBy(long resourceId, TaggedResourceType resourceType) {
SearchCriteria<ResourceTagVO> sc = AllFieldsSearch.create(); SearchCriteria<ResourceTagVO> sc = AllFieldsSearch.create();

View File

@ -556,7 +556,7 @@ public class UserVmDaoImpl extends GenericDaoBase<UserVmVO, Long> implements Use
public boolean remove(Long id) { public boolean remove(Long id) {
Transaction txn = Transaction.currentTxn(); Transaction txn = Transaction.currentTxn();
txn.start(); txn.start();
_tagsDao.removeBy(id, TaggedResourceType.UserVm); _tagsDao.removeByIdAndType(id, TaggedResourceType.UserVm);
boolean result = super.remove(id); boolean result = super.remove(id);
txn.commit(); txn.commit();
return result; return result;

View File

@ -562,7 +562,7 @@ public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> implem
txn.start(); txn.start();
VMInstanceVO vm = findById(id); VMInstanceVO vm = findById(id);
if (vm != null && vm.getType() == Type.User) { if (vm != null && vm.getType() == Type.User) {
_tagsDao.removeBy(id, TaggedResourceType.UserVm); _tagsDao.removeByIdAndType(id, TaggedResourceType.UserVm);
} }
boolean result = super.remove(id); boolean result = super.remove(id);
txn.commit(); txn.commit();

View File

@ -2139,6 +2139,7 @@ CREATE TABLE `cloud`.`resource_tags` (
`key` varchar(255), `key` varchar(255),
`value` varchar(255), `value` varchar(255),
`resource_id` bigint unsigned NOT NULL, `resource_id` bigint unsigned NOT NULL,
`resource_uuid` varchar(40),
`resource_type` varchar(255), `resource_type` varchar(255),
`customer` varchar(255), `customer` varchar(255),
`domain_id` bigint unsigned NOT NULL COMMENT 'foreign key to domain id', `domain_id` bigint unsigned NOT NULL COMMENT 'foreign key to domain id',
@ -2146,7 +2147,9 @@ CREATE TABLE `cloud`.`resource_tags` (
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
CONSTRAINT `fk_tags__account_id` FOREIGN KEY(`account_id`) REFERENCES `account`(`id`), CONSTRAINT `fk_tags__account_id` FOREIGN KEY(`account_id`) REFERENCES `account`(`id`),
CONSTRAINT `fk_tags__domain_id` FOREIGN KEY(`domain_id`) REFERENCES `domain`(`id`), CONSTRAINT `fk_tags__domain_id` FOREIGN KEY(`domain_id`) REFERENCES `domain`(`id`),
UNIQUE `i_tags__resource_id__resource_type__key`(`resource_id`, `resource_type`, `key`) UNIQUE `i_tags__resource_id__resource_type__key`(`resource_id`, `resource_type`, `key`),
CONSTRAINT `uc_resource_tags__uuid` UNIQUE (`uuid`),
CONSTRAINT `uc_resource_tags__resource_uuid` UNIQUE (`resource_uuid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; ) ENGINE=InnoDB DEFAULT CHARSET=utf8;