RANGER-660: updated TagSync with an option to delete tags on removal of 
tag-resource map

Signed-off-by: Madhan Neethiraj <[email protected]>


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

Branch: refs/heads/master
Commit: 819426928a7615a4de538e37c6d226def8fb21ad
Parents: 9aa396b
Author: Abhay Kulkarni <[email protected]>
Authored: Fri Oct 2 16:39:43 2015 -0700
Committer: Madhan Neethiraj <[email protected]>
Committed: Sat Oct 3 10:37:52 2015 -0700

----------------------------------------------------------------------
 .../ranger/plugin/store/file/TagFileStore.java  |  1 +
 .../apache/ranger/plugin/util/ServiceTags.java  | 30 ++++++++++++++++++--
 .../java/org/apache/ranger/biz/TagDBStore.java  |  1 +
 .../ranger/rest/ServiceTagsProcessor.java       | 30 ++++++++++++++------
 tagsync/samples/tags.json                       |  1 +
 .../source/atlas/AtlasNotificationMapper.java   |  1 +
 .../main/resources/etc/ranger/data/tags.json    |  3 +-
 7 files changed, 54 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/81942692/agents-common/src/main/java/org/apache/ranger/plugin/store/file/TagFileStore.java
----------------------------------------------------------------------
diff --git 
a/agents-common/src/main/java/org/apache/ranger/plugin/store/file/TagFileStore.java
 
