usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdun...@apache.org
Subject [1/2] usergrid git commit: USERGRID-992: Add organization config GET and PUT
Date Tue, 08 Sep 2015 20:57:44 GMT
Repository: usergrid
Updated Branches:
  refs/heads/USERGRID-992 [created] 89b213623


USERGRID-992: Add organization config GET and PUT

Currently requires only organization access -- will add superuser requirement.


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

Branch: refs/heads/USERGRID-992
Commit: 94008adf4224a4e3737e79618643384238de49a4
Parents: ab5ace4
Author: Mike Dunker <mdunker@apache.org>
Authored: Tue Sep 8 13:02:19 2015 -0700
Committer: Mike Dunker <mdunker@apache.org>
Committed: Tue Sep 8 13:02:19 2015 -0700

----------------------------------------------------------------------
 .../corepersistence/util/CpNamingUtils.java     |   3 +
 .../org/apache/usergrid/rest/ApiResponse.java   |  14 ++
 .../organizations/OrganizationResource.java     |  54 ++++++
 .../organizations/OrganizationsResource.java    |   1 +
 .../usergrid/management/ManagementService.java  |  10 ++
 .../usergrid/management/OrganizationConfig.java | 178 +++++++++++++++++++
 .../cassandra/ManagementServiceImpl.java        |  84 +++++++++
 7 files changed, 344 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/usergrid/blob/94008adf/stack/core/src/main/java/org/apache/usergrid/corepersistence/util/CpNamingUtils.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/util/CpNamingUtils.java
b/stack/core/src/main/java/org/apache/usergrid/corepersistence/util/CpNamingUtils.java
index acda51f..f32e9d7 100644
--- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/util/CpNamingUtils.java
+++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/util/CpNamingUtils.java
@@ -78,6 +78,9 @@ public class CpNamingUtils {
     public static final String DELETED_APPLICATION_INFO = "deleted_org_application";
     public static final String DELETED_APPLICATION_INFOS = "deleted_org_applications";
 
+    public static final String ORG_CONFIG = "org_config";
+    public static final String ORG_CONFIGS = "org_configs";
+
     /**
      * The name of the map that holds our entity id->type mapping
      */

http://git-wip-us.apache.org/repos/asf/usergrid/blob/94008adf/stack/rest/src/main/java/org/apache/usergrid/rest/ApiResponse.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/main/java/org/apache/usergrid/rest/ApiResponse.java b/stack/rest/src/main/java/org/apache/usergrid/rest/ApiResponse.java
index 7a9e262..0d26395 100644
--- a/stack/rest/src/main/java/org/apache/usergrid/rest/ApiResponse.java
+++ b/stack/rest/src/main/java/org/apache/usergrid/rest/ApiResponse.java
@@ -34,6 +34,8 @@ import java.util.UUID;
 
 import javax.xml.bind.annotation.XmlAnyElement;
 import javax.xml.bind.annotation.XmlRootElement;
+
+import org.apache.usergrid.management.OrganizationConfig;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.apache.commons.lang.ClassUtils;
 import org.apache.commons.lang.StringUtils;
@@ -95,6 +97,7 @@ public class ApiResponse {
     private Map<String, List<String>> params;
     private List<AggregateCounterSet> counters;
     private ClientCredentialsInfo credentials;
+    private OrganizationConfig organizationConfig;
 
     protected Map<String, Object> properties = new TreeMap<String, Object>( String.CASE_INSENSITIVE_ORDER
);
 
@@ -578,6 +581,17 @@ public class ApiResponse {
     }
 
 
+    @JsonProperty("configuration")
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    public OrganizationConfig getOrganizationConfig() {
+        return organizationConfig;
+    }
+
+    public void setOrganizationConfig(OrganizationConfig organizationConfig) {
+        this.organizationConfig = organizationConfig;
+    }
+
+
     @JsonSerialize( include = Inclusion.NON_NULL )
     public Map<String, List<String>> getParams() {
         return params;

http://git-wip-us.apache.org/repos/asf/usergrid/blob/94008adf/stack/rest/src/main/java/org/apache/usergrid/rest/management/organizations/OrganizationResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/main/java/org/apache/usergrid/rest/management/organizations/OrganizationResource.java
b/stack/rest/src/main/java/org/apache/usergrid/rest/management/organizations/OrganizationResource.java
index afd4d26..f45e1b2 100644
--- a/stack/rest/src/main/java/org/apache/usergrid/rest/management/organizations/OrganizationResource.java
+++ b/stack/rest/src/main/java/org/apache/usergrid/rest/management/organizations/OrganizationResource.java
@@ -24,6 +24,7 @@ import org.apache.commons.lang.NullArgumentException;
 
 import org.apache.usergrid.corepersistence.util.CpNamingUtils;
 import org.apache.usergrid.management.ActivationState;
+import org.apache.usergrid.management.OrganizationConfig;
 import org.apache.usergrid.management.OrganizationInfo;
 import org.apache.usergrid.management.export.ExportService;
 import org.apache.usergrid.management.importer.ImportService;
@@ -39,6 +40,7 @@ import org.apache.usergrid.rest.exceptions.RedirectionException;
 import org.apache.usergrid.rest.management.organizations.applications.ApplicationsResource;
 import org.apache.usergrid.rest.management.organizations.users.UsersResource;
 import org.apache.usergrid.rest.security.annotations.RequireOrganizationAccess;
+import org.apache.usergrid.rest.security.annotations.RequireSystemAccess;
 import org.apache.usergrid.rest.utils.JSONPUtils;
 import org.apache.usergrid.security.oauth.ClientCredentialsInfo;
 import org.apache.usergrid.security.tokens.exceptions.TokenException;
@@ -361,4 +363,56 @@ public class OrganizationResource extends AbstractContextResource {
     }
 
 
+    @RequireOrganizationAccess
+    @GET
+    @Path("config")
+    public JSONWithPadding getConfig( @Context UriInfo ui,
+                                      @QueryParam("callback") @DefaultValue("callback") String
callback )
+            throws Exception {
+
+        logger.info( "Get configuration for organization: " + organization.getUuid() );
+
+        // TODO: check for super user, @RequireSystemAccess didn't work
+
+        ApiResponse response = createApiResponse();
+        response.setAction( "get organization configuration" );
+
+        // TODO: check for super user
+
+        OrganizationConfig orgConfig =
+                management.getOrganizationConfigByUuid( organization.getUuid() );
+
+        response.setProperty( "configuration", management.getOrganizationConfigData( orgConfig
) );
+        // response.setOrganizationConfig( orgConfig );
+
+        return new JSONWithPadding( response, callback );
+    }
+
+
+    @RequireOrganizationAccess
+    @Consumes(MediaType.APPLICATION_JSON)
+    @PUT
+    @Path("config")
+    public JSONWithPadding putConfig( @Context UriInfo ui, Map<String, Object> json,
+                                       @QueryParam("callback") @DefaultValue("callback")
String callback )
+            throws Exception {
+
+        logger.debug("Put configuration for organization: " + organization.getUuid());
+
+        ApiResponse response = createApiResponse();
+        response.setAction("put organization configuration");
+
+        // TODO: check for super user
+
+        // response.setParams(ui.getQueryParameters());
+
+        OrganizationConfig orgConfig =
+                management.getOrganizationConfigByUuid( organization.getUuid() );
+        orgConfig.addProperties(json);
+        management.updateOrganizationConfig(orgConfig);
+        response.setProperty( "configuration", management.getOrganizationConfigData( orgConfig
) );
+
+        return new JSONWithPadding( response, callback );
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/usergrid/blob/94008adf/stack/rest/src/main/java/org/apache/usergrid/rest/management/organizations/OrganizationsResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/main/java/org/apache/usergrid/rest/management/organizations/OrganizationsResource.java
b/stack/rest/src/main/java/org/apache/usergrid/rest/management/organizations/OrganizationsResource.java
index 13489a3..4eac80d 100644
--- a/stack/rest/src/main/java/org/apache/usergrid/rest/management/organizations/OrganizationsResource.java
+++ b/stack/rest/src/main/java/org/apache/usergrid/rest/management/organizations/OrganizationsResource.java
@@ -64,6 +64,7 @@ public class OrganizationsResource extends AbstractContextResource {
     private static final Logger logger = LoggerFactory.getLogger( OrganizationsResource.class
);
 
     public static final String ORGANIZATION_PROPERTIES = "properties";
+    public static final String ORGANIZATION_CONFIGURATION = "configuration";
 
     @Autowired
     private ApplicationCreator applicationCreator;

http://git-wip-us.apache.org/repos/asf/usergrid/blob/94008adf/stack/services/src/main/java/org/apache/usergrid/management/ManagementService.java
----------------------------------------------------------------------
diff --git a/stack/services/src/main/java/org/apache/usergrid/management/ManagementService.java
b/stack/services/src/main/java/org/apache/usergrid/management/ManagementService.java
index 25a693b..4c5305b 100644
--- a/stack/services/src/main/java/org/apache/usergrid/management/ManagementService.java
+++ b/stack/services/src/main/java/org/apache/usergrid/management/ManagementService.java
@@ -341,6 +341,16 @@ public interface ManagementService {
 
     Map<String,Long> getEachCollectionSize(final UUID applicationId);
 
+    public OrganizationConfig getOrganizationConfigByName( String organizationName ) throws
Exception;
+
+    public OrganizationConfig getOrganizationConfigByUuid( UUID id ) throws Exception;
+
+    public Map<String, Object> getOrganizationConfigData( OrganizationConfig organizationConfig
) throws Exception;
+
+    public OrganizationConfig getOrganizationConfigForApplication( UUID applicationId ) throws
Exception;
+
+    public void updateOrganizationConfig( OrganizationConfig organizationConfig ) throws
Exception;
+    
     /**
      * will delete all entities
      * @param applicationId

http://git-wip-us.apache.org/repos/asf/usergrid/blob/94008adf/stack/services/src/main/java/org/apache/usergrid/management/OrganizationConfig.java
----------------------------------------------------------------------
diff --git a/stack/services/src/main/java/org/apache/usergrid/management/OrganizationConfig.java
b/stack/services/src/main/java/org/apache/usergrid/management/OrganizationConfig.java
new file mode 100644
index 0000000..2ecd7a6
--- /dev/null
+++ b/stack/services/src/main/java/org/apache/usergrid/management/OrganizationConfig.java
@@ -0,0 +1,178 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.usergrid.management;
+
+
+import java.util.*;
+
+import static org.apache.usergrid.persistence.Schema.PROPERTY_PATH;
+import static org.apache.usergrid.persistence.Schema.PROPERTY_UUID;
+
+
+public class OrganizationConfig {
+
+    public static final String DEFAULT_CONNECTION_PARAM_PROPERTY = "defaultConnectionParam";
+    private static final String DEFAULT_CONNECTION_PARAM_DEFAULT_VALUE = "all";
+
+    private static final String [] propertyNames = {
+            DEFAULT_CONNECTION_PARAM_PROPERTY
+    };
+
+    private static final String [] defaultValues = {
+            DEFAULT_CONNECTION_PARAM_DEFAULT_VALUE
+    };
+
+    private UUID id;
+    private String name;
+    private Map<String, Object> properties;
+
+
+    public OrganizationConfig() {
+    }
+
+
+    public OrganizationConfig(UUID id, String name) {
+        this.id = id;
+        this.name = name;
+    }
+
+
+    public OrganizationConfig(Map<String, Object> properties) {
+        id = ( UUID ) properties.get( PROPERTY_UUID );
+        name = ( String ) properties.get( PROPERTY_PATH );
+    }
+
+
+    public OrganizationConfig(UUID id, String name, Map<String, Object> properties)
{
+        this( id, name );
+        this.properties = properties;
+
+        // add default values to properties map
+        addDefaultsToProperties();
+    }
+
+    private void addDefaultsToProperties()  {
+        for (int i=0; i < propertyNames.length; i++) {
+            if (!properties.containsKey(propertyNames[i])) {
+                properties.put(propertyNames[i], defaultValues[i]);
+            }
+        }
+    }
+
+
+    public UUID getUuid() {
+        return id;
+    }
+
+
+    public void setUuid( UUID id ) {
+        this.id = id;
+    }
+
+
+    public String getName() {
+        return name;
+    }
+
+
+    public void setName( String name ) {
+        this.name = name;
+    }
+
+
+    public String getDefaultConnectionParam() {
+        String defaultParam = DEFAULT_CONNECTION_PARAM_DEFAULT_VALUE;
+        if ( properties != null ) {
+            Object paramValue = properties.get( DEFAULT_CONNECTION_PARAM_PROPERTY );
+            if ( paramValue instanceof String ) {
+                defaultParam = ( String ) paramValue;
+            }
+        }
+        return defaultParam;
+    }
+
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ( ( id == null ) ? 0 : id.hashCode() );
+        result = prime * result + ( ( name == null ) ? 0 : name.hashCode() );
+        return result;
+    }
+
+
+    @Override
+    public boolean equals( Object obj ) {
+        if ( this == obj ) {
+            return true;
+        }
+        if ( obj == null ) {
+            return false;
+        }
+        if ( getClass() != obj.getClass() ) {
+            return false;
+        }
+        OrganizationConfig other = (OrganizationConfig) obj;
+        if ( id == null ) {
+            if ( other.id != null ) {
+                return false;
+            }
+        }
+        else if ( !id.equals( other.id ) ) {
+            return false;
+        }
+        if ( name == null ) {
+            if ( other.name != null ) {
+                return false;
+            }
+        }
+        else if ( !name.equals( other.name ) ) {
+            return false;
+        }
+        return true;
+    }
+
+
+    public Map<String, Object> getProperties() {
+        return properties;
+    }
+
+
+    public void setProperties( Map<String, Object> properties ) {
+        this.properties = properties;
+
+        // add default values to properties map
+        addDefaultsToProperties();
+    }
+
+    public void addProperties( Map<String, Object> properties ) {
+        this.properties.putAll(properties);
+        /*for (Map.Entry<String,Object> entry : properties.entrySet()) {
+            if (entry.getValue() != null) {
+                // add to properties
+                this.properties.put(entry.getKey(), entry.getValue());
+            } else {
+                // remove from properties
+                this.properties.remove(entry.getKey());
+            }
+        }*/
+
+        // add default values to properties map
+        addDefaultsToProperties();
+    }
+}

http://git-wip-us.apache.org/repos/asf/usergrid/blob/94008adf/stack/services/src/main/java/org/apache/usergrid/management/cassandra/ManagementServiceImpl.java
----------------------------------------------------------------------
diff --git a/stack/services/src/main/java/org/apache/usergrid/management/cassandra/ManagementServiceImpl.java
b/stack/services/src/main/java/org/apache/usergrid/management/cassandra/ManagementServiceImpl.java
index 1ef5659..a5ef958 100644
--- a/stack/services/src/main/java/org/apache/usergrid/management/cassandra/ManagementServiceImpl.java
+++ b/stack/services/src/main/java/org/apache/usergrid/management/cassandra/ManagementServiceImpl.java
@@ -132,6 +132,7 @@ public class ManagementServiceImpl implements ManagementService {
     public static final String OAUTH_SECRET_SALT = "super secret oauth value";
 
     private static final String ORGANIZATION_PROPERTIES_DICTIONARY = "orgProperties";
+    private static final String ORGANIZATION_CONFIG_DICTIONARY = "orgConfig";
     public static final String REGISTRATION_REQUIRES_ADMIN_APPROVAL = "registration_requires_admin_approval";
     public static final String REGISTRATION_REQUIRES_EMAIL_CONFIRMATION = "registration_requires_email_confirmation";
     public static final String NOTIFY_ADMIN_OF_NEW_USERS = "notify_admin_of_new_users";
@@ -3093,4 +3094,87 @@ public class ManagementServiceImpl implements ManagementService {
             return Boolean.parseBoolean(obj);
     }
 
+
+    @Override
+    public OrganizationConfig getOrganizationConfigByName( String organizationName ) throws
Exception {
+
+        if ( organizationName == null ) {
+            return null;
+        }
+
+        EntityManager em = emf.getEntityManager(smf.getManagementAppId());
+        EntityRef ref = em.getAlias( Group.ENTITY_TYPE, organizationName );
+        if ( ref == null ) {
+            return null;
+        }
+        return getOrganizationConfigByUuid( ref.getUuid() );
+    }
+
+
+    @Override
+    public OrganizationConfig getOrganizationConfigByUuid( UUID id ) throws Exception {
+
+        EntityManager em = emf.getEntityManager( smf.getManagementAppId() );
+        Entity entity = em.get( new SimpleEntityRef( Group.ENTITY_TYPE, id ) );
+        if ( entity == null ) {
+            return null;
+        }
+        Map properties = em.getDictionaryAsMap( entity, ORGANIZATION_CONFIG_DICTIONARY );
+        OrganizationConfig orgConfig = new OrganizationConfig( entity.getProperties() );
+        orgConfig.setProperties( properties );
+        return orgConfig;
+    }
+
+
+    @Override
+    public Map<String, Object> getOrganizationConfigData( OrganizationConfig organizationConfig
) throws Exception {
+
+        Map<String, Object> jsonOrganizationConfig = new HashMap<>();
+        jsonOrganizationConfig.putAll( JsonUtils.toJsonMap( organizationConfig.getProperties()
) );
+
+        return jsonOrganizationConfig;
+    }
+
+
+    @Override
+    public OrganizationConfig getOrganizationConfigForApplication( UUID applicationInfoId
) throws Exception {
+
+        if ( applicationInfoId == null ) {
+            return null;
+        }
+
+        final EntityManager em = emf.getEntityManager( smf.getManagementAppId() );
+
+        Results r = em.getSourceEntities(
+                new SimpleEntityRef(CpNamingUtils.APPLICATION_INFO, applicationInfoId),
+                ORG_APP_RELATIONSHIP, Group.ENTITY_TYPE, Level.ALL_PROPERTIES);
+
+        Entity entity = r.getEntity();
+
+        if ( entity != null ) {
+            Map properties = em.getDictionaryAsMap(entity, ORGANIZATION_CONFIG_DICTIONARY);
+            return new OrganizationConfig(entity.getUuid(), entity.getName(), properties);
+        }
+
+        return null;
+    }
+
+
+    @Override
+    public void updateOrganizationConfig( OrganizationConfig organizationConfig ) throws
Exception {
+        Map<String, Object> properties = organizationConfig.getProperties();
+        if ( properties != null ) {
+            EntityRef organizationEntity = new SimpleEntityRef( Group.ENTITY_TYPE, organizationConfig.getUuid()
);
+            EntityManager em = emf.getEntityManager( smf.getManagementAppId() );
+            for ( Map.Entry<String, Object> entry : properties.entrySet() ) {
+                if ( "".equals( entry.getValue() ) ) {
+                    em.removeFromDictionary( organizationEntity, ORGANIZATION_CONFIG_DICTIONARY,
entry.getKey() );
+                } else {
+                    em.addToDictionary( organizationEntity, ORGANIZATION_CONFIG_DICTIONARY,
entry.getKey(),
+                            entry.getValue() );
+                }
+            }
+        }
+    }
+
 }


Mime
View raw message