You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stratos.apache.org by ud...@apache.org on 2015/05/18 08:00:03 UTC

stratos git commit: add DELETE applications/{app_id}/properties/{property_name}

Repository: stratos
Updated Branches:
  refs/heads/master 84004c1d9 -> a8dc8c1f8


add DELETE applications/{app_id}/properties/{property_name}


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

Branch: refs/heads/master
Commit: a8dc8c1f8eb8d81e97622f28c9b9bff8c5da9a62
Parents: 84004c1
Author: Udara Liyanage <ud...@wso2.com>
Authored: Mon May 18 11:29:19 2015 +0530
Committer: Udara Liyanage <ud...@wso2.com>
Committed: Mon May 18 11:29:33 2015 +0530

----------------------------------------------------------------------
 .../metadata/service/api/MetadataApi.java       | 37 +++++++++---
 .../service/registry/CarbonRegistry.java        | 62 +++++++++++---------
 .../metadata/service/registry/DataStore.java    |  8 ++-
 3 files changed, 70 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/a8dc8c1f/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/api/MetadataApi.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/api/MetadataApi.java b/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/api/MetadataApi.java
index 38002ba..dea5eba 100644
--- a/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/api/MetadataApi.java
+++ b/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/api/MetadataApi.java
@@ -225,14 +225,32 @@ public class MetadataApi {
             throws RestAPIException {
 
         try {
-            boolean deleted = registry.deleteApplication(applicationId);
+            boolean deleted = registry.deleteApplicationProperties(applicationId);
             if (!deleted) {
                 log.warn(String.format(
-                        "Either no metadata is associated with given appId %s Or resources could not be deleted",
+                        "No metadata is associated with given appId %s",
                         applicationId));
             }
         } catch (RegistryException e) {
-            String msg = "Resource attached with appId could not be deleted";
+            String msg = "Resource attached with appId could not be deleted [application-id] " + applicationId;
+            log.error(msg, e);
+            throw new RestAPIException(msg, e);
+        }
+        return Response.ok().build();
+    }
+
+    @DELETE
+    @Path("applications/{application_id}/properties/{property_name}")
+    @Produces("application/json")
+    @Consumes("application/json")
+    public Response deleteApplicationProperty(@PathParam("application_id") String applicationId, @PathParam("property_name") String propertyName)
+            throws RestAPIException {
+
+        try {
+            boolean deleted = registry.removePropertyFromApplication(applicationId, propertyName);
+
+        } catch (RegistryException e) {
+            String msg = String.format("[application-id] %s [property-name] deletion failed ", applicationId, propertyName);
             log.error(msg, e);
             throw new RestAPIException(msg, e);
         }
@@ -243,14 +261,19 @@ public class MetadataApi {
     @Path("applications/{application_id}/properties/{property_name}/value/{value}")
     @Produces("application/json")
     @Consumes("application/json")
-    public Response deleteApplicationPropertValue(@PathParam("application_id") String applicationId, @PathParam("property_name") String propertyName,
-                                                  @PathParam("value") String propertyValue                      )
+    public Response deleteApplicationPropertyValue(@PathParam("application_id") String applicationId, @PathParam("property_name") String propertyName,
+                                                   @PathParam("value") String propertyValue)
             throws RestAPIException {
 
         try {
-            registry.removePropertyFromApplication(applicationId, propertyName, propertyValue);
+            boolean deleted = registry.removePropertyValueFromApplication(applicationId, propertyName, propertyValue);
+            if (!deleted) {
+                log.warn(String.format(
+                        "No metadata is associated with given [application-id] %s",
+                        applicationId));
+            }
         } catch (RegistryException e) {
-            String msg = "Resource value deletion failed ";
+            String msg = String.format("[application-id] %s [property-name] %s [value] %s deletion failed" + applicationId, propertyName, propertyValue);
             log.error(msg, e);
             throw new RestAPIException(msg, e);
         }

http://git-wip-us.apache.org/repos/asf/stratos/blob/a8dc8c1f/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/registry/CarbonRegistry.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/registry/CarbonRegistry.java b/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/registry/CarbonRegistry.java
index ae28fd2..f7eb014 100644
--- a/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/registry/CarbonRegistry.java
+++ b/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/registry/CarbonRegistry.java
@@ -174,11 +174,10 @@ public class CarbonRegistry implements DataStore {
 
     }
 
-    public boolean removePropertyFromApplication(String applicationId, String propertyName, String valueToRemove) throws RegistryException {
+    public boolean removePropertyValueFromApplication(String applicationId, String propertyName, String valueToRemove) throws RegistryException {
         Registry registry = getRegistry();
         String resourcePath = mainResource + applicationId;
 
-        try {
             PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
             ctx.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
             ctx.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
@@ -191,18 +190,11 @@ public class CarbonRegistry implements DataStore {
                 log.warn(String.format("Registry [resource] %s not found ", resourcePath));
                 return false;
             }
-
             nodeResource.removePropertyValue(propertyName, valueToRemove);
             registry.put(resourcePath, nodeResource);
             registry.commitTransaction();
 
             log.info(String.format("Application %s property %s value %s is removed from metadata ", applicationId, propertyName, valueToRemove));
-        } catch (Exception e) {
-            String msg = "Failed to persist properties in registry: " + resourcePath;
-            registry.rollbackTransaction();
-            log.error(msg, e);
-            throw new RegistryException(msg, e);
-        }
 
         return true;
     }
@@ -219,12 +211,10 @@ public class CarbonRegistry implements DataStore {
         Registry registry = getRegistry();
         String resourcePath = mainResource + applicationId + "/" + clusterId;
 
-        try {
             PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
             ctx.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
             ctx.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
 
-            registry.beginTransaction();
             Resource nodeResource = null;
             if (registry.resourceExists(resourcePath)) {
                 nodeResource = registry.get(resourcePath);
@@ -238,17 +228,8 @@ public class CarbonRegistry implements DataStore {
             nodeResource.setProperty(property.getKey(), Arrays.asList(property.getValues()));
             registry.put(resourcePath, nodeResource);
 
-            registry.commitTransaction();
-
             log.info(String.format("Registry property is persisted: [resource-path] %s [Property Name] %s [Property Values] %s",
                     resourcePath, property.getKey(), Arrays.asList(property.getValues())));
-
-        } catch (Exception e) {
-            String msg = "Failed to persist properties in registry: " + resourcePath;
-            registry.rollbackTransaction();
-            log.error(msg, e);
-            throw new RegistryException(msg, e);
-        }
     }
 
     private UserRegistry getRegistry() throws RegistryException {
@@ -262,7 +243,7 @@ public class CarbonRegistry implements DataStore {
      * @return True if resource exist and able to delete, else false.
      * @throws RegistryException
      */
-    public boolean deleteApplication(String applicationId) throws RegistryException {
+    public boolean deleteApplicationProperties(String applicationId) throws RegistryException {
         if (StringUtils.isBlank(applicationId)) {
             throw new IllegalArgumentException("Application ID can not be null");
         }
@@ -271,22 +252,47 @@ public class CarbonRegistry implements DataStore {
         ctx.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
         ctx.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
 
-        Registry registry = null;
+        Registry registry;
         String resourcePath = mainResource + applicationId;
-        try {
+
             registry = getRegistry();
             registry.beginTransaction();
             if (registry.resourceExists(resourcePath)) {
                 registry.delete(resourcePath);
                 registry.commitTransaction();
-                log.info(String.format("Application removed from registry %s", applicationId));
+                log.info(String.format("Application [application-id ] properties removed from registry %s", applicationId));
                 return true;
             }
-        } catch (RegistryException e) {
-            registry.rollbackTransaction();
-            log.error("Could not remove registry resource: [resource-path] " + resourcePath);
-        }
+
         return false;
     }
 
+    public boolean removePropertyFromApplication(String applicationId, String propertyName) throws org.wso2.carbon.registry.api.RegistryException {
+        Registry registry = getRegistry();
+        String resourcePath = mainResource + applicationId;
+
+            PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
+            ctx.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
+            ctx.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
+
+            Resource nodeResource;
+            if (registry.resourceExists(resourcePath)) {
+                nodeResource = registry.get(resourcePath);
+                if(nodeResource.getProperty(propertyName) == null) {
+                    log.info(String.format("[application-id] %s does not have a property [property-name] %s ", applicationId, propertyName));
+                    return false;
+                }else{
+                    nodeResource.removeProperty(propertyName);
+                    registry.put(resourcePath, nodeResource);
+                }
+
+            }else {
+                log.error("Registry resource not not found at " + resourcePath);
+               return false;
+            }
+
+        log.info(String.format("Application [application-id] %s property [property-name] %s removed from Registry ", applicationId, propertyName));
+        return true;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/a8dc8c1f/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/registry/DataStore.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/registry/DataStore.java b/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/registry/DataStore.java
index dbdd6df..c11a2b9 100644
--- a/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/registry/DataStore.java
+++ b/components/org.apache.stratos.metadata.service/src/main/java/org/apache/stratos/metadata/service/registry/DataStore.java
@@ -38,7 +38,11 @@ public interface DataStore {
 
     public void addPropertyToCluster(String applicationId, String clusterId, Property property) throws RegistryException;
 
-    public boolean deleteApplication(String applicationId) throws RegistryException;
+    public boolean deleteApplicationProperties(String applicationId) throws RegistryException;
+
+    public boolean removePropertyFromApplication(String applicationId, String propertyName) throws RegistryException;
+
+    public boolean removePropertyValueFromApplication(String applicationId, String propertyName, String valueToRemove) throws RegistryException;
+
 
-    public boolean removePropertyFromApplication(String applicationId, String propertyName, String valueToRemove) throws RegistryException;
 }