b/agents-common/src/main/java/org/apache/ranger/plugin/store/file/TagFileStore.java
index 8f94164..6a94f04 100644
--- 
a/agents-common/src/main/java/org/apache/ranger/plugin/store/file/TagFileStore.java
+++ 
b/agents-common/src/main/java/org/apache/ranger/plugin/store/file/TagFileStore.java
@@ -1075,6 +1075,7 @@ public class TagFileStore extends AbstractTagStore {
 
                ServiceTags ret = new ServiceTags();
                ret.setOp(ServiceTags.OP_ADD_OR_UPDATE);
+               ret.setTagModel(ServiceTags.TAGMODEL_SHARED);
 
                boolean tagsChanged = true;
 

http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/81942692/agents-common/src/main/java/org/apache/ranger/plugin/util/ServiceTags.java
----------------------------------------------------------------------
diff --git 
a/agents-common/src/main/java/org/apache/ranger/plugin/util/ServiceTags.java 
b/agents-common/src/main/java/org/apache/ranger/plugin/util/ServiceTags.java
index 146c3f9..d03e7bc 100644
--- a/agents-common/src/main/java/org/apache/ranger/plugin/util/ServiceTags.java
+++ b/agents-common/src/main/java/org/apache/ranger/plugin/util/ServiceTags.java
@@ -47,7 +47,12 @@ public class ServiceTags implements java.io.Serializable {
        public static final String OP_ADD_OR_UPDATE = "add_or_update";
        public static final String OP_DELETE        = "delete";
 
-       private String                      op;
+       public static final String TAGMODEL_SHARED        = "shared";
+       public static final String TAGMODEL_RESOURCE_PRIVATE        = 
"resource_private";
+
+
+       private String                      op = OP_ADD_OR_UPDATE;
+       private String                      tagModel = TAGMODEL_SHARED;
        private String                      serviceName;
        private Long                        tagVersion;
        private Date                        tagUpdateTime;
@@ -64,6 +69,13 @@ public class ServiceTags implements java.io.Serializable {
        }
 
        /**
+        * @return the tagModel
+        */
+       public String getTagModel() {
+               return tagModel;
+       }
+
+       /**
         * @return the serviceName
         */
        public String getServiceName() {
@@ -77,6 +89,14 @@ public class ServiceTags implements java.io.Serializable {
                this.op = op;
        }
 
+
+       /**
+        * @param tagModel the tagModel to set
+        */
+       public void setTagModel(String tagModel) {
+               this.tagModel = tagModel;
+       }
+
        /**
         * @param serviceName the serviceName to set
         */
@@ -154,8 +174,12 @@ public class ServiceTags implements java.io.Serializable {
        }
 
        public StringBuilder toString(StringBuilder sb) {
-               
sb.append("ServiceTags={").append("tagVersion=").append(tagVersion).append(", ")
-                               
.append("tagUpdateTime={").append(tagUpdateTime).append("} ")
+               sb.append("ServiceTags={")
+                               .append("op=").append(op).append(", ")
+                               .append("tagModel=").append(tagModel).append(", 
")
+                               
.append("serviceName=").append(serviceName).append(", ")
+                               
.append("tagVersion=").append(tagVersion).append(", ")
+                               
.append("tagUpdateTime={").append(tagUpdateTime).append("}")
                                .append("}");
 
                return sb;

http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/81942692/security-admin/src/main/java/org/apache/ranger/biz/TagDBStore.java
----------------------------------------------------------------------
diff --git a/security-admin/src/main/java/org/apache/ranger/biz/TagDBStore.java 
b/security-admin/src/main/java/org/apache/ranger/biz/TagDBStore.java
index 98a6e87..300ba8d 100644
--- a/security-admin/src/main/java/org/apache/ranger/biz/TagDBStore.java
+++ b/security-admin/src/main/java/org/apache/ranger/biz/TagDBStore.java
@@ -891,6 +891,7 @@ public class TagDBStore extends AbstractTagStore {
 
                        ret = new ServiceTags();
                        ret.setOp(ServiceTags.OP_ADD_OR_UPDATE);
+                       ret.setTagModel(ServiceTags.TAGMODEL_SHARED);
                        ret.setServiceName(xxService.getName());
                        ret.setTagVersion(xxService.getTagVersion());
                        ret.setTagUpdateTime(xxService.getTagUpdateTime());

http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/81942692/security-admin/src/main/java/org/apache/ranger/rest/ServiceTagsProcessor.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/rest/ServiceTagsProcessor.java 
b/security-admin/src/main/java/org/apache/ranger/rest/ServiceTagsProcessor.java
index b34313f..586f6b7 100644
--- 
a/security-admin/src/main/java/org/apache/ranger/rest/ServiceTagsProcessor.java
+++ 
b/security-admin/src/main/java/org/apache/ranger/rest/ServiceTagsProcessor.java
@@ -51,6 +51,10 @@ public class ServiceTagsProcessor {
                }
 
                if (tagStore != null && serviceTags != null) {
+                       if (LOG.isDebugEnabled()) {
+                               LOG.debug("serviceTags: op=" + 
serviceTags.getOp());
+                               LOG.debug("serviceTags: tagModel=" + 
serviceTags.getTagModel());
+                       }
                        String op = serviceTags.getOp();
 
                        if (StringUtils.equalsIgnoreCase(op, 
ServiceTags.OP_ADD_OR_UPDATE)) {
@@ -237,9 +241,9 @@ public class ServiceTagsProcessor {
                                }
 
                                // Get all tags associated with this resourceId
-                               List<Long> tagMapsToDelete = null;
+                               List<Long> tagsToDelete = null;
                                try {
-                                       tagMapsToDelete = 
tagStore.getTagIdsForResourceId(resourceInStore.getId());
+                                       tagsToDelete = 
tagStore.getTagIdsForResourceId(resourceInStore.getId());
                                } catch (Exception exception) {
                                        LOG.error("RangerTags cannot be 
retrieved for resource with guid=" + resourceInStore.getGuid());
                                        throw exception;
@@ -270,8 +274,8 @@ public class ServiceTagsProcessor {
                                                        tagResourceMap = 
tagStore.createTagResourceMap(tagResourceMap);
                                                }
 
-                                               if(tagMapsToDelete != null) {
-                                                       
tagMapsToDelete.remove((Long)tagInStore.getId());
+                                               if(tagsToDelete != null) {
+                                                       
tagsToDelete.remove((Long)tagInStore.getId());
                                                }
                                        }
                                } catch (Exception exception) {
@@ -279,12 +283,12 @@ public class ServiceTagsProcessor {
                                        throw exception;
                                }
 
-                               if 
(CollectionUtils.isNotEmpty(tagMapsToDelete)) {
+                               if (CollectionUtils.isNotEmpty(tagsToDelete)) {
                                        Long tagId = null;
 
                                        try {
-                                               for(int i = 0; i < 
tagMapsToDelete.size(); i++) {
-                                                       tagId = 
tagMapsToDelete.get(i);
+                                               for(int i = 0; i < 
tagsToDelete.size(); i++) {
+                                                       tagId = 
tagsToDelete.get(i);
 
                                                        RangerTagResourceMap 
tagResourceMap = tagStore.getTagResourceMapForTagAndResourceId(tagId, 
resourceInStore.getId());
 
@@ -293,11 +297,19 @@ public class ServiceTagsProcessor {
                                                        }
 
                                                        if 
(LOG.isDebugEnabled()) {
-                                                               
LOG.debug("Deleted tagResourceMap(tagId=" + tagId + ", resourceId=" + 
resourceId);
+                                                               
LOG.debug("Deleted tagResourceMap(tagId=" + tagId + ", resourceId=" + 
resourceInStore.getId());
+                                                       }
+
+                                                       if 
(StringUtils.equals(serviceTags.getTagModel(), 
ServiceTags.TAGMODEL_RESOURCE_PRIVATE)) {
+                                                               
tagStore.deleteTag(tagId);
+
+                                                               if 
(LOG.isDebugEnabled()) {
+                                                                       
LOG.debug("Deleted tag(tagId=" + tagId + ") as tagModel=" + 
ServiceTags.TAGMODEL_RESOURCE_PRIVATE);
+                                                               }
                                                        }
                                                }
                                        } catch(Exception exception) {
-                                               LOG.error("deleteTagResourceMap 
failed, tagId=" + tagId + ", resourceId=" + resourceId);
+                                               LOG.error("deleteTagResourceMap 
failed, tagId=" + tagId + ", resourceId=" + resourceInStore.getId());
                                                throw exception;
                                        }
                                }

http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/81942692/tagsync/samples/tags.json
----------------------------------------------------------------------
diff --git a/tagsync/samples/tags.json b/tagsync/samples/tags.json
index 1b24fc8..3028f9d 100644
--- a/tagsync/samples/tags.json
+++ b/tagsync/samples/tags.json
@@ -1,5 +1,6 @@
 {
   "op": "add_or_update",
+  "tagModel": "resource_private",
   "serviceName": "cl1_hive",
   "tagVersion": 1,
   "tagUpdateTime": "20150924-22:26:33.000-+0000",

http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/81942692/tagsync/src/main/java/org/apache/ranger/tagsync/source/atlas/AtlasNotificationMapper.java
----------------------------------------------------------------------
diff --git 
a/tagsync/src/main/java/org/apache/ranger/tagsync/source/atlas/AtlasNotificationMapper.java
 
b/tagsync/src/main/java/org/apache/ranger/tagsync/source/atlas/AtlasNotificationMapper.java
index 44c4973..2c843af 100644
--- 
a/tagsync/src/main/java/org/apache/ranger/tagsync/source/atlas/AtlasNotificationMapper.java
+++ 
b/tagsync/src/main/java/org/apache/ranger/tagsync/source/atlas/AtlasNotificationMapper.java
@@ -152,6 +152,7 @@ class AtlasNotificationMapper {
                ret = new ServiceTags();
 
                ret.setOp(ServiceTags.OP_ADD_OR_UPDATE);
+               ret.setTagModel(ServiceTags.TAGMODEL_RESOURCE_PRIVATE);
                ret.setServiceName(serviceResource.getServiceName());
                ret.setServiceResources(serviceResources);
                ret.setTagDefinitions(tagDefs);

http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/81942692/tagsync/src/main/resources/etc/ranger/data/tags.json
----------------------------------------------------------------------
diff --git a/tagsync/src/main/resources/etc/ranger/data/tags.json 
b/tagsync/src/main/resources/etc/ranger/data/tags.json
index 28e7922..274cf69 100644
--- a/tagsync/src/main/resources/etc/ranger/data/tags.json
+++ b/tagsync/src/main/resources/etc/ranger/data/tags.json
@@ -1,5 +1,6 @@
 {
-    "op":"create_or_update",
+    "op":"add_or_update",
+    "tagModel":"shared",
     "serviceName": "cl1_hive",
     "tagVersion": 24,
     "tagUpdateTime": "20150901-20:03:17.000-+0000",

Reply via email to