usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sfeld...@apache.org
Subject [1/2] usergrid git commit: add delete all to two-o
Date Fri, 04 Sep 2015 22:06:00 GMT
Repository: usergrid
Updated Branches:
  refs/heads/two-dot-o bc9265a98 -> 543d2b4eb


add delete all to two-o


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

Branch: refs/heads/two-dot-o
Commit: 4dab25b722141ef72c6119d4b23b88a0f60d5a39
Parents: bc9265a
Author: Shawn Feldman <shawn.feldman@gmail.com>
Authored: Fri Sep 4 11:21:39 2015 -0600
Committer: Shawn Feldman <shawn.feldman@gmail.com>
Committed: Fri Sep 4 11:21:39 2015 -0600

----------------------------------------------------------------------
 .../corepersistence/CpEntityManagerFactory.java | 63 +++++++++++++++++---
 .../persistence/EntityManagerFactory.java       |  4 ++
 .../cassandra/EntityManagerFactoryImpl.java     |  8 +++
 .../apache/usergrid/rest/SystemResource.java    | 54 ++++++++++++++---
 .../apache/usergrid/rest/SystemResourceIT.java  | 26 +++++++-
 .../rest/test/resource2point0/ClientSetup.java  |  4 +-
 .../endpoints/SystemResource.java               | 24 ++++++++
 7 files changed, 163 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/usergrid/blob/4dab25b7/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java
b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java
index fe4d828..2957295 100644
--- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java
+++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java
@@ -30,13 +30,8 @@ import org.apache.commons.lang.StringUtils;
 
 import org.apache.usergrid.corepersistence.rx.AllEntitiesInSystemObservable;
 import org.apache.usergrid.corepersistence.util.CpNamingUtils;
-import org.apache.usergrid.persistence.AbstractEntity;
-import org.apache.usergrid.persistence.Entity;
-import org.apache.usergrid.persistence.EntityFactory;
-import org.apache.usergrid.persistence.EntityManager;
-import org.apache.usergrid.persistence.EntityManagerFactory;
-import org.apache.usergrid.persistence.EntityRef;
-import org.apache.usergrid.persistence.Results;
+import org.apache.usergrid.persistence.*;
+
 import static org.apache.usergrid.persistence.Schema.PROPERTY_NAME;
 import static org.apache.usergrid.persistence.Schema.TYPE_APPLICATION;
 import org.apache.usergrid.persistence.cassandra.CassandraService;
@@ -63,6 +58,7 @@ import org.apache.usergrid.persistence.index.EntityIndex;
 import org.apache.usergrid.persistence.index.query.Query;
 import org.apache.usergrid.persistence.model.entity.Id;
 import org.apache.usergrid.persistence.model.entity.SimpleId;
+import org.apache.usergrid.utils.InflectionUtils;
 import org.apache.usergrid.utils.UUIDUtils;
 import org.apache.usergrid.persistence.model.util.UUIDGenerator;
 import org.slf4j.Logger;
@@ -71,6 +67,8 @@ import org.springframework.beans.BeansException;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.ApplicationContextAware;
 import rx.Observable;
+import rx.functions.Action1;
+import rx.functions.Func1;
 
 
 /**
@@ -80,6 +78,7 @@ import rx.Observable;
 public class CpEntityManagerFactory implements EntityManagerFactory, ApplicationContextAware
{
 
     private static final Logger logger = LoggerFactory.getLogger( CpEntityManagerFactory.class
);
+    private final AllEntitiesInSystemObservable allEntitiesObservable;
 
     private ApplicationContext applicationContext;
 
@@ -118,6 +117,7 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
         this.metricsFactory = injector.getInstance( MetricsFactory.class );
 
         this.orgApplicationCache = new OrgApplicationCacheImpl( this );
+        this.allEntitiesObservable = injector.getInstance(AllEntitiesInSystemObservable.class);
     }
 
 
@@ -338,6 +338,55 @@ public class CpEntityManagerFactory implements EntityManagerFactory,
Application
         em.refreshIndex();
     }
 
+    @Override
+    public Observable<Id> deleteAllEntitiesfromApplication(UUID applicationId) {
+
+        final ApplicationScope applicationScope = CpNamingUtils.getApplicationScope(applicationId);
+        if (applicationScope.getApplication().getUuid().equals(CpNamingUtils.MANAGEMENT_APPLICATION_ID))
{
+            throw new IllegalArgumentException("Can't delete from management app");
+        }
+
+        //EventBuilder eventBuilder = injector.getInstance(EventBuilder.class);
+        Observable appObservable = Observable.just(applicationScope);
+
+        Observable<Id> countObservable = AllEntitiesInSystemObservable.getAllEntitiesInSystem(managerCache,
100)
+            .filter(new Func1<AllEntitiesInSystemObservable.ApplicationEntityGroup, Boolean>()
{
+                @Override
+                public Boolean call(AllEntitiesInSystemObservable.ApplicationEntityGroup
applicationEntityGroup) {
+                    return applicationEntityGroup.applicationScope.equals(applicationScope);
+                }
+            })//skip application entity
+            .flatMap(new Func1<AllEntitiesInSystemObservable.ApplicationEntityGroup, Observable<?
extends Id>>() {
+                @Override
+                public Observable<? extends Id> call(AllEntitiesInSystemObservable.ApplicationEntityGroup
applicationEntityGroup) {
+                    final EntityManager em = getEntityManager(applicationEntityGroup.applicationScope.getApplication().getUuid());
+                    return Observable.from(applicationEntityGroup.entityIds)
+                        .filter(new Func1<Id, Boolean>() {
+                            @Override
+                            public Boolean call(Id id) {
+
+                                final String type = InflectionUtils.pluralize(id.getType());
+                                return !(type.equals(Schema.COLLECTION_USERS) || type.equals(Schema.COLLECTION_GROUPS)
+                                    || type.equals(InflectionUtils.pluralize(Schema.TYPE_APPLICATION))
|| type.equals(Schema.COLLECTION_ROLES));
+                            }
+                        })
+                        .doOnNext(new Action1<Id>() {
+                            @Override
+                            public void call(Id id) {
+                                try {
+                                    em.delete(new SimpleEntityRef(id.getType(), id.getUuid()));
+                                } catch (Exception e) {
+                                    throw new RuntimeException(e);
+                                }
+                            }
+                        });
+                }
+            });
+
+        return countObservable;
+
+    }
+
 
     @Override
     public void restoreApplication(UUID applicationId) throws Exception {

http://git-wip-us.apache.org/repos/asf/usergrid/blob/4dab25b7/stack/core/src/main/java/org/apache/usergrid/persistence/EntityManagerFactory.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/persistence/EntityManagerFactory.java
b/stack/core/src/main/java/org/apache/usergrid/persistence/EntityManagerFactory.java
index 6db05f5..cdc03ab 100644
--- a/stack/core/src/main/java/org/apache/usergrid/persistence/EntityManagerFactory.java
+++ b/stack/core/src/main/java/org/apache/usergrid/persistence/EntityManagerFactory.java
@@ -22,7 +22,9 @@ import java.util.UUID;
 
 import org.apache.usergrid.persistence.core.util.Health;
 import org.apache.usergrid.persistence.index.EntityIndex;
+import org.apache.usergrid.persistence.model.entity.Id;
 import org.springframework.context.ApplicationContext;
+import rx.Observable;
 
 
 /**
@@ -169,6 +171,8 @@ public interface EntityManagerFactory {
 
     public Health getEntityStoreHealth();
 
+    Observable<Id> deleteAllEntitiesfromApplication(UUID applicationId) throws Exception;
+
     void restoreApplication(UUID applicationId) throws Exception;
 
     public interface ProgressObserver {

http://git-wip-us.apache.org/repos/asf/usergrid/blob/4dab25b7/stack/core/src/main/java/org/apache/usergrid/persistence/cassandra/EntityManagerFactoryImpl.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/persistence/cassandra/EntityManagerFactoryImpl.java
b/stack/core/src/main/java/org/apache/usergrid/persistence/cassandra/EntityManagerFactoryImpl.java
index a8c62d9..14bbac3 100644
--- a/stack/core/src/main/java/org/apache/usergrid/persistence/cassandra/EntityManagerFactoryImpl.java
+++ b/stack/core/src/main/java/org/apache/usergrid/persistence/cassandra/EntityManagerFactoryImpl.java
@@ -22,6 +22,8 @@ import java.util.Map;
 import java.util.TreeMap;
 import java.util.UUID;
 
+import org.apache.usergrid.exception.NotImplementedException;
+import org.apache.usergrid.persistence.model.entity.Id;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.BeansException;
@@ -67,6 +69,7 @@ import static org.apache.usergrid.persistence.cassandra.CassandraService.RETRY_C
 import static org.apache.usergrid.utils.ConversionUtils.uuid;
 import static org.apache.usergrid.persistence.cassandra.Serializers.*;
 import org.apache.usergrid.persistence.core.util.Health;
+import rx.Observable;
 
 
 /**
@@ -473,6 +476,11 @@ public class EntityManagerFactoryImpl implements EntityManagerFactory,
Applicati
     }
 
     @Override
+    public Observable<Id> deleteAllEntitiesfromApplication(UUID applicationId) throws
Exception {
+        throw new NotImplementedException("not here!");
+    }
+
+    @Override
     public void restoreApplication(UUID applicationId) throws Exception {
         throw new UnsupportedOperationException("Not supported in v1");
     }

http://git-wip-us.apache.org/repos/asf/usergrid/blob/4dab25b7/stack/rest/src/main/java/org/apache/usergrid/rest/SystemResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/main/java/org/apache/usergrid/rest/SystemResource.java b/stack/rest/src/main/java/org/apache/usergrid/rest/SystemResource.java
index fca3333..b8faca4 100644
--- a/stack/rest/src/main/java/org/apache/usergrid/rest/SystemResource.java
+++ b/stack/rest/src/main/java/org/apache/usergrid/rest/SystemResource.java
@@ -17,17 +17,13 @@
 package org.apache.usergrid.rest;
 
 
+import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
+import java.util.concurrent.atomic.AtomicInteger;
 
-import javax.ws.rs.DefaultValue;
-import javax.ws.rs.GET;
-import javax.ws.rs.PUT;
-import javax.ws.rs.Path;
-import javax.ws.rs.PathParam;
-import javax.ws.rs.Produces;
-import javax.ws.rs.QueryParam;
+import javax.ws.rs.*;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.UriInfo;
@@ -49,6 +45,7 @@ import com.clearspring.analytics.util.Preconditions;
 import com.fasterxml.jackson.databind.node.JsonNodeFactory;
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.sun.jersey.api.json.JSONWithPadding;
+import rx.functions.Action1;
 
 
 @Path( "/system" )
@@ -100,9 +97,9 @@ public class SystemResource extends AbstractContextResource {
             throws Exception {
 
         ApiResponse response = createApiResponse();
-        response.setAction( "superuser setup" );
+        response.setAction("superuser setup");
 
-        logger.info( "Setting up Superuser" );
+        logger.info("Setting up Superuser");
 
         try {
             management.provisionSuperuser();
@@ -116,6 +113,45 @@ public class SystemResource extends AbstractContextResource {
         return new JSONWithPadding( response, callback );
     }
 
+    @RequireSystemAccess
+    @DELETE
+    @Path( "applications/{applicationId}" )
+    public JSONWithPadding clearApplication( @Context UriInfo ui,
+                                             @PathParam("applicationId") UUID applicationId,
+                                             @QueryParam( "confirmApplicationId" ) UUID confirmApplicationId,
+                                             @QueryParam( "callback" ) @DefaultValue( "callback"
) String callback )
+        throws Exception {
+
+        if(confirmApplicationId == null || !confirmApplicationId.equals(applicationId)){
+            throw new IllegalArgumentException("please make confirmApplicationId equal to
applicationId");
+        }
+
+        ApiResponse response = createApiResponse();
+        response.setAction( "clear application" );
+
+        logger.info( "clearing up application" );
+        final AtomicInteger itemsDeleted = new AtomicInteger(0);
+        try {
+            this.emf.deleteAllEntitiesfromApplication(applicationId)
+                .count()
+                .doOnNext(new Action1<Integer>() {
+                    @Override
+                    public void call(Integer count) {
+                        itemsDeleted.set(count);
+                    }
+                })
+                .toBlocking().lastOrDefault(0);
+        }
+        catch ( Exception e ) {
+            logger.error( "Unable to delete all items, deleted: " + itemsDeleted.get(), e
);
+        }
+        Map<String,Object> data = new HashMap<>();
+        data.put("count", itemsDeleted.get());
+        response.setData(data);
+        response.setSuccess();
+        return new JSONWithPadding( response, callback );
+    }
+
     @Path( "migrate" )
     public MigrateResource migrate(){
         return getSubResource( MigrateResource.class );

http://git-wip-us.apache.org/repos/asf/usergrid/blob/4dab25b7/stack/rest/src/test/java/org/apache/usergrid/rest/SystemResourceIT.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/SystemResourceIT.java b/stack/rest/src/test/java/org/apache/usergrid/rest/SystemResourceIT.java
index 5695143..7e34447 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/SystemResourceIT.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/SystemResourceIT.java
@@ -17,15 +17,16 @@
 package org.apache.usergrid.rest;
 
 
+import org.apache.usergrid.rest.test.resource2point0.model.*;
 import org.junit.Test;
 
 import org.apache.usergrid.rest.test.resource2point0.AbstractRestIT;
-import org.apache.usergrid.rest.test.resource2point0.model.Entity;
-import org.apache.usergrid.rest.test.resource2point0.model.QueryParameters;
-import org.apache.usergrid.rest.test.resource2point0.model.Token;
 
 import com.sun.jersey.api.client.UniformInterfaceException;
 
+import java.util.LinkedHashMap;
+
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
 
@@ -57,4 +58,23 @@ public class SystemResourceIT extends AbstractRestIT {
 
     }
 
+    @Test
+    public void testDeleteAllApplicationEntities() {
+        int count = 10;
+        for(int i =0; i<count;i++) {
+            this.app().collection("tests").post(new Entity().chainPut("testval", "test"));
+        }
+        this.refreshIndex();
+        QueryParameters queryParameters = new QueryParameters();
+        queryParameters.addParam("access_token", clientSetup.getSuperuserToken().getAccessToken());
+        queryParameters.addParam("confirmApplicationId", this.clientSetup.getAppUuid());
+
+        org.apache.usergrid.rest.test.resource2point0.model.ApiResponse result = clientSetup.getRestClient().system().applications(this.clientSetup.getAppUuid()).delete(
queryParameters);
+
+        assertNotNull( result );
+        assertNotNull( "ok",result.getStatus() );
+        assertEquals(((LinkedHashMap) result.getData()).get("count"), count);
+    }
+
+
 }

http://git-wip-us.apache.org/repos/asf/usergrid/blob/4dab25b7/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/ClientSetup.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/ClientSetup.java
b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/ClientSetup.java
index 7dc4eb5..4a7fc50 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/ClientSetup.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/ClientSetup.java
@@ -122,7 +122,9 @@ public class ClientSetup implements TestRule {
 
         application = restClient.management().orgs().organization(organization.getName()).app().post(new
Application(appName));
        // application = restClient.management().orgs().organization(organization.getName()).app().get();
-        appUuid = (String)((LinkedHashMap)application.getDynamicProperties().get( "data"
)).get( orgName+"/"+appName );
+        LinkedHashMap map = (LinkedHashMap)application.getDynamicProperties().get( "data"
);
+        appUuid = (String)map.get(map.keySet().iterator().next() );
+
 
         refreshIndex();
 

http://git-wip-us.apache.org/repos/asf/usergrid/blob/4dab25b7/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/SystemResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/SystemResource.java
b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/SystemResource.java
index ee70942..086f503 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/SystemResource.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/SystemResource.java
@@ -20,8 +20,11 @@ package org.apache.usergrid.rest.test.resource2point0.endpoints;
 
 import javax.ws.rs.core.MediaType;
 
+import com.sun.jersey.api.client.WebResource;
+import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
 import org.apache.usergrid.rest.test.resource2point0.model.ApiResponse;
 import org.apache.usergrid.rest.test.resource2point0.model.Entity;
+import org.apache.usergrid.rest.test.resource2point0.model.QueryParameters;
 import org.apache.usergrid.rest.test.resource2point0.state.ClientContext;
 
 
@@ -54,4 +57,25 @@ public class SystemResource extends NamedResource {
                                         .put(ApiResponse.class);
         return response;
     }
+
+    public ApplicationsResource applications(String appid) {
+        return new ApplicationsResource(appid,context,this);
+    }
+    public class ApplicationsResource extends NamedResource {
+        public ApplicationsResource(final String appid, final ClientContext context, final
UrlResource parent ) {
+            super( "applications/"+appid,context, parent );
+        }
+        public ApiResponse delete(QueryParameters queryParameters){
+
+            WebResource resource = getResource();
+            resource = addParametersToResource( resource, queryParameters );
+
+            //added httpBasicauth filter to all setup calls because they all do verification
this way.
+            HTTPBasicAuthFilter httpBasicAuthFilter = new HTTPBasicAuthFilter( "superuser","superpassword"
);
+            resource.addFilter(httpBasicAuthFilter);
+
+            return resource.type( MediaType.APPLICATION_JSON_TYPE ).accept( MediaType.APPLICATION_JSON
)
+                .delete(ApiResponse.class);
+        }
+    }
 }


Mime
View raw message