diff --git a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/api/accounts/AccountIT.java b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/api/accounts/AccountIT.java index 59f0150dfc..74f1d3b09c 100644 --- a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/api/accounts/AccountIT.java +++ b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/api/accounts/AccountIT.java @@ -340,6 +340,7 @@ public class AccountIT extends AbstractDaemonTest { // The test account should initially have exactly one ssh key List info = gApi.accounts().self().listSshKeys(); assertThat(info).hasSize(1); + assertSequenceNumbers(info); SshKeyInfo key = info.get(0); String inital = AccountCreator.publicKey(admin.sshKey, admin.email); assertThat(key.sshPublicKey).isEqualTo(inital); @@ -350,11 +351,27 @@ public class AccountIT extends AbstractDaemonTest { gApi.accounts().self().addSshKey(newKey); info = gApi.accounts().self().listSshKeys(); assertThat(info).hasSize(2); + assertSequenceNumbers(info); // Add an existing key again gApi.accounts().self().addSshKey(inital); info = gApi.accounts().self().listSshKeys(); assertThat(info).hasSize(3); + assertSequenceNumbers(info); + + // Delete second key + gApi.accounts().self().deleteSshKey(2); + info = gApi.accounts().self().listSshKeys(); + assertThat(info).hasSize(2); + assertThat(info.get(0).seq).isEqualTo(1); + assertThat(info.get(1).seq).isEqualTo(3); + } + + private void assertSequenceNumbers(List sshKeys) { + int seq = 1; + for (SshKeyInfo key : sshKeys) { + assertThat(key.seq).isEqualTo(seq++); + } } private PGPPublicKey getOnlyKeyFromStore(TestKey key) throws Exception { diff --git a/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/accounts/AccountApi.java b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/accounts/AccountApi.java index a45d8d3b62..a6e54b2469 100644 --- a/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/accounts/AccountApi.java +++ b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/accounts/AccountApi.java @@ -49,6 +49,7 @@ public interface AccountApi { List listSshKeys() throws RestApiException; SshKeyInfo addSshKey(String key) throws RestApiException; + void deleteSshKey(int seq) throws RestApiException; Map listGpgKeys() throws RestApiException; Map putGpgKeys(List add, List remove) @@ -128,6 +129,11 @@ public interface AccountApi { throw new NotImplementedException(); } + @Override + public void deleteSshKey(int seq) throws RestApiException { + throw new NotImplementedException(); + } + @Override public Map putGpgKeys(List add, List remove) throws RestApiException { diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/api/accounts/AccountApiImpl.java b/gerrit-server/src/main/java/com/google/gerrit/server/api/accounts/AccountApiImpl.java index 6c8e599ead..d85b63d1da 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/api/accounts/AccountApiImpl.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/api/accounts/AccountApiImpl.java @@ -33,6 +33,7 @@ import com.google.gerrit.server.account.AccountLoader; import com.google.gerrit.server.account.AccountResource; import com.google.gerrit.server.account.AddSshKey; import com.google.gerrit.server.account.CreateEmail; +import com.google.gerrit.server.account.DeleteSshKey; import com.google.gerrit.server.account.GetAvatar; import com.google.gerrit.server.account.GetDiffPreferences; import com.google.gerrit.server.account.GetEditPreferences; @@ -41,6 +42,7 @@ import com.google.gerrit.server.account.GetSshKeys; import com.google.gerrit.server.account.SetDiffPreferences; import com.google.gerrit.server.account.SetEditPreferences; import com.google.gerrit.server.account.SetPreferences; +import com.google.gerrit.server.account.SshKeys; import com.google.gerrit.server.account.StarredChanges; import com.google.gerrit.server.change.ChangeResource; import com.google.gerrit.server.change.ChangesCollection; @@ -75,6 +77,8 @@ public class AccountApiImpl implements AccountApi { private final GpgApiAdapter gpgApiAdapter; private final GetSshKeys getSshKeys; private final AddSshKey addSshKey; + private final DeleteSshKey deleteSshKey; + private final SshKeys sshKeys; @Inject AccountApiImpl(AccountLoader.Factory ailf, @@ -92,6 +96,8 @@ public class AccountApiImpl implements AccountApi { GpgApiAdapter gpgApiAdapter, GetSshKeys getSshKeys, AddSshKey addSshKey, + DeleteSshKey deleteSshKey, + SshKeys sshKeys, @Assisted AccountResource account) { this.account = account; this.accountLoaderFactory = ailf; @@ -108,6 +114,8 @@ public class AccountApiImpl implements AccountApi { this.createEmailFactory = createEmailFactory; this.getSshKeys = getSshKeys; this.addSshKey = addSshKey; + this.deleteSshKey = deleteSshKey; + this.sshKeys = sshKeys; this.gpgApiAdapter = gpgApiAdapter; } @@ -241,6 +249,17 @@ public class AccountApiImpl implements AccountApi { } } + @Override + public void deleteSshKey(int seq) throws RestApiException { + try { + AccountResource.SshKey sshKeyRes = + sshKeys.parse(account, IdString.fromDecoded(Integer.toString(seq))); + deleteSshKey.apply(sshKeyRes, null); + } catch (OrmException e) { + throw new RestApiException("Cannot delete SSH key", e); + } + } + @Override public Map listGpgKeys() throws RestApiException { try {