Add REST endpoint to reindex a single account

This may become handy to fix single accounts that are stale in the
index.

Change-Id: If4c1787a26a564c19d1c4c1896afa89120ef4813
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2016-11-22 16:50:10 +01:00
parent 5862be6274
commit ca547ff308
7 changed files with 131 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
//Copyright (C) 2016 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.google.gerrit.server.account;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.Response;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.account.Index.Input;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import java.io.IOException;
@Singleton
public class Index implements RestModifyView<AccountResource, Input> {
public static class Input {
}
private final AccountCache accountCache;
private final Provider<CurrentUser> self;
@Inject
Index(AccountCache accountCache,
Provider<CurrentUser> self) {
this.accountCache = accountCache;
this.self = self;
}
@Override
public Response<?> apply(AccountResource rsrc, Input input)
throws IOException, AuthException, OrmException {
if (self.get() != rsrc.getUser()
&& !self.get().getCapabilities().canModifyAccount()) {
throw new AuthException("not allowed to index account");
}
// evicting the account from the cache, reindexes the account
accountCache.evict(rsrc.getUser().getAccountId());
return Response.none();
}
}

View File

@@ -40,6 +40,7 @@ public class Module extends RestApiModule {
put(ACCOUNT_KIND).to(PutAccount.class);
get(ACCOUNT_KIND).to(GetAccount.class);
get(ACCOUNT_KIND, "detail").to(GetDetail.class);
post(ACCOUNT_KIND, "index").to(Index.class);
get(ACCOUNT_KIND, "name").to(GetName.class);
put(ACCOUNT_KIND, "name").to(PutName.class);
delete(ACCOUNT_KIND, "name").to(PutName.class);

View File

@@ -47,6 +47,7 @@ import com.google.gerrit.server.account.GetEditPreferences;
import com.google.gerrit.server.account.GetPreferences;
import com.google.gerrit.server.account.GetSshKeys;
import com.google.gerrit.server.account.GetWatchedProjects;
import com.google.gerrit.server.account.Index;
import com.google.gerrit.server.account.PostWatchedProjects;
import com.google.gerrit.server.account.PutAgreement;
import com.google.gerrit.server.account.SetDiffPreferences;
@@ -99,6 +100,7 @@ public class AccountApiImpl implements AccountApi {
private final SshKeys sshKeys;
private final GetAgreements getAgreements;
private final PutAgreement putAgreement;
private final Index index;
@Inject
AccountApiImpl(AccountLoader.Factory ailf,
@@ -126,6 +128,7 @@ public class AccountApiImpl implements AccountApi {
SshKeys sshKeys,
GetAgreements getAgreements,
PutAgreement putAgreement,
Index index,
@Assisted AccountResource account) {
this.account = account;
this.accountLoaderFactory = ailf;
@@ -153,6 +156,7 @@ public class AccountApiImpl implements AccountApi {
this.gpgApiAdapter = gpgApiAdapter;
this.getAgreements = getAgreements;
this.putAgreement = putAgreement;
this.index = index;
}
@Override
@@ -401,4 +405,12 @@ public class AccountApiImpl implements AccountApi {
}
}
@Override
public void index() throws RestApiException {
try {
index.apply(account, new Index.Input());
} catch (IOException | OrmException e) {
throw new RestApiException("Cannot index account", e);
}
}
}