Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tenant search #67

Merged
merged 7 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
@Command(name = "tenant-update", description = "Update a Descope tenant")
public class TenantUpdate extends TenantBase implements Callable<Integer> {

@Option(names = { "-n", "--name"}, description = "Tenant name")
@Option(names = { "-n", "--name" }, description = "Tenant name")
String name;
@Option(names = { "-d", "--domain"}, description = "Self provisioned domains. Multiple are supported.")
@Option(names = { "-d", "--domain" }, description = "Self provisioned domains. Multiple are supported.")
List<String> selfProvisionedDomains;

@Override
Expand All @@ -21,7 +21,7 @@ public Integer call() {
try {
var client = new DescopeClient();
var tenantService = client.getManagementServices().getTenantService();
tenantService.update(tenantId, name, selfProvisionedDomains);
tenantService.update(tenantId, name, selfProvisionedDomains, nil);
System.out.printf("Tenant %s [%s] updated\n", name, tenantId);
} catch (DescopeException de) {
exitCode = 1;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/descope/literals/Routes.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public static class ManagementEndPoints {
public static final String UPDATE_TENANT_LINK = "/v1/mgmt/tenant/update";
public static final String DELETE_TENANT_LINK = "/v1/mgmt/tenant/delete";
public static final String LOAD_ALL_TENANTS_LINK = "/v1/mgmt/tenant/all";
public static final String TENANT_SEARCH_ALL_LINK = "/v1/mgmt/tenant/search";

// SSO
public static final String SSO_GET_SETTINGS_LINK = "/mgmt/sso/settings";
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/descope/model/tenant/Tenant.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -16,4 +17,5 @@ public class Tenant {
String id;
String name;
List<String> selfProvisioningDomains;
Map<String, Object> customAttributes;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.descope.model.tenant.request;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TenantSearchRequest {
@JsonProperty("tenantIds")
List<String> ids;
@JsonProperty("tenantNames")
List<String> names;
Map<String, Object> customAttributes;
@JsonProperty("tenantSelfProvisioningDomains")
List<String> selfProvisioningDomains;
}
96 changes: 78 additions & 18 deletions src/main/java/com/descope/sdk/mgmt/TenantService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,95 @@

import com.descope.exception.DescopeException;
import com.descope.model.tenant.Tenant;
import com.descope.model.tenant.request.TenantSearchRequest;
import java.util.List;
import java.util.Map;

/** Provides functions for managing tenants in a project. */
public interface TenantService {

/**
* Create a new tenant with the given name. selfProvisioningDomains is an optional list of domains
* Create a new tenant with the given name. selfProvisioningDomains is an
* optional list of domains
* that are associated with this tenant.
*
* @param name - The tenant name must be unique per project.
* @param selfProvisioningDomains - Users authenticating from these domains will be associated
* with this tenant.
* @param name - The tenant name must be unique per project.
* @param selfProvisioningDomains - Users authenticating from these domains will
* be associated
* with this tenant.
* @return The tenant ID generated automatically for the tenant.
* @throws DescopeException in case of errors
*/
String create(String name, List<String> selfProvisioningDomains) throws DescopeException;

/**
* Create a new tenant with the given name and ID. selfProvisioningDomains is an optional list of
* Create a new tenant with the given name. selfProvisioningDomains is an
* optional list of domains that are associated with this tenant.
*
*
* @param name - The tenant name must be unique per project.
* @param selfProvisioningDomains - Users authenticating from these domains will
* be associated with this tenant.
* @param customAttributes - Custom attributes to apply to tenant (needs
* to be pre-configured)
* @return The tenant ID generated automatically for the tenant.
* @throws DescopeException in case of errors
*/
String create(String name, List<String> selfProvisioningDomains, Map<String, Object> customAttributes)
throws DescopeException;

/**
* Create a new tenant with the given name and ID. selfProvisioningDomains is an
* optional list of
* domains that are associated with this tenant.
*
* @param id - The tenant ID must be unique per project.
* @param name - The tenant name must be unique per project.
* @param selfProvisioningDomains - Users authenticating from these domains will be associated
* with this tenant.
* @param id - The tenant ID must be unique per project.
* @param name - The tenant name must be unique per project.
* @param selfProvisioningDomains - Users authenticating from these domains will
* be associated
* with this tenant.
* @throws DescopeException in case of errors
*/
void createWithId(String id, String name, List<String> selfProvisioningDomains)
throws DescopeException;

/**
* Update an existing tenant's name and domains. IMPORTANT: All parameters are required and will
* override whatever value is currently set in the existing tenant. Use carefully.
* Create a new tenant with the given name and ID. selfProvisioningDomains is an
* optional list of
* domains that are associated with this tenant.
*
* @param id - Tenant ID
* @param name - The tenant name must be unique per project.
* @param selfProvisioningDomains - Users authenticating from these domains will be associated
* with this tenant.
* @param id - The tenant ID must be unique per project.
* @param name - The tenant name must be unique per project.
* @param selfProvisioningDomains - Users authenticating from these domains will
* be associated with this tenant.
* @param customAttributes - Custom attributes to apply to tenant (needs
* to be pre-configured)
* @throws DescopeException in case of errors
*/
void update(String id, String name, List<String> selfProvisioningDomains) throws DescopeException;
void createWithId(String id, String name, List<String> selfProvisioningDomains,
Map<String, Object> customAttributes)
throws DescopeException;

/**
* Delete an existing tenant. IMPORTANT: This action is irreversible. Use carefully.
* Update an existing tenant's name and domains. IMPORTANT: All parameters are
* required and will
* override whatever value is currently set in the existing tenant. Use
* carefully.
*
* @param id - Tenant ID
* @param name - The tenant name must be unique per project.
* @param selfProvisioningDomains - Users authenticating from these domains will
* be associated with this tenant.
* @param customAttributes - Custom attributes to apply to tenant (needs
* to be pre-configured)
* @throws DescopeException in case of errors
*/
void update(String id, String name, List<String> selfProvisioningDomains, Map<String, Object> customAttributes)
throws DescopeException;

/**
* Delete an existing tenant. IMPORTANT: This action is irreversible. Use
* carefully.
*
* @param id - Tenant ID
* @throws DescopeException in case of errors
Expand All @@ -55,8 +100,23 @@ void createWithId(String id, String name, List<String> selfProvisioningDomains)
/**
* Load all project tenants.s
*
* @return {@link Tenant Tenant}
* @return {{@link List} of {@link Tenant}
* @throws DescopeException in case of errors
*/
List<Tenant> loadAll() throws DescopeException;

/**
* Search all tenants according to given filters.
*
* @param request The options optional parameter allows to fine-tune the search
* filters and
* results. Using nil will result in a filter-less query with a
* set amount of results.
* @return {{@link List} of {@link Tenant}
* @throws DescopeException If there occurs any exception, a subtype of this
* exception will be
* thrown.
*/
List<Tenant> searchAll(TenantSearchRequest request) throws DescopeException;

}
49 changes: 45 additions & 4 deletions src/main/java/com/descope/sdk/mgmt/impl/TenantServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import static com.descope.literals.Routes.ManagementEndPoints.CREATE_TENANT_LINK;
import static com.descope.literals.Routes.ManagementEndPoints.DELETE_TENANT_LINK;
import static com.descope.literals.Routes.ManagementEndPoints.LOAD_ALL_TENANTS_LINK;
import static com.descope.literals.Routes.ManagementEndPoints.TENANT_SEARCH_ALL_LINK;
import static com.descope.literals.Routes.ManagementEndPoints.UPDATE_TENANT_LINK;

import com.descope.exception.DescopeException;
import com.descope.exception.ServerCommonException;
import com.descope.model.client.Client;
import com.descope.model.mgmt.ManagementParams;
import com.descope.model.tenant.Tenant;
import com.descope.model.tenant.request.TenantSearchRequest;
import com.descope.model.tenant.response.GetAllTenantsResponse;
import com.descope.sdk.mgmt.TenantService;
import java.net.URI;
Expand All @@ -29,7 +31,17 @@ public String create(String name, List<String> selfProvisioningDomains) throws D
if (StringUtils.isBlank(name)) {
throw ServerCommonException.invalidArgument("name");
}
var tenant = new Tenant("", name, selfProvisioningDomains);
var tenant = new Tenant("", name, selfProvisioningDomains, null);
return create(tenant);
}

@Override
public String create(String name, List<String> selfProvisioningDomains, Map<String, Object> customAttributes)
throws DescopeException {
if (StringUtils.isBlank(name)) {
throw ServerCommonException.invalidArgument("name");
}
var tenant = new Tenant("", name, selfProvisioningDomains, customAttributes);
return create(tenant);
}

Expand All @@ -40,7 +52,19 @@ public void createWithId(String id, String name, List<String> selfProvisioningDo
throw ServerCommonException.invalidArgument("id or name");
}

var tenant = new Tenant(id, name, selfProvisioningDomains);
var tenant = new Tenant(id, name, selfProvisioningDomains, null);
create(tenant);
}

@Override
public void createWithId(String id, String name, List<String> selfProvisioningDomains,
Map<String, Object> customAttributes)
throws DescopeException {
if (StringUtils.isAnyBlank(id, name)) {
throw ServerCommonException.invalidArgument("id or name");
}

var tenant = new Tenant(id, name, selfProvisioningDomains, customAttributes);
create(tenant);
}

Expand All @@ -52,13 +76,13 @@ private String create(Tenant tenant) {
}

@Override
public void update(String id, String name, List<String> selfProvisioningDomains)
public void update(String id, String name, List<String> selfProvisioningDomains, Map<String, Object> customAttributes)
throws DescopeException {
if (StringUtils.isAnyBlank(id, name)) {
throw ServerCommonException.invalidArgument("id or name");
}

var tenant = new Tenant(id, name, selfProvisioningDomains);
var tenant = new Tenant(id, name, selfProvisioningDomains, customAttributes);
update(tenant);
}

Expand Down Expand Up @@ -87,6 +111,19 @@ public List<Tenant> loadAll() throws DescopeException {
return response.getTenants();
}

@Override
public List<Tenant> searchAll(TenantSearchRequest request)
throws DescopeException {
if (request == null) {
request = TenantSearchRequest.builder().build();
}

URI composeSearchAllUri = composeSearchAllUri();
var apiProxy = getApiProxy();
GetAllTenantsResponse response = apiProxy.post(composeSearchAllUri, request, GetAllTenantsResponse.class);
return response.getTenants();
}

private URI composeCreateTenantUri() {
return getUri(CREATE_TENANT_LINK);
}
Expand All @@ -102,4 +139,8 @@ private URI composeDeleteTenantUri() {
private URI loadAllTenantsUri() {
return getUri(LOAD_ALL_TENANTS_LINK);
}

private URI composeSearchAllUri() {
return getUri(TENANT_SEARCH_ALL_LINK);
}
}
2 changes: 1 addition & 1 deletion src/test/java/com/descope/sdk/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,4 @@ public static ManagementParams getManagementParams() {
.build();
}

}
}
Loading