mirror of
https://github.com/google/nomulus.git
synced 2025-05-14 16:37:13 +02:00
Add a Nomulus command to list Datastore operations
Command would be useful when checking the status of daily backup jobs. Only two modes are supported: list all operations, or list those that started within the past period as specified by command line option. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=229239783
This commit is contained in:
parent
6082addb86
commit
9014165148
4 changed files with 139 additions and 0 deletions
|
@ -0,0 +1,63 @@
|
||||||
|
// Copyright 2019 The Nomulus Authors. All Rights Reserved.
|
||||||
|
//
|
||||||
|
// 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 google.registry.tools;
|
||||||
|
|
||||||
|
import com.beust.jcommander.Parameter;
|
||||||
|
import com.beust.jcommander.Parameters;
|
||||||
|
import com.google.api.client.json.JsonFactory;
|
||||||
|
import com.google.api.client.json.jackson2.JacksonFactory;
|
||||||
|
import google.registry.export.datastore.DatastoreAdmin;
|
||||||
|
import google.registry.export.datastore.DatastoreAdmin.ListOperations;
|
||||||
|
import google.registry.util.Clock;
|
||||||
|
import java.util.Optional;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import org.joda.time.DateTime;
|
||||||
|
import org.joda.time.Duration;
|
||||||
|
|
||||||
|
/** Command that lists Datastore operations. */
|
||||||
|
@Parameters(separators = " =", commandDescription = "List Datastore operations.")
|
||||||
|
public class ListDatastoreOperationsCommand implements Command {
|
||||||
|
|
||||||
|
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Parameter(
|
||||||
|
names = "--start_time_filter",
|
||||||
|
description =
|
||||||
|
"Duration relative to current time, used to filter operations by start time. "
|
||||||
|
+ "Value is in ISO-8601 format, e.g., PT10S for 10 seconds.")
|
||||||
|
private Duration startTimeFilter;
|
||||||
|
|
||||||
|
@Inject DatastoreAdmin datastoreAdmin;
|
||||||
|
@Inject Clock clock;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() throws Exception {
|
||||||
|
ListOperations listOperations =
|
||||||
|
getQueryFilter().map(datastoreAdmin::list).orElseGet(() -> datastoreAdmin.listAll());
|
||||||
|
System.out.println(JSON_FACTORY.toPrettyString(listOperations.execute()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Optional<String> getQueryFilter() {
|
||||||
|
if (startTimeFilter == null) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
DateTime earliestStartingTime = clock.nowUtc().minus(startTimeFilter);
|
||||||
|
return Optional.of(
|
||||||
|
String.format("metadata.common.startTime>\"%s\"", earliestStartingTime));
|
||||||
|
}
|
||||||
|
}
|
|
@ -79,6 +79,7 @@ public final class RegistryTool {
|
||||||
.put("hash_certificate", HashCertificateCommand.class)
|
.put("hash_certificate", HashCertificateCommand.class)
|
||||||
.put("import_datastore", ImportDatastoreCommand.class)
|
.put("import_datastore", ImportDatastoreCommand.class)
|
||||||
.put("list_cursors", ListCursorsCommand.class)
|
.put("list_cursors", ListCursorsCommand.class)
|
||||||
|
.put("list_datastore_operations", ListDatastoreOperationsCommand.class)
|
||||||
.put("list_domains", ListDomainsCommand.class)
|
.put("list_domains", ListDomainsCommand.class)
|
||||||
.put("list_hosts", ListHostsCommand.class)
|
.put("list_hosts", ListHostsCommand.class)
|
||||||
.put("list_premium_lists", ListPremiumListsCommand.class)
|
.put("list_premium_lists", ListPremiumListsCommand.class)
|
||||||
|
|
|
@ -97,6 +97,7 @@ interface RegistryToolComponent {
|
||||||
void inject(GhostrydeCommand command);
|
void inject(GhostrydeCommand command);
|
||||||
void inject(ImportDatastoreCommand command);
|
void inject(ImportDatastoreCommand command);
|
||||||
void inject(ListCursorsCommand command);
|
void inject(ListCursorsCommand command);
|
||||||
|
void inject(ListDatastoreOperationsCommand command);
|
||||||
void inject(LoadSnapshotCommand command);
|
void inject(LoadSnapshotCommand command);
|
||||||
void inject(LockDomainCommand command);
|
void inject(LockDomainCommand command);
|
||||||
void inject(LoginCommand command);
|
void inject(LoginCommand command);
|
||||||
|
|
|
@ -0,0 +1,74 @@
|
||||||
|
// Copyright 2019 The Nomulus Authors. All Rights Reserved.
|
||||||
|
//
|
||||||
|
// 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 google.registry.tools;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import google.registry.export.datastore.DatastoreAdmin;
|
||||||
|
import google.registry.export.datastore.DatastoreAdmin.ListOperations;
|
||||||
|
import google.registry.export.datastore.Operation.OperationList;
|
||||||
|
import google.registry.testing.FakeClock;
|
||||||
|
import google.registry.util.Clock;
|
||||||
|
import java.io.IOException;
|
||||||
|
import org.joda.time.DateTime;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.JUnit4;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
|
import org.mockito.Captor;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
|
||||||
|
/** Unit tests for {@link google.registry.tools.ListDatastoreOperationsCommand}. */
|
||||||
|
@RunWith(JUnit4.class)
|
||||||
|
public class ListDatastoreOperationsCommandTest
|
||||||
|
extends CommandTestCase<ListDatastoreOperationsCommand> {
|
||||||
|
|
||||||
|
@Mock private DatastoreAdmin datastoreAdmin;
|
||||||
|
@Mock private ListOperations listOperationsRequest;
|
||||||
|
@Captor ArgumentCaptor<String> filterClause;
|
||||||
|
|
||||||
|
private final Clock clock = new FakeClock(new DateTime("2019-01-01T00:00:30Z"));
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() throws IOException {
|
||||||
|
command.datastoreAdmin = datastoreAdmin;
|
||||||
|
command.clock = clock;
|
||||||
|
|
||||||
|
when(datastoreAdmin.list(filterClause.capture())).thenReturn(listOperationsRequest);
|
||||||
|
when(datastoreAdmin.listAll()).thenReturn(listOperationsRequest);
|
||||||
|
when(listOperationsRequest.execute()).thenReturn(new OperationList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testListAll() throws Exception {
|
||||||
|
runCommand();
|
||||||
|
verify(datastoreAdmin, times(1)).listAll();
|
||||||
|
verifyNoMoreInteractions(datastoreAdmin);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testListWithFilter() throws Exception {
|
||||||
|
runCommand("--start_time_filter=PT30S");
|
||||||
|
verify(datastoreAdmin, times(1)).list(filterClause.capture());
|
||||||
|
assertThat(filterClause.getValue())
|
||||||
|
.isEqualTo("metadata.common.startTime>\"2019-01-01T00:00:00.000Z\"");
|
||||||
|
verifyNoMoreInteractions(datastoreAdmin);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue