Allow to retrieve service user by account-id and not only username

Change-Id: Icf2b8e4b0f2e074f4aed828b96f82657733d8227
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/serviceuser/ServiceUserCollection.java b/src/main/java/com/googlesource/gerrit/plugins/serviceuser/ServiceUserCollection.java
index e8546cd..698525a 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/serviceuser/ServiceUserCollection.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/serviceuser/ServiceUserCollection.java
@@ -27,7 +27,6 @@
 import com.google.gerrit.extensions.restapi.IdString;
 import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
 import com.google.gerrit.extensions.restapi.RestView;
-import com.google.gerrit.extensions.restapi.TopLevelResource;
 import com.google.gerrit.extensions.restapi.UnprocessableEntityException;
 import com.google.gerrit.reviewdb.client.Account;
 import com.google.gerrit.server.CurrentUser;
@@ -71,7 +70,8 @@
   @Override
   public ServiceUserResource parse(ConfigResource parent, IdString id)
       throws ResourceNotFoundException, AuthException, OrmException {
-    if (!storage.get().getSubsections(USER).contains(id.get())) {
+    IdentifiedUser serviceUser = accounts.get().parseId(id.get());
+    if (!storage.get().getSubsections(USER).contains(serviceUser.getUserName())) {
       throw new ResourceNotFoundException(id);
     }
     CurrentUser user = userProvider.get();
@@ -94,8 +94,7 @@
         throw new ResourceNotFoundException(id);
       }
     }
-    return new ServiceUserResource(
-        accounts.get().parse(TopLevelResource.INSTANCE, id).getUser());
+    return new ServiceUserResource(serviceUser);
   }
 
   @Override
diff --git a/src/main/resources/Documentation/rest-api-config.md b/src/main/resources/Documentation/rest-api-config.md
index b9a4d34..6360c22 100644
--- a/src/main/resources/Documentation/rest-api-config.md
+++ b/src/main/resources/Documentation/rest-api-config.md
@@ -54,7 +54,7 @@
 ```
 
 ### <a id="get-service-user"> Get Service User
-_GET /config/server/@PLUGIN@~serviceusers/\{username\}_
+_GET /config/server/@PLUGIN@~serviceusers/[\{account-id\}](../../../Documentation/rest-api-accounts.html#account-id)_
 
 Gets a service user.
 
@@ -139,7 +139,7 @@
 ```
 
 ### <a id="list-ssh-keys"> List SSH keys
