Commit 7d0a4a89 authored by Greg Messner's avatar Greg Messner
Browse files

Added SSH key support to UserApi (#37).

parent ba4cd1f5
...@@ -7,6 +7,7 @@ import javax.ws.rs.core.GenericType; ...@@ -7,6 +7,7 @@ import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import org.gitlab4j.api.GitLabApi.ApiVersion; import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.SshKey;
import org.gitlab4j.api.models.User; import org.gitlab4j.api.models.User;
/** /**
...@@ -214,10 +215,134 @@ public class UserApi extends AbstractApi { ...@@ -214,10 +215,134 @@ public class UserApi extends AbstractApi {
deleteUser(user.getId()); deleteUser(user.getId());
} }
/**
* Get a list of currently authenticated user's SSH keys.
*
* GET /user/keys
*
* @return a list of currently authenticated user's SSH keys
* @throws GitLabApiException if any exception occurs
*/
public List<SshKey> getSshKeys() throws GitLabApiException {
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "user", "keys");
return (response.readEntity(new GenericType<List<SshKey>>() {}));
}
/**
* Get a list of a specified user's SSH keys. Available only for admin users.
*
* GET /users/:id/keys
*
* @param userId the ID of the user to get SSH keys for
* @return a list of a specified user's SSH keys
* @throws GitLabApiException if any exception occurs
*/
public List<SshKey> getSshKeys(Integer userId) throws GitLabApiException {
if (userId == null) {
throw new RuntimeException("userId cannot be null");
}
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "users", userId, "keys");
return (response.readEntity(new GenericType<List<SshKey>>() {}));
}
/**
* Get a single SSH Key.
*
* GET /user/keys/:key_id
*
* @param keyId the ID of the SSH key.
* @return an SshKey instance holding the info on the SSH key specified by keyId
* @throws GitLabApiException if any exception occurs
*/
public SshKey getSshKey(Integer keyId) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "user", "keys", keyId);
return (response.readEntity(SshKey.class));
}
/**
* Creates a new key owned by the currently authenticated user.
*
* POST /user/keys
*
* @param title the new SSH Key's title
* @param key the new SSH key
* @return an SshKey instance with info on the added SSH key
* @throws GitLabApiException if any exception occurs
*/
public SshKey addSshKey(String title, String key) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm().withParam("title", title).withParam("key", key);
Response response = post(Response.Status.CREATED, formData, "user", "keys");
return (response.readEntity(SshKey.class));
}
/**
* Create new key owned by specified user. Available only for admin users.
*
* POST /users/:id/keys
*
* @param userId the ID of the user to add the SSH key for
* @param title the new SSH Key's title
* @param key the new SSH key
* @return an SshKey instance with info on the added SSH key
* @throws GitLabApiException if any exception occurs
*/
public SshKey addSshKey(Integer userId, String title, String key) throws GitLabApiException {
if (userId == null) {
throw new RuntimeException("userId cannot be null");
}
GitLabApiForm formData = new GitLabApiForm().withParam("title", title).withParam("key", key);
Response response = post(Response.Status.CREATED, formData, "users", userId, "keys");
return (response.readEntity(SshKey.class));
}
/**
* Deletes key owned by currently authenticated user. This is an idempotent function and calling it
* on a key that is already deleted or not available results in success.
*
* DELETE /user/keys/:key_id
*
* @param keyId the key ID to delete
* @throws GitLabApiException if any exception occurs
*/
public void deleteSshKey(Integer keyId) throws GitLabApiException {
if (keyId == null) {
throw new RuntimeException("keyId cannot be null");
}
delete(Response.Status.OK, null, "user", "keys", keyId);
}
/**
* Deletes key owned by a specified user. Available only for admin users.
*
* DELETE /users/:id/keys/:key_id
*
* @param userId the user ID of the user to delete the key for
* @param keyId the key ID to delete
* @throws GitLabApiException if any exception occurs
*/
public void deleteSshKey(Integer userId, Integer keyId) throws GitLabApiException {
if (userId == null) {
throw new RuntimeException("userId cannot be null");
}
if (keyId == null) {
throw new RuntimeException("keyId cannot be null");
}
delete(Response.Status.OK, null, "users", userId, "keys", keyId);
}
/** /**
* Populate the REST form with data from the User instance. * Populate the REST form with data from the User instance.
* *
* @param user the User iunstance to populate the Form instance with * @param user the User instance to populate the Form instance with
* @param projectsLimit the maximum number of projects the user is allowed (optional) * @param projectsLimit the maximum number of projects the user is allowed (optional)
* @param password the password, required when creating a new user * @param password the password, required when creating a new user
* @param create whether the form is being populated to create a new user * @param create whether the form is being populated to create a new user
......
package org.gitlab4j.api.models;
import java.util.Date;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
@XmlAccessorType(XmlAccessType.FIELD)
public class SshKey {
private Integer id;
private String title;
private String key;
private Date createdAt;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
}
...@@ -24,6 +24,7 @@ import org.gitlab4j.api.models.Project; ...@@ -24,6 +24,7 @@ import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.ProjectHook; import org.gitlab4j.api.models.ProjectHook;
import org.gitlab4j.api.models.ProjectSnippet; import org.gitlab4j.api.models.ProjectSnippet;
import org.gitlab4j.api.models.Session; import org.gitlab4j.api.models.Session;
import org.gitlab4j.api.models.SshKey;
import org.gitlab4j.api.models.SystemHook; import org.gitlab4j.api.models.SystemHook;
import org.gitlab4j.api.models.Tag; import org.gitlab4j.api.models.Tag;
import org.gitlab4j.api.models.TreeItem; import org.gitlab4j.api.models.TreeItem;
...@@ -277,6 +278,17 @@ public class TestGitLabApiBeans { ...@@ -277,6 +278,17 @@ public class TestGitLabApiBeans {
} }
} }
@Test
public void testSshKey() {
try {
SshKey sshKey = makeFakeApiCall(SshKey.class, "sshkey");
assertTrue(compareJson(sshKey, "sshkey"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test @Test
public void testTree() { public void testTree() {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment