Skip to content

Commit 09e0719

Browse files
authored
Add access key management
1 parent 923594a commit 09e0719

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

cloudinary-core/src/main/java/com/cloudinary/provisioning/Account.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ public class Account {
1414
private static final String CLOUDINARY_ACCOUNT_URL = "CLOUDINARY_ACCOUNT_URL";
1515
public static final String PROVISIONING = "provisioning";
1616
public static final String ACCOUNTS = "accounts";
17+
public static final String SUB_ACCOUNTS = "sub_accounts";
1718
public static final String USERS = "users";
1819
public static final String USER_GROUPS = "user_groups";
20+
public static final String ACCESS_KEYS = "access_keys";
1921

2022
private final AccountConfiguration configuration;
2123
private final String accountId;
@@ -619,6 +621,60 @@ public ApiResponse userGroupUsers(String groupId, Map<String, Object> options) t
619621
return callAccountApi(Api.HttpMethod.GET, uri, Collections.<String, Object>emptyMap(), options);
620622
}
621623

624+
/**
625+
* Lists the access keys belonging to this sub account id.
626+
* @param subAccountId The id of the user group.
627+
* @param options Generic advanced options map, see online documentation.
628+
* @return The list of access keys in that sub account id.
629+
* @throws Exception If the request fails.
630+
*/
631+
public ApiResponse getAccessKeys(String subAccountId, Map<String, Object> options) throws Exception {
632+
List<String> uri = Arrays.asList(PROVISIONING, ACCOUNTS, accountId, SUB_ACCOUNTS, subAccountId);
633+
return callAccountApi(Api.HttpMethod.GET, uri, Collections.<String, Object>emptyMap(), options);
634+
}
635+
636+
/**
637+
* Creates a new access key for this sub account id.
638+
* @param subAccountId The id of the user group.
639+
* @param name The name for the access key.
640+
* @param enabled Access key's status (enabled or disabled).
641+
* @param options Generic advanced options map, see online documentation.
642+
* @return The created access key.
643+
* @throws Exception If the request fails.
644+
*/
645+
public ApiResponse createAccessKey(String subAccountId, String name, Boolean enabled, Map<String, Object> options) throws Exception {
646+
List<String> uri = Arrays.asList(PROVISIONING, ACCOUNTS, accountId, SUB_ACCOUNTS, subAccountId, ACCESS_KEYS);
647+
return callAccountApi(Api.HttpMethod.POST, uri, ObjectUtils.asMap("name", name, "enabled", enabled), options);
648+
}
649+
650+
/**
651+
* Updates an existing access key for this sub account id.
652+
* @param subAccountId The id of the user group.
653+
* @param accessKey The key of the access key.
654+
* @param name The name for the access key.
655+
* @param enabled Access key's status (enabled or disabled).
656+
* @param options Generic advanced options map, see online documentation.
657+
* @return The updated access key.
658+
* @throws Exception If the request fails.
659+
*/
660+
public ApiResponse updateAccessKey(String subAccountId, String accessKey, String name, Boolean enabled, Map<String, Object> options) throws Exception {
661+
List<String> uri = Arrays.asList(PROVISIONING, ACCOUNTS, accountId, SUB_ACCOUNTS, subAccountId, ACCESS_KEYS, accessKey);
662+
return callAccountApi(Api.HttpMethod.PUT, uri, ObjectUtils.asMap("name", name, "enabled", enabled), options);
663+
}
664+
665+
/**
666+
* Deletes an existing access key for this sub account id.
667+
* @param subAccountId The id of the user group.
668+
* @param accessKey The key of the access key.
669+
* @param options Generic advanced options map, see online documentation.
670+
* @return "message": "ok".
671+
* @throws Exception If the request fails.
672+
*/
673+
public ApiResponse deleteAccessKey(String subAccountId, String accessKey, Map<String, Object> options) throws Exception {
674+
List<String> uri = Arrays.asList(PROVISIONING, ACCOUNTS, accountId, SUB_ACCOUNTS, subAccountId, ACCESS_KEYS, accessKey);
675+
return callAccountApi(Api.HttpMethod.DELETE, uri, Collections.<String, Object>emptyMap(), options);
676+
}
677+
622678
/**
623679
* Private helper method for users api calls
624680
* @param method Http method

cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractAccountApiTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,47 @@ public void testListUsersInGroup() throws Exception {
423423
deleteUser(user2Id);
424424
}
425425

426+
@Test
427+
public void testGetAccessKeys() throws Exception {
428+
ApiResponse createResult = createSubAccount();
429+
ApiResponse result = account.getAccessKeys((String) createResult.get("id"), ObjectUtils.emptyMap());
430+
assertNotNull(result);
431+
}
432+
433+
@Test
434+
public void testCreateNewAccessKey() throws Exception {
435+
ApiResponse createResult = createSubAccount();
436+
String name = randomLetters();
437+
ApiResponse result = account.createAccessKey((String)createResult.get("id"), name, true, ObjectUtils.emptyMap());
438+
assertNotNull(result);
439+
assertTrue((Boolean) result.get("enabled"));
440+
}
441+
442+
@Test
443+
public void testUpdateAccessKey() throws Exception {
444+
ApiResponse createResult = createSubAccount();
445+
String name = randomLetters();
446+
ApiResponse result = account.createAccessKey((String)createResult.get("id"), name, false, ObjectUtils.emptyMap());
447+
assertNotNull(result);
448+
449+
String updatedName = randomLetters();
450+
result = account.updateAccessKey((String)createResult.get("id"), (String) result.get("api_key"), updatedName, true, ObjectUtils.emptyMap());
451+
assertNotNull(result);
452+
assertEquals(updatedName, result.get("name"));
453+
assertTrue((Boolean) result.get("enabled"));
454+
}
455+
456+
@Test
457+
public void testDeleteAccessKey() throws Exception {
458+
ApiResponse createResult = createSubAccount();
459+
String name = randomLetters();
460+
ApiResponse result = account.createAccessKey((String)createResult.get("id"), name, false, ObjectUtils.emptyMap());
461+
assertNotNull(result);
462+
463+
result = account.deleteAccessKey((String)createResult.get("id"), (String) result.get("api_key"), ObjectUtils.emptyMap());
464+
assertNotNull(result);
465+
}
466+
426467

427468
// Helpers
428469
private ApiResponse createGroup() throws Exception {

0 commit comments

Comments
 (0)