cancel
Showing results for 
Search instead for 
Did you mean: 

Creating tenants with REST API?

avoliva
Champ in-the-making
Champ in-the-making
Is it possible to create tenants with the REST API on Community Edition?
1 REPLY 1

janv
Employee
Employee
We do not currently provide a REST API for Tenant Admin (out-of-the-box) however it would be very straightforward for you to wrap the existing TenantAdminService with one or more Java-backed web scripts (to create tenant etc).

Here's some example code (untested) to get you going:

tenant.post.desc.xml
<webscript>   <shortname>Create Tenant</shortname>   <description><![CDATA[      Create a tenant.            <br />You must have "administrator" privileges to create a tenant.            <dl>      <dt>tenantDomain</dt> <dd> mandatory </dd>      <dt>tenantAdminPassword</dt> <dd> mandatory </dd>      <dt>tenantContentStoreRoot</dt> <dd> optional </dd>      </dl>   ]]>   </description>   <url>/api/tenants</url>   <format default="json">argument</format>   <authentication>admin</authentication>   <transaction>required</transaction></webscript>‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

tenant.post.json.ftl
      {         "tenantDomain": "${tenant.tenantDomain}",         "enabled": "${tenant.enabled?string}",         "contentRoot": "${tenant.rootContentStoreDir!""}"      }‍‍‍‍‍‍‍

TenantPost.java
package org.changemetoyourorg;import java.io.IOException;import java.util.HashMap;import java.util.Map;import org.alfresco.repo.tenant.TenantAdminService;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.json.JSONException;import org.json.JSONObject;import org.json.JSONTokener;import org.springframework.extensions.webscripts.Cache;import org.springframework.extensions.webscripts.DeclarativeWebScript;import org.springframework.extensions.webscripts.Status;import org.springframework.extensions.webscripts.WebScriptException;import org.springframework.extensions.webscripts.WebScriptRequest;/** * REST API - create tenant *  * @author janv * @since Experimental - not tested or support 🙂 */public class TenantPost extends DeclarativeWebScript{    protected static final Log logger = LogFactory.getLog(TenantPost.class);        protected static final String TENANT_DOMAIN             = "tenantDomain";    protected static final String TENANT_ADMIN_PASSWORD     = "tenantAdminPassword";    protected static final String TENANT_CONTENT_STORE_ROOT = "tenantContentStoreRoot";        protected TenantAdminService tenantAdminService;        public void setTenantAdminService(TenantAdminService tenantAdminService)    {        this.tenantAdminService = tenantAdminService;    }        @Override    protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)    {        String tenantDomain = null;        String tenantAdminPassword = null;        String contentStoreRoot = null;                try        {            JSONObject json = new JSONObject(new JSONTokener(req.getContent().getContent()));                        if (! json.has(TENANT_DOMAIN))            {                throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not find required 'tenantDomain' parameter");            }            tenantDomain = json.getString(TENANT_DOMAIN);                        if (! json.has(TENANT_ADMIN_PASSWORD))            {                throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not find required 'tenantAdminPassword' parameter");            }            tenantAdminPassword = json.getString(TENANT_ADMIN_PASSWORD);                        if (json.has(TENANT_CONTENT_STORE_ROOT))            {                contentStoreRoot = json.getString(TENANT_CONTENT_STORE_ROOT);            }        }        catch (IOException iox)        {            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not read content from req.", iox);        }        catch (JSONException je)        {            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not parse JSON from req.", je);        }                tenantAdminService.createTenant(tenantDomain, tenantAdminPassword.toCharArray(), contentStoreRoot);                Map<String, Object> model = new HashMap<String, Object>(0);        return model;    }}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

You can also look at the "TenantInterpreter.java" code to see how it is done for the existing Alfresco Explorer Tenant Admin console page: http://localhost:8080/alfresco/faces/jsp/admin/tenantadmin-console.jsp

Regards,
Jan