diff --git a/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/ConfigServiceV2Api.java b/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/ConfigServiceV2Api.java index 350b8de61b1e..bc0eec93e22b 100644 --- a/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/ConfigServiceV2Api.java +++ b/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/ConfigServiceV2Api.java @@ -33,6 +33,7 @@ package com.google.cloud.logging.spi.v2; +import com.google.api.gax.core.PageAccessor; import com.google.api.gax.grpc.ApiCallable; import com.google.api.gax.protobuf.PathTemplate; import com.google.logging.v2.CreateSinkRequest; @@ -48,6 +49,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.ScheduledExecutorService; // Manually-added imports: add custom (non-generated) imports after this point. @@ -55,26 +57,80 @@ /** * Service Description: * + *

This class provides the ability to make remote calls to the backing service through method + * calls that map to API methods. Sample code to get started: + * + *

+ * 
+ * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+ *   String formattedProjectName = ConfigServiceV2Api.formatProjectName("[PROJECT]");
+ *   ListSinksResponse response = configServiceV2Api.listSinks(formattedProjectName);
+ * }
+ * 
+ * 
+ * + *

Note: close() needs to be called on the configServiceV2Api object to clean up resources such + * as threads. In the example above, try-with-resources is used, which automatically calls + * close(). + * + *

The surface of this class includes several types of Java methods for each of the API's methods: + * + *

    + *
  1. A "flattened" method. With this type of method, the fields of the request type have been + * converted into function parameters. It may be the case that not all fields are available + * as parameters, and not every API method will have a flattened method entry point. + *
  2. A "request object" method. This type of method only takes one parameter, a request + * object, which must be constructed before the call. Not every API method will have a request + * object method. + *
  3. A "callable" method. This type of method takes no parameters and returns an immutable + * ApiCallable object, which can be used to initiate calls to the service. + *
+ * + *

See the individual methods for example code. + * + *

Many parameters require resource names to be formatted in a particular way. To assist + * with these names, this class includes a format method for each type of name, and additionally + * a parse method to extract the individual identifiers contained within names that are + * returned. + * + *

This class can be customized by passing in a custom instance of ConfigServiceV2Settings to + * create(). For example: + * + *

+ * 
+ * ConfigServiceV2Settings configServiceV2Settings = ConfigServiceV2Settings.defaultBuilder()
+ *     .provideChannelWith(myCredentials)
+ *     .build();
+ * ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.create(configServiceV2Settings);
+ * 
+ * 
+ * * * */ @javax.annotation.Generated("by GAPIC") public class ConfigServiceV2Api implements AutoCloseable { + private final ConfigServiceV2Settings settings; private final ManagedChannel channel; + private final ScheduledExecutorService executor; private final List closeables = new ArrayList<>(); private final ApiCallable listSinksCallable; - private final ApiCallable> listSinksIterableCallable; + private final ApiCallable> listSinksPagedCallable; private final ApiCallable getSinkCallable; private final ApiCallable createSinkCallable; private final ApiCallable updateSinkCallable; private final ApiCallable deleteSinkCallable; + public final ConfigServiceV2Settings getSettings() { + return settings; + } + private static final PathTemplate PROJECT_PATH_TEMPLATE = - PathTemplate.create("projects/{project}"); + PathTemplate.createWithoutUrlEncoding("projects/{project}"); private static final PathTemplate SINK_PATH_TEMPLATE = - PathTemplate.create("projects/{project}/sinks/{sink}"); + PathTemplate.createWithoutUrlEncoding("projects/{project}/sinks/{sink}"); /** * Formats a string containing the fully-qualified path to represent @@ -137,8 +193,8 @@ public static final String parseSinkFromSinkName(String sinkName) { * * */ - public static final ConfigServiceV2Api defaultInstance() throws IOException { - return create(ConfigServiceV2Settings.defaultInstance()); + public static final ConfigServiceV2Api createWithDefaults() throws IOException { + return create(ConfigServiceV2Settings.defaultBuilder().build()); } /** @@ -163,17 +219,24 @@ public static final ConfigServiceV2Api create(ConfigServiceV2Settings settings) * */ protected ConfigServiceV2Api(ConfigServiceV2Settings settings) throws IOException { - this.channel = settings.getChannel(); - - this.listSinksCallable = ApiCallable.create(settings.listSinksSettings(), settings); - this.listSinksIterableCallable = - ApiCallable.createIterable(settings.listSinksSettings(), settings); - this.getSinkCallable = ApiCallable.create(settings.getSinkSettings(), settings); - this.createSinkCallable = ApiCallable.create(settings.createSinkSettings(), settings); - this.updateSinkCallable = ApiCallable.create(settings.updateSinkSettings(), settings); - this.deleteSinkCallable = ApiCallable.create(settings.deleteSinkSettings(), settings); - - if (settings.shouldAutoCloseChannel()) { + this.settings = settings; + this.executor = settings.getExecutorProvider().getOrBuildExecutor(); + this.channel = settings.getChannelProvider().getOrBuildChannel(this.executor); + + this.listSinksCallable = + ApiCallable.create(settings.listSinksSettings(), this.channel, this.executor); + this.listSinksPagedCallable = + ApiCallable.createPagedVariant(settings.listSinksSettings(), this.channel, this.executor); + this.getSinkCallable = + ApiCallable.create(settings.getSinkSettings(), this.channel, this.executor); + this.createSinkCallable = + ApiCallable.create(settings.createSinkSettings(), this.channel, this.executor); + this.updateSinkCallable = + ApiCallable.create(settings.updateSinkSettings(), this.channel, this.executor); + this.deleteSinkCallable = + ApiCallable.create(settings.deleteSinkSettings(), this.channel, this.executor); + + if (settings.getChannelProvider().shouldAutoClose()) { closeables.add( new Closeable() { @Override @@ -182,6 +245,15 @@ public void close() throws IOException { } }); } + if (settings.getExecutorProvider().shouldAutoClose()) { + closeables.add( + new Closeable() { + @Override + public void close() throws IOException { + executor.shutdown(); + } + }); + } } // ----- listSinks ----- @@ -190,14 +262,25 @@ public void close() throws IOException { /** * Lists sinks. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = ConfigServiceV2Api.formatProjectName("[PROJECT]");
+   *   for (LogSink elements : configServiceV2Api.listSinks(formattedProjectName)) {
+   *     // doThingsWith(elements);
+   *   }
+   * }
+   * 
+ * * * * * @param projectName Required. The resource name of the project containing the sinks. * Example: `"projects/my-logging-project"`, `"projects/01234567890"`. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ - public final Iterable listSinks(String projectName) { + public final PageAccessor listSinks(String projectName) { + PROJECT_PATH_TEMPLATE.validate(projectName); ListSinksRequest request = ListSinksRequest.newBuilder().setProjectName(projectName).build(); return listSinks(request); } @@ -206,35 +289,83 @@ public final Iterable listSinks(String projectName) { /** * Lists sinks. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = ConfigServiceV2Api.formatProjectName("[PROJECT]");
+   *   ListSinksRequest request = ListSinksRequest.newBuilder()
+   *     .setProjectName(formattedProjectName)
+   *     .build();
+   *   for (LogSink elements : configServiceV2Api.listSinks(request)) {
+   *     // doThingsWith(elements);
+   *   }
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ - public final Iterable listSinks(ListSinksRequest request) { - return listSinksIterableCallable().call(request); + public final PageAccessor listSinks(ListSinksRequest request) { + return listSinksPagedCallable().call(request); } // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. /** * Lists sinks. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = ConfigServiceV2Api.formatProjectName("[PROJECT]");
+   *   ListSinksRequest request = ListSinksRequest.newBuilder()
+   *     .setProjectName(formattedProjectName)
+   *     .build();
+   *   ListenableFuture<PageAccessor<LogSink>> future = configServiceV2Api.listSinksPagedCallable().futureCall(request);
+   *   // Do something
+   *   for (LogSink elements : future.get()) {
+   *     // doThingsWith(elements);
+   *   }
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ - public final ApiCallable> listSinksIterableCallable() { - return listSinksIterableCallable; + public final ApiCallable> listSinksPagedCallable() { + return listSinksPagedCallable; } // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. /** * Lists sinks. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = ConfigServiceV2Api.formatProjectName("[PROJECT]");
+   *   ListSinksRequest request = ListSinksRequest.newBuilder()
+   *     .setProjectName(formattedProjectName)
+   *     .build();
+   *   while (true) {
+   *     ListSinksResponse response = configServiceV2Api.listSinksCallable().call(request);
+   *     for (LogSink elements : response.getSinksList()) {
+   *       // doThingsWith(elements);
+   *     }
+   *     String nextPageToken = response.getNextPageToken();
+   *     if (!Strings.isNullOrEmpty(nextPageToken)) {
+   *       request = request.toBuilder().setPageToken(nextPageToken).build();
+   *     } else {
+   *       break;
+   *     }
+   *   }
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable listSinksCallable() { return listSinksCallable; @@ -246,16 +377,24 @@ public final ApiCallable listSinksCallable( /** * Gets a sink. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedSinkName = ConfigServiceV2Api.formatSinkName("[PROJECT]", "[SINK]");
+   *   LogSink response = configServiceV2Api.getSink(formattedSinkName);
+   * }
+   * 
+ * * * * * @param sinkName The resource name of the sink to return. * Example: `"projects/my-project-id/sinks/my-sink-id"`. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public final LogSink getSink(String sinkName) { + SINK_PATH_TEMPLATE.validate(sinkName); GetSinkRequest request = GetSinkRequest.newBuilder().setSinkName(sinkName).build(); - return getSink(request); } @@ -263,11 +402,22 @@ public final LogSink getSink(String sinkName) { /** * Gets a sink. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedSinkName = ConfigServiceV2Api.formatSinkName("[PROJECT]", "[SINK]");
+   *   GetSinkRequest request = GetSinkRequest.newBuilder()
+   *     .setSinkName(formattedSinkName)
+   *     .build();
+   *   LogSink response = configServiceV2Api.getSink(request);
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ private LogSink getSink(GetSinkRequest request) { return getSinkCallable().call(request); @@ -277,9 +427,21 @@ private LogSink getSink(GetSinkRequest request) { /** * Gets a sink. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedSinkName = ConfigServiceV2Api.formatSinkName("[PROJECT]", "[SINK]");
+   *   GetSinkRequest request = GetSinkRequest.newBuilder()
+   *     .setSinkName(formattedSinkName)
+   *     .build();
+   *   ListenableFuture<LogSink> future = configServiceV2Api.getSinkCallable().futureCall(request);
+   *   // Do something
+   *   LogSink response = future.get();
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable getSinkCallable() { return getSinkCallable; @@ -291,6 +453,15 @@ public final ApiCallable getSinkCallable() { /** * Creates a sink. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = ConfigServiceV2Api.formatProjectName("[PROJECT]");
+   *   LogSink sink = LogSink.newBuilder().build();
+   *   LogSink response = configServiceV2Api.createSink(formattedProjectName, sink);
+   * }
+   * 
+ * * * * @@ -300,12 +471,13 @@ public final ApiCallable getSinkCallable() { * The new sink must be provided in the request. * @param sink The new sink, which must not have an identifier that already * exists. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public final LogSink createSink(String projectName, LogSink sink) { + PROJECT_PATH_TEMPLATE.validate(projectName); + CreateSinkRequest request = CreateSinkRequest.newBuilder().setProjectName(projectName).setSink(sink).build(); - return createSink(request); } @@ -313,11 +485,24 @@ public final LogSink createSink(String projectName, LogSink sink) { /** * Creates a sink. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = ConfigServiceV2Api.formatProjectName("[PROJECT]");
+   *   LogSink sink = LogSink.newBuilder().build();
+   *   CreateSinkRequest request = CreateSinkRequest.newBuilder()
+   *     .setProjectName(formattedProjectName)
+   *     .setSink(sink)
+   *     .build();
+   *   LogSink response = configServiceV2Api.createSink(request);
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public LogSink createSink(CreateSinkRequest request) { return createSinkCallable().call(request); @@ -327,9 +512,23 @@ public LogSink createSink(CreateSinkRequest request) { /** * Creates a sink. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = ConfigServiceV2Api.formatProjectName("[PROJECT]");
+   *   LogSink sink = LogSink.newBuilder().build();
+   *   CreateSinkRequest request = CreateSinkRequest.newBuilder()
+   *     .setProjectName(formattedProjectName)
+   *     .setSink(sink)
+   *     .build();
+   *   ListenableFuture<LogSink> future = configServiceV2Api.createSinkCallable().futureCall(request);
+   *   // Do something
+   *   LogSink response = future.get();
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable createSinkCallable() { return createSinkCallable; @@ -341,6 +540,15 @@ public final ApiCallable createSinkCallable() { /** * Creates or updates a sink. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedSinkName = ConfigServiceV2Api.formatSinkName("[PROJECT]", "[SINK]");
+   *   LogSink sink = LogSink.newBuilder().build();
+   *   LogSink response = configServiceV2Api.updateSink(formattedSinkName, sink);
+   * }
+   * 
+ * * * * @@ -353,12 +561,13 @@ public final ApiCallable createSinkCallable() { * @param sink The updated sink, whose name must be the same as the sink * identifier in `sinkName`. If `sinkName` does not exist, then * this method creates a new sink. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public final LogSink updateSink(String sinkName, LogSink sink) { + SINK_PATH_TEMPLATE.validate(sinkName); + UpdateSinkRequest request = UpdateSinkRequest.newBuilder().setSinkName(sinkName).setSink(sink).build(); - return updateSink(request); } @@ -366,11 +575,24 @@ public final LogSink updateSink(String sinkName, LogSink sink) { /** * Creates or updates a sink. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedSinkName = ConfigServiceV2Api.formatSinkName("[PROJECT]", "[SINK]");
+   *   LogSink sink = LogSink.newBuilder().build();
+   *   UpdateSinkRequest request = UpdateSinkRequest.newBuilder()
+   *     .setSinkName(formattedSinkName)
+   *     .setSink(sink)
+   *     .build();
+   *   LogSink response = configServiceV2Api.updateSink(request);
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public LogSink updateSink(UpdateSinkRequest request) { return updateSinkCallable().call(request); @@ -380,9 +602,23 @@ public LogSink updateSink(UpdateSinkRequest request) { /** * Creates or updates a sink. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedSinkName = ConfigServiceV2Api.formatSinkName("[PROJECT]", "[SINK]");
+   *   LogSink sink = LogSink.newBuilder().build();
+   *   UpdateSinkRequest request = UpdateSinkRequest.newBuilder()
+   *     .setSinkName(formattedSinkName)
+   *     .setSink(sink)
+   *     .build();
+   *   ListenableFuture<LogSink> future = configServiceV2Api.updateSinkCallable().futureCall(request);
+   *   // Do something
+   *   LogSink response = future.get();
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable updateSinkCallable() { return updateSinkCallable; @@ -394,16 +630,24 @@ public final ApiCallable updateSinkCallable() { /** * Deletes a sink. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedSinkName = ConfigServiceV2Api.formatSinkName("[PROJECT]", "[SINK]");
+   *   configServiceV2Api.deleteSink(formattedSinkName);
+   * }
+   * 
+ * * * * * @param sinkName The resource name of the sink to delete. * Example: `"projects/my-project-id/sinks/my-sink-id"`. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public final void deleteSink(String sinkName) { + SINK_PATH_TEMPLATE.validate(sinkName); DeleteSinkRequest request = DeleteSinkRequest.newBuilder().setSinkName(sinkName).build(); - deleteSink(request); } @@ -411,11 +655,22 @@ public final void deleteSink(String sinkName) { /** * Deletes a sink. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedSinkName = ConfigServiceV2Api.formatSinkName("[PROJECT]", "[SINK]");
+   *   DeleteSinkRequest request = DeleteSinkRequest.newBuilder()
+   *     .setSinkName(formattedSinkName)
+   *     .build();
+   *   configServiceV2Api.deleteSink(request);
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ private void deleteSink(DeleteSinkRequest request) { deleteSinkCallable().call(request); @@ -425,9 +680,21 @@ private void deleteSink(DeleteSinkRequest request) { /** * Deletes a sink. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedSinkName = ConfigServiceV2Api.formatSinkName("[PROJECT]", "[SINK]");
+   *   DeleteSinkRequest request = DeleteSinkRequest.newBuilder()
+   *     .setSinkName(formattedSinkName)
+   *     .build();
+   *   ListenableFuture<Void> future = configServiceV2Api.deleteSinkCallable().futureCall(request);
+   *   // Do something
+   *   future.get();
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable deleteSinkCallable() { return deleteSinkCallable; diff --git a/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/ConfigServiceV2Settings.java b/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/ConfigServiceV2Settings.java index 157f4b5d2427..9c1558fec565 100644 --- a/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/ConfigServiceV2Settings.java +++ b/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/ConfigServiceV2Settings.java @@ -40,6 +40,7 @@ import com.google.api.gax.grpc.PageStreamingDescriptor; import com.google.api.gax.grpc.ServiceApiSettings; import com.google.api.gax.grpc.SimpleCallSettings; +import com.google.auth.Credentials; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -57,12 +58,39 @@ import io.grpc.ManagedChannel; import io.grpc.Status; import java.io.IOException; +import java.util.List; import java.util.concurrent.ScheduledExecutorService; import org.joda.time.Duration; // Manually-added imports: add custom (non-generated) imports after this point. // AUTO-GENERATED DOCUMENTATION AND CLASS - see instructions at the top of the file for editing. +/** + * Settings class to configure an instance of {@link ConfigServiceV2Api}. + * + *

The default instance has everything set to sensible defaults: + * + *

    + *
  • The default service address (logging.googleapis.com) and default port (443) + * are used. + *
  • Credentials are acquired automatically through Application Default Credentials. + *
  • Retries are configured for idempotent methods but not for non-idempotent methods. + *
+ * + *

The builder of this class is recursive, so contained classes are themselves builders. + * When build() is called, the tree of builders is called to create the complete settings + * object. For example, to set the total timeout of ListSinks to 30 seconds: + * + *

+ * 
+ * ConfigServiceV2Settings.Builder configServiceV2SettingsBuilder =
+ *     ConfigServiceV2Settings.defaultBuilder();
+ * configServiceV2SettingsBuilder.ListSinksSettings().getRetrySettingsBuilder()
+ *     .setTotalTimeout(Duration.standardSeconds(30));
+ * ConfigServiceV2Settings configServiceV2Settings = configServiceV2SettingsBuilder.build();
+ * 
+ * 
+ */ @javax.annotation.Generated("by GAPIC") public class ConfigServiceV2Settings extends ServiceApiSettings { @@ -94,6 +122,16 @@ public class ConfigServiceV2Settings extends ServiceApiSettings { .add("https://www.googleapis.com/auth/cloud-platform") .build(); + /** + * The default connection settings of the service. + */ + public static final ConnectionSettings DEFAULT_CONNECTION_SETTINGS = + ConnectionSettings.newBuilder() + .setServiceAddress(DEFAULT_SERVICE_ADDRESS) + .setPort(DEFAULT_SERVICE_PORT) + .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) + .build(); + private final PageStreamingCallSettings listSinksSettings; @@ -102,45 +140,67 @@ public class ConfigServiceV2Settings extends ServiceApiSettings { private final SimpleCallSettings updateSinkSettings; private final SimpleCallSettings deleteSinkSettings; + /** + * Returns the object with the settings used for calls to listSinks. + */ public PageStreamingCallSettings listSinksSettings() { return listSinksSettings; } + /** + * Returns the object with the settings used for calls to getSink. + */ public SimpleCallSettings getSinkSettings() { return getSinkSettings; } + /** + * Returns the object with the settings used for calls to createSink. + */ public SimpleCallSettings createSinkSettings() { return createSinkSettings; } + /** + * Returns the object with the settings used for calls to updateSink. + */ public SimpleCallSettings updateSinkSettings() { return updateSinkSettings; } + /** + * Returns the object with the settings used for calls to deleteSink. + */ public SimpleCallSettings deleteSinkSettings() { return deleteSinkSettings; } - public static ConfigServiceV2Settings defaultInstance() throws IOException { - return newBuilder().build(); + /** + * Returns a builder for this class with recommended defaults. + */ + public static Builder defaultBuilder() { + return Builder.createDefault(); } + /** + * Returns a new builder for this class. + */ public static Builder newBuilder() { return new Builder(); } + /** + * Returns a builder containing all the values of this settings class. + */ public Builder toBuilder() { return new Builder(this); } private ConfigServiceV2Settings(Builder settingsBuilder) throws IOException { super( - settingsBuilder.getOrBuildChannel(), - settingsBuilder.shouldAutoCloseChannel(), - settingsBuilder.getOrBuildExecutor(), - settingsBuilder.getConnectionSettings(), + settingsBuilder.getChannelProvider(), + settingsBuilder.getExecutorProvider(), settingsBuilder.getGeneratorName(), settingsBuilder.getGeneratorVersion(), settingsBuilder.getClientLibName(), @@ -177,6 +237,9 @@ public Iterable extractResources(ListSinksResponse payload) { } }; + /** + * Builder for ConfigServiceV2Settings. + */ public static class Builder extends ServiceApiSettings.Builder { private final ImmutableList methodSettingsBuilders; @@ -219,38 +282,19 @@ public static class Builder extends ServiceApiSettings.Builder { } private Builder() { - super( - ConnectionSettings.builder() - .setServiceAddress(DEFAULT_SERVICE_ADDRESS) - .setPort(DEFAULT_SERVICE_PORT) - .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) - .build()); + super(DEFAULT_CONNECTION_SETTINGS); listSinksSettings = PageStreamingCallSettings.newBuilder( - ConfigServiceV2Grpc.METHOD_LIST_SINKS, LIST_SINKS_PAGE_STR_DESC) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); - - getSinkSettings = - SimpleCallSettings.newBuilder(ConfigServiceV2Grpc.METHOD_GET_SINK) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); - - createSinkSettings = - SimpleCallSettings.newBuilder(ConfigServiceV2Grpc.METHOD_CREATE_SINK) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); - - updateSinkSettings = - SimpleCallSettings.newBuilder(ConfigServiceV2Grpc.METHOD_UPDATE_SINK) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); - - deleteSinkSettings = - SimpleCallSettings.newBuilder(ConfigServiceV2Grpc.METHOD_DELETE_SINK) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + ConfigServiceV2Grpc.METHOD_LIST_SINKS, LIST_SINKS_PAGE_STR_DESC); + + getSinkSettings = SimpleCallSettings.newBuilder(ConfigServiceV2Grpc.METHOD_GET_SINK); + + createSinkSettings = SimpleCallSettings.newBuilder(ConfigServiceV2Grpc.METHOD_CREATE_SINK); + + updateSinkSettings = SimpleCallSettings.newBuilder(ConfigServiceV2Grpc.METHOD_UPDATE_SINK); + + deleteSinkSettings = SimpleCallSettings.newBuilder(ConfigServiceV2Grpc.METHOD_DELETE_SINK); methodSettingsBuilders = ImmutableList.of( @@ -261,6 +305,36 @@ private Builder() { deleteSinkSettings); } + private static Builder createDefault() { + Builder builder = new Builder(); + builder + .listSinksSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .getSinkSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .createSinkSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .updateSinkSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .deleteSinkSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + return builder; + } + private Builder(ConfigServiceV2Settings settings) { super(settings); @@ -279,6 +353,17 @@ private Builder(ConfigServiceV2Settings settings) { deleteSinkSettings); } + @Override + protected ConnectionSettings getDefaultConnectionSettings() { + return DEFAULT_CONNECTION_SETTINGS; + } + + @Override + public Builder provideExecutorWith(ScheduledExecutorService executor, boolean shouldAutoClose) { + super.provideExecutorWith(executor, shouldAutoClose); + return this; + } + @Override public Builder provideChannelWith(ManagedChannel channel, boolean shouldAutoClose) { super.provideChannelWith(channel, shouldAutoClose); @@ -292,8 +377,14 @@ public Builder provideChannelWith(ConnectionSettings settings) { } @Override - public Builder setExecutor(ScheduledExecutorService executor) { - super.setExecutor(executor); + public Builder provideChannelWith(Credentials credentials) { + super.provideChannelWith(credentials); + return this; + } + + @Override + public Builder provideChannelWith(List scopes) { + super.provideChannelWith(scopes); return this; } @@ -309,28 +400,48 @@ public Builder setClientLibHeader(String name, String version) { return this; } + /** + * Applies the given settings to all of the API methods in this service. Only + * values that are non-null will be applied, so this method is not capable + * of un-setting any values. + */ public Builder applyToAllApiMethods(ApiCallSettings.Builder apiCallSettings) throws Exception { super.applyToAllApiMethods(methodSettingsBuilders, apiCallSettings); return this; } + /** + * Returns the builder for the settings used for calls to listSinks. + */ public PageStreamingCallSettings.Builder listSinksSettings() { return listSinksSettings; } + /** + * Returns the builder for the settings used for calls to getSink. + */ public SimpleCallSettings.Builder getSinkSettings() { return getSinkSettings; } + /** + * Returns the builder for the settings used for calls to createSink. + */ public SimpleCallSettings.Builder createSinkSettings() { return createSinkSettings; } + /** + * Returns the builder for the settings used for calls to updateSink. + */ public SimpleCallSettings.Builder updateSinkSettings() { return updateSinkSettings; } + /** + * Returns the builder for the settings used for calls to deleteSink. + */ public SimpleCallSettings.Builder deleteSinkSettings() { return deleteSinkSettings; } diff --git a/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/LoggingServiceV2Api.java b/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/LoggingServiceV2Api.java index ead7c1b886aa..70960a025987 100644 --- a/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/LoggingServiceV2Api.java +++ b/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/LoggingServiceV2Api.java @@ -35,6 +35,7 @@ import com.google.api.MonitoredResource; import com.google.api.MonitoredResourceDescriptor; +import com.google.api.gax.core.PageAccessor; import com.google.api.gax.grpc.ApiCallable; import com.google.api.gax.protobuf.PathTemplate; import com.google.logging.v2.DeleteLogRequest; @@ -52,6 +53,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.concurrent.ScheduledExecutorService; // Manually-added imports: add custom (non-generated) imports after this point. @@ -59,32 +61,86 @@ /** * Service Description: Service for ingesting and querying logs. * + *

This class provides the ability to make remote calls to the backing service through method + * calls that map to API methods. Sample code to get started: + * + *

+ * 
+ * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+ *   String formattedLogName = LoggingServiceV2Api.formatLogName("[PROJECT]", "[LOG]");
+ *   loggingServiceV2Api.deleteLog(formattedLogName);
+ * }
+ * 
+ * 
+ * + *

Note: close() needs to be called on the loggingServiceV2Api object to clean up resources such + * as threads. In the example above, try-with-resources is used, which automatically calls + * close(). + * + *

The surface of this class includes several types of Java methods for each of the API's methods: + * + *

    + *
  1. A "flattened" method. With this type of method, the fields of the request type have been + * converted into function parameters. It may be the case that not all fields are available + * as parameters, and not every API method will have a flattened method entry point. + *
  2. A "request object" method. This type of method only takes one parameter, a request + * object, which must be constructed before the call. Not every API method will have a request + * object method. + *
  3. A "callable" method. This type of method takes no parameters and returns an immutable + * ApiCallable object, which can be used to initiate calls to the service. + *
+ * + *

See the individual methods for example code. + * + *

Many parameters require resource names to be formatted in a particular way. To assist + * with these names, this class includes a format method for each type of name, and additionally + * a parse method to extract the individual identifiers contained within names that are + * returned. + * + *

This class can be customized by passing in a custom instance of LoggingServiceV2Settings to + * create(). For example: + * + *

+ * 
+ * LoggingServiceV2Settings loggingServiceV2Settings = LoggingServiceV2Settings.defaultBuilder()
+ *     .provideChannelWith(myCredentials)
+ *     .build();
+ * LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.create(loggingServiceV2Settings);
+ * 
+ * 
+ * * * */ @javax.annotation.Generated("by GAPIC") public class LoggingServiceV2Api implements AutoCloseable { + private final LoggingServiceV2Settings settings; private final ManagedChannel channel; + private final ScheduledExecutorService executor; private final List closeables = new ArrayList<>(); private final ApiCallable deleteLogCallable; private final ApiCallable writeLogEntriesCallable; private final ApiCallable listLogEntriesCallable; - private final ApiCallable> - listLogEntriesIterableCallable; + private final ApiCallable> + listLogEntriesPagedCallable; private final ApiCallable< ListMonitoredResourceDescriptorsRequest, ListMonitoredResourceDescriptorsResponse> listMonitoredResourceDescriptorsCallable; private final ApiCallable< - ListMonitoredResourceDescriptorsRequest, Iterable> - listMonitoredResourceDescriptorsIterableCallable; + ListMonitoredResourceDescriptorsRequest, PageAccessor> + listMonitoredResourceDescriptorsPagedCallable; + + public final LoggingServiceV2Settings getSettings() { + return settings; + } private static final PathTemplate PROJECT_PATH_TEMPLATE = - PathTemplate.create("projects/{project}"); + PathTemplate.createWithoutUrlEncoding("projects/{project}"); private static final PathTemplate LOG_PATH_TEMPLATE = - PathTemplate.create("projects/{project}/logs/{log}"); + PathTemplate.createWithoutUrlEncoding("projects/{project}/logs/{log}"); /** * Formats a string containing the fully-qualified path to represent @@ -147,8 +203,8 @@ public static final String parseLogFromLogName(String logName) { * * */ - public static final LoggingServiceV2Api defaultInstance() throws IOException { - return create(LoggingServiceV2Settings.defaultInstance()); + public static final LoggingServiceV2Api createWithDefaults() throws IOException { + return create(LoggingServiceV2Settings.defaultBuilder().build()); } /** @@ -173,19 +229,27 @@ public static final LoggingServiceV2Api create(LoggingServiceV2Settings settings * */ protected LoggingServiceV2Api(LoggingServiceV2Settings settings) throws IOException { - this.channel = settings.getChannel(); - - this.deleteLogCallable = ApiCallable.create(settings.deleteLogSettings(), settings); - this.writeLogEntriesCallable = ApiCallable.create(settings.writeLogEntriesSettings(), settings); - this.listLogEntriesCallable = ApiCallable.create(settings.listLogEntriesSettings(), settings); - this.listLogEntriesIterableCallable = - ApiCallable.createIterable(settings.listLogEntriesSettings(), settings); + this.settings = settings; + this.executor = settings.getExecutorProvider().getOrBuildExecutor(); + this.channel = settings.getChannelProvider().getOrBuildChannel(this.executor); + + this.deleteLogCallable = + ApiCallable.create(settings.deleteLogSettings(), this.channel, this.executor); + this.writeLogEntriesCallable = + ApiCallable.create(settings.writeLogEntriesSettings(), this.channel, this.executor); + this.listLogEntriesCallable = + ApiCallable.create(settings.listLogEntriesSettings(), this.channel, this.executor); + this.listLogEntriesPagedCallable = + ApiCallable.createPagedVariant( + settings.listLogEntriesSettings(), this.channel, this.executor); this.listMonitoredResourceDescriptorsCallable = - ApiCallable.create(settings.listMonitoredResourceDescriptorsSettings(), settings); - this.listMonitoredResourceDescriptorsIterableCallable = - ApiCallable.createIterable(settings.listMonitoredResourceDescriptorsSettings(), settings); + ApiCallable.create( + settings.listMonitoredResourceDescriptorsSettings(), this.channel, this.executor); + this.listMonitoredResourceDescriptorsPagedCallable = + ApiCallable.createPagedVariant( + settings.listMonitoredResourceDescriptorsSettings(), this.channel, this.executor); - if (settings.shouldAutoCloseChannel()) { + if (settings.getChannelProvider().shouldAutoClose()) { closeables.add( new Closeable() { @Override @@ -194,6 +258,15 @@ public void close() throws IOException { } }); } + if (settings.getExecutorProvider().shouldAutoClose()) { + closeables.add( + new Closeable() { + @Override + public void close() throws IOException { + executor.shutdown(); + } + }); + } } // ----- deleteLog ----- @@ -203,16 +276,24 @@ public void close() throws IOException { * Deletes a log and all its log entries. * The log will reappear if it receives new entries. * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   String formattedLogName = LoggingServiceV2Api.formatLogName("[PROJECT]", "[LOG]");
+   *   loggingServiceV2Api.deleteLog(formattedLogName);
+   * }
+   * 
+ * * * * * @param logName Required. The resource name of the log to delete. Example: * `"projects/my-project/logs/syslog"`. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public final void deleteLog(String logName) { + LOG_PATH_TEMPLATE.validate(logName); DeleteLogRequest request = DeleteLogRequest.newBuilder().setLogName(logName).build(); - deleteLog(request); } @@ -221,11 +302,22 @@ public final void deleteLog(String logName) { * Deletes a log and all its log entries. * The log will reappear if it receives new entries. * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   String formattedLogName = LoggingServiceV2Api.formatLogName("[PROJECT]", "[LOG]");
+   *   DeleteLogRequest request = DeleteLogRequest.newBuilder()
+   *     .setLogName(formattedLogName)
+   *     .build();
+   *   loggingServiceV2Api.deleteLog(request);
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ private void deleteLog(DeleteLogRequest request) { deleteLogCallable().call(request); @@ -236,9 +328,21 @@ private void deleteLog(DeleteLogRequest request) { * Deletes a log and all its log entries. * The log will reappear if it receives new entries. * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   String formattedLogName = LoggingServiceV2Api.formatLogName("[PROJECT]", "[LOG]");
+   *   DeleteLogRequest request = DeleteLogRequest.newBuilder()
+   *     .setLogName(formattedLogName)
+   *     .build();
+   *   ListenableFuture<Void> future = loggingServiceV2Api.deleteLogCallable().futureCall(request);
+   *   // Do something
+   *   future.get();
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable deleteLogCallable() { return deleteLogCallable; @@ -251,6 +355,17 @@ public final ApiCallable deleteLogCallable() { * Writes log entries to Cloud Logging. * All log entries in Cloud Logging are written by this method. * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   String logName = "";
+   *   MonitoredResource resource = MonitoredResource.newBuilder().build();
+   *   Map<String, String> labels = new HashMap<>();
+   *   List<LogEntry> entries = new ArrayList<>();
+   *   WriteLogEntriesResponse response = loggingServiceV2Api.writeLogEntries(logName, resource, labels, entries);
+   * }
+   * 
+ * * * * @@ -266,13 +381,15 @@ public final ApiCallable deleteLogCallable() { * Example: `{ "size": "large", "color":"red" }` * @param entries Required. The log entries to write. The log entries must have values for * all required fields. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public final WriteLogEntriesResponse writeLogEntries( String logName, MonitoredResource resource, Map labels, List entries) { + LOG_PATH_TEMPLATE.validate(logName); + WriteLogEntriesRequest request = WriteLogEntriesRequest.newBuilder() .setLogName(logName) @@ -280,7 +397,6 @@ public final WriteLogEntriesResponse writeLogEntries( .putAllLabels(labels) .addAllEntries(entries) .build(); - return writeLogEntries(request); } @@ -289,11 +405,22 @@ public final WriteLogEntriesResponse writeLogEntries( * Writes log entries to Cloud Logging. * All log entries in Cloud Logging are written by this method. * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   List<LogEntry> entries = new ArrayList<>();
+   *   WriteLogEntriesRequest request = WriteLogEntriesRequest.newBuilder()
+   *     .addAllEntries(entries)
+   *     .build();
+   *   WriteLogEntriesResponse response = loggingServiceV2Api.writeLogEntries(request);
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public WriteLogEntriesResponse writeLogEntries(WriteLogEntriesRequest request) { return writeLogEntriesCallable().call(request); @@ -304,9 +431,21 @@ public WriteLogEntriesResponse writeLogEntries(WriteLogEntriesRequest request) { * Writes log entries to Cloud Logging. * All log entries in Cloud Logging are written by this method. * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   List<LogEntry> entries = new ArrayList<>();
+   *   WriteLogEntriesRequest request = WriteLogEntriesRequest.newBuilder()
+   *     .addAllEntries(entries)
+   *     .build();
+   *   ListenableFuture<WriteLogEntriesResponse> future = loggingServiceV2Api.writeLogEntriesCallable().futureCall(request);
+   *   // Do something
+   *   WriteLogEntriesResponse response = future.get();
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable writeLogEntriesCallable() { @@ -321,6 +460,18 @@ public WriteLogEntriesResponse writeLogEntries(WriteLogEntriesRequest request) { * Logging. For ways to export log entries, see * [Exporting Logs](/logging/docs/export). * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   List<String> projectIds = new ArrayList<>();
+   *   String filter = "";
+   *   String orderBy = "";
+   *   for (LogEntry elements : loggingServiceV2Api.listLogEntries(projectIds, filter, orderBy)) {
+   *     // doThingsWith(elements);
+   *   }
+   * }
+   * 
+ * * * * @@ -336,10 +487,11 @@ public WriteLogEntriesResponse writeLogEntries(WriteLogEntriesRequest request) { * `LogEntry.timestamp` (oldest first), and the second option returns entries * in order of decreasing timestamps (newest first). Entries with equal * timestamps are returned in order of `LogEntry.insertId`. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ - public final Iterable listLogEntries( + public final PageAccessor listLogEntries( List projectIds, String filter, String orderBy) { + ListLogEntriesRequest request = ListLogEntriesRequest.newBuilder() .addAllProjectIds(projectIds) @@ -355,14 +507,27 @@ public final Iterable listLogEntries( * Logging. For ways to export log entries, see * [Exporting Logs](/logging/docs/export). * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   List<String> projectIds = new ArrayList<>();
+   *   ListLogEntriesRequest request = ListLogEntriesRequest.newBuilder()
+   *     .addAllProjectIds(projectIds)
+   *     .build();
+   *   for (LogEntry elements : loggingServiceV2Api.listLogEntries(request)) {
+   *     // doThingsWith(elements);
+   *   }
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ - public final Iterable listLogEntries(ListLogEntriesRequest request) { - return listLogEntriesIterableCallable().call(request); + public final PageAccessor listLogEntries(ListLogEntriesRequest request) { + return listLogEntriesPagedCallable().call(request); } // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. @@ -371,13 +536,27 @@ public final Iterable listLogEntries(ListLogEntriesRequest request) { * Logging. For ways to export log entries, see * [Exporting Logs](/logging/docs/export). * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   List<String> projectIds = new ArrayList<>();
+   *   ListLogEntriesRequest request = ListLogEntriesRequest.newBuilder()
+   *     .addAllProjectIds(projectIds)
+   *     .build();
+   *   ListenableFuture<PageAccessor<LogEntry>> future = loggingServiceV2Api.listLogEntriesPagedCallable().futureCall(request);
+   *   // Do something
+   *   for (LogEntry elements : future.get()) {
+   *     // doThingsWith(elements);
+   *   }
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ - public final ApiCallable> - listLogEntriesIterableCallable() { - return listLogEntriesIterableCallable; + public final ApiCallable> + listLogEntriesPagedCallable() { + return listLogEntriesPagedCallable; } // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. @@ -386,9 +565,30 @@ public final Iterable listLogEntries(ListLogEntriesRequest request) { * Logging. For ways to export log entries, see * [Exporting Logs](/logging/docs/export). * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   List<String> projectIds = new ArrayList<>();
+   *   ListLogEntriesRequest request = ListLogEntriesRequest.newBuilder()
+   *     .addAllProjectIds(projectIds)
+   *     .build();
+   *   while (true) {
+   *     ListLogEntriesResponse response = loggingServiceV2Api.listLogEntriesCallable().call(request);
+   *     for (LogEntry elements : response.getEntriesList()) {
+   *       // doThingsWith(elements);
+   *     }
+   *     String nextPageToken = response.getNextPageToken();
+   *     if (!Strings.isNullOrEmpty(nextPageToken)) {
+   *       request = request.toBuilder().setPageToken(nextPageToken).build();
+   *     } else {
+   *       break;
+   *     }
+   *   }
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable listLogEntriesCallable() { return listLogEntriesCallable; @@ -400,38 +600,80 @@ public final ApiCallable listLogE /** * Lists monitored resource descriptors that are used by Cloud Logging. * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   ListMonitoredResourceDescriptorsRequest request = ListMonitoredResourceDescriptorsRequest.newBuilder()
+   *     .build();
+   *   for (MonitoredResourceDescriptor elements : loggingServiceV2Api.listMonitoredResourceDescriptors(request)) {
+   *     // doThingsWith(elements);
+   *   }
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ - public final Iterable listMonitoredResourceDescriptors( + public final PageAccessor listMonitoredResourceDescriptors( ListMonitoredResourceDescriptorsRequest request) { - return listMonitoredResourceDescriptorsIterableCallable().call(request); + return listMonitoredResourceDescriptorsPagedCallable().call(request); } // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. /** * Lists monitored resource descriptors that are used by Cloud Logging. * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   ListMonitoredResourceDescriptorsRequest request = ListMonitoredResourceDescriptorsRequest.newBuilder()
+   *     .build();
+   *   ListenableFuture<PageAccessor<MonitoredResourceDescriptor>> future = loggingServiceV2Api.listMonitoredResourceDescriptorsPagedCallable().futureCall(request);
+   *   // Do something
+   *   for (MonitoredResourceDescriptor elements : future.get()) {
+   *     // doThingsWith(elements);
+   *   }
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable< - ListMonitoredResourceDescriptorsRequest, Iterable> - listMonitoredResourceDescriptorsIterableCallable() { - return listMonitoredResourceDescriptorsIterableCallable; + ListMonitoredResourceDescriptorsRequest, PageAccessor> + listMonitoredResourceDescriptorsPagedCallable() { + return listMonitoredResourceDescriptorsPagedCallable; } // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. /** * Lists monitored resource descriptors that are used by Cloud Logging. * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   ListMonitoredResourceDescriptorsRequest request = ListMonitoredResourceDescriptorsRequest.newBuilder()
+   *     .build();
+   *   while (true) {
+   *     ListMonitoredResourceDescriptorsResponse response = loggingServiceV2Api.listMonitoredResourceDescriptorsCallable().call(request);
+   *     for (MonitoredResourceDescriptor elements : response.getResourceDescriptorsList()) {
+   *       // doThingsWith(elements);
+   *     }
+   *     String nextPageToken = response.getNextPageToken();
+   *     if (!Strings.isNullOrEmpty(nextPageToken)) {
+   *       request = request.toBuilder().setPageToken(nextPageToken).build();
+   *     } else {
+   *       break;
+   *     }
+   *   }
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable< ListMonitoredResourceDescriptorsRequest, ListMonitoredResourceDescriptorsResponse> diff --git a/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/LoggingServiceV2Settings.java b/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/LoggingServiceV2Settings.java index da6ae99fd9a7..c37e5c4ad305 100644 --- a/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/LoggingServiceV2Settings.java +++ b/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/LoggingServiceV2Settings.java @@ -41,6 +41,7 @@ import com.google.api.gax.grpc.PageStreamingDescriptor; import com.google.api.gax.grpc.ServiceApiSettings; import com.google.api.gax.grpc.SimpleCallSettings; +import com.google.auth.Credentials; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -59,12 +60,39 @@ import io.grpc.ManagedChannel; import io.grpc.Status; import java.io.IOException; +import java.util.List; import java.util.concurrent.ScheduledExecutorService; import org.joda.time.Duration; // Manually-added imports: add custom (non-generated) imports after this point. // AUTO-GENERATED DOCUMENTATION AND CLASS - see instructions at the top of the file for editing. +/** + * Settings class to configure an instance of {@link LoggingServiceV2Api}. + * + *

The default instance has everything set to sensible defaults: + * + *

    + *
  • The default service address (logging.googleapis.com) and default port (443) + * are used. + *
  • Credentials are acquired automatically through Application Default Credentials. + *
  • Retries are configured for idempotent methods but not for non-idempotent methods. + *
+ * + *

The builder of this class is recursive, so contained classes are themselves builders. + * When build() is called, the tree of builders is called to create the complete settings + * object. For example, to set the total timeout of DeleteLog to 30 seconds: + * + *

+ * 
+ * LoggingServiceV2Settings.Builder loggingServiceV2SettingsBuilder =
+ *     LoggingServiceV2Settings.defaultBuilder();
+ * loggingServiceV2SettingsBuilder.DeleteLogSettings().getRetrySettingsBuilder()
+ *     .setTotalTimeout(Duration.standardSeconds(30));
+ * LoggingServiceV2Settings loggingServiceV2Settings = loggingServiceV2SettingsBuilder.build();
+ * 
+ * 
+ */ @javax.annotation.Generated("by GAPIC") public class LoggingServiceV2Settings extends ServiceApiSettings { @@ -96,6 +124,16 @@ public class LoggingServiceV2Settings extends ServiceApiSettings { .add("https://www.googleapis.com/auth/cloud-platform") .build(); + /** + * The default connection settings of the service. + */ + public static final ConnectionSettings DEFAULT_CONNECTION_SETTINGS = + ConnectionSettings.newBuilder() + .setServiceAddress(DEFAULT_SERVICE_ADDRESS) + .setPort(DEFAULT_SERVICE_PORT) + .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) + .build(); + private final SimpleCallSettings deleteLogSettings; private final SimpleCallSettings writeLogEntriesSettings; @@ -107,20 +145,32 @@ public class LoggingServiceV2Settings extends ServiceApiSettings { MonitoredResourceDescriptor> listMonitoredResourceDescriptorsSettings; + /** + * Returns the object with the settings used for calls to deleteLog. + */ public SimpleCallSettings deleteLogSettings() { return deleteLogSettings; } + /** + * Returns the object with the settings used for calls to writeLogEntries. + */ public SimpleCallSettings writeLogEntriesSettings() { return writeLogEntriesSettings; } + /** + * Returns the object with the settings used for calls to listLogEntries. + */ public PageStreamingCallSettings listLogEntriesSettings() { return listLogEntriesSettings; } + /** + * Returns the object with the settings used for calls to listMonitoredResourceDescriptors. + */ public PageStreamingCallSettings< ListMonitoredResourceDescriptorsRequest, ListMonitoredResourceDescriptorsResponse, MonitoredResourceDescriptor> @@ -128,24 +178,31 @@ public SimpleCallSettings deleteLogSettings() { return listMonitoredResourceDescriptorsSettings; } - public static LoggingServiceV2Settings defaultInstance() throws IOException { - return newBuilder().build(); + /** + * Returns a builder for this class with recommended defaults. + */ + public static Builder defaultBuilder() { + return Builder.createDefault(); } + /** + * Returns a new builder for this class. + */ public static Builder newBuilder() { return new Builder(); } + /** + * Returns a builder containing all the values of this settings class. + */ public Builder toBuilder() { return new Builder(this); } private LoggingServiceV2Settings(Builder settingsBuilder) throws IOException { super( - settingsBuilder.getOrBuildChannel(), - settingsBuilder.shouldAutoCloseChannel(), - settingsBuilder.getOrBuildExecutor(), - settingsBuilder.getConnectionSettings(), + settingsBuilder.getChannelProvider(), + settingsBuilder.getExecutorProvider(), settingsBuilder.getGeneratorName(), settingsBuilder.getGeneratorVersion(), settingsBuilder.getClientLibName(), @@ -214,6 +271,9 @@ public Iterable extractResources( } }; + /** + * Builder for LoggingServiceV2Settings. + */ public static class Builder extends ServiceApiSettings.Builder { private final ImmutableList methodSettingsBuilders; @@ -270,35 +330,21 @@ public static class Builder extends ServiceApiSettings.Builder { } private Builder() { - super( - ConnectionSettings.builder() - .setServiceAddress(DEFAULT_SERVICE_ADDRESS) - .setPort(DEFAULT_SERVICE_PORT) - .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) - .build()); - - deleteLogSettings = - SimpleCallSettings.newBuilder(LoggingServiceV2Grpc.METHOD_DELETE_LOG) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + super(DEFAULT_CONNECTION_SETTINGS); + + deleteLogSettings = SimpleCallSettings.newBuilder(LoggingServiceV2Grpc.METHOD_DELETE_LOG); writeLogEntriesSettings = - SimpleCallSettings.newBuilder(LoggingServiceV2Grpc.METHOD_WRITE_LOG_ENTRIES) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + SimpleCallSettings.newBuilder(LoggingServiceV2Grpc.METHOD_WRITE_LOG_ENTRIES); listLogEntriesSettings = PageStreamingCallSettings.newBuilder( - LoggingServiceV2Grpc.METHOD_LIST_LOG_ENTRIES, LIST_LOG_ENTRIES_PAGE_STR_DESC) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("list")); + LoggingServiceV2Grpc.METHOD_LIST_LOG_ENTRIES, LIST_LOG_ENTRIES_PAGE_STR_DESC); listMonitoredResourceDescriptorsSettings = PageStreamingCallSettings.newBuilder( - LoggingServiceV2Grpc.METHOD_LIST_MONITORED_RESOURCE_DESCRIPTORS, - LIST_MONITORED_RESOURCE_DESCRIPTORS_PAGE_STR_DESC) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + LoggingServiceV2Grpc.METHOD_LIST_MONITORED_RESOURCE_DESCRIPTORS, + LIST_MONITORED_RESOURCE_DESCRIPTORS_PAGE_STR_DESC); methodSettingsBuilders = ImmutableList.of( @@ -308,6 +354,31 @@ private Builder() { listMonitoredResourceDescriptorsSettings); } + private static Builder createDefault() { + Builder builder = new Builder(); + builder + .deleteLogSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .writeLogEntriesSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .listLogEntriesSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("list")); + + builder + .listMonitoredResourceDescriptorsSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + return builder; + } + private Builder(LoggingServiceV2Settings settings) { super(settings); @@ -325,6 +396,17 @@ private Builder(LoggingServiceV2Settings settings) { listMonitoredResourceDescriptorsSettings); } + @Override + protected ConnectionSettings getDefaultConnectionSettings() { + return DEFAULT_CONNECTION_SETTINGS; + } + + @Override + public Builder provideExecutorWith(ScheduledExecutorService executor, boolean shouldAutoClose) { + super.provideExecutorWith(executor, shouldAutoClose); + return this; + } + @Override public Builder provideChannelWith(ManagedChannel channel, boolean shouldAutoClose) { super.provideChannelWith(channel, shouldAutoClose); @@ -338,8 +420,14 @@ public Builder provideChannelWith(ConnectionSettings settings) { } @Override - public Builder setExecutor(ScheduledExecutorService executor) { - super.setExecutor(executor); + public Builder provideChannelWith(Credentials credentials) { + super.provideChannelWith(credentials); + return this; + } + + @Override + public Builder provideChannelWith(List scopes) { + super.provideChannelWith(scopes); return this; } @@ -355,26 +443,43 @@ public Builder setClientLibHeader(String name, String version) { return this; } + /** + * Applies the given settings to all of the API methods in this service. Only + * values that are non-null will be applied, so this method is not capable + * of un-setting any values. + */ public Builder applyToAllApiMethods(ApiCallSettings.Builder apiCallSettings) throws Exception { super.applyToAllApiMethods(methodSettingsBuilders, apiCallSettings); return this; } + /** + * Returns the builder for the settings used for calls to deleteLog. + */ public SimpleCallSettings.Builder deleteLogSettings() { return deleteLogSettings; } + /** + * Returns the builder for the settings used for calls to writeLogEntries. + */ public SimpleCallSettings.Builder writeLogEntriesSettings() { return writeLogEntriesSettings; } + /** + * Returns the builder for the settings used for calls to listLogEntries. + */ public PageStreamingCallSettings.Builder< ListLogEntriesRequest, ListLogEntriesResponse, LogEntry> listLogEntriesSettings() { return listLogEntriesSettings; } + /** + * Returns the builder for the settings used for calls to listMonitoredResourceDescriptors. + */ public PageStreamingCallSettings.Builder< ListMonitoredResourceDescriptorsRequest, ListMonitoredResourceDescriptorsResponse, MonitoredResourceDescriptor> diff --git a/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/MetricsServiceV2Api.java b/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/MetricsServiceV2Api.java index a5f2306577b2..adb4d1c325cd 100644 --- a/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/MetricsServiceV2Api.java +++ b/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/MetricsServiceV2Api.java @@ -33,6 +33,7 @@ package com.google.cloud.logging.spi.v2; +import com.google.api.gax.core.PageAccessor; import com.google.api.gax.grpc.ApiCallable; import com.google.api.gax.protobuf.PathTemplate; import com.google.logging.v2.CreateLogMetricRequest; @@ -48,6 +49,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.ScheduledExecutorService; // Manually-added imports: add custom (non-generated) imports after this point. @@ -55,27 +57,81 @@ /** * Service Description: * + *

This class provides the ability to make remote calls to the backing service through method + * calls that map to API methods. Sample code to get started: + * + *

+ * 
+ * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+ *   String formattedProjectName = MetricsServiceV2Api.formatProjectName("[PROJECT]");
+ *   ListLogMetricsResponse response = metricsServiceV2Api.listLogMetrics(formattedProjectName);
+ * }
+ * 
+ * 
+ * + *

Note: close() needs to be called on the metricsServiceV2Api object to clean up resources such + * as threads. In the example above, try-with-resources is used, which automatically calls + * close(). + * + *

The surface of this class includes several types of Java methods for each of the API's methods: + * + *

    + *
  1. A "flattened" method. With this type of method, the fields of the request type have been + * converted into function parameters. It may be the case that not all fields are available + * as parameters, and not every API method will have a flattened method entry point. + *
  2. A "request object" method. This type of method only takes one parameter, a request + * object, which must be constructed before the call. Not every API method will have a request + * object method. + *
  3. A "callable" method. This type of method takes no parameters and returns an immutable + * ApiCallable object, which can be used to initiate calls to the service. + *
+ * + *

See the individual methods for example code. + * + *

Many parameters require resource names to be formatted in a particular way. To assist + * with these names, this class includes a format method for each type of name, and additionally + * a parse method to extract the individual identifiers contained within names that are + * returned. + * + *

This class can be customized by passing in a custom instance of MetricsServiceV2Settings to + * create(). For example: + * + *

+ * 
+ * MetricsServiceV2Settings metricsServiceV2Settings = MetricsServiceV2Settings.defaultBuilder()
+ *     .provideChannelWith(myCredentials)
+ *     .build();
+ * MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.create(metricsServiceV2Settings);
+ * 
+ * 
+ * * * */ @javax.annotation.Generated("by GAPIC") public class MetricsServiceV2Api implements AutoCloseable { + private final MetricsServiceV2Settings settings; private final ManagedChannel channel; + private final ScheduledExecutorService executor; private final List closeables = new ArrayList<>(); private final ApiCallable listLogMetricsCallable; - private final ApiCallable> - listLogMetricsIterableCallable; + private final ApiCallable> + listLogMetricsPagedCallable; private final ApiCallable getLogMetricCallable; private final ApiCallable createLogMetricCallable; private final ApiCallable updateLogMetricCallable; private final ApiCallable deleteLogMetricCallable; + public final MetricsServiceV2Settings getSettings() { + return settings; + } + private static final PathTemplate PROJECT_PATH_TEMPLATE = - PathTemplate.create("projects/{project}"); + PathTemplate.createWithoutUrlEncoding("projects/{project}"); private static final PathTemplate METRIC_PATH_TEMPLATE = - PathTemplate.create("projects/{project}/metrics/{metric}"); + PathTemplate.createWithoutUrlEncoding("projects/{project}/metrics/{metric}"); /** * Formats a string containing the fully-qualified path to represent @@ -138,8 +194,8 @@ public static final String parseMetricFromMetricName(String metricName) { * * */ - public static final MetricsServiceV2Api defaultInstance() throws IOException { - return create(MetricsServiceV2Settings.defaultInstance()); + public static final MetricsServiceV2Api createWithDefaults() throws IOException { + return create(MetricsServiceV2Settings.defaultBuilder().build()); } /** @@ -164,17 +220,25 @@ public static final MetricsServiceV2Api create(MetricsServiceV2Settings settings * */ protected MetricsServiceV2Api(MetricsServiceV2Settings settings) throws IOException { - this.channel = settings.getChannel(); - - this.listLogMetricsCallable = ApiCallable.create(settings.listLogMetricsSettings(), settings); - this.listLogMetricsIterableCallable = - ApiCallable.createIterable(settings.listLogMetricsSettings(), settings); - this.getLogMetricCallable = ApiCallable.create(settings.getLogMetricSettings(), settings); - this.createLogMetricCallable = ApiCallable.create(settings.createLogMetricSettings(), settings); - this.updateLogMetricCallable = ApiCallable.create(settings.updateLogMetricSettings(), settings); - this.deleteLogMetricCallable = ApiCallable.create(settings.deleteLogMetricSettings(), settings); - - if (settings.shouldAutoCloseChannel()) { + this.settings = settings; + this.executor = settings.getExecutorProvider().getOrBuildExecutor(); + this.channel = settings.getChannelProvider().getOrBuildChannel(this.executor); + + this.listLogMetricsCallable = + ApiCallable.create(settings.listLogMetricsSettings(), this.channel, this.executor); + this.listLogMetricsPagedCallable = + ApiCallable.createPagedVariant( + settings.listLogMetricsSettings(), this.channel, this.executor); + this.getLogMetricCallable = + ApiCallable.create(settings.getLogMetricSettings(), this.channel, this.executor); + this.createLogMetricCallable = + ApiCallable.create(settings.createLogMetricSettings(), this.channel, this.executor); + this.updateLogMetricCallable = + ApiCallable.create(settings.updateLogMetricSettings(), this.channel, this.executor); + this.deleteLogMetricCallable = + ApiCallable.create(settings.deleteLogMetricSettings(), this.channel, this.executor); + + if (settings.getChannelProvider().shouldAutoClose()) { closeables.add( new Closeable() { @Override @@ -183,6 +247,15 @@ public void close() throws IOException { } }); } + if (settings.getExecutorProvider().shouldAutoClose()) { + closeables.add( + new Closeable() { + @Override + public void close() throws IOException { + executor.shutdown(); + } + }); + } } // ----- listLogMetrics ----- @@ -191,14 +264,25 @@ public void close() throws IOException { /** * Lists logs-based metrics. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = MetricsServiceV2Api.formatProjectName("[PROJECT]");
+   *   for (LogMetric elements : metricsServiceV2Api.listLogMetrics(formattedProjectName)) {
+   *     // doThingsWith(elements);
+   *   }
+   * }
+   * 
+ * * * * * @param projectName Required. The resource name of the project containing the metrics. * Example: `"projects/my-project-id"`. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ - public final Iterable listLogMetrics(String projectName) { + public final PageAccessor listLogMetrics(String projectName) { + PROJECT_PATH_TEMPLATE.validate(projectName); ListLogMetricsRequest request = ListLogMetricsRequest.newBuilder().setProjectName(projectName).build(); return listLogMetrics(request); @@ -208,36 +292,84 @@ public final Iterable listLogMetrics(String projectName) { /** * Lists logs-based metrics. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = MetricsServiceV2Api.formatProjectName("[PROJECT]");
+   *   ListLogMetricsRequest request = ListLogMetricsRequest.newBuilder()
+   *     .setProjectName(formattedProjectName)
+   *     .build();
+   *   for (LogMetric elements : metricsServiceV2Api.listLogMetrics(request)) {
+   *     // doThingsWith(elements);
+   *   }
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ - public final Iterable listLogMetrics(ListLogMetricsRequest request) { - return listLogMetricsIterableCallable().call(request); + public final PageAccessor listLogMetrics(ListLogMetricsRequest request) { + return listLogMetricsPagedCallable().call(request); } // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. /** * Lists logs-based metrics. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = MetricsServiceV2Api.formatProjectName("[PROJECT]");
+   *   ListLogMetricsRequest request = ListLogMetricsRequest.newBuilder()
+   *     .setProjectName(formattedProjectName)
+   *     .build();
+   *   ListenableFuture<PageAccessor<LogMetric>> future = metricsServiceV2Api.listLogMetricsPagedCallable().futureCall(request);
+   *   // Do something
+   *   for (LogMetric elements : future.get()) {
+   *     // doThingsWith(elements);
+   *   }
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ - public final ApiCallable> - listLogMetricsIterableCallable() { - return listLogMetricsIterableCallable; + public final ApiCallable> + listLogMetricsPagedCallable() { + return listLogMetricsPagedCallable; } // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. /** * Lists logs-based metrics. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = MetricsServiceV2Api.formatProjectName("[PROJECT]");
+   *   ListLogMetricsRequest request = ListLogMetricsRequest.newBuilder()
+   *     .setProjectName(formattedProjectName)
+   *     .build();
+   *   while (true) {
+   *     ListLogMetricsResponse response = metricsServiceV2Api.listLogMetricsCallable().call(request);
+   *     for (LogMetric elements : response.getMetricsList()) {
+   *       // doThingsWith(elements);
+   *     }
+   *     String nextPageToken = response.getNextPageToken();
+   *     if (!Strings.isNullOrEmpty(nextPageToken)) {
+   *       request = request.toBuilder().setPageToken(nextPageToken).build();
+   *     } else {
+   *       break;
+   *     }
+   *   }
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable listLogMetricsCallable() { return listLogMetricsCallable; @@ -249,17 +381,25 @@ public final ApiCallable listLogM /** * Gets a logs-based metric. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedMetricName = MetricsServiceV2Api.formatMetricName("[PROJECT]", "[METRIC]");
+   *   LogMetric response = metricsServiceV2Api.getLogMetric(formattedMetricName);
+   * }
+   * 
+ * * * * * @param metricName The resource name of the desired metric. * Example: `"projects/my-project-id/metrics/my-metric-id"`. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public final LogMetric getLogMetric(String metricName) { + METRIC_PATH_TEMPLATE.validate(metricName); GetLogMetricRequest request = GetLogMetricRequest.newBuilder().setMetricName(metricName).build(); - return getLogMetric(request); } @@ -267,11 +407,22 @@ public final LogMetric getLogMetric(String metricName) { /** * Gets a logs-based metric. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedMetricName = MetricsServiceV2Api.formatMetricName("[PROJECT]", "[METRIC]");
+   *   GetLogMetricRequest request = GetLogMetricRequest.newBuilder()
+   *     .setMetricName(formattedMetricName)
+   *     .build();
+   *   LogMetric response = metricsServiceV2Api.getLogMetric(request);
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ private LogMetric getLogMetric(GetLogMetricRequest request) { return getLogMetricCallable().call(request); @@ -281,9 +432,21 @@ private LogMetric getLogMetric(GetLogMetricRequest request) { /** * Gets a logs-based metric. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedMetricName = MetricsServiceV2Api.formatMetricName("[PROJECT]", "[METRIC]");
+   *   GetLogMetricRequest request = GetLogMetricRequest.newBuilder()
+   *     .setMetricName(formattedMetricName)
+   *     .build();
+   *   ListenableFuture<LogMetric> future = metricsServiceV2Api.getLogMetricCallable().futureCall(request);
+   *   // Do something
+   *   LogMetric response = future.get();
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable getLogMetricCallable() { return getLogMetricCallable; @@ -295,6 +458,15 @@ public final ApiCallable getLogMetricCallable() /** * Creates a logs-based metric. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = MetricsServiceV2Api.formatProjectName("[PROJECT]");
+   *   LogMetric metric = LogMetric.newBuilder().build();
+   *   LogMetric response = metricsServiceV2Api.createLogMetric(formattedProjectName, metric);
+   * }
+   * 
+ * * * * @@ -304,12 +476,13 @@ public final ApiCallable getLogMetricCallable() * The new metric must be provided in the request. * @param metric The new logs-based metric, which must not have an identifier that * already exists. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public final LogMetric createLogMetric(String projectName, LogMetric metric) { + PROJECT_PATH_TEMPLATE.validate(projectName); + CreateLogMetricRequest request = CreateLogMetricRequest.newBuilder().setProjectName(projectName).setMetric(metric).build(); - return createLogMetric(request); } @@ -317,11 +490,24 @@ public final LogMetric createLogMetric(String projectName, LogMetric metric) { /** * Creates a logs-based metric. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = MetricsServiceV2Api.formatProjectName("[PROJECT]");
+   *   LogMetric metric = LogMetric.newBuilder().build();
+   *   CreateLogMetricRequest request = CreateLogMetricRequest.newBuilder()
+   *     .setProjectName(formattedProjectName)
+   *     .setMetric(metric)
+   *     .build();
+   *   LogMetric response = metricsServiceV2Api.createLogMetric(request);
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public LogMetric createLogMetric(CreateLogMetricRequest request) { return createLogMetricCallable().call(request); @@ -331,9 +517,23 @@ public LogMetric createLogMetric(CreateLogMetricRequest request) { /** * Creates a logs-based metric. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = MetricsServiceV2Api.formatProjectName("[PROJECT]");
+   *   LogMetric metric = LogMetric.newBuilder().build();
+   *   CreateLogMetricRequest request = CreateLogMetricRequest.newBuilder()
+   *     .setProjectName(formattedProjectName)
+   *     .setMetric(metric)
+   *     .build();
+   *   ListenableFuture<LogMetric> future = metricsServiceV2Api.createLogMetricCallable().futureCall(request);
+   *   // Do something
+   *   LogMetric response = future.get();
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable createLogMetricCallable() { return createLogMetricCallable; @@ -345,6 +545,15 @@ public final ApiCallable createLogMetricCalla /** * Creates or updates a logs-based metric. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedMetricName = MetricsServiceV2Api.formatMetricName("[PROJECT]", "[METRIC]");
+   *   LogMetric metric = LogMetric.newBuilder().build();
+   *   LogMetric response = metricsServiceV2Api.updateLogMetric(formattedMetricName, metric);
+   * }
+   * 
+ * * * * @@ -357,12 +566,13 @@ public final ApiCallable createLogMetricCalla * @param metric The updated metric, whose name must be the same as the * metric identifier in `metricName`. If `metricName` does not * exist, then a new metric is created. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public final LogMetric updateLogMetric(String metricName, LogMetric metric) { + METRIC_PATH_TEMPLATE.validate(metricName); + UpdateLogMetricRequest request = UpdateLogMetricRequest.newBuilder().setMetricName(metricName).setMetric(metric).build(); - return updateLogMetric(request); } @@ -370,11 +580,24 @@ public final LogMetric updateLogMetric(String metricName, LogMetric metric) { /** * Creates or updates a logs-based metric. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedMetricName = MetricsServiceV2Api.formatMetricName("[PROJECT]", "[METRIC]");
+   *   LogMetric metric = LogMetric.newBuilder().build();
+   *   UpdateLogMetricRequest request = UpdateLogMetricRequest.newBuilder()
+   *     .setMetricName(formattedMetricName)
+   *     .setMetric(metric)
+   *     .build();
+   *   LogMetric response = metricsServiceV2Api.updateLogMetric(request);
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public LogMetric updateLogMetric(UpdateLogMetricRequest request) { return updateLogMetricCallable().call(request); @@ -384,9 +607,23 @@ public LogMetric updateLogMetric(UpdateLogMetricRequest request) { /** * Creates or updates a logs-based metric. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedMetricName = MetricsServiceV2Api.formatMetricName("[PROJECT]", "[METRIC]");
+   *   LogMetric metric = LogMetric.newBuilder().build();
+   *   UpdateLogMetricRequest request = UpdateLogMetricRequest.newBuilder()
+   *     .setMetricName(formattedMetricName)
+   *     .setMetric(metric)
+   *     .build();
+   *   ListenableFuture<LogMetric> future = metricsServiceV2Api.updateLogMetricCallable().futureCall(request);
+   *   // Do something
+   *   LogMetric response = future.get();
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable updateLogMetricCallable() { return updateLogMetricCallable; @@ -398,17 +635,25 @@ public final ApiCallable updateLogMetricCalla /** * Deletes a logs-based metric. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedMetricName = MetricsServiceV2Api.formatMetricName("[PROJECT]", "[METRIC]");
+   *   metricsServiceV2Api.deleteLogMetric(formattedMetricName);
+   * }
+   * 
+ * * * * * @param metricName The resource name of the metric to delete. * Example: `"projects/my-project-id/metrics/my-metric-id"`. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public final void deleteLogMetric(String metricName) { + METRIC_PATH_TEMPLATE.validate(metricName); DeleteLogMetricRequest request = DeleteLogMetricRequest.newBuilder().setMetricName(metricName).build(); - deleteLogMetric(request); } @@ -416,11 +661,22 @@ public final void deleteLogMetric(String metricName) { /** * Deletes a logs-based metric. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedMetricName = MetricsServiceV2Api.formatMetricName("[PROJECT]", "[METRIC]");
+   *   DeleteLogMetricRequest request = DeleteLogMetricRequest.newBuilder()
+   *     .setMetricName(formattedMetricName)
+   *     .build();
+   *   metricsServiceV2Api.deleteLogMetric(request);
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ private void deleteLogMetric(DeleteLogMetricRequest request) { deleteLogMetricCallable().call(request); @@ -430,9 +686,21 @@ private void deleteLogMetric(DeleteLogMetricRequest request) { /** * Deletes a logs-based metric. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedMetricName = MetricsServiceV2Api.formatMetricName("[PROJECT]", "[METRIC]");
+   *   DeleteLogMetricRequest request = DeleteLogMetricRequest.newBuilder()
+   *     .setMetricName(formattedMetricName)
+   *     .build();
+   *   ListenableFuture<Void> future = metricsServiceV2Api.deleteLogMetricCallable().futureCall(request);
+   *   // Do something
+   *   future.get();
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable deleteLogMetricCallable() { return deleteLogMetricCallable; diff --git a/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/MetricsServiceV2Settings.java b/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/MetricsServiceV2Settings.java index 316a9851bf11..1308b95eb029 100644 --- a/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/MetricsServiceV2Settings.java +++ b/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/MetricsServiceV2Settings.java @@ -40,6 +40,7 @@ import com.google.api.gax.grpc.PageStreamingDescriptor; import com.google.api.gax.grpc.ServiceApiSettings; import com.google.api.gax.grpc.SimpleCallSettings; +import com.google.auth.Credentials; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -57,12 +58,39 @@ import io.grpc.ManagedChannel; import io.grpc.Status; import java.io.IOException; +import java.util.List; import java.util.concurrent.ScheduledExecutorService; import org.joda.time.Duration; // Manually-added imports: add custom (non-generated) imports after this point. // AUTO-GENERATED DOCUMENTATION AND CLASS - see instructions at the top of the file for editing. +/** + * Settings class to configure an instance of {@link MetricsServiceV2Api}. + * + *

The default instance has everything set to sensible defaults: + * + *

    + *
  • The default service address (logging.googleapis.com) and default port (443) + * are used. + *
  • Credentials are acquired automatically through Application Default Credentials. + *
  • Retries are configured for idempotent methods but not for non-idempotent methods. + *
+ * + *

The builder of this class is recursive, so contained classes are themselves builders. + * When build() is called, the tree of builders is called to create the complete settings + * object. For example, to set the total timeout of ListLogMetrics to 30 seconds: + * + *

+ * 
+ * MetricsServiceV2Settings.Builder metricsServiceV2SettingsBuilder =
+ *     MetricsServiceV2Settings.defaultBuilder();
+ * metricsServiceV2SettingsBuilder.ListLogMetricsSettings().getRetrySettingsBuilder()
+ *     .setTotalTimeout(Duration.standardSeconds(30));
+ * MetricsServiceV2Settings metricsServiceV2Settings = metricsServiceV2SettingsBuilder.build();
+ * 
+ * 
+ */ @javax.annotation.Generated("by GAPIC") public class MetricsServiceV2Settings extends ServiceApiSettings { @@ -94,6 +122,16 @@ public class MetricsServiceV2Settings extends ServiceApiSettings { .add("https://www.googleapis.com/auth/cloud-platform") .build(); + /** + * The default connection settings of the service. + */ + public static final ConnectionSettings DEFAULT_CONNECTION_SETTINGS = + ConnectionSettings.newBuilder() + .setServiceAddress(DEFAULT_SERVICE_ADDRESS) + .setPort(DEFAULT_SERVICE_PORT) + .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) + .build(); + private final PageStreamingCallSettings listLogMetricsSettings; @@ -102,45 +140,67 @@ public class MetricsServiceV2Settings extends ServiceApiSettings { private final SimpleCallSettings updateLogMetricSettings; private final SimpleCallSettings deleteLogMetricSettings; + /** + * Returns the object with the settings used for calls to listLogMetrics. + */ public PageStreamingCallSettings listLogMetricsSettings() { return listLogMetricsSettings; } + /** + * Returns the object with the settings used for calls to getLogMetric. + */ public SimpleCallSettings getLogMetricSettings() { return getLogMetricSettings; } + /** + * Returns the object with the settings used for calls to createLogMetric. + */ public SimpleCallSettings createLogMetricSettings() { return createLogMetricSettings; } + /** + * Returns the object with the settings used for calls to updateLogMetric. + */ public SimpleCallSettings updateLogMetricSettings() { return updateLogMetricSettings; } + /** + * Returns the object with the settings used for calls to deleteLogMetric. + */ public SimpleCallSettings deleteLogMetricSettings() { return deleteLogMetricSettings; } - public static MetricsServiceV2Settings defaultInstance() throws IOException { - return newBuilder().build(); + /** + * Returns a builder for this class with recommended defaults. + */ + public static Builder defaultBuilder() { + return Builder.createDefault(); } + /** + * Returns a new builder for this class. + */ public static Builder newBuilder() { return new Builder(); } + /** + * Returns a builder containing all the values of this settings class. + */ public Builder toBuilder() { return new Builder(this); } private MetricsServiceV2Settings(Builder settingsBuilder) throws IOException { super( - settingsBuilder.getOrBuildChannel(), - settingsBuilder.shouldAutoCloseChannel(), - settingsBuilder.getOrBuildExecutor(), - settingsBuilder.getConnectionSettings(), + settingsBuilder.getChannelProvider(), + settingsBuilder.getExecutorProvider(), settingsBuilder.getGeneratorName(), settingsBuilder.getGeneratorVersion(), settingsBuilder.getClientLibName(), @@ -177,6 +237,9 @@ public Iterable extractResources(ListLogMetricsResponse payload) { } }; + /** + * Builder for MetricsServiceV2Settings. + */ public static class Builder extends ServiceApiSettings.Builder { private final ImmutableList methodSettingsBuilders; @@ -220,38 +283,23 @@ public static class Builder extends ServiceApiSettings.Builder { } private Builder() { - super( - ConnectionSettings.builder() - .setServiceAddress(DEFAULT_SERVICE_ADDRESS) - .setPort(DEFAULT_SERVICE_PORT) - .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) - .build()); + super(DEFAULT_CONNECTION_SETTINGS); listLogMetricsSettings = PageStreamingCallSettings.newBuilder( - MetricsServiceV2Grpc.METHOD_LIST_LOG_METRICS, LIST_LOG_METRICS_PAGE_STR_DESC) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + MetricsServiceV2Grpc.METHOD_LIST_LOG_METRICS, LIST_LOG_METRICS_PAGE_STR_DESC); getLogMetricSettings = - SimpleCallSettings.newBuilder(MetricsServiceV2Grpc.METHOD_GET_LOG_METRIC) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + SimpleCallSettings.newBuilder(MetricsServiceV2Grpc.METHOD_GET_LOG_METRIC); createLogMetricSettings = - SimpleCallSettings.newBuilder(MetricsServiceV2Grpc.METHOD_CREATE_LOG_METRIC) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + SimpleCallSettings.newBuilder(MetricsServiceV2Grpc.METHOD_CREATE_LOG_METRIC); updateLogMetricSettings = - SimpleCallSettings.newBuilder(MetricsServiceV2Grpc.METHOD_UPDATE_LOG_METRIC) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + SimpleCallSettings.newBuilder(MetricsServiceV2Grpc.METHOD_UPDATE_LOG_METRIC); deleteLogMetricSettings = - SimpleCallSettings.newBuilder(MetricsServiceV2Grpc.METHOD_DELETE_LOG_METRIC) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + SimpleCallSettings.newBuilder(MetricsServiceV2Grpc.METHOD_DELETE_LOG_METRIC); methodSettingsBuilders = ImmutableList.of( @@ -262,6 +310,36 @@ private Builder() { deleteLogMetricSettings); } + private static Builder createDefault() { + Builder builder = new Builder(); + builder + .listLogMetricsSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .getLogMetricSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .createLogMetricSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .updateLogMetricSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .deleteLogMetricSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + return builder; + } + private Builder(MetricsServiceV2Settings settings) { super(settings); @@ -280,6 +358,17 @@ private Builder(MetricsServiceV2Settings settings) { deleteLogMetricSettings); } + @Override + protected ConnectionSettings getDefaultConnectionSettings() { + return DEFAULT_CONNECTION_SETTINGS; + } + + @Override + public Builder provideExecutorWith(ScheduledExecutorService executor, boolean shouldAutoClose) { + super.provideExecutorWith(executor, shouldAutoClose); + return this; + } + @Override public Builder provideChannelWith(ManagedChannel channel, boolean shouldAutoClose) { super.provideChannelWith(channel, shouldAutoClose); @@ -293,8 +382,14 @@ public Builder provideChannelWith(ConnectionSettings settings) { } @Override - public Builder setExecutor(ScheduledExecutorService executor) { - super.setExecutor(executor); + public Builder provideChannelWith(Credentials credentials) { + super.provideChannelWith(credentials); + return this; + } + + @Override + public Builder provideChannelWith(List scopes) { + super.provideChannelWith(scopes); return this; } @@ -310,29 +405,49 @@ public Builder setClientLibHeader(String name, String version) { return this; } + /** + * Applies the given settings to all of the API methods in this service. Only + * values that are non-null will be applied, so this method is not capable + * of un-setting any values. + */ public Builder applyToAllApiMethods(ApiCallSettings.Builder apiCallSettings) throws Exception { super.applyToAllApiMethods(methodSettingsBuilders, apiCallSettings); return this; } + /** + * Returns the builder for the settings used for calls to listLogMetrics. + */ public PageStreamingCallSettings.Builder< ListLogMetricsRequest, ListLogMetricsResponse, LogMetric> listLogMetricsSettings() { return listLogMetricsSettings; } + /** + * Returns the builder for the settings used for calls to getLogMetric. + */ public SimpleCallSettings.Builder getLogMetricSettings() { return getLogMetricSettings; } + /** + * Returns the builder for the settings used for calls to createLogMetric. + */ public SimpleCallSettings.Builder createLogMetricSettings() { return createLogMetricSettings; } + /** + * Returns the builder for the settings used for calls to updateLogMetric. + */ public SimpleCallSettings.Builder updateLogMetricSettings() { return updateLogMetricSettings; } + /** + * Returns the builder for the settings used for calls to deleteLogMetric. + */ public SimpleCallSettings.Builder deleteLogMetricSettings() { return deleteLogMetricSettings; } diff --git a/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/package-info.java b/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/package-info.java new file mode 100644 index 000000000000..81ee3d3fce7e --- /dev/null +++ b/gcloud-java-logging/baseline/src/main/java/com/google/cloud/logging/spi/v2/package-info.java @@ -0,0 +1,86 @@ +/* + * Copyright 2016 Google Inc. 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. + */ + +/* + * EDITING INSTRUCTIONS + * This file was automatically generated. For the short term, the process for + * refreshing this file will only be runnable by Google engineers. + * Manual additions are allowed because the refresh process performs + * a 3-way merge in order to preserve those manual additions. In order to not + * break the refresh process, additional documentation should be added between + * "manual edit" demarcations. + * + * Other generated files allow additional edits - see the editing instructions + * in those files for more details. + * + * Happy editing! + */ + +/** + * A client to Google Cloud Logging API. + * + * The interfaces provided are listed below, along with a usage sample + * + * =================== + * LoggingServiceV2Api + * =================== + * + * Service Description: Service for ingesting and querying logs. + * + * Sample for LoggingServiceV2Api: + *
+ * 
+ * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+ *   String formattedLogName = LoggingServiceV2Api.formatLogName("[PROJECT]", "[LOG]");
+ *   loggingServiceV2Api.deleteLog(formattedLogName);
+ * }
+ * 
+ * 
+ * + * ================== + * ConfigServiceV2Api + * ================== + * + * Service Description: + * + * Sample for ConfigServiceV2Api: + *
+ * 
+ * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+ *   String formattedProjectName = ConfigServiceV2Api.formatProjectName("[PROJECT]");
+ *   ListSinksResponse response = configServiceV2Api.listSinks(formattedProjectName);
+ * }
+ * 
+ * 
+ * + * =================== + * MetricsServiceV2Api + * =================== + * + * Service Description: + * + * Sample for MetricsServiceV2Api: + *
+ * 
+ * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+ *   String formattedProjectName = MetricsServiceV2Api.formatProjectName("[PROJECT]");
+ *   ListLogMetricsResponse response = metricsServiceV2Api.listLogMetrics(formattedProjectName);
+ * }
+ * 
+ * 
+ * + * + * + */ +package com.google.cloud.logging.spi.v2; diff --git a/gcloud-java-logging/pom.xml b/gcloud-java-logging/pom.xml index 213c597648fa..02b004b681d7 100644 --- a/gcloud-java-logging/pom.xml +++ b/gcloud-java-logging/pom.xml @@ -1,18 +1,18 @@ 4.0.0 - com.google.gcloud - gcloud-java-logging - jar GCloud Java Logging Java idiomatic client for Google Cloud Logging. + com.google.gcloud + gcloud-java-logging com.google.gcloud gcloud-java-pom 0.1.4 + jar gcloud-java-logging @@ -20,27 +20,22 @@ com.google.api gax - 0.0.9 - - - com.google.api.grpc - grpc-core-proto - 0.0.2 + 0.0.13 com.google.api.grpc grpc-logging-type - 0.0.1 + 0.0.2 com.google.api.grpc grpc-logging-v2 - 0.0.1 + 0.0.2 io.grpc grpc-all - 0.9.0 + 0.12.0 com.google.auto.value @@ -54,6 +49,18 @@ test + + + doclint-java8-disable + + [1.8,) + + + + -Xdoclint:none + + + @@ -66,12 +73,41 @@ add-source - generated/src/main + generated/src/main/java + + org.apache.maven.plugins + maven-javadoc-plugin + 2.10.3 + + + attach-javadocs + + jar + + + ${javadoc.opts} + + + + + + maven-compiler-plugin + + + + 3.1 + + 1.7 + 1.7 + UTF-8 + -Xlint:unchecked + + diff --git a/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/ConfigServiceV2Api.java b/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/ConfigServiceV2Api.java index 350b8de61b1e..bc0eec93e22b 100644 --- a/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/ConfigServiceV2Api.java +++ b/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/ConfigServiceV2Api.java @@ -33,6 +33,7 @@ package com.google.cloud.logging.spi.v2; +import com.google.api.gax.core.PageAccessor; import com.google.api.gax.grpc.ApiCallable; import com.google.api.gax.protobuf.PathTemplate; import com.google.logging.v2.CreateSinkRequest; @@ -48,6 +49,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.ScheduledExecutorService; // Manually-added imports: add custom (non-generated) imports after this point. @@ -55,26 +57,80 @@ /** * Service Description: * + *

This class provides the ability to make remote calls to the backing service through method + * calls that map to API methods. Sample code to get started: + * + *

+ * 
+ * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+ *   String formattedProjectName = ConfigServiceV2Api.formatProjectName("[PROJECT]");
+ *   ListSinksResponse response = configServiceV2Api.listSinks(formattedProjectName);
+ * }
+ * 
+ * 
+ * + *

Note: close() needs to be called on the configServiceV2Api object to clean up resources such + * as threads. In the example above, try-with-resources is used, which automatically calls + * close(). + * + *

The surface of this class includes several types of Java methods for each of the API's methods: + * + *

    + *
  1. A "flattened" method. With this type of method, the fields of the request type have been + * converted into function parameters. It may be the case that not all fields are available + * as parameters, and not every API method will have a flattened method entry point. + *
  2. A "request object" method. This type of method only takes one parameter, a request + * object, which must be constructed before the call. Not every API method will have a request + * object method. + *
  3. A "callable" method. This type of method takes no parameters and returns an immutable + * ApiCallable object, which can be used to initiate calls to the service. + *
+ * + *

See the individual methods for example code. + * + *

Many parameters require resource names to be formatted in a particular way. To assist + * with these names, this class includes a format method for each type of name, and additionally + * a parse method to extract the individual identifiers contained within names that are + * returned. + * + *

This class can be customized by passing in a custom instance of ConfigServiceV2Settings to + * create(). For example: + * + *

+ * 
+ * ConfigServiceV2Settings configServiceV2Settings = ConfigServiceV2Settings.defaultBuilder()
+ *     .provideChannelWith(myCredentials)
+ *     .build();
+ * ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.create(configServiceV2Settings);
+ * 
+ * 
+ * * * */ @javax.annotation.Generated("by GAPIC") public class ConfigServiceV2Api implements AutoCloseable { + private final ConfigServiceV2Settings settings; private final ManagedChannel channel; + private final ScheduledExecutorService executor; private final List closeables = new ArrayList<>(); private final ApiCallable listSinksCallable; - private final ApiCallable> listSinksIterableCallable; + private final ApiCallable> listSinksPagedCallable; private final ApiCallable getSinkCallable; private final ApiCallable createSinkCallable; private final ApiCallable updateSinkCallable; private final ApiCallable deleteSinkCallable; + public final ConfigServiceV2Settings getSettings() { + return settings; + } + private static final PathTemplate PROJECT_PATH_TEMPLATE = - PathTemplate.create("projects/{project}"); + PathTemplate.createWithoutUrlEncoding("projects/{project}"); private static final PathTemplate SINK_PATH_TEMPLATE = - PathTemplate.create("projects/{project}/sinks/{sink}"); + PathTemplate.createWithoutUrlEncoding("projects/{project}/sinks/{sink}"); /** * Formats a string containing the fully-qualified path to represent @@ -137,8 +193,8 @@ public static final String parseSinkFromSinkName(String sinkName) { * * */ - public static final ConfigServiceV2Api defaultInstance() throws IOException { - return create(ConfigServiceV2Settings.defaultInstance()); + public static final ConfigServiceV2Api createWithDefaults() throws IOException { + return create(ConfigServiceV2Settings.defaultBuilder().build()); } /** @@ -163,17 +219,24 @@ public static final ConfigServiceV2Api create(ConfigServiceV2Settings settings) * */ protected ConfigServiceV2Api(ConfigServiceV2Settings settings) throws IOException { - this.channel = settings.getChannel(); - - this.listSinksCallable = ApiCallable.create(settings.listSinksSettings(), settings); - this.listSinksIterableCallable = - ApiCallable.createIterable(settings.listSinksSettings(), settings); - this.getSinkCallable = ApiCallable.create(settings.getSinkSettings(), settings); - this.createSinkCallable = ApiCallable.create(settings.createSinkSettings(), settings); - this.updateSinkCallable = ApiCallable.create(settings.updateSinkSettings(), settings); - this.deleteSinkCallable = ApiCallable.create(settings.deleteSinkSettings(), settings); - - if (settings.shouldAutoCloseChannel()) { + this.settings = settings; + this.executor = settings.getExecutorProvider().getOrBuildExecutor(); + this.channel = settings.getChannelProvider().getOrBuildChannel(this.executor); + + this.listSinksCallable = + ApiCallable.create(settings.listSinksSettings(), this.channel, this.executor); + this.listSinksPagedCallable = + ApiCallable.createPagedVariant(settings.listSinksSettings(), this.channel, this.executor); + this.getSinkCallable = + ApiCallable.create(settings.getSinkSettings(), this.channel, this.executor); + this.createSinkCallable = + ApiCallable.create(settings.createSinkSettings(), this.channel, this.executor); + this.updateSinkCallable = + ApiCallable.create(settings.updateSinkSettings(), this.channel, this.executor); + this.deleteSinkCallable = + ApiCallable.create(settings.deleteSinkSettings(), this.channel, this.executor); + + if (settings.getChannelProvider().shouldAutoClose()) { closeables.add( new Closeable() { @Override @@ -182,6 +245,15 @@ public void close() throws IOException { } }); } + if (settings.getExecutorProvider().shouldAutoClose()) { + closeables.add( + new Closeable() { + @Override + public void close() throws IOException { + executor.shutdown(); + } + }); + } } // ----- listSinks ----- @@ -190,14 +262,25 @@ public void close() throws IOException { /** * Lists sinks. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = ConfigServiceV2Api.formatProjectName("[PROJECT]");
+   *   for (LogSink elements : configServiceV2Api.listSinks(formattedProjectName)) {
+   *     // doThingsWith(elements);
+   *   }
+   * }
+   * 
+ * * * * * @param projectName Required. The resource name of the project containing the sinks. * Example: `"projects/my-logging-project"`, `"projects/01234567890"`. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ - public final Iterable listSinks(String projectName) { + public final PageAccessor listSinks(String projectName) { + PROJECT_PATH_TEMPLATE.validate(projectName); ListSinksRequest request = ListSinksRequest.newBuilder().setProjectName(projectName).build(); return listSinks(request); } @@ -206,35 +289,83 @@ public final Iterable listSinks(String projectName) { /** * Lists sinks. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = ConfigServiceV2Api.formatProjectName("[PROJECT]");
+   *   ListSinksRequest request = ListSinksRequest.newBuilder()
+   *     .setProjectName(formattedProjectName)
+   *     .build();
+   *   for (LogSink elements : configServiceV2Api.listSinks(request)) {
+   *     // doThingsWith(elements);
+   *   }
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ - public final Iterable listSinks(ListSinksRequest request) { - return listSinksIterableCallable().call(request); + public final PageAccessor listSinks(ListSinksRequest request) { + return listSinksPagedCallable().call(request); } // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. /** * Lists sinks. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = ConfigServiceV2Api.formatProjectName("[PROJECT]");
+   *   ListSinksRequest request = ListSinksRequest.newBuilder()
+   *     .setProjectName(formattedProjectName)
+   *     .build();
+   *   ListenableFuture<PageAccessor<LogSink>> future = configServiceV2Api.listSinksPagedCallable().futureCall(request);
+   *   // Do something
+   *   for (LogSink elements : future.get()) {
+   *     // doThingsWith(elements);
+   *   }
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ - public final ApiCallable> listSinksIterableCallable() { - return listSinksIterableCallable; + public final ApiCallable> listSinksPagedCallable() { + return listSinksPagedCallable; } // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. /** * Lists sinks. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = ConfigServiceV2Api.formatProjectName("[PROJECT]");
+   *   ListSinksRequest request = ListSinksRequest.newBuilder()
+   *     .setProjectName(formattedProjectName)
+   *     .build();
+   *   while (true) {
+   *     ListSinksResponse response = configServiceV2Api.listSinksCallable().call(request);
+   *     for (LogSink elements : response.getSinksList()) {
+   *       // doThingsWith(elements);
+   *     }
+   *     String nextPageToken = response.getNextPageToken();
+   *     if (!Strings.isNullOrEmpty(nextPageToken)) {
+   *       request = request.toBuilder().setPageToken(nextPageToken).build();
+   *     } else {
+   *       break;
+   *     }
+   *   }
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable listSinksCallable() { return listSinksCallable; @@ -246,16 +377,24 @@ public final ApiCallable listSinksCallable( /** * Gets a sink. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedSinkName = ConfigServiceV2Api.formatSinkName("[PROJECT]", "[SINK]");
+   *   LogSink response = configServiceV2Api.getSink(formattedSinkName);
+   * }
+   * 
+ * * * * * @param sinkName The resource name of the sink to return. * Example: `"projects/my-project-id/sinks/my-sink-id"`. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public final LogSink getSink(String sinkName) { + SINK_PATH_TEMPLATE.validate(sinkName); GetSinkRequest request = GetSinkRequest.newBuilder().setSinkName(sinkName).build(); - return getSink(request); } @@ -263,11 +402,22 @@ public final LogSink getSink(String sinkName) { /** * Gets a sink. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedSinkName = ConfigServiceV2Api.formatSinkName("[PROJECT]", "[SINK]");
+   *   GetSinkRequest request = GetSinkRequest.newBuilder()
+   *     .setSinkName(formattedSinkName)
+   *     .build();
+   *   LogSink response = configServiceV2Api.getSink(request);
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ private LogSink getSink(GetSinkRequest request) { return getSinkCallable().call(request); @@ -277,9 +427,21 @@ private LogSink getSink(GetSinkRequest request) { /** * Gets a sink. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedSinkName = ConfigServiceV2Api.formatSinkName("[PROJECT]", "[SINK]");
+   *   GetSinkRequest request = GetSinkRequest.newBuilder()
+   *     .setSinkName(formattedSinkName)
+   *     .build();
+   *   ListenableFuture<LogSink> future = configServiceV2Api.getSinkCallable().futureCall(request);
+   *   // Do something
+   *   LogSink response = future.get();
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable getSinkCallable() { return getSinkCallable; @@ -291,6 +453,15 @@ public final ApiCallable getSinkCallable() { /** * Creates a sink. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = ConfigServiceV2Api.formatProjectName("[PROJECT]");
+   *   LogSink sink = LogSink.newBuilder().build();
+   *   LogSink response = configServiceV2Api.createSink(formattedProjectName, sink);
+   * }
+   * 
+ * * * * @@ -300,12 +471,13 @@ public final ApiCallable getSinkCallable() { * The new sink must be provided in the request. * @param sink The new sink, which must not have an identifier that already * exists. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public final LogSink createSink(String projectName, LogSink sink) { + PROJECT_PATH_TEMPLATE.validate(projectName); + CreateSinkRequest request = CreateSinkRequest.newBuilder().setProjectName(projectName).setSink(sink).build(); - return createSink(request); } @@ -313,11 +485,24 @@ public final LogSink createSink(String projectName, LogSink sink) { /** * Creates a sink. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = ConfigServiceV2Api.formatProjectName("[PROJECT]");
+   *   LogSink sink = LogSink.newBuilder().build();
+   *   CreateSinkRequest request = CreateSinkRequest.newBuilder()
+   *     .setProjectName(formattedProjectName)
+   *     .setSink(sink)
+   *     .build();
+   *   LogSink response = configServiceV2Api.createSink(request);
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public LogSink createSink(CreateSinkRequest request) { return createSinkCallable().call(request); @@ -327,9 +512,23 @@ public LogSink createSink(CreateSinkRequest request) { /** * Creates a sink. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = ConfigServiceV2Api.formatProjectName("[PROJECT]");
+   *   LogSink sink = LogSink.newBuilder().build();
+   *   CreateSinkRequest request = CreateSinkRequest.newBuilder()
+   *     .setProjectName(formattedProjectName)
+   *     .setSink(sink)
+   *     .build();
+   *   ListenableFuture<LogSink> future = configServiceV2Api.createSinkCallable().futureCall(request);
+   *   // Do something
+   *   LogSink response = future.get();
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable createSinkCallable() { return createSinkCallable; @@ -341,6 +540,15 @@ public final ApiCallable createSinkCallable() { /** * Creates or updates a sink. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedSinkName = ConfigServiceV2Api.formatSinkName("[PROJECT]", "[SINK]");
+   *   LogSink sink = LogSink.newBuilder().build();
+   *   LogSink response = configServiceV2Api.updateSink(formattedSinkName, sink);
+   * }
+   * 
+ * * * * @@ -353,12 +561,13 @@ public final ApiCallable createSinkCallable() { * @param sink The updated sink, whose name must be the same as the sink * identifier in `sinkName`. If `sinkName` does not exist, then * this method creates a new sink. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public final LogSink updateSink(String sinkName, LogSink sink) { + SINK_PATH_TEMPLATE.validate(sinkName); + UpdateSinkRequest request = UpdateSinkRequest.newBuilder().setSinkName(sinkName).setSink(sink).build(); - return updateSink(request); } @@ -366,11 +575,24 @@ public final LogSink updateSink(String sinkName, LogSink sink) { /** * Creates or updates a sink. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedSinkName = ConfigServiceV2Api.formatSinkName("[PROJECT]", "[SINK]");
+   *   LogSink sink = LogSink.newBuilder().build();
+   *   UpdateSinkRequest request = UpdateSinkRequest.newBuilder()
+   *     .setSinkName(formattedSinkName)
+   *     .setSink(sink)
+   *     .build();
+   *   LogSink response = configServiceV2Api.updateSink(request);
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public LogSink updateSink(UpdateSinkRequest request) { return updateSinkCallable().call(request); @@ -380,9 +602,23 @@ public LogSink updateSink(UpdateSinkRequest request) { /** * Creates or updates a sink. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedSinkName = ConfigServiceV2Api.formatSinkName("[PROJECT]", "[SINK]");
+   *   LogSink sink = LogSink.newBuilder().build();
+   *   UpdateSinkRequest request = UpdateSinkRequest.newBuilder()
+   *     .setSinkName(formattedSinkName)
+   *     .setSink(sink)
+   *     .build();
+   *   ListenableFuture<LogSink> future = configServiceV2Api.updateSinkCallable().futureCall(request);
+   *   // Do something
+   *   LogSink response = future.get();
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable updateSinkCallable() { return updateSinkCallable; @@ -394,16 +630,24 @@ public final ApiCallable updateSinkCallable() { /** * Deletes a sink. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedSinkName = ConfigServiceV2Api.formatSinkName("[PROJECT]", "[SINK]");
+   *   configServiceV2Api.deleteSink(formattedSinkName);
+   * }
+   * 
+ * * * * * @param sinkName The resource name of the sink to delete. * Example: `"projects/my-project-id/sinks/my-sink-id"`. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public final void deleteSink(String sinkName) { + SINK_PATH_TEMPLATE.validate(sinkName); DeleteSinkRequest request = DeleteSinkRequest.newBuilder().setSinkName(sinkName).build(); - deleteSink(request); } @@ -411,11 +655,22 @@ public final void deleteSink(String sinkName) { /** * Deletes a sink. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedSinkName = ConfigServiceV2Api.formatSinkName("[PROJECT]", "[SINK]");
+   *   DeleteSinkRequest request = DeleteSinkRequest.newBuilder()
+   *     .setSinkName(formattedSinkName)
+   *     .build();
+   *   configServiceV2Api.deleteSink(request);
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ private void deleteSink(DeleteSinkRequest request) { deleteSinkCallable().call(request); @@ -425,9 +680,21 @@ private void deleteSink(DeleteSinkRequest request) { /** * Deletes a sink. * + * Sample code: + *

+   * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+   *   String formattedSinkName = ConfigServiceV2Api.formatSinkName("[PROJECT]", "[SINK]");
+   *   DeleteSinkRequest request = DeleteSinkRequest.newBuilder()
+   *     .setSinkName(formattedSinkName)
+   *     .build();
+   *   ListenableFuture<Void> future = configServiceV2Api.deleteSinkCallable().futureCall(request);
+   *   // Do something
+   *   future.get();
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable deleteSinkCallable() { return deleteSinkCallable; diff --git a/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/ConfigServiceV2Settings.java b/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/ConfigServiceV2Settings.java index 157f4b5d2427..9c1558fec565 100644 --- a/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/ConfigServiceV2Settings.java +++ b/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/ConfigServiceV2Settings.java @@ -40,6 +40,7 @@ import com.google.api.gax.grpc.PageStreamingDescriptor; import com.google.api.gax.grpc.ServiceApiSettings; import com.google.api.gax.grpc.SimpleCallSettings; +import com.google.auth.Credentials; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -57,12 +58,39 @@ import io.grpc.ManagedChannel; import io.grpc.Status; import java.io.IOException; +import java.util.List; import java.util.concurrent.ScheduledExecutorService; import org.joda.time.Duration; // Manually-added imports: add custom (non-generated) imports after this point. // AUTO-GENERATED DOCUMENTATION AND CLASS - see instructions at the top of the file for editing. +/** + * Settings class to configure an instance of {@link ConfigServiceV2Api}. + * + *

The default instance has everything set to sensible defaults: + * + *

    + *
  • The default service address (logging.googleapis.com) and default port (443) + * are used. + *
  • Credentials are acquired automatically through Application Default Credentials. + *
  • Retries are configured for idempotent methods but not for non-idempotent methods. + *
+ * + *

The builder of this class is recursive, so contained classes are themselves builders. + * When build() is called, the tree of builders is called to create the complete settings + * object. For example, to set the total timeout of ListSinks to 30 seconds: + * + *

+ * 
+ * ConfigServiceV2Settings.Builder configServiceV2SettingsBuilder =
+ *     ConfigServiceV2Settings.defaultBuilder();
+ * configServiceV2SettingsBuilder.ListSinksSettings().getRetrySettingsBuilder()
+ *     .setTotalTimeout(Duration.standardSeconds(30));
+ * ConfigServiceV2Settings configServiceV2Settings = configServiceV2SettingsBuilder.build();
+ * 
+ * 
+ */ @javax.annotation.Generated("by GAPIC") public class ConfigServiceV2Settings extends ServiceApiSettings { @@ -94,6 +122,16 @@ public class ConfigServiceV2Settings extends ServiceApiSettings { .add("https://www.googleapis.com/auth/cloud-platform") .build(); + /** + * The default connection settings of the service. + */ + public static final ConnectionSettings DEFAULT_CONNECTION_SETTINGS = + ConnectionSettings.newBuilder() + .setServiceAddress(DEFAULT_SERVICE_ADDRESS) + .setPort(DEFAULT_SERVICE_PORT) + .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) + .build(); + private final PageStreamingCallSettings listSinksSettings; @@ -102,45 +140,67 @@ public class ConfigServiceV2Settings extends ServiceApiSettings { private final SimpleCallSettings updateSinkSettings; private final SimpleCallSettings deleteSinkSettings; + /** + * Returns the object with the settings used for calls to listSinks. + */ public PageStreamingCallSettings listSinksSettings() { return listSinksSettings; } + /** + * Returns the object with the settings used for calls to getSink. + */ public SimpleCallSettings getSinkSettings() { return getSinkSettings; } + /** + * Returns the object with the settings used for calls to createSink. + */ public SimpleCallSettings createSinkSettings() { return createSinkSettings; } + /** + * Returns the object with the settings used for calls to updateSink. + */ public SimpleCallSettings updateSinkSettings() { return updateSinkSettings; } + /** + * Returns the object with the settings used for calls to deleteSink. + */ public SimpleCallSettings deleteSinkSettings() { return deleteSinkSettings; } - public static ConfigServiceV2Settings defaultInstance() throws IOException { - return newBuilder().build(); + /** + * Returns a builder for this class with recommended defaults. + */ + public static Builder defaultBuilder() { + return Builder.createDefault(); } + /** + * Returns a new builder for this class. + */ public static Builder newBuilder() { return new Builder(); } + /** + * Returns a builder containing all the values of this settings class. + */ public Builder toBuilder() { return new Builder(this); } private ConfigServiceV2Settings(Builder settingsBuilder) throws IOException { super( - settingsBuilder.getOrBuildChannel(), - settingsBuilder.shouldAutoCloseChannel(), - settingsBuilder.getOrBuildExecutor(), - settingsBuilder.getConnectionSettings(), + settingsBuilder.getChannelProvider(), + settingsBuilder.getExecutorProvider(), settingsBuilder.getGeneratorName(), settingsBuilder.getGeneratorVersion(), settingsBuilder.getClientLibName(), @@ -177,6 +237,9 @@ public Iterable extractResources(ListSinksResponse payload) { } }; + /** + * Builder for ConfigServiceV2Settings. + */ public static class Builder extends ServiceApiSettings.Builder { private final ImmutableList methodSettingsBuilders; @@ -219,38 +282,19 @@ public static class Builder extends ServiceApiSettings.Builder { } private Builder() { - super( - ConnectionSettings.builder() - .setServiceAddress(DEFAULT_SERVICE_ADDRESS) - .setPort(DEFAULT_SERVICE_PORT) - .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) - .build()); + super(DEFAULT_CONNECTION_SETTINGS); listSinksSettings = PageStreamingCallSettings.newBuilder( - ConfigServiceV2Grpc.METHOD_LIST_SINKS, LIST_SINKS_PAGE_STR_DESC) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); - - getSinkSettings = - SimpleCallSettings.newBuilder(ConfigServiceV2Grpc.METHOD_GET_SINK) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); - - createSinkSettings = - SimpleCallSettings.newBuilder(ConfigServiceV2Grpc.METHOD_CREATE_SINK) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); - - updateSinkSettings = - SimpleCallSettings.newBuilder(ConfigServiceV2Grpc.METHOD_UPDATE_SINK) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); - - deleteSinkSettings = - SimpleCallSettings.newBuilder(ConfigServiceV2Grpc.METHOD_DELETE_SINK) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + ConfigServiceV2Grpc.METHOD_LIST_SINKS, LIST_SINKS_PAGE_STR_DESC); + + getSinkSettings = SimpleCallSettings.newBuilder(ConfigServiceV2Grpc.METHOD_GET_SINK); + + createSinkSettings = SimpleCallSettings.newBuilder(ConfigServiceV2Grpc.METHOD_CREATE_SINK); + + updateSinkSettings = SimpleCallSettings.newBuilder(ConfigServiceV2Grpc.METHOD_UPDATE_SINK); + + deleteSinkSettings = SimpleCallSettings.newBuilder(ConfigServiceV2Grpc.METHOD_DELETE_SINK); methodSettingsBuilders = ImmutableList.of( @@ -261,6 +305,36 @@ private Builder() { deleteSinkSettings); } + private static Builder createDefault() { + Builder builder = new Builder(); + builder + .listSinksSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .getSinkSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .createSinkSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .updateSinkSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .deleteSinkSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + return builder; + } + private Builder(ConfigServiceV2Settings settings) { super(settings); @@ -279,6 +353,17 @@ private Builder(ConfigServiceV2Settings settings) { deleteSinkSettings); } + @Override + protected ConnectionSettings getDefaultConnectionSettings() { + return DEFAULT_CONNECTION_SETTINGS; + } + + @Override + public Builder provideExecutorWith(ScheduledExecutorService executor, boolean shouldAutoClose) { + super.provideExecutorWith(executor, shouldAutoClose); + return this; + } + @Override public Builder provideChannelWith(ManagedChannel channel, boolean shouldAutoClose) { super.provideChannelWith(channel, shouldAutoClose); @@ -292,8 +377,14 @@ public Builder provideChannelWith(ConnectionSettings settings) { } @Override - public Builder setExecutor(ScheduledExecutorService executor) { - super.setExecutor(executor); + public Builder provideChannelWith(Credentials credentials) { + super.provideChannelWith(credentials); + return this; + } + + @Override + public Builder provideChannelWith(List scopes) { + super.provideChannelWith(scopes); return this; } @@ -309,28 +400,48 @@ public Builder setClientLibHeader(String name, String version) { return this; } + /** + * Applies the given settings to all of the API methods in this service. Only + * values that are non-null will be applied, so this method is not capable + * of un-setting any values. + */ public Builder applyToAllApiMethods(ApiCallSettings.Builder apiCallSettings) throws Exception { super.applyToAllApiMethods(methodSettingsBuilders, apiCallSettings); return this; } + /** + * Returns the builder for the settings used for calls to listSinks. + */ public PageStreamingCallSettings.Builder listSinksSettings() { return listSinksSettings; } + /** + * Returns the builder for the settings used for calls to getSink. + */ public SimpleCallSettings.Builder getSinkSettings() { return getSinkSettings; } + /** + * Returns the builder for the settings used for calls to createSink. + */ public SimpleCallSettings.Builder createSinkSettings() { return createSinkSettings; } + /** + * Returns the builder for the settings used for calls to updateSink. + */ public SimpleCallSettings.Builder updateSinkSettings() { return updateSinkSettings; } + /** + * Returns the builder for the settings used for calls to deleteSink. + */ public SimpleCallSettings.Builder deleteSinkSettings() { return deleteSinkSettings; } diff --git a/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/LoggingServiceV2Api.java b/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/LoggingServiceV2Api.java index ead7c1b886aa..70960a025987 100644 --- a/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/LoggingServiceV2Api.java +++ b/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/LoggingServiceV2Api.java @@ -35,6 +35,7 @@ import com.google.api.MonitoredResource; import com.google.api.MonitoredResourceDescriptor; +import com.google.api.gax.core.PageAccessor; import com.google.api.gax.grpc.ApiCallable; import com.google.api.gax.protobuf.PathTemplate; import com.google.logging.v2.DeleteLogRequest; @@ -52,6 +53,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.concurrent.ScheduledExecutorService; // Manually-added imports: add custom (non-generated) imports after this point. @@ -59,32 +61,86 @@ /** * Service Description: Service for ingesting and querying logs. * + *

This class provides the ability to make remote calls to the backing service through method + * calls that map to API methods. Sample code to get started: + * + *

+ * 
+ * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+ *   String formattedLogName = LoggingServiceV2Api.formatLogName("[PROJECT]", "[LOG]");
+ *   loggingServiceV2Api.deleteLog(formattedLogName);
+ * }
+ * 
+ * 
+ * + *

Note: close() needs to be called on the loggingServiceV2Api object to clean up resources such + * as threads. In the example above, try-with-resources is used, which automatically calls + * close(). + * + *

The surface of this class includes several types of Java methods for each of the API's methods: + * + *

    + *
  1. A "flattened" method. With this type of method, the fields of the request type have been + * converted into function parameters. It may be the case that not all fields are available + * as parameters, and not every API method will have a flattened method entry point. + *
  2. A "request object" method. This type of method only takes one parameter, a request + * object, which must be constructed before the call. Not every API method will have a request + * object method. + *
  3. A "callable" method. This type of method takes no parameters and returns an immutable + * ApiCallable object, which can be used to initiate calls to the service. + *
+ * + *

See the individual methods for example code. + * + *

Many parameters require resource names to be formatted in a particular way. To assist + * with these names, this class includes a format method for each type of name, and additionally + * a parse method to extract the individual identifiers contained within names that are + * returned. + * + *

This class can be customized by passing in a custom instance of LoggingServiceV2Settings to + * create(). For example: + * + *

+ * 
+ * LoggingServiceV2Settings loggingServiceV2Settings = LoggingServiceV2Settings.defaultBuilder()
+ *     .provideChannelWith(myCredentials)
+ *     .build();
+ * LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.create(loggingServiceV2Settings);
+ * 
+ * 
+ * * * */ @javax.annotation.Generated("by GAPIC") public class LoggingServiceV2Api implements AutoCloseable { + private final LoggingServiceV2Settings settings; private final ManagedChannel channel; + private final ScheduledExecutorService executor; private final List closeables = new ArrayList<>(); private final ApiCallable deleteLogCallable; private final ApiCallable writeLogEntriesCallable; private final ApiCallable listLogEntriesCallable; - private final ApiCallable> - listLogEntriesIterableCallable; + private final ApiCallable> + listLogEntriesPagedCallable; private final ApiCallable< ListMonitoredResourceDescriptorsRequest, ListMonitoredResourceDescriptorsResponse> listMonitoredResourceDescriptorsCallable; private final ApiCallable< - ListMonitoredResourceDescriptorsRequest, Iterable> - listMonitoredResourceDescriptorsIterableCallable; + ListMonitoredResourceDescriptorsRequest, PageAccessor> + listMonitoredResourceDescriptorsPagedCallable; + + public final LoggingServiceV2Settings getSettings() { + return settings; + } private static final PathTemplate PROJECT_PATH_TEMPLATE = - PathTemplate.create("projects/{project}"); + PathTemplate.createWithoutUrlEncoding("projects/{project}"); private static final PathTemplate LOG_PATH_TEMPLATE = - PathTemplate.create("projects/{project}/logs/{log}"); + PathTemplate.createWithoutUrlEncoding("projects/{project}/logs/{log}"); /** * Formats a string containing the fully-qualified path to represent @@ -147,8 +203,8 @@ public static final String parseLogFromLogName(String logName) { * * */ - public static final LoggingServiceV2Api defaultInstance() throws IOException { - return create(LoggingServiceV2Settings.defaultInstance()); + public static final LoggingServiceV2Api createWithDefaults() throws IOException { + return create(LoggingServiceV2Settings.defaultBuilder().build()); } /** @@ -173,19 +229,27 @@ public static final LoggingServiceV2Api create(LoggingServiceV2Settings settings * */ protected LoggingServiceV2Api(LoggingServiceV2Settings settings) throws IOException { - this.channel = settings.getChannel(); - - this.deleteLogCallable = ApiCallable.create(settings.deleteLogSettings(), settings); - this.writeLogEntriesCallable = ApiCallable.create(settings.writeLogEntriesSettings(), settings); - this.listLogEntriesCallable = ApiCallable.create(settings.listLogEntriesSettings(), settings); - this.listLogEntriesIterableCallable = - ApiCallable.createIterable(settings.listLogEntriesSettings(), settings); + this.settings = settings; + this.executor = settings.getExecutorProvider().getOrBuildExecutor(); + this.channel = settings.getChannelProvider().getOrBuildChannel(this.executor); + + this.deleteLogCallable = + ApiCallable.create(settings.deleteLogSettings(), this.channel, this.executor); + this.writeLogEntriesCallable = + ApiCallable.create(settings.writeLogEntriesSettings(), this.channel, this.executor); + this.listLogEntriesCallable = + ApiCallable.create(settings.listLogEntriesSettings(), this.channel, this.executor); + this.listLogEntriesPagedCallable = + ApiCallable.createPagedVariant( + settings.listLogEntriesSettings(), this.channel, this.executor); this.listMonitoredResourceDescriptorsCallable = - ApiCallable.create(settings.listMonitoredResourceDescriptorsSettings(), settings); - this.listMonitoredResourceDescriptorsIterableCallable = - ApiCallable.createIterable(settings.listMonitoredResourceDescriptorsSettings(), settings); + ApiCallable.create( + settings.listMonitoredResourceDescriptorsSettings(), this.channel, this.executor); + this.listMonitoredResourceDescriptorsPagedCallable = + ApiCallable.createPagedVariant( + settings.listMonitoredResourceDescriptorsSettings(), this.channel, this.executor); - if (settings.shouldAutoCloseChannel()) { + if (settings.getChannelProvider().shouldAutoClose()) { closeables.add( new Closeable() { @Override @@ -194,6 +258,15 @@ public void close() throws IOException { } }); } + if (settings.getExecutorProvider().shouldAutoClose()) { + closeables.add( + new Closeable() { + @Override + public void close() throws IOException { + executor.shutdown(); + } + }); + } } // ----- deleteLog ----- @@ -203,16 +276,24 @@ public void close() throws IOException { * Deletes a log and all its log entries. * The log will reappear if it receives new entries. * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   String formattedLogName = LoggingServiceV2Api.formatLogName("[PROJECT]", "[LOG]");
+   *   loggingServiceV2Api.deleteLog(formattedLogName);
+   * }
+   * 
+ * * * * * @param logName Required. The resource name of the log to delete. Example: * `"projects/my-project/logs/syslog"`. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public final void deleteLog(String logName) { + LOG_PATH_TEMPLATE.validate(logName); DeleteLogRequest request = DeleteLogRequest.newBuilder().setLogName(logName).build(); - deleteLog(request); } @@ -221,11 +302,22 @@ public final void deleteLog(String logName) { * Deletes a log and all its log entries. * The log will reappear if it receives new entries. * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   String formattedLogName = LoggingServiceV2Api.formatLogName("[PROJECT]", "[LOG]");
+   *   DeleteLogRequest request = DeleteLogRequest.newBuilder()
+   *     .setLogName(formattedLogName)
+   *     .build();
+   *   loggingServiceV2Api.deleteLog(request);
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ private void deleteLog(DeleteLogRequest request) { deleteLogCallable().call(request); @@ -236,9 +328,21 @@ private void deleteLog(DeleteLogRequest request) { * Deletes a log and all its log entries. * The log will reappear if it receives new entries. * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   String formattedLogName = LoggingServiceV2Api.formatLogName("[PROJECT]", "[LOG]");
+   *   DeleteLogRequest request = DeleteLogRequest.newBuilder()
+   *     .setLogName(formattedLogName)
+   *     .build();
+   *   ListenableFuture<Void> future = loggingServiceV2Api.deleteLogCallable().futureCall(request);
+   *   // Do something
+   *   future.get();
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable deleteLogCallable() { return deleteLogCallable; @@ -251,6 +355,17 @@ public final ApiCallable deleteLogCallable() { * Writes log entries to Cloud Logging. * All log entries in Cloud Logging are written by this method. * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   String logName = "";
+   *   MonitoredResource resource = MonitoredResource.newBuilder().build();
+   *   Map<String, String> labels = new HashMap<>();
+   *   List<LogEntry> entries = new ArrayList<>();
+   *   WriteLogEntriesResponse response = loggingServiceV2Api.writeLogEntries(logName, resource, labels, entries);
+   * }
+   * 
+ * * * * @@ -266,13 +381,15 @@ public final ApiCallable deleteLogCallable() { * Example: `{ "size": "large", "color":"red" }` * @param entries Required. The log entries to write. The log entries must have values for * all required fields. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public final WriteLogEntriesResponse writeLogEntries( String logName, MonitoredResource resource, Map labels, List entries) { + LOG_PATH_TEMPLATE.validate(logName); + WriteLogEntriesRequest request = WriteLogEntriesRequest.newBuilder() .setLogName(logName) @@ -280,7 +397,6 @@ public final WriteLogEntriesResponse writeLogEntries( .putAllLabels(labels) .addAllEntries(entries) .build(); - return writeLogEntries(request); } @@ -289,11 +405,22 @@ public final WriteLogEntriesResponse writeLogEntries( * Writes log entries to Cloud Logging. * All log entries in Cloud Logging are written by this method. * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   List<LogEntry> entries = new ArrayList<>();
+   *   WriteLogEntriesRequest request = WriteLogEntriesRequest.newBuilder()
+   *     .addAllEntries(entries)
+   *     .build();
+   *   WriteLogEntriesResponse response = loggingServiceV2Api.writeLogEntries(request);
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public WriteLogEntriesResponse writeLogEntries(WriteLogEntriesRequest request) { return writeLogEntriesCallable().call(request); @@ -304,9 +431,21 @@ public WriteLogEntriesResponse writeLogEntries(WriteLogEntriesRequest request) { * Writes log entries to Cloud Logging. * All log entries in Cloud Logging are written by this method. * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   List<LogEntry> entries = new ArrayList<>();
+   *   WriteLogEntriesRequest request = WriteLogEntriesRequest.newBuilder()
+   *     .addAllEntries(entries)
+   *     .build();
+   *   ListenableFuture<WriteLogEntriesResponse> future = loggingServiceV2Api.writeLogEntriesCallable().futureCall(request);
+   *   // Do something
+   *   WriteLogEntriesResponse response = future.get();
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable writeLogEntriesCallable() { @@ -321,6 +460,18 @@ public WriteLogEntriesResponse writeLogEntries(WriteLogEntriesRequest request) { * Logging. For ways to export log entries, see * [Exporting Logs](/logging/docs/export). * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   List<String> projectIds = new ArrayList<>();
+   *   String filter = "";
+   *   String orderBy = "";
+   *   for (LogEntry elements : loggingServiceV2Api.listLogEntries(projectIds, filter, orderBy)) {
+   *     // doThingsWith(elements);
+   *   }
+   * }
+   * 
+ * * * * @@ -336,10 +487,11 @@ public WriteLogEntriesResponse writeLogEntries(WriteLogEntriesRequest request) { * `LogEntry.timestamp` (oldest first), and the second option returns entries * in order of decreasing timestamps (newest first). Entries with equal * timestamps are returned in order of `LogEntry.insertId`. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ - public final Iterable listLogEntries( + public final PageAccessor listLogEntries( List projectIds, String filter, String orderBy) { + ListLogEntriesRequest request = ListLogEntriesRequest.newBuilder() .addAllProjectIds(projectIds) @@ -355,14 +507,27 @@ public final Iterable listLogEntries( * Logging. For ways to export log entries, see * [Exporting Logs](/logging/docs/export). * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   List<String> projectIds = new ArrayList<>();
+   *   ListLogEntriesRequest request = ListLogEntriesRequest.newBuilder()
+   *     .addAllProjectIds(projectIds)
+   *     .build();
+   *   for (LogEntry elements : loggingServiceV2Api.listLogEntries(request)) {
+   *     // doThingsWith(elements);
+   *   }
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ - public final Iterable listLogEntries(ListLogEntriesRequest request) { - return listLogEntriesIterableCallable().call(request); + public final PageAccessor listLogEntries(ListLogEntriesRequest request) { + return listLogEntriesPagedCallable().call(request); } // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. @@ -371,13 +536,27 @@ public final Iterable listLogEntries(ListLogEntriesRequest request) { * Logging. For ways to export log entries, see * [Exporting Logs](/logging/docs/export). * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   List<String> projectIds = new ArrayList<>();
+   *   ListLogEntriesRequest request = ListLogEntriesRequest.newBuilder()
+   *     .addAllProjectIds(projectIds)
+   *     .build();
+   *   ListenableFuture<PageAccessor<LogEntry>> future = loggingServiceV2Api.listLogEntriesPagedCallable().futureCall(request);
+   *   // Do something
+   *   for (LogEntry elements : future.get()) {
+   *     // doThingsWith(elements);
+   *   }
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ - public final ApiCallable> - listLogEntriesIterableCallable() { - return listLogEntriesIterableCallable; + public final ApiCallable> + listLogEntriesPagedCallable() { + return listLogEntriesPagedCallable; } // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. @@ -386,9 +565,30 @@ public final Iterable listLogEntries(ListLogEntriesRequest request) { * Logging. For ways to export log entries, see * [Exporting Logs](/logging/docs/export). * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   List<String> projectIds = new ArrayList<>();
+   *   ListLogEntriesRequest request = ListLogEntriesRequest.newBuilder()
+   *     .addAllProjectIds(projectIds)
+   *     .build();
+   *   while (true) {
+   *     ListLogEntriesResponse response = loggingServiceV2Api.listLogEntriesCallable().call(request);
+   *     for (LogEntry elements : response.getEntriesList()) {
+   *       // doThingsWith(elements);
+   *     }
+   *     String nextPageToken = response.getNextPageToken();
+   *     if (!Strings.isNullOrEmpty(nextPageToken)) {
+   *       request = request.toBuilder().setPageToken(nextPageToken).build();
+   *     } else {
+   *       break;
+   *     }
+   *   }
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable listLogEntriesCallable() { return listLogEntriesCallable; @@ -400,38 +600,80 @@ public final ApiCallable listLogE /** * Lists monitored resource descriptors that are used by Cloud Logging. * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   ListMonitoredResourceDescriptorsRequest request = ListMonitoredResourceDescriptorsRequest.newBuilder()
+   *     .build();
+   *   for (MonitoredResourceDescriptor elements : loggingServiceV2Api.listMonitoredResourceDescriptors(request)) {
+   *     // doThingsWith(elements);
+   *   }
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ - public final Iterable listMonitoredResourceDescriptors( + public final PageAccessor listMonitoredResourceDescriptors( ListMonitoredResourceDescriptorsRequest request) { - return listMonitoredResourceDescriptorsIterableCallable().call(request); + return listMonitoredResourceDescriptorsPagedCallable().call(request); } // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. /** * Lists monitored resource descriptors that are used by Cloud Logging. * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   ListMonitoredResourceDescriptorsRequest request = ListMonitoredResourceDescriptorsRequest.newBuilder()
+   *     .build();
+   *   ListenableFuture<PageAccessor<MonitoredResourceDescriptor>> future = loggingServiceV2Api.listMonitoredResourceDescriptorsPagedCallable().futureCall(request);
+   *   // Do something
+   *   for (MonitoredResourceDescriptor elements : future.get()) {
+   *     // doThingsWith(elements);
+   *   }
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable< - ListMonitoredResourceDescriptorsRequest, Iterable> - listMonitoredResourceDescriptorsIterableCallable() { - return listMonitoredResourceDescriptorsIterableCallable; + ListMonitoredResourceDescriptorsRequest, PageAccessor> + listMonitoredResourceDescriptorsPagedCallable() { + return listMonitoredResourceDescriptorsPagedCallable; } // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. /** * Lists monitored resource descriptors that are used by Cloud Logging. * + * Sample code: + *

+   * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+   *   ListMonitoredResourceDescriptorsRequest request = ListMonitoredResourceDescriptorsRequest.newBuilder()
+   *     .build();
+   *   while (true) {
+   *     ListMonitoredResourceDescriptorsResponse response = loggingServiceV2Api.listMonitoredResourceDescriptorsCallable().call(request);
+   *     for (MonitoredResourceDescriptor elements : response.getResourceDescriptorsList()) {
+   *       // doThingsWith(elements);
+   *     }
+   *     String nextPageToken = response.getNextPageToken();
+   *     if (!Strings.isNullOrEmpty(nextPageToken)) {
+   *       request = request.toBuilder().setPageToken(nextPageToken).build();
+   *     } else {
+   *       break;
+   *     }
+   *   }
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable< ListMonitoredResourceDescriptorsRequest, ListMonitoredResourceDescriptorsResponse> diff --git a/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/LoggingServiceV2Settings.java b/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/LoggingServiceV2Settings.java index da6ae99fd9a7..c37e5c4ad305 100644 --- a/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/LoggingServiceV2Settings.java +++ b/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/LoggingServiceV2Settings.java @@ -41,6 +41,7 @@ import com.google.api.gax.grpc.PageStreamingDescriptor; import com.google.api.gax.grpc.ServiceApiSettings; import com.google.api.gax.grpc.SimpleCallSettings; +import com.google.auth.Credentials; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -59,12 +60,39 @@ import io.grpc.ManagedChannel; import io.grpc.Status; import java.io.IOException; +import java.util.List; import java.util.concurrent.ScheduledExecutorService; import org.joda.time.Duration; // Manually-added imports: add custom (non-generated) imports after this point. // AUTO-GENERATED DOCUMENTATION AND CLASS - see instructions at the top of the file for editing. +/** + * Settings class to configure an instance of {@link LoggingServiceV2Api}. + * + *

The default instance has everything set to sensible defaults: + * + *

    + *
  • The default service address (logging.googleapis.com) and default port (443) + * are used. + *
  • Credentials are acquired automatically through Application Default Credentials. + *
  • Retries are configured for idempotent methods but not for non-idempotent methods. + *
+ * + *

The builder of this class is recursive, so contained classes are themselves builders. + * When build() is called, the tree of builders is called to create the complete settings + * object. For example, to set the total timeout of DeleteLog to 30 seconds: + * + *

+ * 
+ * LoggingServiceV2Settings.Builder loggingServiceV2SettingsBuilder =
+ *     LoggingServiceV2Settings.defaultBuilder();
+ * loggingServiceV2SettingsBuilder.DeleteLogSettings().getRetrySettingsBuilder()
+ *     .setTotalTimeout(Duration.standardSeconds(30));
+ * LoggingServiceV2Settings loggingServiceV2Settings = loggingServiceV2SettingsBuilder.build();
+ * 
+ * 
+ */ @javax.annotation.Generated("by GAPIC") public class LoggingServiceV2Settings extends ServiceApiSettings { @@ -96,6 +124,16 @@ public class LoggingServiceV2Settings extends ServiceApiSettings { .add("https://www.googleapis.com/auth/cloud-platform") .build(); + /** + * The default connection settings of the service. + */ + public static final ConnectionSettings DEFAULT_CONNECTION_SETTINGS = + ConnectionSettings.newBuilder() + .setServiceAddress(DEFAULT_SERVICE_ADDRESS) + .setPort(DEFAULT_SERVICE_PORT) + .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) + .build(); + private final SimpleCallSettings deleteLogSettings; private final SimpleCallSettings writeLogEntriesSettings; @@ -107,20 +145,32 @@ public class LoggingServiceV2Settings extends ServiceApiSettings { MonitoredResourceDescriptor> listMonitoredResourceDescriptorsSettings; + /** + * Returns the object with the settings used for calls to deleteLog. + */ public SimpleCallSettings deleteLogSettings() { return deleteLogSettings; } + /** + * Returns the object with the settings used for calls to writeLogEntries. + */ public SimpleCallSettings writeLogEntriesSettings() { return writeLogEntriesSettings; } + /** + * Returns the object with the settings used for calls to listLogEntries. + */ public PageStreamingCallSettings listLogEntriesSettings() { return listLogEntriesSettings; } + /** + * Returns the object with the settings used for calls to listMonitoredResourceDescriptors. + */ public PageStreamingCallSettings< ListMonitoredResourceDescriptorsRequest, ListMonitoredResourceDescriptorsResponse, MonitoredResourceDescriptor> @@ -128,24 +178,31 @@ public SimpleCallSettings deleteLogSettings() { return listMonitoredResourceDescriptorsSettings; } - public static LoggingServiceV2Settings defaultInstance() throws IOException { - return newBuilder().build(); + /** + * Returns a builder for this class with recommended defaults. + */ + public static Builder defaultBuilder() { + return Builder.createDefault(); } + /** + * Returns a new builder for this class. + */ public static Builder newBuilder() { return new Builder(); } + /** + * Returns a builder containing all the values of this settings class. + */ public Builder toBuilder() { return new Builder(this); } private LoggingServiceV2Settings(Builder settingsBuilder) throws IOException { super( - settingsBuilder.getOrBuildChannel(), - settingsBuilder.shouldAutoCloseChannel(), - settingsBuilder.getOrBuildExecutor(), - settingsBuilder.getConnectionSettings(), + settingsBuilder.getChannelProvider(), + settingsBuilder.getExecutorProvider(), settingsBuilder.getGeneratorName(), settingsBuilder.getGeneratorVersion(), settingsBuilder.getClientLibName(), @@ -214,6 +271,9 @@ public Iterable extractResources( } }; + /** + * Builder for LoggingServiceV2Settings. + */ public static class Builder extends ServiceApiSettings.Builder { private final ImmutableList methodSettingsBuilders; @@ -270,35 +330,21 @@ public static class Builder extends ServiceApiSettings.Builder { } private Builder() { - super( - ConnectionSettings.builder() - .setServiceAddress(DEFAULT_SERVICE_ADDRESS) - .setPort(DEFAULT_SERVICE_PORT) - .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) - .build()); - - deleteLogSettings = - SimpleCallSettings.newBuilder(LoggingServiceV2Grpc.METHOD_DELETE_LOG) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + super(DEFAULT_CONNECTION_SETTINGS); + + deleteLogSettings = SimpleCallSettings.newBuilder(LoggingServiceV2Grpc.METHOD_DELETE_LOG); writeLogEntriesSettings = - SimpleCallSettings.newBuilder(LoggingServiceV2Grpc.METHOD_WRITE_LOG_ENTRIES) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + SimpleCallSettings.newBuilder(LoggingServiceV2Grpc.METHOD_WRITE_LOG_ENTRIES); listLogEntriesSettings = PageStreamingCallSettings.newBuilder( - LoggingServiceV2Grpc.METHOD_LIST_LOG_ENTRIES, LIST_LOG_ENTRIES_PAGE_STR_DESC) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("list")); + LoggingServiceV2Grpc.METHOD_LIST_LOG_ENTRIES, LIST_LOG_ENTRIES_PAGE_STR_DESC); listMonitoredResourceDescriptorsSettings = PageStreamingCallSettings.newBuilder( - LoggingServiceV2Grpc.METHOD_LIST_MONITORED_RESOURCE_DESCRIPTORS, - LIST_MONITORED_RESOURCE_DESCRIPTORS_PAGE_STR_DESC) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + LoggingServiceV2Grpc.METHOD_LIST_MONITORED_RESOURCE_DESCRIPTORS, + LIST_MONITORED_RESOURCE_DESCRIPTORS_PAGE_STR_DESC); methodSettingsBuilders = ImmutableList.of( @@ -308,6 +354,31 @@ private Builder() { listMonitoredResourceDescriptorsSettings); } + private static Builder createDefault() { + Builder builder = new Builder(); + builder + .deleteLogSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .writeLogEntriesSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .listLogEntriesSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("list")); + + builder + .listMonitoredResourceDescriptorsSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + return builder; + } + private Builder(LoggingServiceV2Settings settings) { super(settings); @@ -325,6 +396,17 @@ private Builder(LoggingServiceV2Settings settings) { listMonitoredResourceDescriptorsSettings); } + @Override + protected ConnectionSettings getDefaultConnectionSettings() { + return DEFAULT_CONNECTION_SETTINGS; + } + + @Override + public Builder provideExecutorWith(ScheduledExecutorService executor, boolean shouldAutoClose) { + super.provideExecutorWith(executor, shouldAutoClose); + return this; + } + @Override public Builder provideChannelWith(ManagedChannel channel, boolean shouldAutoClose) { super.provideChannelWith(channel, shouldAutoClose); @@ -338,8 +420,14 @@ public Builder provideChannelWith(ConnectionSettings settings) { } @Override - public Builder setExecutor(ScheduledExecutorService executor) { - super.setExecutor(executor); + public Builder provideChannelWith(Credentials credentials) { + super.provideChannelWith(credentials); + return this; + } + + @Override + public Builder provideChannelWith(List scopes) { + super.provideChannelWith(scopes); return this; } @@ -355,26 +443,43 @@ public Builder setClientLibHeader(String name, String version) { return this; } + /** + * Applies the given settings to all of the API methods in this service. Only + * values that are non-null will be applied, so this method is not capable + * of un-setting any values. + */ public Builder applyToAllApiMethods(ApiCallSettings.Builder apiCallSettings) throws Exception { super.applyToAllApiMethods(methodSettingsBuilders, apiCallSettings); return this; } + /** + * Returns the builder for the settings used for calls to deleteLog. + */ public SimpleCallSettings.Builder deleteLogSettings() { return deleteLogSettings; } + /** + * Returns the builder for the settings used for calls to writeLogEntries. + */ public SimpleCallSettings.Builder writeLogEntriesSettings() { return writeLogEntriesSettings; } + /** + * Returns the builder for the settings used for calls to listLogEntries. + */ public PageStreamingCallSettings.Builder< ListLogEntriesRequest, ListLogEntriesResponse, LogEntry> listLogEntriesSettings() { return listLogEntriesSettings; } + /** + * Returns the builder for the settings used for calls to listMonitoredResourceDescriptors. + */ public PageStreamingCallSettings.Builder< ListMonitoredResourceDescriptorsRequest, ListMonitoredResourceDescriptorsResponse, MonitoredResourceDescriptor> diff --git a/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/MetricsServiceV2Api.java b/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/MetricsServiceV2Api.java index a5f2306577b2..adb4d1c325cd 100644 --- a/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/MetricsServiceV2Api.java +++ b/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/MetricsServiceV2Api.java @@ -33,6 +33,7 @@ package com.google.cloud.logging.spi.v2; +import com.google.api.gax.core.PageAccessor; import com.google.api.gax.grpc.ApiCallable; import com.google.api.gax.protobuf.PathTemplate; import com.google.logging.v2.CreateLogMetricRequest; @@ -48,6 +49,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.ScheduledExecutorService; // Manually-added imports: add custom (non-generated) imports after this point. @@ -55,27 +57,81 @@ /** * Service Description: * + *

This class provides the ability to make remote calls to the backing service through method + * calls that map to API methods. Sample code to get started: + * + *

+ * 
+ * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+ *   String formattedProjectName = MetricsServiceV2Api.formatProjectName("[PROJECT]");
+ *   ListLogMetricsResponse response = metricsServiceV2Api.listLogMetrics(formattedProjectName);
+ * }
+ * 
+ * 
+ * + *

Note: close() needs to be called on the metricsServiceV2Api object to clean up resources such + * as threads. In the example above, try-with-resources is used, which automatically calls + * close(). + * + *

The surface of this class includes several types of Java methods for each of the API's methods: + * + *

    + *
  1. A "flattened" method. With this type of method, the fields of the request type have been + * converted into function parameters. It may be the case that not all fields are available + * as parameters, and not every API method will have a flattened method entry point. + *
  2. A "request object" method. This type of method only takes one parameter, a request + * object, which must be constructed before the call. Not every API method will have a request + * object method. + *
  3. A "callable" method. This type of method takes no parameters and returns an immutable + * ApiCallable object, which can be used to initiate calls to the service. + *
+ * + *

See the individual methods for example code. + * + *

Many parameters require resource names to be formatted in a particular way. To assist + * with these names, this class includes a format method for each type of name, and additionally + * a parse method to extract the individual identifiers contained within names that are + * returned. + * + *

This class can be customized by passing in a custom instance of MetricsServiceV2Settings to + * create(). For example: + * + *

+ * 
+ * MetricsServiceV2Settings metricsServiceV2Settings = MetricsServiceV2Settings.defaultBuilder()
+ *     .provideChannelWith(myCredentials)
+ *     .build();
+ * MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.create(metricsServiceV2Settings);
+ * 
+ * 
+ * * * */ @javax.annotation.Generated("by GAPIC") public class MetricsServiceV2Api implements AutoCloseable { + private final MetricsServiceV2Settings settings; private final ManagedChannel channel; + private final ScheduledExecutorService executor; private final List closeables = new ArrayList<>(); private final ApiCallable listLogMetricsCallable; - private final ApiCallable> - listLogMetricsIterableCallable; + private final ApiCallable> + listLogMetricsPagedCallable; private final ApiCallable getLogMetricCallable; private final ApiCallable createLogMetricCallable; private final ApiCallable updateLogMetricCallable; private final ApiCallable deleteLogMetricCallable; + public final MetricsServiceV2Settings getSettings() { + return settings; + } + private static final PathTemplate PROJECT_PATH_TEMPLATE = - PathTemplate.create("projects/{project}"); + PathTemplate.createWithoutUrlEncoding("projects/{project}"); private static final PathTemplate METRIC_PATH_TEMPLATE = - PathTemplate.create("projects/{project}/metrics/{metric}"); + PathTemplate.createWithoutUrlEncoding("projects/{project}/metrics/{metric}"); /** * Formats a string containing the fully-qualified path to represent @@ -138,8 +194,8 @@ public static final String parseMetricFromMetricName(String metricName) { * * */ - public static final MetricsServiceV2Api defaultInstance() throws IOException { - return create(MetricsServiceV2Settings.defaultInstance()); + public static final MetricsServiceV2Api createWithDefaults() throws IOException { + return create(MetricsServiceV2Settings.defaultBuilder().build()); } /** @@ -164,17 +220,25 @@ public static final MetricsServiceV2Api create(MetricsServiceV2Settings settings * */ protected MetricsServiceV2Api(MetricsServiceV2Settings settings) throws IOException { - this.channel = settings.getChannel(); - - this.listLogMetricsCallable = ApiCallable.create(settings.listLogMetricsSettings(), settings); - this.listLogMetricsIterableCallable = - ApiCallable.createIterable(settings.listLogMetricsSettings(), settings); - this.getLogMetricCallable = ApiCallable.create(settings.getLogMetricSettings(), settings); - this.createLogMetricCallable = ApiCallable.create(settings.createLogMetricSettings(), settings); - this.updateLogMetricCallable = ApiCallable.create(settings.updateLogMetricSettings(), settings); - this.deleteLogMetricCallable = ApiCallable.create(settings.deleteLogMetricSettings(), settings); - - if (settings.shouldAutoCloseChannel()) { + this.settings = settings; + this.executor = settings.getExecutorProvider().getOrBuildExecutor(); + this.channel = settings.getChannelProvider().getOrBuildChannel(this.executor); + + this.listLogMetricsCallable = + ApiCallable.create(settings.listLogMetricsSettings(), this.channel, this.executor); + this.listLogMetricsPagedCallable = + ApiCallable.createPagedVariant( + settings.listLogMetricsSettings(), this.channel, this.executor); + this.getLogMetricCallable = + ApiCallable.create(settings.getLogMetricSettings(), this.channel, this.executor); + this.createLogMetricCallable = + ApiCallable.create(settings.createLogMetricSettings(), this.channel, this.executor); + this.updateLogMetricCallable = + ApiCallable.create(settings.updateLogMetricSettings(), this.channel, this.executor); + this.deleteLogMetricCallable = + ApiCallable.create(settings.deleteLogMetricSettings(), this.channel, this.executor); + + if (settings.getChannelProvider().shouldAutoClose()) { closeables.add( new Closeable() { @Override @@ -183,6 +247,15 @@ public void close() throws IOException { } }); } + if (settings.getExecutorProvider().shouldAutoClose()) { + closeables.add( + new Closeable() { + @Override + public void close() throws IOException { + executor.shutdown(); + } + }); + } } // ----- listLogMetrics ----- @@ -191,14 +264,25 @@ public void close() throws IOException { /** * Lists logs-based metrics. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = MetricsServiceV2Api.formatProjectName("[PROJECT]");
+   *   for (LogMetric elements : metricsServiceV2Api.listLogMetrics(formattedProjectName)) {
+   *     // doThingsWith(elements);
+   *   }
+   * }
+   * 
+ * * * * * @param projectName Required. The resource name of the project containing the metrics. * Example: `"projects/my-project-id"`. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ - public final Iterable listLogMetrics(String projectName) { + public final PageAccessor listLogMetrics(String projectName) { + PROJECT_PATH_TEMPLATE.validate(projectName); ListLogMetricsRequest request = ListLogMetricsRequest.newBuilder().setProjectName(projectName).build(); return listLogMetrics(request); @@ -208,36 +292,84 @@ public final Iterable listLogMetrics(String projectName) { /** * Lists logs-based metrics. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = MetricsServiceV2Api.formatProjectName("[PROJECT]");
+   *   ListLogMetricsRequest request = ListLogMetricsRequest.newBuilder()
+   *     .setProjectName(formattedProjectName)
+   *     .build();
+   *   for (LogMetric elements : metricsServiceV2Api.listLogMetrics(request)) {
+   *     // doThingsWith(elements);
+   *   }
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ - public final Iterable listLogMetrics(ListLogMetricsRequest request) { - return listLogMetricsIterableCallable().call(request); + public final PageAccessor listLogMetrics(ListLogMetricsRequest request) { + return listLogMetricsPagedCallable().call(request); } // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. /** * Lists logs-based metrics. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = MetricsServiceV2Api.formatProjectName("[PROJECT]");
+   *   ListLogMetricsRequest request = ListLogMetricsRequest.newBuilder()
+   *     .setProjectName(formattedProjectName)
+   *     .build();
+   *   ListenableFuture<PageAccessor<LogMetric>> future = metricsServiceV2Api.listLogMetricsPagedCallable().futureCall(request);
+   *   // Do something
+   *   for (LogMetric elements : future.get()) {
+   *     // doThingsWith(elements);
+   *   }
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ - public final ApiCallable> - listLogMetricsIterableCallable() { - return listLogMetricsIterableCallable; + public final ApiCallable> + listLogMetricsPagedCallable() { + return listLogMetricsPagedCallable; } // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. /** * Lists logs-based metrics. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = MetricsServiceV2Api.formatProjectName("[PROJECT]");
+   *   ListLogMetricsRequest request = ListLogMetricsRequest.newBuilder()
+   *     .setProjectName(formattedProjectName)
+   *     .build();
+   *   while (true) {
+   *     ListLogMetricsResponse response = metricsServiceV2Api.listLogMetricsCallable().call(request);
+   *     for (LogMetric elements : response.getMetricsList()) {
+   *       // doThingsWith(elements);
+   *     }
+   *     String nextPageToken = response.getNextPageToken();
+   *     if (!Strings.isNullOrEmpty(nextPageToken)) {
+   *       request = request.toBuilder().setPageToken(nextPageToken).build();
+   *     } else {
+   *       break;
+   *     }
+   *   }
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable listLogMetricsCallable() { return listLogMetricsCallable; @@ -249,17 +381,25 @@ public final ApiCallable listLogM /** * Gets a logs-based metric. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedMetricName = MetricsServiceV2Api.formatMetricName("[PROJECT]", "[METRIC]");
+   *   LogMetric response = metricsServiceV2Api.getLogMetric(formattedMetricName);
+   * }
+   * 
+ * * * * * @param metricName The resource name of the desired metric. * Example: `"projects/my-project-id/metrics/my-metric-id"`. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public final LogMetric getLogMetric(String metricName) { + METRIC_PATH_TEMPLATE.validate(metricName); GetLogMetricRequest request = GetLogMetricRequest.newBuilder().setMetricName(metricName).build(); - return getLogMetric(request); } @@ -267,11 +407,22 @@ public final LogMetric getLogMetric(String metricName) { /** * Gets a logs-based metric. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedMetricName = MetricsServiceV2Api.formatMetricName("[PROJECT]", "[METRIC]");
+   *   GetLogMetricRequest request = GetLogMetricRequest.newBuilder()
+   *     .setMetricName(formattedMetricName)
+   *     .build();
+   *   LogMetric response = metricsServiceV2Api.getLogMetric(request);
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ private LogMetric getLogMetric(GetLogMetricRequest request) { return getLogMetricCallable().call(request); @@ -281,9 +432,21 @@ private LogMetric getLogMetric(GetLogMetricRequest request) { /** * Gets a logs-based metric. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedMetricName = MetricsServiceV2Api.formatMetricName("[PROJECT]", "[METRIC]");
+   *   GetLogMetricRequest request = GetLogMetricRequest.newBuilder()
+   *     .setMetricName(formattedMetricName)
+   *     .build();
+   *   ListenableFuture<LogMetric> future = metricsServiceV2Api.getLogMetricCallable().futureCall(request);
+   *   // Do something
+   *   LogMetric response = future.get();
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable getLogMetricCallable() { return getLogMetricCallable; @@ -295,6 +458,15 @@ public final ApiCallable getLogMetricCallable() /** * Creates a logs-based metric. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = MetricsServiceV2Api.formatProjectName("[PROJECT]");
+   *   LogMetric metric = LogMetric.newBuilder().build();
+   *   LogMetric response = metricsServiceV2Api.createLogMetric(formattedProjectName, metric);
+   * }
+   * 
+ * * * * @@ -304,12 +476,13 @@ public final ApiCallable getLogMetricCallable() * The new metric must be provided in the request. * @param metric The new logs-based metric, which must not have an identifier that * already exists. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public final LogMetric createLogMetric(String projectName, LogMetric metric) { + PROJECT_PATH_TEMPLATE.validate(projectName); + CreateLogMetricRequest request = CreateLogMetricRequest.newBuilder().setProjectName(projectName).setMetric(metric).build(); - return createLogMetric(request); } @@ -317,11 +490,24 @@ public final LogMetric createLogMetric(String projectName, LogMetric metric) { /** * Creates a logs-based metric. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = MetricsServiceV2Api.formatProjectName("[PROJECT]");
+   *   LogMetric metric = LogMetric.newBuilder().build();
+   *   CreateLogMetricRequest request = CreateLogMetricRequest.newBuilder()
+   *     .setProjectName(formattedProjectName)
+   *     .setMetric(metric)
+   *     .build();
+   *   LogMetric response = metricsServiceV2Api.createLogMetric(request);
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public LogMetric createLogMetric(CreateLogMetricRequest request) { return createLogMetricCallable().call(request); @@ -331,9 +517,23 @@ public LogMetric createLogMetric(CreateLogMetricRequest request) { /** * Creates a logs-based metric. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedProjectName = MetricsServiceV2Api.formatProjectName("[PROJECT]");
+   *   LogMetric metric = LogMetric.newBuilder().build();
+   *   CreateLogMetricRequest request = CreateLogMetricRequest.newBuilder()
+   *     .setProjectName(formattedProjectName)
+   *     .setMetric(metric)
+   *     .build();
+   *   ListenableFuture<LogMetric> future = metricsServiceV2Api.createLogMetricCallable().futureCall(request);
+   *   // Do something
+   *   LogMetric response = future.get();
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable createLogMetricCallable() { return createLogMetricCallable; @@ -345,6 +545,15 @@ public final ApiCallable createLogMetricCalla /** * Creates or updates a logs-based metric. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedMetricName = MetricsServiceV2Api.formatMetricName("[PROJECT]", "[METRIC]");
+   *   LogMetric metric = LogMetric.newBuilder().build();
+   *   LogMetric response = metricsServiceV2Api.updateLogMetric(formattedMetricName, metric);
+   * }
+   * 
+ * * * * @@ -357,12 +566,13 @@ public final ApiCallable createLogMetricCalla * @param metric The updated metric, whose name must be the same as the * metric identifier in `metricName`. If `metricName` does not * exist, then a new metric is created. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public final LogMetric updateLogMetric(String metricName, LogMetric metric) { + METRIC_PATH_TEMPLATE.validate(metricName); + UpdateLogMetricRequest request = UpdateLogMetricRequest.newBuilder().setMetricName(metricName).setMetric(metric).build(); - return updateLogMetric(request); } @@ -370,11 +580,24 @@ public final LogMetric updateLogMetric(String metricName, LogMetric metric) { /** * Creates or updates a logs-based metric. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedMetricName = MetricsServiceV2Api.formatMetricName("[PROJECT]", "[METRIC]");
+   *   LogMetric metric = LogMetric.newBuilder().build();
+   *   UpdateLogMetricRequest request = UpdateLogMetricRequest.newBuilder()
+   *     .setMetricName(formattedMetricName)
+   *     .setMetric(metric)
+   *     .build();
+   *   LogMetric response = metricsServiceV2Api.updateLogMetric(request);
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public LogMetric updateLogMetric(UpdateLogMetricRequest request) { return updateLogMetricCallable().call(request); @@ -384,9 +607,23 @@ public LogMetric updateLogMetric(UpdateLogMetricRequest request) { /** * Creates or updates a logs-based metric. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedMetricName = MetricsServiceV2Api.formatMetricName("[PROJECT]", "[METRIC]");
+   *   LogMetric metric = LogMetric.newBuilder().build();
+   *   UpdateLogMetricRequest request = UpdateLogMetricRequest.newBuilder()
+   *     .setMetricName(formattedMetricName)
+   *     .setMetric(metric)
+   *     .build();
+   *   ListenableFuture<LogMetric> future = metricsServiceV2Api.updateLogMetricCallable().futureCall(request);
+   *   // Do something
+   *   LogMetric response = future.get();
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable updateLogMetricCallable() { return updateLogMetricCallable; @@ -398,17 +635,25 @@ public final ApiCallable updateLogMetricCalla /** * Deletes a logs-based metric. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedMetricName = MetricsServiceV2Api.formatMetricName("[PROJECT]", "[METRIC]");
+   *   metricsServiceV2Api.deleteLogMetric(formattedMetricName);
+   * }
+   * 
+ * * * * * @param metricName The resource name of the metric to delete. * Example: `"projects/my-project-id/metrics/my-metric-id"`. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ public final void deleteLogMetric(String metricName) { + METRIC_PATH_TEMPLATE.validate(metricName); DeleteLogMetricRequest request = DeleteLogMetricRequest.newBuilder().setMetricName(metricName).build(); - deleteLogMetric(request); } @@ -416,11 +661,22 @@ public final void deleteLogMetric(String metricName) { /** * Deletes a logs-based metric. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedMetricName = MetricsServiceV2Api.formatMetricName("[PROJECT]", "[METRIC]");
+   *   DeleteLogMetricRequest request = DeleteLogMetricRequest.newBuilder()
+   *     .setMetricName(formattedMetricName)
+   *     .build();
+   *   metricsServiceV2Api.deleteLogMetric(request);
+   * }
+   * 
+ * * * * * @param request The request object containing all of the parameters for the API call. - * @throws ApiException if the remote call fails + * @throws com.google.api.gax.grpc.ApiException if the remote call fails */ private void deleteLogMetric(DeleteLogMetricRequest request) { deleteLogMetricCallable().call(request); @@ -430,9 +686,21 @@ private void deleteLogMetric(DeleteLogMetricRequest request) { /** * Deletes a logs-based metric. * + * Sample code: + *

+   * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+   *   String formattedMetricName = MetricsServiceV2Api.formatMetricName("[PROJECT]", "[METRIC]");
+   *   DeleteLogMetricRequest request = DeleteLogMetricRequest.newBuilder()
+   *     .setMetricName(formattedMetricName)
+   *     .build();
+   *   ListenableFuture<Void> future = metricsServiceV2Api.deleteLogMetricCallable().futureCall(request);
+   *   // Do something
+   *   future.get();
+   * }
+   * 
+ * * * - * @throws ApiException if the remote call fails */ public final ApiCallable deleteLogMetricCallable() { return deleteLogMetricCallable; diff --git a/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/MetricsServiceV2Settings.java b/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/MetricsServiceV2Settings.java index 316a9851bf11..1308b95eb029 100644 --- a/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/MetricsServiceV2Settings.java +++ b/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/MetricsServiceV2Settings.java @@ -40,6 +40,7 @@ import com.google.api.gax.grpc.PageStreamingDescriptor; import com.google.api.gax.grpc.ServiceApiSettings; import com.google.api.gax.grpc.SimpleCallSettings; +import com.google.auth.Credentials; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -57,12 +58,39 @@ import io.grpc.ManagedChannel; import io.grpc.Status; import java.io.IOException; +import java.util.List; import java.util.concurrent.ScheduledExecutorService; import org.joda.time.Duration; // Manually-added imports: add custom (non-generated) imports after this point. // AUTO-GENERATED DOCUMENTATION AND CLASS - see instructions at the top of the file for editing. +/** + * Settings class to configure an instance of {@link MetricsServiceV2Api}. + * + *

The default instance has everything set to sensible defaults: + * + *

    + *
  • The default service address (logging.googleapis.com) and default port (443) + * are used. + *
  • Credentials are acquired automatically through Application Default Credentials. + *
  • Retries are configured for idempotent methods but not for non-idempotent methods. + *
+ * + *

The builder of this class is recursive, so contained classes are themselves builders. + * When build() is called, the tree of builders is called to create the complete settings + * object. For example, to set the total timeout of ListLogMetrics to 30 seconds: + * + *

+ * 
+ * MetricsServiceV2Settings.Builder metricsServiceV2SettingsBuilder =
+ *     MetricsServiceV2Settings.defaultBuilder();
+ * metricsServiceV2SettingsBuilder.ListLogMetricsSettings().getRetrySettingsBuilder()
+ *     .setTotalTimeout(Duration.standardSeconds(30));
+ * MetricsServiceV2Settings metricsServiceV2Settings = metricsServiceV2SettingsBuilder.build();
+ * 
+ * 
+ */ @javax.annotation.Generated("by GAPIC") public class MetricsServiceV2Settings extends ServiceApiSettings { @@ -94,6 +122,16 @@ public class MetricsServiceV2Settings extends ServiceApiSettings { .add("https://www.googleapis.com/auth/cloud-platform") .build(); + /** + * The default connection settings of the service. + */ + public static final ConnectionSettings DEFAULT_CONNECTION_SETTINGS = + ConnectionSettings.newBuilder() + .setServiceAddress(DEFAULT_SERVICE_ADDRESS) + .setPort(DEFAULT_SERVICE_PORT) + .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) + .build(); + private final PageStreamingCallSettings listLogMetricsSettings; @@ -102,45 +140,67 @@ public class MetricsServiceV2Settings extends ServiceApiSettings { private final SimpleCallSettings updateLogMetricSettings; private final SimpleCallSettings deleteLogMetricSettings; + /** + * Returns the object with the settings used for calls to listLogMetrics. + */ public PageStreamingCallSettings listLogMetricsSettings() { return listLogMetricsSettings; } + /** + * Returns the object with the settings used for calls to getLogMetric. + */ public SimpleCallSettings getLogMetricSettings() { return getLogMetricSettings; } + /** + * Returns the object with the settings used for calls to createLogMetric. + */ public SimpleCallSettings createLogMetricSettings() { return createLogMetricSettings; } + /** + * Returns the object with the settings used for calls to updateLogMetric. + */ public SimpleCallSettings updateLogMetricSettings() { return updateLogMetricSettings; } + /** + * Returns the object with the settings used for calls to deleteLogMetric. + */ public SimpleCallSettings deleteLogMetricSettings() { return deleteLogMetricSettings; } - public static MetricsServiceV2Settings defaultInstance() throws IOException { - return newBuilder().build(); + /** + * Returns a builder for this class with recommended defaults. + */ + public static Builder defaultBuilder() { + return Builder.createDefault(); } + /** + * Returns a new builder for this class. + */ public static Builder newBuilder() { return new Builder(); } + /** + * Returns a builder containing all the values of this settings class. + */ public Builder toBuilder() { return new Builder(this); } private MetricsServiceV2Settings(Builder settingsBuilder) throws IOException { super( - settingsBuilder.getOrBuildChannel(), - settingsBuilder.shouldAutoCloseChannel(), - settingsBuilder.getOrBuildExecutor(), - settingsBuilder.getConnectionSettings(), + settingsBuilder.getChannelProvider(), + settingsBuilder.getExecutorProvider(), settingsBuilder.getGeneratorName(), settingsBuilder.getGeneratorVersion(), settingsBuilder.getClientLibName(), @@ -177,6 +237,9 @@ public Iterable extractResources(ListLogMetricsResponse payload) { } }; + /** + * Builder for MetricsServiceV2Settings. + */ public static class Builder extends ServiceApiSettings.Builder { private final ImmutableList methodSettingsBuilders; @@ -220,38 +283,23 @@ public static class Builder extends ServiceApiSettings.Builder { } private Builder() { - super( - ConnectionSettings.builder() - .setServiceAddress(DEFAULT_SERVICE_ADDRESS) - .setPort(DEFAULT_SERVICE_PORT) - .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) - .build()); + super(DEFAULT_CONNECTION_SETTINGS); listLogMetricsSettings = PageStreamingCallSettings.newBuilder( - MetricsServiceV2Grpc.METHOD_LIST_LOG_METRICS, LIST_LOG_METRICS_PAGE_STR_DESC) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + MetricsServiceV2Grpc.METHOD_LIST_LOG_METRICS, LIST_LOG_METRICS_PAGE_STR_DESC); getLogMetricSettings = - SimpleCallSettings.newBuilder(MetricsServiceV2Grpc.METHOD_GET_LOG_METRIC) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + SimpleCallSettings.newBuilder(MetricsServiceV2Grpc.METHOD_GET_LOG_METRIC); createLogMetricSettings = - SimpleCallSettings.newBuilder(MetricsServiceV2Grpc.METHOD_CREATE_LOG_METRIC) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + SimpleCallSettings.newBuilder(MetricsServiceV2Grpc.METHOD_CREATE_LOG_METRIC); updateLogMetricSettings = - SimpleCallSettings.newBuilder(MetricsServiceV2Grpc.METHOD_UPDATE_LOG_METRIC) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + SimpleCallSettings.newBuilder(MetricsServiceV2Grpc.METHOD_UPDATE_LOG_METRIC); deleteLogMetricSettings = - SimpleCallSettings.newBuilder(MetricsServiceV2Grpc.METHOD_DELETE_LOG_METRIC) - .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) - .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + SimpleCallSettings.newBuilder(MetricsServiceV2Grpc.METHOD_DELETE_LOG_METRIC); methodSettingsBuilders = ImmutableList.of( @@ -262,6 +310,36 @@ private Builder() { deleteLogMetricSettings); } + private static Builder createDefault() { + Builder builder = new Builder(); + builder + .listLogMetricsSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .getLogMetricSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .createLogMetricSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .updateLogMetricSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .deleteLogMetricSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + return builder; + } + private Builder(MetricsServiceV2Settings settings) { super(settings); @@ -280,6 +358,17 @@ private Builder(MetricsServiceV2Settings settings) { deleteLogMetricSettings); } + @Override + protected ConnectionSettings getDefaultConnectionSettings() { + return DEFAULT_CONNECTION_SETTINGS; + } + + @Override + public Builder provideExecutorWith(ScheduledExecutorService executor, boolean shouldAutoClose) { + super.provideExecutorWith(executor, shouldAutoClose); + return this; + } + @Override public Builder provideChannelWith(ManagedChannel channel, boolean shouldAutoClose) { super.provideChannelWith(channel, shouldAutoClose); @@ -293,8 +382,14 @@ public Builder provideChannelWith(ConnectionSettings settings) { } @Override - public Builder setExecutor(ScheduledExecutorService executor) { - super.setExecutor(executor); + public Builder provideChannelWith(Credentials credentials) { + super.provideChannelWith(credentials); + return this; + } + + @Override + public Builder provideChannelWith(List scopes) { + super.provideChannelWith(scopes); return this; } @@ -310,29 +405,49 @@ public Builder setClientLibHeader(String name, String version) { return this; } + /** + * Applies the given settings to all of the API methods in this service. Only + * values that are non-null will be applied, so this method is not capable + * of un-setting any values. + */ public Builder applyToAllApiMethods(ApiCallSettings.Builder apiCallSettings) throws Exception { super.applyToAllApiMethods(methodSettingsBuilders, apiCallSettings); return this; } + /** + * Returns the builder for the settings used for calls to listLogMetrics. + */ public PageStreamingCallSettings.Builder< ListLogMetricsRequest, ListLogMetricsResponse, LogMetric> listLogMetricsSettings() { return listLogMetricsSettings; } + /** + * Returns the builder for the settings used for calls to getLogMetric. + */ public SimpleCallSettings.Builder getLogMetricSettings() { return getLogMetricSettings; } + /** + * Returns the builder for the settings used for calls to createLogMetric. + */ public SimpleCallSettings.Builder createLogMetricSettings() { return createLogMetricSettings; } + /** + * Returns the builder for the settings used for calls to updateLogMetric. + */ public SimpleCallSettings.Builder updateLogMetricSettings() { return updateLogMetricSettings; } + /** + * Returns the builder for the settings used for calls to deleteLogMetric. + */ public SimpleCallSettings.Builder deleteLogMetricSettings() { return deleteLogMetricSettings; } diff --git a/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/package-info.java b/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/package-info.java new file mode 100644 index 000000000000..81ee3d3fce7e --- /dev/null +++ b/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/package-info.java @@ -0,0 +1,86 @@ +/* + * Copyright 2016 Google Inc. 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. + */ + +/* + * EDITING INSTRUCTIONS + * This file was automatically generated. For the short term, the process for + * refreshing this file will only be runnable by Google engineers. + * Manual additions are allowed because the refresh process performs + * a 3-way merge in order to preserve those manual additions. In order to not + * break the refresh process, additional documentation should be added between + * "manual edit" demarcations. + * + * Other generated files allow additional edits - see the editing instructions + * in those files for more details. + * + * Happy editing! + */ + +/** + * A client to Google Cloud Logging API. + * + * The interfaces provided are listed below, along with a usage sample + * + * =================== + * LoggingServiceV2Api + * =================== + * + * Service Description: Service for ingesting and querying logs. + * + * Sample for LoggingServiceV2Api: + *
+ * 
+ * try (LoggingServiceV2Api loggingServiceV2Api = LoggingServiceV2Api.createWithDefaults()) {
+ *   String formattedLogName = LoggingServiceV2Api.formatLogName("[PROJECT]", "[LOG]");
+ *   loggingServiceV2Api.deleteLog(formattedLogName);
+ * }
+ * 
+ * 
+ * + * ================== + * ConfigServiceV2Api + * ================== + * + * Service Description: + * + * Sample for ConfigServiceV2Api: + *
+ * 
+ * try (ConfigServiceV2Api configServiceV2Api = ConfigServiceV2Api.createWithDefaults()) {
+ *   String formattedProjectName = ConfigServiceV2Api.formatProjectName("[PROJECT]");
+ *   ListSinksResponse response = configServiceV2Api.listSinks(formattedProjectName);
+ * }
+ * 
+ * 
+ * + * =================== + * MetricsServiceV2Api + * =================== + * + * Service Description: + * + * Sample for MetricsServiceV2Api: + *
+ * 
+ * try (MetricsServiceV2Api metricsServiceV2Api = MetricsServiceV2Api.createWithDefaults()) {
+ *   String formattedProjectName = MetricsServiceV2Api.formatProjectName("[PROJECT]");
+ *   ListLogMetricsResponse response = metricsServiceV2Api.listLogMetrics(formattedProjectName);
+ * }
+ * 
+ * 
+ * + * + * + */ +package com.google.cloud.logging.spi.v2; diff --git a/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/testing/LocalLoggingImpl.java b/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/testing/LocalLoggingImpl.java index 142c48b248e0..81e09944f935 100644 --- a/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/testing/LocalLoggingImpl.java +++ b/gcloud-java-logging/src/main/java/com/google/cloud/logging/spi/v2/testing/LocalLoggingImpl.java @@ -21,8 +21,6 @@ import com.google.logging.v2.ListMonitoredResourceDescriptorsResponse; import com.google.logging.v2.LogEntry; import com.google.logging.v2.LoggingServiceV2Grpc; -import com.google.logging.v2.ReadLogEntriesRequest; -import com.google.logging.v2.ReadLogEntriesResponse; import com.google.logging.v2.WriteLogEntriesRequest; import com.google.logging.v2.WriteLogEntriesResponse; import com.google.protobuf.ByteString; @@ -77,22 +75,6 @@ public void listLogEntries( responseObserver.onCompleted(); } - @Override - public void readLogEntries( - ReadLogEntriesRequest request, StreamObserver responseObserver) { - List entries = new ArrayList<>(); - for (ByteString proj : request.getProjectIdsList().asByteStringList()) { - String prefix = "projects/" + proj.toStringUtf8() + "/"; - for (Map.Entry> entry : logs.entrySet()) { - if (entry.getKey().startsWith(prefix)) { - entries.addAll(entry.getValue()); - } - } - } - responseObserver.onNext(ReadLogEntriesResponse.newBuilder().addAllEntries(entries).build()); - responseObserver.onCompleted(); - } - @Override public void listMonitoredResourceDescriptors( ListMonitoredResourceDescriptorsRequest request, diff --git a/gcloud-java-logging/src/test/java/com/google/cloud/logging/spi/v2/LoggingServiceV2ApiTest.java b/gcloud-java-logging/src/test/java/com/google/cloud/logging/spi/v2/LoggingServiceV2ApiTest.java index cde54a4966e7..a463cc1c69a3 100644 --- a/gcloud-java-logging/src/test/java/com/google/cloud/logging/spi/v2/LoggingServiceV2ApiTest.java +++ b/gcloud-java-logging/src/test/java/com/google/cloud/logging/spi/v2/LoggingServiceV2ApiTest.java @@ -15,8 +15,9 @@ package com.google.cloud.logging.spi.v2; import com.google.api.MonitoredResource; -import com.google.common.collect.Iterables; +import com.google.api.gax.core.PageAccessor; import com.google.cloud.logging.spi.v2.testing.LocalLoggingHelper; +import com.google.common.collect.Iterables; import com.google.logging.v2.LogEntry; import org.junit.After; @@ -49,7 +50,7 @@ public static void stopServer() { @Before public void setUp() throws IOException { loggingHelper.reset(); - LoggingServiceV2Settings settings = LoggingServiceV2Settings.newBuilder() + LoggingServiceV2Settings settings = LoggingServiceV2Settings.defaultBuilder() .provideChannelWith(loggingHelper.createChannel(), true) .build(); loggingApi = LoggingServiceV2Api.create(settings); @@ -77,14 +78,14 @@ public void testListLog() { entries.add(LogEntry.newBuilder().setLogName(logName).setTextPayload("foobar").build()); loggingApi.writeLogEntries(logName, resource, Collections.emptyMap(), entries); - Iterable gotEntries = + PageAccessor gotEntries = loggingApi.listLogEntries(Collections.singletonList("my-project"), "", ""); Assert.assertTrue(Iterables.elementsEqual(entries, gotEntries)); } @Test public void testListNoLog() { - Iterable entries = + PageAccessor entries = loggingApi.listLogEntries(Collections.singletonList("my-project"), "", ""); Assert.assertTrue(Iterables.isEmpty(entries)); } @@ -99,7 +100,7 @@ public void testDeleteLog() { loggingApi.deleteLog(logName); - Iterable gotEntries = + PageAccessor gotEntries = loggingApi.listLogEntries(Collections.singletonList("my-project"), "", ""); Assert.assertTrue(Iterables.isEmpty(gotEntries)); } diff --git a/pom.xml b/pom.xml index 4c307cda3909..45455bf1ee01 100644 --- a/pom.xml +++ b/pom.xml @@ -102,6 +102,7 @@ gcloud-java-dns gcloud-java-examples gcloud-java-pubsub + gcloud-java-logging gcloud-java-resourcemanager gcloud-java-storage