-GET /config/server/@PLUGIN@~serviceusers/\{username\}/sshkeys/_
+GET /config/server/@PLUGIN@~serviceusers/[\{account-id\}](../../../Documentation/rest-api-accounts.html#account-id)/sshkeys/_
 
 Lists the SSH keys of a service user.
 
@@ -173,7 +173,7 @@
 ```
 
 ### <a id="get-ssh-key"> Get SSH key
-GET /config/server/@PLUGIN@~serviceusers/\{username\}/sshkeys/[\{ssh-key-id\}](../../../Documentation/rest-api-accounts.html#ssh-key-id)_
+GET /config/server/@PLUGIN@~serviceusers/[\{account-id\}](../../../Documentation/rest-api-accounts.html#account-id)/sshkeys/[\{ssh-key-id\}](../../../Documentation/rest-api-accounts.html#ssh-key-id)_
 
 Gets an SSH key of a service user.
 
@@ -205,7 +205,7 @@
 ```
 
 ### <a id="add-ssh-key"> Add SSH key
-POST /config/server/@PLUGIN@~serviceusers/\{username\}/sshkeys_
+POST /config/server/@PLUGIN@~serviceusers/[\{account-id\}](../../../Documentation/rest-api-accounts.html#account-id)/sshkeys_
 
 Adds an SSH key for a service user.
 
@@ -242,7 +242,7 @@
 ```
 
 ### <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)_
+DELETE /config/server/@PLUGIN@~serviceusers/[\{account-id\}](../../../Documentation/rest-api-accounts.html#account-id)/sshkeys/[\{ssh-key-id\}](../../../Documentation/rest-api-accounts.html#ssh-key-id)_
 
 Deletes an SSH key of a service user.
 
@@ -259,7 +259,7 @@
 ```
 
 ### <a id="get-full-name"> Get Full Name
-GET /config/server/@PLUGIN@~serviceusers/\{username\}/name_
+GET /config/server/@PLUGIN@~serviceusers/[\{account-id\}](../../../Documentation/rest-api-accounts.html#account-id)/name_
 
 Retrieves the full name of a service user.
 
@@ -283,7 +283,7 @@
 If the service user does not have a name an empty string is returned.
 
 ### <a id="set-full-name"> Set Full Name
-PUT /config/server/@PLUGIN@~serviceusers/\{username\}/name_
+PUT /config/server/@PLUGIN@~serviceusers/[\{account-id\}](../../../Documentation/rest-api-accounts.html#account-id)/name_
 
 Sets the full name of a service user.
 
@@ -321,7 +321,7 @@
 request is rejected with "`405 Method Not Allowed`".
 
 ### <a id="delete-full-name"> Delete Full Name
-DELETE /config/server/@PLUGIN@~serviceusers/\{username\}/name_
+DELETE /config/server/@PLUGIN@~serviceusers/[\{account-id\}](../../../Documentation/rest-api-accounts.html#account-id)/name_
 
 Deletes the full name of a service user.
 
@@ -343,7 +343,7 @@
 request is rejected with "`405 Method Not Allowed`".
 
 ### <a id="get-email"> Get Email
-GET /config/server/@PLUGIN@~serviceusers/\{username\}/email_
+GET /config/server/@PLUGIN@~serviceusers/[\{account-id\}](../../../Documentation/rest-api-accounts.html#account-id)/email_
 
 Retrieves the (preferred) email of a service user.
 
@@ -367,7 +367,7 @@
 If the service user does not have an email address an empty string is returned.
 
 ### <a id="set-email"> Set Email
-PUT /config/server/@PLUGIN@~serviceusers/\{username\}/email_
+PUT /config/server/@PLUGIN@~serviceusers/[\{account-id\}](../../../Documentation/rest-api-accounts.html#account-id)/email_
 
 Sets the (preferred) email of a service user.
 
@@ -404,7 +404,7 @@
 request is rejected with "`405 Method Not Allowed`".
 
 ### <a id="delete-email"> Delete Email
-DELETE /config/server/@PLUGIN@~serviceusers/\{username\}/email_
+DELETE /config/server/@PLUGIN@~serviceusers/[\{account-id\}](../../../Documentation/rest-api-accounts.html#account-id)/email_
 
 Deletes the email of a service user.
 
@@ -426,7 +426,7 @@
 request is rejected with "`405 Method Not Allowed`".
 
 ### <a id="get-active"> Get Active
-GET /config/server/@PLUGIN@~serviceusers/\{username\}/active_
+GET /config/server/@PLUGIN@~serviceusers/[\{account-id\}](../../../Documentation/rest-api-accounts.html#account-id)/active_
 
 Checks if a service user is active.
 
@@ -449,7 +449,7 @@
 If the service user is inactive the response is `204 No Content`.
 
 ### <a id="set-active"> Set Active
-PUT /config/server/@PLUGIN@~serviceusers/\{username\}/active_
+PUT /config/server/@PLUGIN@~serviceusers/[\{account-id\}](../../../Documentation/rest-api-accounts.html#account-id)/active_
 
 Sets the service user state to active.
 
@@ -468,7 +468,7 @@
 If the service user was already active the response is `200 OK`.
 
 ### <a id="delete-active"> Delete Active
-DELETE /config/server/@PLUGIN@~serviceusers/\{username\}/active_
+DELETE /config/server/@PLUGIN@~serviceusers/[\{account-id\}](../../../Documentation/rest-api-accounts.html#account-id)/active_
 
 Sets the service user state to inactive.
 
@@ -485,7 +485,7 @@
 ```
 
 ### <a id="get-owner"> Get Owner
-GET /config/server/@PLUGIN@~serviceusers/\{username\}/owner_
+GET /config/server/@PLUGIN@~serviceusers/[\{account-id\}](../../../Documentation/rest-api-accounts.html#account-id)/owner_
 
 Retrieves the owner group of the service user.
 
@@ -525,7 +525,7 @@
 doesn't exist anymore the response is `404 Not Found`.
 
 ### <a id="set-owner"> Set Owner
-PUT /config/server/@PLUGIN@~serviceusers/\{username\}/owner_
+PUT /config/server/@PLUGIN@~serviceusers/[\{account-id\}](../../../Documentation/rest-api-accounts.html#account-id)/owner_
 
 Sets the owner group for a service user.
 
@@ -573,7 +573,7 @@
 `204 No Content`.
 
 ### <a id="delete-owner"> Delete Owner
-DELETE /config/server/@PLUGIN@~serviceusers/\{username\}/owner_
+DELETE /config/server/@PLUGIN@~serviceusers/[\{account-id\}](../../../Documentation/rest-api-accounts.html#account-id)/owner_
 
 Delete the owner group of a service user.