Add REST endpoints to add/delete SSH keys for service user

Change-Id: Id4ed35b0f8694a44b6dfcb6b05f56e3443bae3d9
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/serviceuser/AddSshKey.java b/src/main/java/com/googlesource/gerrit/plugins/serviceuser/AddSshKey.java
new file mode 100644
index 0000000..6549578
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/serviceuser/AddSshKey.java
@@ -0,0 +1,42 @@
+// Copyright (C) 2014 The Android Open Source Project
+//
+// Licensed 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 com.googlesource.gerrit.plugins.serviceuser;
+
+import com.google.gerrit.extensions.restapi.AuthException;
+import com.google.gerrit.extensions.restapi.BadRequestException;
+import com.google.gerrit.extensions.restapi.Response;
+import com.google.gerrit.extensions.restapi.RestModifyView;
+import com.google.gerrit.server.account.AddSshKey.Input;
+import com.google.gerrit.server.account.GetSshKeys.SshKeyInfo;
+import com.google.gwtorm.server.OrmException;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+
+import java.io.IOException;
+
+public class AddSshKey implements RestModifyView<ServiceUserResource, Input> {
+  private final Provider<com.google.gerrit.server.account.AddSshKey> addSshKey;
+
+  @Inject
+  AddSshKey(Provider<com.google.gerrit.server.account.AddSshKey> addSshKey) {
+    this.addSshKey = addSshKey;
+  }
+
+  @Override
+  public Response<SshKeyInfo> apply(ServiceUserResource rsrc, Input input)
+      throws AuthException, BadRequestException, OrmException, IOException {
+    return addSshKey.get().apply(rsrc.getUser(), input);
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/serviceuser/DeleteSshKey.java b/src/main/java/com/googlesource/gerrit/plugins/serviceuser/DeleteSshKey.java
new file mode 100644
index 0000000..84feaa4
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/serviceuser/DeleteSshKey.java
@@ -0,0 +1,40 @@
+// Copyright (C) 2014 The Android Open Source Project
+//
+// Licensed 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 com.googlesource.gerrit.plugins.serviceuser;
+
+import com.google.gerrit.extensions.restapi.Response;
+import com.google.gerrit.extensions.restapi.RestModifyView;
+import com.google.gerrit.server.account.AccountResource;
+import com.google.gerrit.server.account.DeleteSshKey.Input;
+import com.google.gwtorm.server.OrmException;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+
+public class DeleteSshKey implements
+    RestModifyView<ServiceUserResource.SshKey, Input> {
+  private final Provider<com.google.gerrit.server.account.DeleteSshKey> deleteSshKey;
+
+  @Inject
+  DeleteSshKey(Provider<com.google.gerrit.server.account.DeleteSshKey> deleteSshKey) {
+    this.deleteSshKey = deleteSshKey;
+  }
+
+  @Override
+  public Response<?> apply(ServiceUserResource.SshKey rsrc, Input input)
+      throws OrmException {
+    return deleteSshKey.get().apply(
+        new AccountResource.SshKey(rsrc.getUser(), rsrc.getSshKey()), input);
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/serviceuser/Module.java b/src/main/java/com/googlesource/gerrit/plugins/serviceuser/Module.java
index 14f0152..39fd762 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/serviceuser/Module.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/serviceuser/Module.java
@@ -48,6 +48,8 @@
         put(CONFIG_KIND, "config").to(PutConfig.class);
         child(SERVICE_USER_KIND, "sshkeys").to(SshKeys.class);
         get(SSH_KEY_KIND).to(GetSshKey.class);
+        post(SERVICE_USER_KIND, "sshkeys").to(AddSshKey.class);
+        delete(SSH_KEY_KIND).to(DeleteSshKey.class);
       }
     });
   }
diff --git a/src/main/resources/Documentation/rest-api-config.md b/src/main/resources/Documentation/rest-api-config.md
index 60c92be..ac55b5a 100644
--- a/src/main/resources/Documentation/rest-api-config.md
+++ b/src/main/resources/Documentation/rest-api-config.md
@@ -198,6 +198,60 @@
   }
 ```
 
+### <a id="add-ssh-key"> Add SSH key
+POST /config/server/@PLUGIN@~serviceusers/\{username\}/sshkeys_
+
+Adds an SSH key for a service user.
+
+#### Request
+
+```
+  POST /config/server/@PLUGIN@~serviceusers/JenkinsVoter/sshkeys HTTP/1.0
+  Content-Type: plain/text
+
+  AAAAB3NzaC1yc2EAAAABIwAAAQEA0T...YImydZAw\u003d\u003d
+```
+
+As response an [SshKeyInfo](../../../Documentation/rest-api-accounts.html#ssh-key-info)
+entity is returned that describes the new SSH key.
+
+#### Response
+
+```
+  HTTP/1.1 200 OK
+  Content-Disposition: attachment
+  Content-Type: application/json;charset=UTF-8
+
+  )]}'
+  [
+    {
+      "seq": 2,
+      "ssh_public_key": "ssh-rsa AAAAB1NzaA2...",
+      "encoded_key": "AAAAB1NzaA2...",
+      "algorithm": "ssh-rsa",
+      "comment": "jenkins.voter@gerrit.com",
+      "valid": true
+    }
+  ]
+```
+
+### <a id="delete-ssh-key"> Delete SSH key
+DELETE /config/server/@PLUGIN@~serviceusers/\{username\}/sshkeys/[\{ssh-key-id\}](../../../Documentation/rest-api-accounts.html#ssh-key-id)_
+
+Deletes an SSH key of a service user.
+
+#### Request
+
+```
+  DELETE /config/server/@PLUGIN@~serviceusers/JenkinsVoter/sshkeys/2 HTTP/1.0
+```
+
+#### Response
+
+```
+  HTTP/1.1 204 No Content
+```
+
 ### <a id="get-config"> Get Config
 _GET /config/server/@PLUGIN@~config_