diff --git a/java/google/registry/tools/ListDatastoreOperationsCommand.java b/java/google/registry/tools/ListDatastoreOperationsCommand.java new file mode 100644 index 000000000..bcef363f5 --- /dev/null +++ b/java/google/registry/tools/ListDatastoreOperationsCommand.java @@ -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 getQueryFilter() { + if (startTimeFilter == null) { + return Optional.empty(); + } + + DateTime earliestStartingTime = clock.nowUtc().minus(startTimeFilter); + return Optional.of( + String.format("metadata.common.startTime>\"%s\"", earliestStartingTime)); + } +} diff --git a/java/google/registry/tools/RegistryTool.java b/java/google/registry/tools/RegistryTool.java index 342e3f439..6c5322c7e 100644 --- a/java/google/registry/tools/RegistryTool.java +++ b/java/google/registry/tools/RegistryTool.java @@ -79,6 +79,7 @@ public final class RegistryTool { .put("hash_certificate", HashCertificateCommand.class) .put("import_datastore", ImportDatastoreCommand.class) .put("list_cursors", ListCursorsCommand.class) + .put("list_datastore_operations", ListDatastoreOperationsCommand.class) .put("list_domains", ListDomainsCommand.class) .put("list_hosts", ListHostsCommand.class) .put("list_premium_lists", ListPremiumListsCommand.class) diff --git a/java/google/registry/tools/RegistryToolComponent.java b/java/google/registry/tools/RegistryToolComponent.java index cb233c0cf..a5ef69cfb 100644 --- a/java/google/registry/tools/RegistryToolComponent.java +++ b/java/google/registry/tools/RegistryToolComponent.java @@ -97,6 +97,7 @@ interface RegistryToolComponent { void inject(GhostrydeCommand command); void inject(ImportDatastoreCommand command); void inject(ListCursorsCommand command); + void inject(ListDatastoreOperationsCommand command); void inject(LoadSnapshotCommand command); void inject(LockDomainCommand command); void inject(LoginCommand command); diff --git a/javatests/google/registry/tools/ListDatastoreOperationsCommandTest.java b/javatests/google/registry/tools/ListDatastoreOperationsCommandTest.java new file mode 100644 index 000000000..4845f4643 --- /dev/null +++ b/javatests/google/registry/tools/ListDatastoreOperationsCommandTest.java @@ -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 { + + @Mock private DatastoreAdmin datastoreAdmin; + @Mock private ListOperations listOperationsRequest; + @Captor ArgumentCaptor 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); + } +}