cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From alena1...@apache.org
Subject [3/3] git commit: updated refs/heads/master to aa9f8e6
Date Fri, 25 Oct 2013 19:04:53 GMT
Resource details: added way of generic access of resource DAO based on the resourceType passed
in (was hardcoded before)


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/aa9f8e64
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/aa9f8e64
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/aa9f8e64

Branch: refs/heads/master
Commit: aa9f8e64ae35adab531c92c7ee5c1dc9f60bc6cd
Parents: 0fb4d9d
Author: Alena Prokharchyk <alena.prokharchyk@citrix.com>
Authored: Fri Oct 25 11:49:13 2013 -0700
Committer: Alena Prokharchyk <alena.prokharchyk@citrix.com>
Committed: Fri Oct 25 11:57:17 2013 -0700

----------------------------------------------------------------------
 .../cloud/server/ResourceMetaDataService.java   |   9 ++
 .../user/volume/ListResourceDetailsCmd.java     |   9 +-
 .../com/cloud/api/query/QueryManagerImpl.java   |  50 ++-----
 .../metadata/ResourceMetaDataManagerImpl.java   | 145 +++++++++++--------
 4 files changed, 112 insertions(+), 101 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/aa9f8e64/api/src/com/cloud/server/ResourceMetaDataService.java
----------------------------------------------------------------------
diff --git a/api/src/com/cloud/server/ResourceMetaDataService.java b/api/src/com/cloud/server/ResourceMetaDataService.java
index 1a12b6a..46f1c4a 100644
--- a/api/src/com/cloud/server/ResourceMetaDataService.java
+++ b/api/src/com/cloud/server/ResourceMetaDataService.java
@@ -16,8 +16,11 @@
 // under the License.package com.cloud.server;
 
 package com.cloud.server;
+import java.util.List;
 import java.util.Map;
 
+import org.apache.cloudstack.api.ResourceDetail;
+
 import com.cloud.server.ResourceTag.ResourceObjectType;
 
 public interface ResourceMetaDataService {
@@ -41,4 +44,10 @@ public interface ResourceMetaDataService {
     public boolean deleteResourceMetaData(String resourceId, ResourceObjectType resourceType,
String key);
 
 
+    List<? extends ResourceDetail> getDetails(long resourceId, ResourceObjectType resourceType);
+
+
+    ResourceDetail getDetail(long resourceId, ResourceObjectType resourceType, String key);
+
+
     }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/aa9f8e64/api/src/org/apache/cloudstack/api/command/user/volume/ListResourceDetailsCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/volume/ListResourceDetailsCmd.java
b/api/src/org/apache/cloudstack/api/command/user/volume/ListResourceDetailsCmd.java
index 4c2856b..18accac 100644
--- a/api/src/org/apache/cloudstack/api/command/user/volume/ListResourceDetailsCmd.java
+++ b/api/src/org/apache/cloudstack/api/command/user/volume/ListResourceDetailsCmd.java
@@ -17,7 +17,8 @@
 
 package org.apache.cloudstack.api.command.user.volume;
 
-import com.cloud.server.ResourceTag;
+import java.util.List;
+
 import org.apache.cloudstack.api.APICommand;
 import org.apache.cloudstack.api.ApiConstants;
 import org.apache.cloudstack.api.BaseListProjectAndAccountResourcesCmd;
@@ -26,16 +27,16 @@ import org.apache.cloudstack.api.response.ListResponse;
 import org.apache.cloudstack.api.response.ResourceDetailResponse;
 import org.apache.cloudstack.api.response.ResourceTagResponse;
 
-import java.util.List;
+import com.cloud.server.ResourceTag;
 
 @APICommand(name = "listResourceDetails", description = "List resource detail(s)", responseObject
= ResourceTagResponse.class, since = "4.2")
 public class ListResourceDetailsCmd extends BaseListProjectAndAccountResourcesCmd{
     private static final String s_name = "listresourcedetailsresponse";
 
-    @Parameter(name=ApiConstants.RESOURCE_TYPE, type=CommandType.STRING, description="list
by resource type")
+    @Parameter(name=ApiConstants.RESOURCE_TYPE, type=CommandType.STRING, description="list
by resource type", required=true)
     private String resourceType;
 
-    @Parameter(name=ApiConstants.RESOURCE_ID, type=CommandType.STRING, description="list
by resource id")
+    @Parameter(name=ApiConstants.RESOURCE_ID, type=CommandType.STRING, description="list
by resource id", required=true)
     private String resourceId;
 
     @Parameter(name=ApiConstants.KEY, type=CommandType.STRING, description="list by key")

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/aa9f8e64/server/src/com/cloud/api/query/QueryManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/api/query/QueryManagerImpl.java b/server/src/com/cloud/api/query/QueryManagerImpl.java
index 8cf09bf..85674ea 100644
--- a/server/src/com/cloud/api/query/QueryManagerImpl.java
+++ b/server/src/com/cloud/api/query/QueryManagerImpl.java
@@ -138,7 +138,6 @@ import com.cloud.event.dao.EventJoinDao;
 import com.cloud.exception.CloudAuthenticationException;
 import com.cloud.exception.InvalidParameterValueException;
 import com.cloud.exception.PermissionDeniedException;
-import com.cloud.exception.UnsupportedServiceException;
 import com.cloud.ha.HighAvailabilityManager;
 import com.cloud.hypervisor.Hypervisor.HypervisorType;
 import com.cloud.network.dao.NetworkDetailsDao;
@@ -3264,54 +3263,25 @@ public class QueryManagerImpl extends ManagerBase implements QueryService
{
     public List<ResourceDetailResponse> listResource(ListResourceDetailsCmd cmd) {
         String key = cmd.getKey();
         ResourceTag.ResourceObjectType resourceType = cmd.getResourceType();
-        String resourceId = cmd.getResourceId();
-        long id = _taggedResourceMgr.getResourceId(resourceId, resourceType);
-        List<ResourceDetailResponse> responseList = new ArrayList<ResourceDetailResponse>();
-        List<? extends ResourceDetail> detailList = new ArrayList<ResourceDetail>();
+        String resourceIdStr = cmd.getResourceId();
+        long resourceId = _taggedResourceMgr.getResourceId(resourceIdStr, resourceType);
+        List<? extends ResourceDetail> detailList = new ArrayList<ResourceDetail>();
       
         ResourceDetail requestedDetail = null;
 
-        
-        if (resourceType == ResourceTag.ResourceObjectType.Volume) {
-            if (key == null) {
-                detailList = _volumeDetailDao.findDetailsList(id);
-            } else {
-                requestedDetail = _volumeDetailDao.findDetail(id, key);
-            }
-        } else if (resourceType == ResourceTag.ResourceObjectType.Nic){
-            if (key == null) {
-                detailList = _nicDetailDao.findDetailsList(id);
-            } else {
-                requestedDetail = _nicDetailDao.findDetail(id, key);
-            }
-        } else if (resourceType == ResourceTag.ResourceObjectType.UserVm){
-            if (key == null) {
-                detailList = _userVmDetailDao.findDetailsList(id);
-            } else {
-                requestedDetail = _userVmDetailDao.findDetail(id, key);
-            }
-        } else if (resourceType == ResourceTag.ResourceObjectType.Zone){
-            if (key == null) {
-                detailList = _dcDetailsDao.findDetailsList(id);
-            } else {
-                requestedDetail = _dcDetailsDao.findDetail(id, key);
-            }
-        } else if (resourceType == ResourceObjectType.Network){
-            if (key == null) {
-                detailList = _networkDetailsDao.findDetailsList(id);
-            } else {
-                requestedDetail = _networkDetailsDao.findDetail(id, key);
-            }
-        }else {
-            throw new UnsupportedServiceException("Resource type " + resourceType + " is
not supported by the cloudStack");
+        if (key == null) {
+            detailList = _resourceMetaDataMgr.getDetails(resourceId, resourceType);
+        } else {
+            requestedDetail = _resourceMetaDataMgr.getDetail(resourceId, resourceType, key);
         }
         
+        List<ResourceDetailResponse> responseList = new ArrayList<ResourceDetailResponse>();
         if (requestedDetail != null) {
-            ResourceDetailResponse detailResponse = createResourceDetailsResponse(id, requestedDetail.getName(),
requestedDetail.getValue(),
+            ResourceDetailResponse detailResponse = createResourceDetailsResponse(resourceId,
requestedDetail.getName(), requestedDetail.getValue(),
                     resourceType);
             responseList.add(detailResponse);
         } else {
             for (ResourceDetail detail : detailList) {
-                ResourceDetailResponse detailResponse = createResourceDetailsResponse(id,
detail.getName(), detail.getValue(),
+                ResourceDetailResponse detailResponse = createResourceDetailsResponse(resourceId,
detail.getName(), detail.getValue(),
                         resourceType);
                 responseList.add(detailResponse);
             }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/aa9f8e64/server/src/com/cloud/metadata/ResourceMetaDataManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/metadata/ResourceMetaDataManagerImpl.java b/server/src/com/cloud/metadata/ResourceMetaDataManagerImpl.java
index 152e57f..5bfe004 100644
--- a/server/src/com/cloud/metadata/ResourceMetaDataManagerImpl.java
+++ b/server/src/com/cloud/metadata/ResourceMetaDataManagerImpl.java
@@ -16,17 +16,22 @@
 // under the License.
 package com.cloud.metadata;
 
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import javax.ejb.Local;
 import javax.inject.Inject;
 import javax.naming.ConfigurationException;
 
+import org.apache.cloudstack.api.ResourceDetail;
 import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
 
 import com.cloud.dc.DataCenterDetailVO;
 import com.cloud.dc.dao.DataCenterDetailsDao;
+import com.cloud.dc.dao.ResourceDetailDao;
 import com.cloud.event.ActionEvent;
 import com.cloud.event.EventTypes;
 import com.cloud.exception.InvalidParameterValueException;
@@ -71,13 +76,21 @@ public class ResourceMetaDataManagerImpl extends ManagerBase implements
Resource
     @Inject
     VMTemplateDetailsDao _templateDetailsDao;
     @Inject
-    UserVmDetailsDao _userVmDetailsDao;
-    @Inject
     ServiceOfferingDetailsDao _serviceOfferingDetailsDao;
     
+    private static Map<ResourceObjectType, ResourceDetailDao<? extends ResourceDetail>>
_daoMap= 
+            new HashMap<ResourceObjectType, ResourceDetailDao<? extends ResourceDetail>>();
+    
 
     @Override
     public boolean configure(String name, Map<String, Object> params) throws ConfigurationException
{
+        _daoMap.put(ResourceObjectType.UserVm, _userVmDetailDao);
+        _daoMap.put(ResourceObjectType.Volume, _volumeDetailDao);
+        _daoMap.put(ResourceObjectType.Template, _templateDetailsDao);
+        _daoMap.put(ResourceObjectType.Network, _networkDetailsDao);
+        _daoMap.put(ResourceObjectType.Nic, _nicDetailDao);
+        _daoMap.put(ResourceObjectType.ServiceOffering, _serviceOfferingDetailsDao);
+        _daoMap.put(ResourceObjectType.Zone, _dcDetailsDao);
         return true;
     }
 
@@ -100,40 +113,34 @@ public class ResourceMetaDataManagerImpl extends ManagerBase implements
Resource
             @Override
             public Boolean doInTransaction(TransactionStatus status) {
                 for (String key : details.keySet()) {
-                        String value = details.get(key);
-
-                        if (value == null || value.isEmpty()) {
-                            throw new InvalidParameterValueException("Value for the key "
+ key + " is either null or empty");
-                        }
+                    long id = _taggedResourceMgr.getResourceId(resourceId, resourceType);
+                    String value = details.get(key);
+
+                    if (value == null || value.isEmpty()) {
+                        throw new InvalidParameterValueException("Value for the key " + key
+ " is either null or empty");
+                    }
+
+                    DetailDaoHelper newDetailDaoHelper = new DetailDaoHelper(resourceType);
+                    ResourceDetail detail = null;
+                    
+                    // TODO - Have a better design here for getting the DAO.
+                    if(resourceType == ResourceObjectType.Volume){
+                        detail = new VolumeDetailVO(id, key, value);
+                    } else if (resourceType == ResourceObjectType.Nic){
+                        detail = new NicDetailVO(id, key, value);
+                    } else if (resourceType == ResourceObjectType.Zone){
+                        detail = new DataCenterDetailVO(id, key, value);
+                    } else if (resourceType == ResourceObjectType.Network){
+                        detail = new NetworkDetailVO(id, key, value);
+                    } else if (resourceType == ResourceObjectType.UserVm) {
+                        detail = new UserVmDetailVO(id, key, value);
+                    } else if (resourceType == ResourceObjectType.Template) {
+                        detail = new VMTemplateDetailVO(id, key, value);
+                    } else if (resourceType == ResourceObjectType.ServiceOffering) {
+                        detail = new ServiceOfferingDetailsVO(id, key, value);
+                    }
+                    newDetailDaoHelper.addDetail(detail);
                         
-                        if (!resourceType.resourceMetadataSupport())  {
-                            throw new InvalidParameterValueException("The resource type "
+ resourceType + " doesn't support metadata (resource details)");
-                        }
-
-                        long id = _taggedResourceMgr.getResourceId(resourceId, resourceType);
-                        // TODO - Have a better design here for getting the DAO.
-                        if(resourceType == ResourceObjectType.Volume){
-                            VolumeDetailVO detail = new VolumeDetailVO(id, key, value);
-                            _volumeDetailDao.addDetail(detail);
-                        } else if (resourceType == ResourceObjectType.Nic){
-                            NicDetailVO detail = new NicDetailVO(id, key, value);
-                            _nicDetailDao.addDetail(detail);
-                        } else if (resourceType == ResourceObjectType.Zone){
-                             DataCenterDetailVO dataCenterDetail = new DataCenterDetailVO(id,
key, value);
-                             _dcDetailsDao.addDetail(dataCenterDetail);
-                        } else if (resourceType == ResourceObjectType.Network){
-                            NetworkDetailVO detail = new NetworkDetailVO(id, key, value);
-                            _networkDetailsDao.addDetail(detail);
-                        } else if (resourceType == ResourceObjectType.UserVm) {
-                            UserVmDetailVO detail = new UserVmDetailVO(id, key, value);
-                            _userVmDetailsDao.addDetail(detail);
-                        } else if (resourceType == ResourceObjectType.Template) {
-                             VMTemplateDetailVO detail = new VMTemplateDetailVO(id, key,
value);
-                            _templateDetailsDao.addDetail(detail);
-                        } else if (resourceType == ResourceObjectType.ServiceOffering) {
-                            ServiceOfferingDetailsVO detail = new ServiceOfferingDetailsVO(id,
key, value);
-                            _serviceOfferingDetailsDao.addDetail(detail);
-                        }
                 }
                 
                 
@@ -147,32 +154,56 @@ public class ResourceMetaDataManagerImpl extends ManagerBase implements
Resource
     @DB
     @ActionEvent(eventType = EventTypes.EVENT_RESOURCE_DETAILS_DELETE, eventDescription =
"deleting resource meta data")
     public boolean deleteResourceMetaData(String resourceId, ResourceObjectType resourceType,
String key){
-
         long id = _taggedResourceMgr.getResourceId(resourceId, resourceType);
         
-        if (!resourceType.resourceMetadataSupport()) {
-            throw new InvalidParameterValueException("The resource type " + resourceType
+ " is not supported by the API yet");
-        }
-        
-        // TODO - Have a better design here for getting the DAO.
-        if (resourceType == ResourceObjectType.Volume){
-           _volumeDetailDao.removeDetail(id, key);
-        } else if (resourceType == ResourceObjectType.Nic){
-            _nicDetailDao.removeDetail(id, key);
-        } else if (resourceType == ResourceObjectType.UserVm) {
-            _userVmDetailsDao.removeDetail(id, key); 
-        } else if (resourceType == ResourceObjectType.Template) {
-            _templateDetailsDao.removeDetail(id, key);
-        } else if (resourceType == ResourceObjectType.Zone){
-            _dcDetailsDao.removeDetail(id, key);
-        } else if (resourceType == ResourceObjectType.ServiceOffering) {
-            _serviceOfferingDetailsDao.removeDetail(id, key);
-        } else if (resourceType == ResourceObjectType.Network) {
-            _networkDetailsDao.removeDetail(id, key);
-        }
+        DetailDaoHelper newDetailDaoHelper = new DetailDaoHelper(resourceType);
+        newDetailDaoHelper.removeDetail(id, key);
 
         return true;
     }
 
-
+    private class DetailDaoHelper {
+        private ResourceObjectType resourceType;
+        
+        private DetailDaoHelper(ResourceObjectType resourceType) {
+            if (!resourceType.resourceMetadataSupport()) {
+                throw new UnsupportedOperationException("ResourceType " + resourceType +
" doesn't support metadata");
+            }
+            this.resourceType = resourceType;
+        }
+        
+        private void addDetail(ResourceDetail detail) {
+            ResourceDetailDao<ResourceDetail> dao = (ResourceDetailDao<ResourceDetail>)_daoMap.get(resourceType);
+            dao.addDetail(detail);   
+        }
+        
+        private void removeDetail(long resourceId, String key) {
+            ResourceDetailDao<? extends ResourceDetail> dao = _daoMap.get(resourceType);
+            dao.removeDetail(resourceId, key);
+        }
+        
+        private List<? extends ResourceDetail> getDetails(long resourceId) {
+            ResourceDetailDao<? extends ResourceDetail> dao = _daoMap.get(resourceType);
+            List<? extends ResourceDetail> detailList = new ArrayList<ResourceDetail>();
       
+            detailList = dao.findDetailsList(resourceId);
+            return detailList;
+        }
+        
+        private ResourceDetail getDetail(long resourceId, String key) {
+            ResourceDetailDao<? extends ResourceDetail> dao = _daoMap.get(resourceType);
+            return dao.findDetail(resourceId, key);
+        }
+    }
+    
+    @Override
+    public List<? extends ResourceDetail> getDetails(long resourceId, ResourceObjectType
resourceType) {
+        DetailDaoHelper newDetailDaoHelper = new DetailDaoHelper(resourceType);
+        return newDetailDaoHelper.getDetails(resourceId);  
+    }
+    
+    @Override
+    public ResourceDetail getDetail(long resourceId, ResourceObjectType resourceType, String
key) {
+        DetailDaoHelper newDetailDaoHelper = new DetailDaoHelper(resourceType);
+        return newDetailDaoHelper.getDetail(resourceId, key);  
+    }
 }


Mime
View raw message