diff --git a/google-cloud-monitoring/pom.xml b/google-cloud-monitoring/pom.xml new file mode 100644 index 000000000000..8ff98bf15c44 --- /dev/null +++ b/google-cloud-monitoring/pom.xml @@ -0,0 +1,126 @@ + + + 4.0.0 + google-cloud-monitoring + jar + Google Cloud Monitoring + https://github.com/GoogleCloudPlatform/google-cloud-java/tree/master/google-cloud-monitoring + + Java idiomatic client for Stackdriver Monitoring. + + + com.google.cloud + google-cloud-pom + 0.3.1-SNAPSHOT + + + google-cloud-monitoring + + + + io.netty + netty-tcnative-boringssl-static + 1.1.33.Fork17 + + + ${project.groupId} + google-cloud-core + ${project.version} + + + com.google.api.grpc + grpc-google-monitoring-v3 + 0.0.7 + + + io.grpc + grpc-all + 0.15.0 + + + com.google.auto.value + auto-value + 1.1 + + + ${project.groupId} + google-cloud-core + ${project.version} + test-jar + test + + + junit + junit + 4.12 + test + + + org.easymock + easymock + 3.4 + test + + + + + doclint-java8-disable + + [1.8,) + + + + -Xdoclint:none + + + + + + + org.codehaus.mojo + build-helper-maven-plugin + 1.9.1 + + + generate-sources + add-source + + + 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/google-cloud-monitoring/src/main/java/com/google/cloud/monitoring/spi/v3/AgentTranslationServiceApi.java b/google-cloud-monitoring/src/main/java/com/google/cloud/monitoring/spi/v3/AgentTranslationServiceApi.java new file mode 100644 index 000000000000..acec003d843f --- /dev/null +++ b/google-cloud-monitoring/src/main/java/com/google/cloud/monitoring/spi/v3/AgentTranslationServiceApi.java @@ -0,0 +1,290 @@ +/* + * 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. + */ +package com.google.cloud.monitoring.spi.v3; + +import com.google.api.MonitoredResource; +import com.google.api.gax.grpc.ApiCallable; +import com.google.api.gax.protobuf.PathTemplate; +import com.google.monitoring.v3.CollectdPayload; +import com.google.monitoring.v3.CreateCollectdTimeSeriesRequest; +import com.google.protobuf.Empty; +import io.grpc.ManagedChannel; +import java.io.Closeable; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ScheduledExecutorService; + +// AUTO-GENERATED DOCUMENTATION AND SERVICE +/** + * Service Description: The AgentTranslation API allows `collectd`-based agents to + * write time series data to Cloud Monitoring. + * See [google.monitoring.v3.MetricService.CreateTimeSeries] instead. + * + *

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 (AgentTranslationServiceApi agentTranslationServiceApi = AgentTranslationServiceApi.create()) {
+ *   String formattedName = AgentTranslationServiceApi.formatProjectName("[PROJECT]");
+ *   MonitoredResource resource = MonitoredResource.newBuilder().build();
+ *   String collectdVersion = "";
+ *   List<CollectdPayload> collectdPayloads = new ArrayList<>();
+ *   agentTranslationServiceApi.createCollectdTimeSeries(formattedName, resource, collectdVersion, collectdPayloads);
+ * }
+ * 
+ * 
+ * + *

Note: close() needs to be called on the agentTranslationServiceApi 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 AgentTranslationServiceSettings to + * create(). For example: + * + *

+ * 
+ * AgentTranslationServiceSettings agentTranslationServiceSettings = AgentTranslationServiceSettings.defaultBuilder()
+ *     .provideChannelWith(myCredentials)
+ *     .build();
+ * AgentTranslationServiceApi agentTranslationServiceApi = AgentTranslationServiceApi.create(agentTranslationServiceSettings);
+ * 
+ * 
+ */ +@javax.annotation.Generated("by GAPIC") +public class AgentTranslationServiceApi implements AutoCloseable { + private final AgentTranslationServiceSettings settings; + private final ManagedChannel channel; + private final ScheduledExecutorService executor; + private final List closeables = new ArrayList<>(); + + private final ApiCallable + createCollectdTimeSeriesCallable; + + public final AgentTranslationServiceSettings getSettings() { + return settings; + } + + private static final PathTemplate PROJECT_PATH_TEMPLATE = + PathTemplate.createWithoutUrlEncoding("projects/{project}"); + + /** + * Formats a string containing the fully-qualified path to represent + * a project resource. + */ + public static final String formatProjectName(String project) { + return PROJECT_PATH_TEMPLATE.instantiate("project", project); + } + + /** + * Parses the project from the given fully-qualified path which + * represents a project resource. + */ + public static final String parseProjectFromProjectName(String projectName) { + return PROJECT_PATH_TEMPLATE.parse(projectName).get("project"); + } + + /** + * Constructs an instance of AgentTranslationServiceApi with default settings. + */ + public static final AgentTranslationServiceApi create() throws IOException { + return create(AgentTranslationServiceSettings.defaultBuilder().build()); + } + + /** + * Constructs an instance of AgentTranslationServiceApi, using the given settings. + * The channels are created based on the settings passed in, or defaults for any + * settings that are not set. + */ + public static final AgentTranslationServiceApi create(AgentTranslationServiceSettings settings) + throws IOException { + return new AgentTranslationServiceApi(settings); + } + + /** + * Constructs an instance of AgentTranslationServiceApi, using the given settings. + * This is protected so that it easy to make a subclass, but otherwise, the static + * factory methods should be preferred. + */ + protected AgentTranslationServiceApi(AgentTranslationServiceSettings settings) + throws IOException { + this.settings = settings; + this.executor = settings.getExecutorProvider().getOrBuildExecutor(); + this.channel = settings.getChannelProvider().getOrBuildChannel(this.executor); + + this.createCollectdTimeSeriesCallable = + ApiCallable.create( + settings.createCollectdTimeSeriesSettings(), this.channel, this.executor); + + if (settings.getChannelProvider().shouldAutoClose()) { + closeables.add( + new Closeable() { + @Override + public void close() throws IOException { + channel.shutdown(); + } + }); + } + if (settings.getExecutorProvider().shouldAutoClose()) { + closeables.add( + new Closeable() { + @Override + public void close() throws IOException { + executor.shutdown(); + } + }); + } + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * **Stackdriver Monitoring Agent only:** Creates a new time series. + * + * <aside class="caution">This method is only for use by the Google Monitoring Agent. + * Use [projects.timeSeries.create][google.monitoring.v3.MetricService.CreateTimeSeries] + * instead.</aside> + * + * Sample code: + *

+   * try (AgentTranslationServiceApi agentTranslationServiceApi = AgentTranslationServiceApi.create()) {
+   *   String formattedName = AgentTranslationServiceApi.formatProjectName("[PROJECT]");
+   *   MonitoredResource resource = MonitoredResource.newBuilder().build();
+   *   String collectdVersion = "";
+   *   List<CollectdPayload> collectdPayloads = new ArrayList<>();
+   *   agentTranslationServiceApi.createCollectdTimeSeries(formattedName, resource, collectdVersion, collectdPayloads);
+   * }
+   * 
+ * + * @param name The project in which to create the time series. The format is + * `"projects/PROJECT_ID_OR_NUMBER"`. + * @param resource The monitored resource associated with the time series. + * @param collectdVersion The version of `collectd` that collected the data. Example: `"5.3.0-192.el6"`. + * @param collectdPayloads The `collectd` payloads representing the time series data. + * You must not include more than a single point for each + * time series, so no two payloads can have the same values + * for all of the fields `plugin`, `plugin_instance`, `type`, and `type_instance`. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final void createCollectdTimeSeries( + String name, + MonitoredResource resource, + String collectdVersion, + List collectdPayloads) { + PROJECT_PATH_TEMPLATE.validate(name, "createCollectdTimeSeries"); + CreateCollectdTimeSeriesRequest request = + CreateCollectdTimeSeriesRequest.newBuilder() + .setName(name) + .setResource(resource) + .setCollectdVersion(collectdVersion) + .addAllCollectdPayloads(collectdPayloads) + .build(); + createCollectdTimeSeries(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * **Stackdriver Monitoring Agent only:** Creates a new time series. + * + * <aside class="caution">This method is only for use by the Google Monitoring Agent. + * Use [projects.timeSeries.create][google.monitoring.v3.MetricService.CreateTimeSeries] + * instead.</aside> + * + * Sample code: + *

+   * try (AgentTranslationServiceApi agentTranslationServiceApi = AgentTranslationServiceApi.create()) {
+   *   String formattedName = AgentTranslationServiceApi.formatProjectName("[PROJECT]");
+   *   MonitoredResource resource = MonitoredResource.newBuilder().build();
+   *   String collectdVersion = "";
+   *   List<CollectdPayload> collectdPayloads = new ArrayList<>();
+   *   CreateCollectdTimeSeriesRequest request = CreateCollectdTimeSeriesRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .setResource(resource)
+   *     .setCollectdVersion(collectdVersion)
+   *     .addAllCollectdPayloads(collectdPayloads)
+   *     .build();
+   *   agentTranslationServiceApi.createCollectdTimeSeries(request);
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final void createCollectdTimeSeries(CreateCollectdTimeSeriesRequest request) { + createCollectdTimeSeriesCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * **Stackdriver Monitoring Agent only:** Creates a new time series. + * + * <aside class="caution">This method is only for use by the Google Monitoring Agent. + * Use [projects.timeSeries.create][google.monitoring.v3.MetricService.CreateTimeSeries] + * instead.</aside> + * + * Sample code: + *

+   * try (AgentTranslationServiceApi agentTranslationServiceApi = AgentTranslationServiceApi.create()) {
+   *   String formattedName = AgentTranslationServiceApi.formatProjectName("[PROJECT]");
+   *   MonitoredResource resource = MonitoredResource.newBuilder().build();
+   *   String collectdVersion = "";
+   *   List<CollectdPayload> collectdPayloads = new ArrayList<>();
+   *   CreateCollectdTimeSeriesRequest request = CreateCollectdTimeSeriesRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .setResource(resource)
+   *     .setCollectdVersion(collectdVersion)
+   *     .addAllCollectdPayloads(collectdPayloads)
+   *     .build();
+   *   ListenableFuture<Void> future = agentTranslationServiceApi.createCollectdTimeSeriesCallable().futureCall(request);
+   *   // Do something
+   *   future.get();
+   * }
+   * 
+ */ + public final ApiCallable + createCollectdTimeSeriesCallable() { + return createCollectdTimeSeriesCallable; + } + + /** + * Initiates an orderly shutdown in which preexisting calls continue but new calls are immediately + * cancelled. + */ + @Override + public final void close() throws Exception { + for (AutoCloseable closeable : closeables) { + closeable.close(); + } + } +} diff --git a/google-cloud-monitoring/src/main/java/com/google/cloud/monitoring/spi/v3/AgentTranslationServiceSettings.java b/google-cloud-monitoring/src/main/java/com/google/cloud/monitoring/spi/v3/AgentTranslationServiceSettings.java new file mode 100644 index 000000000000..ae87487fde4c --- /dev/null +++ b/google-cloud-monitoring/src/main/java/com/google/cloud/monitoring/spi/v3/AgentTranslationServiceSettings.java @@ -0,0 +1,299 @@ +/* + * 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. + */ +package com.google.cloud.monitoring.spi.v3; + +import com.google.api.gax.core.ConnectionSettings; +import com.google.api.gax.core.RetrySettings; +import com.google.api.gax.grpc.ApiCallSettings; +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; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; +import com.google.monitoring.v3.AgentTranslationServiceGrpc; +import com.google.monitoring.v3.CreateCollectdTimeSeriesRequest; +import com.google.protobuf.Empty; +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; + +// AUTO-GENERATED DOCUMENTATION AND CLASS +/** + * Settings class to configure an instance of {@link AgentTranslationServiceApi}. + * + *

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

    + *
  • The default service address (monitoring.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 createCollectdTimeSeries to 30 seconds: + * + *

+ * 
+ * AgentTranslationServiceSettings.Builder agentTranslationServiceSettingsBuilder =
+ *     AgentTranslationServiceSettings.defaultBuilder();
+ * agentTranslationServiceSettingsBuilder.createCollectdTimeSeriesSettings().getRetrySettingsBuilder()
+ *     .setTotalTimeout(Duration.standardSeconds(30));
+ * AgentTranslationServiceSettings agentTranslationServiceSettings = agentTranslationServiceSettingsBuilder.build();
+ * 
+ * 
+ */ +@javax.annotation.Generated("by GAPIC") +public class AgentTranslationServiceSettings extends ServiceApiSettings { + /** + * The default address of the service. + */ + private static final String DEFAULT_SERVICE_ADDRESS = "monitoring.googleapis.com"; + + /** + * The default port of the service. + */ + private static final int DEFAULT_SERVICE_PORT = 443; + + /** + * The default scopes of the service. + */ + private static final ImmutableList DEFAULT_SERVICE_SCOPES = + ImmutableList.builder().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 + createCollectdTimeSeriesSettings; + + /** + * Returns the object with the settings used for calls to createCollectdTimeSeries. + */ + public SimpleCallSettings + createCollectdTimeSeriesSettings() { + return createCollectdTimeSeriesSettings; + } + + /** + * Returns the default service address. + */ + public static String getDefaultServiceAddress() { + return DEFAULT_SERVICE_ADDRESS; + } + + /** + * Returns the default service port. + */ + public static int getDefaultServicePort() { + return DEFAULT_SERVICE_PORT; + } + + /** + * Returns the default service scopes. + */ + public static ImmutableList getDefaultServiceScopes() { + return DEFAULT_SERVICE_SCOPES; + } + + /** + * 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 AgentTranslationServiceSettings(Builder settingsBuilder) throws IOException { + super( + settingsBuilder.getChannelProvider(), + settingsBuilder.getExecutorProvider(), + settingsBuilder.getGeneratorName(), + settingsBuilder.getGeneratorVersion(), + settingsBuilder.getClientLibName(), + settingsBuilder.getClientLibVersion()); + + createCollectdTimeSeriesSettings = settingsBuilder.createCollectdTimeSeriesSettings().build(); + } + + /** + * Builder for AgentTranslationServiceSettings. + */ + public static class Builder extends ServiceApiSettings.Builder { + private final ImmutableList methodSettingsBuilders; + + private SimpleCallSettings.Builder + createCollectdTimeSeriesSettings; + + private static final ImmutableMap> RETRYABLE_CODE_DEFINITIONS; + + static { + ImmutableMap.Builder> definitions = ImmutableMap.builder(); + definitions.put( + "idempotent", + Sets.immutableEnumSet( + Lists.newArrayList( + Status.Code.DEADLINE_EXCEEDED, Status.Code.UNAVAILABLE))); + definitions.put("non_idempotent", Sets.immutableEnumSet(Lists.newArrayList())); + RETRYABLE_CODE_DEFINITIONS = definitions.build(); + } + + private static final ImmutableMap RETRY_PARAM_DEFINITIONS; + + static { + ImmutableMap.Builder definitions = ImmutableMap.builder(); + RetrySettings.Builder settingsBuilder = null; + settingsBuilder = + RetrySettings.newBuilder() + .setInitialRetryDelay(Duration.millis(100L)) + .setRetryDelayMultiplier(1.3) + .setMaxRetryDelay(Duration.millis(60000L)) + .setInitialRpcTimeout(Duration.millis(20000L)) + .setRpcTimeoutMultiplier(1.0) + .setMaxRpcTimeout(Duration.millis(20000L)) + .setTotalTimeout(Duration.millis(600000L)); + definitions.put("default", settingsBuilder); + RETRY_PARAM_DEFINITIONS = definitions.build(); + } + + private Builder() { + super(DEFAULT_CONNECTION_SETTINGS); + + createCollectdTimeSeriesSettings = + SimpleCallSettings.newBuilder( + AgentTranslationServiceGrpc.METHOD_CREATE_COLLECTD_TIME_SERIES); + + methodSettingsBuilders = + ImmutableList.of(createCollectdTimeSeriesSettings); + } + + private static Builder createDefault() { + Builder builder = new Builder(); + + builder + .createCollectdTimeSeriesSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + return builder; + } + + private Builder(AgentTranslationServiceSettings settings) { + super(settings); + + createCollectdTimeSeriesSettings = settings.createCollectdTimeSeriesSettings.toBuilder(); + + methodSettingsBuilders = + ImmutableList.of(createCollectdTimeSeriesSettings); + } + + @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); + return this; + } + + @Override + public Builder provideChannelWith(ConnectionSettings settings) { + super.provideChannelWith(settings); + return this; + } + + @Override + public Builder provideChannelWith(Credentials credentials) { + super.provideChannelWith(credentials); + return this; + } + + @Override + public Builder provideChannelWith(List scopes) { + super.provideChannelWith(scopes); + return this; + } + + @Override + public Builder setGeneratorHeader(String name, String version) { + super.setGeneratorHeader(name, version); + return this; + } + + @Override + public Builder setClientLibHeader(String name, String version) { + super.setClientLibHeader(name, 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 createCollectdTimeSeries. + */ + public SimpleCallSettings.Builder + createCollectdTimeSeriesSettings() { + return createCollectdTimeSeriesSettings; + } + + @Override + public AgentTranslationServiceSettings build() throws IOException { + return new AgentTranslationServiceSettings(this); + } + } +} diff --git a/google-cloud-monitoring/src/main/java/com/google/cloud/monitoring/spi/v3/GroupServiceApi.java b/google-cloud-monitoring/src/main/java/com/google/cloud/monitoring/spi/v3/GroupServiceApi.java new file mode 100644 index 000000000000..b0e44057ca58 --- /dev/null +++ b/google-cloud-monitoring/src/main/java/com/google/cloud/monitoring/spi/v3/GroupServiceApi.java @@ -0,0 +1,762 @@ +/* + * 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. + */ +package com.google.cloud.monitoring.spi.v3; + +import com.google.api.MonitoredResource; +import com.google.api.gax.core.PageAccessor; +import com.google.api.gax.grpc.ApiCallable; +import com.google.api.gax.protobuf.PathTemplate; +import com.google.monitoring.v3.CreateGroupRequest; +import com.google.monitoring.v3.DeleteGroupRequest; +import com.google.monitoring.v3.GetGroupRequest; +import com.google.monitoring.v3.Group; +import com.google.monitoring.v3.ListGroupMembersRequest; +import com.google.monitoring.v3.ListGroupMembersResponse; +import com.google.monitoring.v3.ListGroupsRequest; +import com.google.monitoring.v3.ListGroupsResponse; +import com.google.monitoring.v3.TimeInterval; +import com.google.monitoring.v3.UpdateGroupRequest; +import com.google.protobuf.Empty; +import io.grpc.ManagedChannel; +import java.io.Closeable; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ScheduledExecutorService; + +// AUTO-GENERATED DOCUMENTATION AND SERVICE +/** + * Service Description: The Group API lets you inspect and manage your + * [groups](google.monitoring.v3.Group). + * + * A group is a named filter that is used to identify + * a collection of monitored resources. Groups are typically used to + * mirror the physical and/or logical topology of the environment. + * Because group membership is computed dynamically, monitored + * resources that are started in the future are automatically placed + * in matching groups. By using a group to name monitored resources in, + * for example, an alert policy, the target of that alert policy is + * updated automatically as monitored resources are added and removed + * from the infrastructure. + * + *

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 (GroupServiceApi groupServiceApi = GroupServiceApi.create()) {
+ *   String formattedName = GroupServiceApi.formatGroupName("[PROJECT]", "[GROUP]");
+ *   Group response = groupServiceApi.getGroup(formattedName);
+ * }
+ * 
+ * 
+ * + *

Note: close() needs to be called on the groupServiceApi 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 GroupServiceSettings to + * create(). For example: + * + *

+ * 
+ * GroupServiceSettings groupServiceSettings = GroupServiceSettings.defaultBuilder()
+ *     .provideChannelWith(myCredentials)
+ *     .build();
+ * GroupServiceApi groupServiceApi = GroupServiceApi.create(groupServiceSettings);
+ * 
+ * 
+ */ +@javax.annotation.Generated("by GAPIC") +public class GroupServiceApi implements AutoCloseable { + private final GroupServiceSettings settings; + private final ManagedChannel channel; + private final ScheduledExecutorService executor; + private final List closeables = new ArrayList<>(); + + private final ApiCallable listGroupsCallable; + private final ApiCallable> listGroupsPagedCallable; + private final ApiCallable getGroupCallable; + private final ApiCallable createGroupCallable; + private final ApiCallable updateGroupCallable; + private final ApiCallable deleteGroupCallable; + private final ApiCallable + listGroupMembersCallable; + private final ApiCallable> + listGroupMembersPagedCallable; + + public final GroupServiceSettings getSettings() { + return settings; + } + + private static final PathTemplate PROJECT_PATH_TEMPLATE = + PathTemplate.createWithoutUrlEncoding("projects/{project}"); + + private static final PathTemplate GROUP_PATH_TEMPLATE = + PathTemplate.createWithoutUrlEncoding("projects/{project}/groups/{group}"); + + /** + * Formats a string containing the fully-qualified path to represent + * a project resource. + */ + public static final String formatProjectName(String project) { + return PROJECT_PATH_TEMPLATE.instantiate("project", project); + } + + /** + * Formats a string containing the fully-qualified path to represent + * a group resource. + */ + public static final String formatGroupName(String project, String group) { + return GROUP_PATH_TEMPLATE.instantiate( + "project", project, + "group", group); + } + + /** + * Parses the project from the given fully-qualified path which + * represents a project resource. + */ + public static final String parseProjectFromProjectName(String projectName) { + return PROJECT_PATH_TEMPLATE.parse(projectName).get("project"); + } + + /** + * Parses the project from the given fully-qualified path which + * represents a group resource. + */ + public static final String parseProjectFromGroupName(String groupName) { + return GROUP_PATH_TEMPLATE.parse(groupName).get("project"); + } + + /** + * Parses the group from the given fully-qualified path which + * represents a group resource. + */ + public static final String parseGroupFromGroupName(String groupName) { + return GROUP_PATH_TEMPLATE.parse(groupName).get("group"); + } + + /** + * Constructs an instance of GroupServiceApi with default settings. + */ + public static final GroupServiceApi create() throws IOException { + return create(GroupServiceSettings.defaultBuilder().build()); + } + + /** + * Constructs an instance of GroupServiceApi, using the given settings. + * The channels are created based on the settings passed in, or defaults for any + * settings that are not set. + */ + public static final GroupServiceApi create(GroupServiceSettings settings) throws IOException { + return new GroupServiceApi(settings); + } + + /** + * Constructs an instance of GroupServiceApi, using the given settings. + * This is protected so that it easy to make a subclass, but otherwise, the static + * factory methods should be preferred. + */ + protected GroupServiceApi(GroupServiceSettings settings) throws IOException { + this.settings = settings; + this.executor = settings.getExecutorProvider().getOrBuildExecutor(); + this.channel = settings.getChannelProvider().getOrBuildChannel(this.executor); + + this.listGroupsCallable = + ApiCallable.create(settings.listGroupsSettings(), this.channel, this.executor); + this.listGroupsPagedCallable = + ApiCallable.createPagedVariant(settings.listGroupsSettings(), this.channel, this.executor); + this.getGroupCallable = + ApiCallable.create(settings.getGroupSettings(), this.channel, this.executor); + this.createGroupCallable = + ApiCallable.create(settings.createGroupSettings(), this.channel, this.executor); + this.updateGroupCallable = + ApiCallable.create(settings.updateGroupSettings(), this.channel, this.executor); + this.deleteGroupCallable = + ApiCallable.create(settings.deleteGroupSettings(), this.channel, this.executor); + this.listGroupMembersCallable = + ApiCallable.create(settings.listGroupMembersSettings(), this.channel, this.executor); + this.listGroupMembersPagedCallable = + ApiCallable.createPagedVariant( + settings.listGroupMembersSettings(), this.channel, this.executor); + + if (settings.getChannelProvider().shouldAutoClose()) { + closeables.add( + new Closeable() { + @Override + public void close() throws IOException { + channel.shutdown(); + } + }); + } + if (settings.getExecutorProvider().shouldAutoClose()) { + closeables.add( + new Closeable() { + @Override + public void close() throws IOException { + executor.shutdown(); + } + }); + } + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Lists the existing groups. The project ID in the URL path must refer + * to a Stackdriver account. + * + * Sample code: + *

+   * try (GroupServiceApi groupServiceApi = GroupServiceApi.create()) {
+   *   String formattedName = GroupServiceApi.formatProjectName("[PROJECT]");
+   *   ListGroupsRequest request = ListGroupsRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .build();
+   *   for (Group element : groupServiceApi.listGroups(request)) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final PageAccessor listGroups(ListGroupsRequest request) { + return listGroupsPagedCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Lists the existing groups. The project ID in the URL path must refer + * to a Stackdriver account. + * + * Sample code: + *

+   * try (GroupServiceApi groupServiceApi = GroupServiceApi.create()) {
+   *   String formattedName = GroupServiceApi.formatProjectName("[PROJECT]");
+   *   ListGroupsRequest request = ListGroupsRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .build();
+   *   ListenableFuture<PageAccessor<Group>> future = groupServiceApi.listGroupsPagedCallable().futureCall(request);
+   *   // Do something
+   *   for (Group element : future.get()) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * 
+ */ + public final ApiCallable> listGroupsPagedCallable() { + return listGroupsPagedCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Lists the existing groups. The project ID in the URL path must refer + * to a Stackdriver account. + * + * Sample code: + *

+   * try (GroupServiceApi groupServiceApi = GroupServiceApi.create()) {
+   *   String formattedName = GroupServiceApi.formatProjectName("[PROJECT]");
+   *   ListGroupsRequest request = ListGroupsRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .build();
+   *   while (true) {
+   *     ListGroupsResponse response = groupServiceApi.listGroupsCallable().call(request);
+   *     for (Group element : response.getGroupList()) {
+   *       // doThingsWith(element);
+   *     }
+   *     String nextPageToken = response.getNextPageToken();
+   *     if (!Strings.isNullOrEmpty(nextPageToken)) {
+   *       request = request.toBuilder().setPageToken(nextPageToken).build();
+   *     } else {
+   *       break;
+   *     }
+   *   }
+   * }
+   * 
+ */ + public final ApiCallable listGroupsCallable() { + return listGroupsCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Gets a single group. The project ID in the URL path must refer to a + * Stackdriver account. + * + * Sample code: + *

+   * try (GroupServiceApi groupServiceApi = GroupServiceApi.create()) {
+   *   String formattedName = GroupServiceApi.formatGroupName("[PROJECT]", "[GROUP]");
+   *   Group response = groupServiceApi.getGroup(formattedName);
+   * }
+   * 
+ * + * @param name The group to retrieve. The format is + * `"projects/{project_id_or_number}/groups/{group_id}"`. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final Group getGroup(String name) { + GROUP_PATH_TEMPLATE.validate(name, "getGroup"); + GetGroupRequest request = GetGroupRequest.newBuilder().setName(name).build(); + return getGroup(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Gets a single group. The project ID in the URL path must refer to a + * Stackdriver account. + * + * Sample code: + *

+   * try (GroupServiceApi groupServiceApi = GroupServiceApi.create()) {
+   *   String formattedName = GroupServiceApi.formatGroupName("[PROJECT]", "[GROUP]");
+   *   GetGroupRequest request = GetGroupRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .build();
+   *   Group response = groupServiceApi.getGroup(request);
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + private final Group getGroup(GetGroupRequest request) { + return getGroupCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Gets a single group. The project ID in the URL path must refer to a + * Stackdriver account. + * + * Sample code: + *

+   * try (GroupServiceApi groupServiceApi = GroupServiceApi.create()) {
+   *   String formattedName = GroupServiceApi.formatGroupName("[PROJECT]", "[GROUP]");
+   *   GetGroupRequest request = GetGroupRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .build();
+   *   ListenableFuture<Group> future = groupServiceApi.getGroupCallable().futureCall(request);
+   *   // Do something
+   *   Group response = future.get();
+   * }
+   * 
+ */ + public final ApiCallable getGroupCallable() { + return getGroupCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Creates a new group. The project ID in the URL path must refer to a + * Stackdriver account. + * + * Sample code: + *

+   * try (GroupServiceApi groupServiceApi = GroupServiceApi.create()) {
+   *   String formattedName = GroupServiceApi.formatProjectName("[PROJECT]");
+   *   Group group = Group.newBuilder().build();
+   *   boolean validateOnly = false;
+   *   Group response = groupServiceApi.createGroup(formattedName, group, validateOnly);
+   * }
+   * 
+ * + * @param name The project in which to create the group. The format is + * `"projects/{project_id_or_number}"`. + * @param group A group definition. It is an error to define the `name` field because + * the system assigns the name. + * @param validateOnly If true, validate this request but do not create the group. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final Group createGroup(String name, Group group, boolean validateOnly) { + PROJECT_PATH_TEMPLATE.validate(name, "createGroup"); + CreateGroupRequest request = + CreateGroupRequest.newBuilder() + .setName(name) + .setGroup(group) + .setValidateOnly(validateOnly) + .build(); + return createGroup(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Creates a new group. The project ID in the URL path must refer to a + * Stackdriver account. + * + * Sample code: + *

+   * try (GroupServiceApi groupServiceApi = GroupServiceApi.create()) {
+   *   String formattedName = GroupServiceApi.formatProjectName("[PROJECT]");
+   *   Group group = Group.newBuilder().build();
+   *   boolean validateOnly = false;
+   *   CreateGroupRequest request = CreateGroupRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .setGroup(group)
+   *     .setValidateOnly(validateOnly)
+   *     .build();
+   *   Group response = groupServiceApi.createGroup(request);
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final Group createGroup(CreateGroupRequest request) { + return createGroupCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Creates a new group. The project ID in the URL path must refer to a + * Stackdriver account. + * + * Sample code: + *

+   * try (GroupServiceApi groupServiceApi = GroupServiceApi.create()) {
+   *   String formattedName = GroupServiceApi.formatProjectName("[PROJECT]");
+   *   Group group = Group.newBuilder().build();
+   *   boolean validateOnly = false;
+   *   CreateGroupRequest request = CreateGroupRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .setGroup(group)
+   *     .setValidateOnly(validateOnly)
+   *     .build();
+   *   ListenableFuture<Group> future = groupServiceApi.createGroupCallable().futureCall(request);
+   *   // Do something
+   *   Group response = future.get();
+   * }
+   * 
+ */ + public final ApiCallable createGroupCallable() { + return createGroupCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Updates an existing group. + * You can change any group attributes except `name`. + * The project ID in the URL path must refer to a Stackdriver account. + * + * Sample code: + *

+   * try (GroupServiceApi groupServiceApi = GroupServiceApi.create()) {
+   *   Group group = Group.newBuilder().build();
+   *   boolean validateOnly = false;
+   *   Group response = groupServiceApi.updateGroup(group, validateOnly);
+   * }
+   * 
+ * + * @param group The new definition of the group. All fields of the existing group, + * excepting `name`, are replaced with the corresponding fields of this group. + * @param validateOnly If true, validate this request but do not update the existing group. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final Group updateGroup(Group group, boolean validateOnly) { + UpdateGroupRequest request = + UpdateGroupRequest.newBuilder().setGroup(group).setValidateOnly(validateOnly).build(); + return updateGroup(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Updates an existing group. + * You can change any group attributes except `name`. + * The project ID in the URL path must refer to a Stackdriver account. + * + * Sample code: + *

+   * try (GroupServiceApi groupServiceApi = GroupServiceApi.create()) {
+   *   Group group = Group.newBuilder().build();
+   *   boolean validateOnly = false;
+   *   UpdateGroupRequest request = UpdateGroupRequest.newBuilder()
+   *     .setGroup(group)
+   *     .setValidateOnly(validateOnly)
+   *     .build();
+   *   Group response = groupServiceApi.updateGroup(request);
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final Group updateGroup(UpdateGroupRequest request) { + return updateGroupCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Updates an existing group. + * You can change any group attributes except `name`. + * The project ID in the URL path must refer to a Stackdriver account. + * + * Sample code: + *

+   * try (GroupServiceApi groupServiceApi = GroupServiceApi.create()) {
+   *   Group group = Group.newBuilder().build();
+   *   boolean validateOnly = false;
+   *   UpdateGroupRequest request = UpdateGroupRequest.newBuilder()
+   *     .setGroup(group)
+   *     .setValidateOnly(validateOnly)
+   *     .build();
+   *   ListenableFuture<Group> future = groupServiceApi.updateGroupCallable().futureCall(request);
+   *   // Do something
+   *   Group response = future.get();
+   * }
+   * 
+ */ + public final ApiCallable updateGroupCallable() { + return updateGroupCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Deletes an existing group. The project ID in the URL path must refer to a + * Stackdriver account. + * + * Sample code: + *

+   * try (GroupServiceApi groupServiceApi = GroupServiceApi.create()) {
+   *   String formattedName = GroupServiceApi.formatGroupName("[PROJECT]", "[GROUP]");
+   *   groupServiceApi.deleteGroup(formattedName);
+   * }
+   * 
+ * + * @param name The group to delete. The format is + * `"projects/{project_id_or_number}/groups/{group_id}"`. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final void deleteGroup(String name) { + GROUP_PATH_TEMPLATE.validate(name, "deleteGroup"); + DeleteGroupRequest request = DeleteGroupRequest.newBuilder().setName(name).build(); + deleteGroup(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Deletes an existing group. The project ID in the URL path must refer to a + * Stackdriver account. + * + * Sample code: + *

+   * try (GroupServiceApi groupServiceApi = GroupServiceApi.create()) {
+   *   String formattedName = GroupServiceApi.formatGroupName("[PROJECT]", "[GROUP]");
+   *   DeleteGroupRequest request = DeleteGroupRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .build();
+   *   groupServiceApi.deleteGroup(request);
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + private final void deleteGroup(DeleteGroupRequest request) { + deleteGroupCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Deletes an existing group. The project ID in the URL path must refer to a + * Stackdriver account. + * + * Sample code: + *

+   * try (GroupServiceApi groupServiceApi = GroupServiceApi.create()) {
+   *   String formattedName = GroupServiceApi.formatGroupName("[PROJECT]", "[GROUP]");
+   *   DeleteGroupRequest request = DeleteGroupRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .build();
+   *   ListenableFuture<Void> future = groupServiceApi.deleteGroupCallable().futureCall(request);
+   *   // Do something
+   *   future.get();
+   * }
+   * 
+ */ + public final ApiCallable deleteGroupCallable() { + return deleteGroupCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Lists the monitored resources that are members of a group. The project ID + * in the URL path must refer to a Stackdriver account. + * + * Sample code: + *

+   * try (GroupServiceApi groupServiceApi = GroupServiceApi.create()) {
+   *   String formattedName = GroupServiceApi.formatGroupName("[PROJECT]", "[GROUP]");
+   *   String filter = "";
+   *   TimeInterval interval = TimeInterval.newBuilder().build();
+   *   for (MonitoredResource element : groupServiceApi.listGroupMembers(formattedName, filter, interval)) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * 
+ * + * @param name The group whose members are listed. The format is + * `"projects/{project_id_or_number}/groups/{group_id}"`. + * @param filter An optional [list filter](/monitoring/api/learn_more#filtering) describing + * the members to be returned. The filter may reference the type, labels, and + * metadata of monitored resources that comprise the group. + * For example, to return only resources representing Compute Engine VM + * instances, use this filter: + * + * resource.type = "gce_instance" + * @param interval An optional time interval for which results should be returned. Only + * members that were part of the group during the specified interval are + * included in the response. If no interval is provided then the group + * membership over the last minute is returned. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final PageAccessor listGroupMembers( + String name, String filter, TimeInterval interval) { + GROUP_PATH_TEMPLATE.validate(name, "listGroupMembers"); + ListGroupMembersRequest request = + ListGroupMembersRequest.newBuilder() + .setName(name) + .setFilter(filter) + .setInterval(interval) + .build(); + return listGroupMembers(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Lists the monitored resources that are members of a group. The project ID + * in the URL path must refer to a Stackdriver account. + * + * Sample code: + *

+   * try (GroupServiceApi groupServiceApi = GroupServiceApi.create()) {
+   *   String formattedName = GroupServiceApi.formatGroupName("[PROJECT]", "[GROUP]");
+   *   String filter = "";
+   *   TimeInterval interval = TimeInterval.newBuilder().build();
+   *   ListGroupMembersRequest request = ListGroupMembersRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .setFilter(filter)
+   *     .setInterval(interval)
+   *     .build();
+   *   for (MonitoredResource element : groupServiceApi.listGroupMembers(request)) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final PageAccessor listGroupMembers(ListGroupMembersRequest request) { + return listGroupMembersPagedCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Lists the monitored resources that are members of a group. The project ID + * in the URL path must refer to a Stackdriver account. + * + * Sample code: + *

+   * try (GroupServiceApi groupServiceApi = GroupServiceApi.create()) {
+   *   String formattedName = GroupServiceApi.formatGroupName("[PROJECT]", "[GROUP]");
+   *   String filter = "";
+   *   TimeInterval interval = TimeInterval.newBuilder().build();
+   *   ListGroupMembersRequest request = ListGroupMembersRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .setFilter(filter)
+   *     .setInterval(interval)
+   *     .build();
+   *   ListenableFuture<PageAccessor<MonitoredResource>> future = groupServiceApi.listGroupMembersPagedCallable().futureCall(request);
+   *   // Do something
+   *   for (MonitoredResource element : future.get()) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * 
+ */ + public final ApiCallable> + listGroupMembersPagedCallable() { + return listGroupMembersPagedCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Lists the monitored resources that are members of a group. The project ID + * in the URL path must refer to a Stackdriver account. + * + * Sample code: + *

+   * try (GroupServiceApi groupServiceApi = GroupServiceApi.create()) {
+   *   String formattedName = GroupServiceApi.formatGroupName("[PROJECT]", "[GROUP]");
+   *   String filter = "";
+   *   TimeInterval interval = TimeInterval.newBuilder().build();
+   *   ListGroupMembersRequest request = ListGroupMembersRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .setFilter(filter)
+   *     .setInterval(interval)
+   *     .build();
+   *   while (true) {
+   *     ListGroupMembersResponse response = groupServiceApi.listGroupMembersCallable().call(request);
+   *     for (MonitoredResource element : response.getMembersList()) {
+   *       // doThingsWith(element);
+   *     }
+   *     String nextPageToken = response.getNextPageToken();
+   *     if (!Strings.isNullOrEmpty(nextPageToken)) {
+   *       request = request.toBuilder().setPageToken(nextPageToken).build();
+   *     } else {
+   *       break;
+   *     }
+   *   }
+   * }
+   * 
+ */ + public final ApiCallable + listGroupMembersCallable() { + return listGroupMembersCallable; + } + + /** + * Initiates an orderly shutdown in which preexisting calls continue but new calls are immediately + * cancelled. + */ + @Override + public final void close() throws Exception { + for (AutoCloseable closeable : closeables) { + closeable.close(); + } + } +} diff --git a/google-cloud-monitoring/src/main/java/com/google/cloud/monitoring/spi/v3/GroupServiceSettings.java b/google-cloud-monitoring/src/main/java/com/google/cloud/monitoring/spi/v3/GroupServiceSettings.java new file mode 100644 index 000000000000..a414f70a079e --- /dev/null +++ b/google-cloud-monitoring/src/main/java/com/google/cloud/monitoring/spi/v3/GroupServiceSettings.java @@ -0,0 +1,509 @@ +/* + * 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. + */ +package com.google.cloud.monitoring.spi.v3; + +import com.google.api.MonitoredResource; +import com.google.api.gax.core.ConnectionSettings; +import com.google.api.gax.core.RetrySettings; +import com.google.api.gax.grpc.ApiCallSettings; +import com.google.api.gax.grpc.PageStreamingCallSettings; +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; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; +import com.google.monitoring.v3.CreateGroupRequest; +import com.google.monitoring.v3.DeleteGroupRequest; +import com.google.monitoring.v3.GetGroupRequest; +import com.google.monitoring.v3.Group; +import com.google.monitoring.v3.GroupServiceGrpc; +import com.google.monitoring.v3.ListGroupMembersRequest; +import com.google.monitoring.v3.ListGroupMembersResponse; +import com.google.monitoring.v3.ListGroupsRequest; +import com.google.monitoring.v3.ListGroupsResponse; +import com.google.monitoring.v3.UpdateGroupRequest; +import com.google.protobuf.Empty; +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; + +// AUTO-GENERATED DOCUMENTATION AND CLASS +/** + * Settings class to configure an instance of {@link GroupServiceApi}. + * + *

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

    + *
  • The default service address (monitoring.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 getGroup to 30 seconds: + * + *

+ * 
+ * GroupServiceSettings.Builder groupServiceSettingsBuilder =
+ *     GroupServiceSettings.defaultBuilder();
+ * groupServiceSettingsBuilder.getGroupSettings().getRetrySettingsBuilder()
+ *     .setTotalTimeout(Duration.standardSeconds(30));
+ * GroupServiceSettings groupServiceSettings = groupServiceSettingsBuilder.build();
+ * 
+ * 
+ */ +@javax.annotation.Generated("by GAPIC") +public class GroupServiceSettings extends ServiceApiSettings { + /** + * The default address of the service. + */ + private static final String DEFAULT_SERVICE_ADDRESS = "monitoring.googleapis.com"; + + /** + * The default port of the service. + */ + private static final int DEFAULT_SERVICE_PORT = 443; + + /** + * The default scopes of the service. + */ + private static final ImmutableList DEFAULT_SERVICE_SCOPES = + ImmutableList.builder().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 + listGroupsSettings; + private final SimpleCallSettings getGroupSettings; + private final SimpleCallSettings createGroupSettings; + private final SimpleCallSettings updateGroupSettings; + private final SimpleCallSettings deleteGroupSettings; + private final PageStreamingCallSettings< + ListGroupMembersRequest, ListGroupMembersResponse, MonitoredResource> + listGroupMembersSettings; + + /** + * Returns the object with the settings used for calls to listGroups. + */ + public PageStreamingCallSettings + listGroupsSettings() { + return listGroupsSettings; + } + + /** + * Returns the object with the settings used for calls to getGroup. + */ + public SimpleCallSettings getGroupSettings() { + return getGroupSettings; + } + + /** + * Returns the object with the settings used for calls to createGroup. + */ + public SimpleCallSettings createGroupSettings() { + return createGroupSettings; + } + + /** + * Returns the object with the settings used for calls to updateGroup. + */ + public SimpleCallSettings updateGroupSettings() { + return updateGroupSettings; + } + + /** + * Returns the object with the settings used for calls to deleteGroup. + */ + public SimpleCallSettings deleteGroupSettings() { + return deleteGroupSettings; + } + + /** + * Returns the object with the settings used for calls to listGroupMembers. + */ + public PageStreamingCallSettings< + ListGroupMembersRequest, ListGroupMembersResponse, MonitoredResource> + listGroupMembersSettings() { + return listGroupMembersSettings; + } + + /** + * Returns the default service address. + */ + public static String getDefaultServiceAddress() { + return DEFAULT_SERVICE_ADDRESS; + } + + /** + * Returns the default service port. + */ + public static int getDefaultServicePort() { + return DEFAULT_SERVICE_PORT; + } + + /** + * Returns the default service scopes. + */ + public static ImmutableList getDefaultServiceScopes() { + return DEFAULT_SERVICE_SCOPES; + } + + /** + * 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 GroupServiceSettings(Builder settingsBuilder) throws IOException { + super( + settingsBuilder.getChannelProvider(), + settingsBuilder.getExecutorProvider(), + settingsBuilder.getGeneratorName(), + settingsBuilder.getGeneratorVersion(), + settingsBuilder.getClientLibName(), + settingsBuilder.getClientLibVersion()); + + listGroupsSettings = settingsBuilder.listGroupsSettings().build(); + getGroupSettings = settingsBuilder.getGroupSettings().build(); + createGroupSettings = settingsBuilder.createGroupSettings().build(); + updateGroupSettings = settingsBuilder.updateGroupSettings().build(); + deleteGroupSettings = settingsBuilder.deleteGroupSettings().build(); + listGroupMembersSettings = settingsBuilder.listGroupMembersSettings().build(); + } + + private static PageStreamingDescriptor + LIST_GROUPS_PAGE_STR_DESC = + new PageStreamingDescriptor() { + @Override + public Object emptyToken() { + return ""; + } + + @Override + public ListGroupsRequest injectToken(ListGroupsRequest payload, Object token) { + return ListGroupsRequest.newBuilder(payload).setPageToken((String) token).build(); + } + + @Override + public Object extractNextToken(ListGroupsResponse payload) { + return payload.getNextPageToken(); + } + + @Override + public Iterable extractResources(ListGroupsResponse payload) { + return payload.getGroupList(); + } + }; + + private static PageStreamingDescriptor< + ListGroupMembersRequest, ListGroupMembersResponse, MonitoredResource> + LIST_GROUP_MEMBERS_PAGE_STR_DESC = + new PageStreamingDescriptor< + ListGroupMembersRequest, ListGroupMembersResponse, MonitoredResource>() { + @Override + public Object emptyToken() { + return ""; + } + + @Override + public ListGroupMembersRequest injectToken( + ListGroupMembersRequest payload, Object token) { + return ListGroupMembersRequest.newBuilder(payload) + .setPageToken((String) token) + .build(); + } + + @Override + public Object extractNextToken(ListGroupMembersResponse payload) { + return payload.getNextPageToken(); + } + + @Override + public Iterable extractResources(ListGroupMembersResponse payload) { + return payload.getMembersList(); + } + }; + + /** + * Builder for GroupServiceSettings. + */ + public static class Builder extends ServiceApiSettings.Builder { + private final ImmutableList methodSettingsBuilders; + + private PageStreamingCallSettings.Builder + listGroupsSettings; + private SimpleCallSettings.Builder getGroupSettings; + private SimpleCallSettings.Builder createGroupSettings; + private SimpleCallSettings.Builder updateGroupSettings; + private SimpleCallSettings.Builder deleteGroupSettings; + private PageStreamingCallSettings.Builder< + ListGroupMembersRequest, ListGroupMembersResponse, MonitoredResource> + listGroupMembersSettings; + + private static final ImmutableMap> RETRYABLE_CODE_DEFINITIONS; + + static { + ImmutableMap.Builder> definitions = ImmutableMap.builder(); + definitions.put( + "idempotent", + Sets.immutableEnumSet( + Lists.newArrayList( + Status.Code.DEADLINE_EXCEEDED, Status.Code.UNAVAILABLE))); + definitions.put("non_idempotent", Sets.immutableEnumSet(Lists.newArrayList())); + RETRYABLE_CODE_DEFINITIONS = definitions.build(); + } + + private static final ImmutableMap RETRY_PARAM_DEFINITIONS; + + static { + ImmutableMap.Builder definitions = ImmutableMap.builder(); + RetrySettings.Builder settingsBuilder = null; + settingsBuilder = + RetrySettings.newBuilder() + .setInitialRetryDelay(Duration.millis(100L)) + .setRetryDelayMultiplier(1.3) + .setMaxRetryDelay(Duration.millis(60000L)) + .setInitialRpcTimeout(Duration.millis(20000L)) + .setRpcTimeoutMultiplier(1.0) + .setMaxRpcTimeout(Duration.millis(20000L)) + .setTotalTimeout(Duration.millis(600000L)); + definitions.put("default", settingsBuilder); + RETRY_PARAM_DEFINITIONS = definitions.build(); + } + + private Builder() { + super(DEFAULT_CONNECTION_SETTINGS); + + listGroupsSettings = + PageStreamingCallSettings.newBuilder( + GroupServiceGrpc.METHOD_LIST_GROUPS, LIST_GROUPS_PAGE_STR_DESC); + + getGroupSettings = SimpleCallSettings.newBuilder(GroupServiceGrpc.METHOD_GET_GROUP); + + createGroupSettings = SimpleCallSettings.newBuilder(GroupServiceGrpc.METHOD_CREATE_GROUP); + + updateGroupSettings = SimpleCallSettings.newBuilder(GroupServiceGrpc.METHOD_UPDATE_GROUP); + + deleteGroupSettings = SimpleCallSettings.newBuilder(GroupServiceGrpc.METHOD_DELETE_GROUP); + + listGroupMembersSettings = + PageStreamingCallSettings.newBuilder( + GroupServiceGrpc.METHOD_LIST_GROUP_MEMBERS, LIST_GROUP_MEMBERS_PAGE_STR_DESC); + + methodSettingsBuilders = + ImmutableList.of( + listGroupsSettings, + getGroupSettings, + createGroupSettings, + updateGroupSettings, + deleteGroupSettings, + listGroupMembersSettings); + } + + private static Builder createDefault() { + Builder builder = new Builder(); + + builder + .listGroupsSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .getGroupSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .createGroupSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .updateGroupSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .deleteGroupSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .listGroupMembersSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + return builder; + } + + private Builder(GroupServiceSettings settings) { + super(settings); + + listGroupsSettings = settings.listGroupsSettings.toBuilder(); + getGroupSettings = settings.getGroupSettings.toBuilder(); + createGroupSettings = settings.createGroupSettings.toBuilder(); + updateGroupSettings = settings.updateGroupSettings.toBuilder(); + deleteGroupSettings = settings.deleteGroupSettings.toBuilder(); + listGroupMembersSettings = settings.listGroupMembersSettings.toBuilder(); + + methodSettingsBuilders = + ImmutableList.of( + listGroupsSettings, + getGroupSettings, + createGroupSettings, + updateGroupSettings, + deleteGroupSettings, + listGroupMembersSettings); + } + + @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); + return this; + } + + @Override + public Builder provideChannelWith(ConnectionSettings settings) { + super.provideChannelWith(settings); + return this; + } + + @Override + public Builder provideChannelWith(Credentials credentials) { + super.provideChannelWith(credentials); + return this; + } + + @Override + public Builder provideChannelWith(List scopes) { + super.provideChannelWith(scopes); + return this; + } + + @Override + public Builder setGeneratorHeader(String name, String version) { + super.setGeneratorHeader(name, version); + return this; + } + + @Override + public Builder setClientLibHeader(String name, String version) { + super.setClientLibHeader(name, 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 listGroups. + */ + public PageStreamingCallSettings.Builder + listGroupsSettings() { + return listGroupsSettings; + } + + /** + * Returns the builder for the settings used for calls to getGroup. + */ + public SimpleCallSettings.Builder getGroupSettings() { + return getGroupSettings; + } + + /** + * Returns the builder for the settings used for calls to createGroup. + */ + public SimpleCallSettings.Builder createGroupSettings() { + return createGroupSettings; + } + + /** + * Returns the builder for the settings used for calls to updateGroup. + */ + public SimpleCallSettings.Builder updateGroupSettings() { + return updateGroupSettings; + } + + /** + * Returns the builder for the settings used for calls to deleteGroup. + */ + public SimpleCallSettings.Builder deleteGroupSettings() { + return deleteGroupSettings; + } + + /** + * Returns the builder for the settings used for calls to listGroupMembers. + */ + public PageStreamingCallSettings.Builder< + ListGroupMembersRequest, ListGroupMembersResponse, MonitoredResource> + listGroupMembersSettings() { + return listGroupMembersSettings; + } + + @Override + public GroupServiceSettings build() throws IOException { + return new GroupServiceSettings(this); + } + } +} diff --git a/google-cloud-monitoring/src/main/java/com/google/cloud/monitoring/spi/v3/MetricServiceApi.java b/google-cloud-monitoring/src/main/java/com/google/cloud/monitoring/spi/v3/MetricServiceApi.java new file mode 100644 index 000000000000..09d5ba3d45f5 --- /dev/null +++ b/google-cloud-monitoring/src/main/java/com/google/cloud/monitoring/spi/v3/MetricServiceApi.java @@ -0,0 +1,1037 @@ +/* + * 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. + */ +package com.google.cloud.monitoring.spi.v3; + +import com.google.api.MetricDescriptor; +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.monitoring.v3.Aggregation; +import com.google.monitoring.v3.CreateMetricDescriptorRequest; +import com.google.monitoring.v3.CreateTimeSeriesRequest; +import com.google.monitoring.v3.DeleteMetricDescriptorRequest; +import com.google.monitoring.v3.GetMetricDescriptorRequest; +import com.google.monitoring.v3.GetMonitoredResourceDescriptorRequest; +import com.google.monitoring.v3.ListMetricDescriptorsRequest; +import com.google.monitoring.v3.ListMetricDescriptorsResponse; +import com.google.monitoring.v3.ListMonitoredResourceDescriptorsRequest; +import com.google.monitoring.v3.ListMonitoredResourceDescriptorsResponse; +import com.google.monitoring.v3.ListTimeSeriesRequest; +import com.google.monitoring.v3.ListTimeSeriesRequest.TimeSeriesView; +import com.google.monitoring.v3.ListTimeSeriesResponse; +import com.google.monitoring.v3.TimeInterval; +import com.google.monitoring.v3.TimeSeries; +import com.google.protobuf.Empty; +import io.grpc.ManagedChannel; +import java.io.Closeable; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ScheduledExecutorService; + +// AUTO-GENERATED DOCUMENTATION AND SERVICE +/** + * Service Description: Manages metric descriptors, monitored resource descriptors, and + * time series data. + * + *

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 (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+ *   String formattedName = MetricServiceApi.formatMonitoredResourceDescriptorName("[PROJECT]", "[MONITORED_RESOURCE_DESCRIPTOR]");
+ *   MonitoredResourceDescriptor response = metricServiceApi.getMonitoredResourceDescriptor(formattedName);
+ * }
+ * 
+ * 
+ * + *

Note: close() needs to be called on the metricServiceApi 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 MetricServiceSettings to + * create(). For example: + * + *

+ * 
+ * MetricServiceSettings metricServiceSettings = MetricServiceSettings.defaultBuilder()
+ *     .provideChannelWith(myCredentials)
+ *     .build();
+ * MetricServiceApi metricServiceApi = MetricServiceApi.create(metricServiceSettings);
+ * 
+ * 
+ */ +@javax.annotation.Generated("by GAPIC") +public class MetricServiceApi implements AutoCloseable { + private final MetricServiceSettings settings; + private final ManagedChannel channel; + private final ScheduledExecutorService executor; + private final List closeables = new ArrayList<>(); + + private final ApiCallable< + ListMonitoredResourceDescriptorsRequest, ListMonitoredResourceDescriptorsResponse> + listMonitoredResourceDescriptorsCallable; + private final ApiCallable< + ListMonitoredResourceDescriptorsRequest, PageAccessor> + listMonitoredResourceDescriptorsPagedCallable; + private final ApiCallable + getMonitoredResourceDescriptorCallable; + private final ApiCallable + listMetricDescriptorsCallable; + private final ApiCallable> + listMetricDescriptorsPagedCallable; + private final ApiCallable + getMetricDescriptorCallable; + private final ApiCallable + createMetricDescriptorCallable; + private final ApiCallable deleteMetricDescriptorCallable; + private final ApiCallable listTimeSeriesCallable; + private final ApiCallable> + listTimeSeriesPagedCallable; + private final ApiCallable createTimeSeriesCallable; + + public final MetricServiceSettings getSettings() { + return settings; + } + + private static final PathTemplate PROJECT_PATH_TEMPLATE = + PathTemplate.createWithoutUrlEncoding("projects/{project}"); + + private static final PathTemplate METRIC_DESCRIPTOR_PATH_PATH_TEMPLATE = + PathTemplate.createWithoutUrlEncoding( + "projects/{project}/metricDescriptors/{metric_descriptor_path=**}"); + + private static final PathTemplate MONITORED_RESOURCE_DESCRIPTOR_PATH_TEMPLATE = + PathTemplate.createWithoutUrlEncoding( + "projects/{project}/monitoredResourceDescriptors/{monitored_resource_descriptor}"); + + /** + * Formats a string containing the fully-qualified path to represent + * a project resource. + */ + public static final String formatProjectName(String project) { + return PROJECT_PATH_TEMPLATE.instantiate("project", project); + } + + /** + * Formats a string containing the fully-qualified path to represent + * a metric_descriptor_path resource. + */ + public static final String formatMetricDescriptorPathName( + String project, String metricDescriptorPath) { + return METRIC_DESCRIPTOR_PATH_PATH_TEMPLATE.instantiate( + "project", project, + "metric_descriptor_path", metricDescriptorPath); + } + + /** + * Formats a string containing the fully-qualified path to represent + * a monitored_resource_descriptor resource. + */ + public static final String formatMonitoredResourceDescriptorName( + String project, String monitoredResourceDescriptor) { + return MONITORED_RESOURCE_DESCRIPTOR_PATH_TEMPLATE.instantiate( + "project", project, + "monitored_resource_descriptor", monitoredResourceDescriptor); + } + + /** + * Parses the project from the given fully-qualified path which + * represents a project resource. + */ + public static final String parseProjectFromProjectName(String projectName) { + return PROJECT_PATH_TEMPLATE.parse(projectName).get("project"); + } + + /** + * Parses the project from the given fully-qualified path which + * represents a metricDescriptorPath resource. + */ + public static final String parseProjectFromMetricDescriptorPathName( + String metricDescriptorPathName) { + return METRIC_DESCRIPTOR_PATH_PATH_TEMPLATE.parse(metricDescriptorPathName).get("project"); + } + + /** + * Parses the metric_descriptor_path from the given fully-qualified path which + * represents a metricDescriptorPath resource. + */ + public static final String parseMetricDescriptorPathFromMetricDescriptorPathName( + String metricDescriptorPathName) { + return METRIC_DESCRIPTOR_PATH_PATH_TEMPLATE + .parse(metricDescriptorPathName) + .get("metric_descriptor_path"); + } + + /** + * Parses the project from the given fully-qualified path which + * represents a monitoredResourceDescriptor resource. + */ + public static final String parseProjectFromMonitoredResourceDescriptorName( + String monitoredResourceDescriptorName) { + return MONITORED_RESOURCE_DESCRIPTOR_PATH_TEMPLATE + .parse(monitoredResourceDescriptorName) + .get("project"); + } + + /** + * Parses the monitored_resource_descriptor from the given fully-qualified path which + * represents a monitoredResourceDescriptor resource. + */ + public static final String parseMonitoredResourceDescriptorFromMonitoredResourceDescriptorName( + String monitoredResourceDescriptorName) { + return MONITORED_RESOURCE_DESCRIPTOR_PATH_TEMPLATE + .parse(monitoredResourceDescriptorName) + .get("monitored_resource_descriptor"); + } + + /** + * Constructs an instance of MetricServiceApi with default settings. + */ + public static final MetricServiceApi create() throws IOException { + return create(MetricServiceSettings.defaultBuilder().build()); + } + + /** + * Constructs an instance of MetricServiceApi, using the given settings. + * The channels are created based on the settings passed in, or defaults for any + * settings that are not set. + */ + public static final MetricServiceApi create(MetricServiceSettings settings) throws IOException { + return new MetricServiceApi(settings); + } + + /** + * Constructs an instance of MetricServiceApi, using the given settings. + * This is protected so that it easy to make a subclass, but otherwise, the static + * factory methods should be preferred. + */ + protected MetricServiceApi(MetricServiceSettings settings) throws IOException { + this.settings = settings; + this.executor = settings.getExecutorProvider().getOrBuildExecutor(); + this.channel = settings.getChannelProvider().getOrBuildChannel(this.executor); + + this.listMonitoredResourceDescriptorsCallable = + ApiCallable.create( + settings.listMonitoredResourceDescriptorsSettings(), this.channel, this.executor); + this.listMonitoredResourceDescriptorsPagedCallable = + ApiCallable.createPagedVariant( + settings.listMonitoredResourceDescriptorsSettings(), this.channel, this.executor); + this.getMonitoredResourceDescriptorCallable = + ApiCallable.create( + settings.getMonitoredResourceDescriptorSettings(), this.channel, this.executor); + this.listMetricDescriptorsCallable = + ApiCallable.create(settings.listMetricDescriptorsSettings(), this.channel, this.executor); + this.listMetricDescriptorsPagedCallable = + ApiCallable.createPagedVariant( + settings.listMetricDescriptorsSettings(), this.channel, this.executor); + this.getMetricDescriptorCallable = + ApiCallable.create(settings.getMetricDescriptorSettings(), this.channel, this.executor); + this.createMetricDescriptorCallable = + ApiCallable.create(settings.createMetricDescriptorSettings(), this.channel, this.executor); + this.deleteMetricDescriptorCallable = + ApiCallable.create(settings.deleteMetricDescriptorSettings(), this.channel, this.executor); + this.listTimeSeriesCallable = + ApiCallable.create(settings.listTimeSeriesSettings(), this.channel, this.executor); + this.listTimeSeriesPagedCallable = + ApiCallable.createPagedVariant( + settings.listTimeSeriesSettings(), this.channel, this.executor); + this.createTimeSeriesCallable = + ApiCallable.create(settings.createTimeSeriesSettings(), this.channel, this.executor); + + if (settings.getChannelProvider().shouldAutoClose()) { + closeables.add( + new Closeable() { + @Override + public void close() throws IOException { + channel.shutdown(); + } + }); + } + if (settings.getExecutorProvider().shouldAutoClose()) { + closeables.add( + new Closeable() { + @Override + public void close() throws IOException { + executor.shutdown(); + } + }); + } + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Lists monitored resource descriptors that match a filter. This method does not require a Stackdriver account. + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatProjectName("[PROJECT]");
+   *   String filter = "";
+   *   for (MonitoredResourceDescriptor element : metricServiceApi.listMonitoredResourceDescriptors(formattedName, filter)) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * 
+ * + * @param name The project on which to execute the request. The format is + * `"projects/{project_id_or_number}"`. + * @param filter An optional [filter](/monitoring/api/v3/filters) describing + * the descriptors to be returned. The filter can reference + * the descriptor's type and labels. For example, the + * following filter returns only Google Compute Engine descriptors + * that have an `id` label: + * + * resource.type = starts_with("gce_") AND resource.label:id + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final PageAccessor listMonitoredResourceDescriptors( + String name, String filter) { + PROJECT_PATH_TEMPLATE.validate(name, "listMonitoredResourceDescriptors"); + ListMonitoredResourceDescriptorsRequest request = + ListMonitoredResourceDescriptorsRequest.newBuilder() + .setName(name) + .setFilter(filter) + .build(); + return listMonitoredResourceDescriptors(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Lists monitored resource descriptors that match a filter. This method does not require a Stackdriver account. + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatProjectName("[PROJECT]");
+   *   String filter = "";
+   *   ListMonitoredResourceDescriptorsRequest request = ListMonitoredResourceDescriptorsRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .setFilter(filter)
+   *     .build();
+   *   for (MonitoredResourceDescriptor element : metricServiceApi.listMonitoredResourceDescriptors(request)) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final PageAccessor listMonitoredResourceDescriptors( + ListMonitoredResourceDescriptorsRequest request) { + return listMonitoredResourceDescriptorsPagedCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Lists monitored resource descriptors that match a filter. This method does not require a Stackdriver account. + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatProjectName("[PROJECT]");
+   *   String filter = "";
+   *   ListMonitoredResourceDescriptorsRequest request = ListMonitoredResourceDescriptorsRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .setFilter(filter)
+   *     .build();
+   *   ListenableFuture<PageAccessor<MonitoredResourceDescriptor>> future = metricServiceApi.listMonitoredResourceDescriptorsPagedCallable().futureCall(request);
+   *   // Do something
+   *   for (MonitoredResourceDescriptor element : future.get()) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * 
+ */ + public final ApiCallable< + ListMonitoredResourceDescriptorsRequest, PageAccessor> + listMonitoredResourceDescriptorsPagedCallable() { + return listMonitoredResourceDescriptorsPagedCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Lists monitored resource descriptors that match a filter. This method does not require a Stackdriver account. + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatProjectName("[PROJECT]");
+   *   String filter = "";
+   *   ListMonitoredResourceDescriptorsRequest request = ListMonitoredResourceDescriptorsRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .setFilter(filter)
+   *     .build();
+   *   while (true) {
+   *     ListMonitoredResourceDescriptorsResponse response = metricServiceApi.listMonitoredResourceDescriptorsCallable().call(request);
+   *     for (MonitoredResourceDescriptor element : response.getResourceDescriptorsList()) {
+   *       // doThingsWith(element);
+   *     }
+   *     String nextPageToken = response.getNextPageToken();
+   *     if (!Strings.isNullOrEmpty(nextPageToken)) {
+   *       request = request.toBuilder().setPageToken(nextPageToken).build();
+   *     } else {
+   *       break;
+   *     }
+   *   }
+   * }
+   * 
+ */ + public final ApiCallable< + ListMonitoredResourceDescriptorsRequest, ListMonitoredResourceDescriptorsResponse> + listMonitoredResourceDescriptorsCallable() { + return listMonitoredResourceDescriptorsCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Gets a single monitored resource descriptor. This method does not require a Stackdriver account. + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatMonitoredResourceDescriptorName("[PROJECT]", "[MONITORED_RESOURCE_DESCRIPTOR]");
+   *   MonitoredResourceDescriptor response = metricServiceApi.getMonitoredResourceDescriptor(formattedName);
+   * }
+   * 
+ * + * @param name The monitored resource descriptor to get. The format is + * `"projects/{project_id_or_number}/monitoredResourceDescriptors/{resource_type}"`. + * The `{resource_type}` is a predefined type, such as + * `cloudsql_database`. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final MonitoredResourceDescriptor getMonitoredResourceDescriptor(String name) { + MONITORED_RESOURCE_DESCRIPTOR_PATH_TEMPLATE.validate(name, "getMonitoredResourceDescriptor"); + GetMonitoredResourceDescriptorRequest request = + GetMonitoredResourceDescriptorRequest.newBuilder().setName(name).build(); + return getMonitoredResourceDescriptor(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Gets a single monitored resource descriptor. This method does not require a Stackdriver account. + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatMonitoredResourceDescriptorName("[PROJECT]", "[MONITORED_RESOURCE_DESCRIPTOR]");
+   *   GetMonitoredResourceDescriptorRequest request = GetMonitoredResourceDescriptorRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .build();
+   *   MonitoredResourceDescriptor response = metricServiceApi.getMonitoredResourceDescriptor(request);
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + private final MonitoredResourceDescriptor getMonitoredResourceDescriptor( + GetMonitoredResourceDescriptorRequest request) { + return getMonitoredResourceDescriptorCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Gets a single monitored resource descriptor. This method does not require a Stackdriver account. + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatMonitoredResourceDescriptorName("[PROJECT]", "[MONITORED_RESOURCE_DESCRIPTOR]");
+   *   GetMonitoredResourceDescriptorRequest request = GetMonitoredResourceDescriptorRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .build();
+   *   ListenableFuture<MonitoredResourceDescriptor> future = metricServiceApi.getMonitoredResourceDescriptorCallable().futureCall(request);
+   *   // Do something
+   *   MonitoredResourceDescriptor response = future.get();
+   * }
+   * 
+ */ + public final ApiCallable + getMonitoredResourceDescriptorCallable() { + return getMonitoredResourceDescriptorCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Lists metric descriptors that match a filter. This method does not require a Stackdriver account. + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatProjectName("[PROJECT]");
+   *   String filter = "";
+   *   for (MetricDescriptor element : metricServiceApi.listMetricDescriptors(formattedName, filter)) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * 
+ * + * @param name The project on which to execute the request. The format is + * `"projects/{project_id_or_number}"`. + * @param filter If this field is empty, all custom and + * system-defined metric descriptors are returned. + * Otherwise, the [filter](/monitoring/api/v3/filters) + * specifies which metric descriptors are to be + * returned. For example, the following filter matches all + * [custom metrics](/monitoring/custom-metrics): + * + * metric.type = starts_with("custom.googleapis.com/") + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final PageAccessor listMetricDescriptors(String name, String filter) { + PROJECT_PATH_TEMPLATE.validate(name, "listMetricDescriptors"); + ListMetricDescriptorsRequest request = + ListMetricDescriptorsRequest.newBuilder().setName(name).setFilter(filter).build(); + return listMetricDescriptors(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Lists metric descriptors that match a filter. This method does not require a Stackdriver account. + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatProjectName("[PROJECT]");
+   *   String filter = "";
+   *   ListMetricDescriptorsRequest request = ListMetricDescriptorsRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .setFilter(filter)
+   *     .build();
+   *   for (MetricDescriptor element : metricServiceApi.listMetricDescriptors(request)) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final PageAccessor listMetricDescriptors( + ListMetricDescriptorsRequest request) { + return listMetricDescriptorsPagedCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Lists metric descriptors that match a filter. This method does not require a Stackdriver account. + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatProjectName("[PROJECT]");
+   *   String filter = "";
+   *   ListMetricDescriptorsRequest request = ListMetricDescriptorsRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .setFilter(filter)
+   *     .build();
+   *   ListenableFuture<PageAccessor<MetricDescriptor>> future = metricServiceApi.listMetricDescriptorsPagedCallable().futureCall(request);
+   *   // Do something
+   *   for (MetricDescriptor element : future.get()) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * 
+ */ + public final ApiCallable> + listMetricDescriptorsPagedCallable() { + return listMetricDescriptorsPagedCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Lists metric descriptors that match a filter. This method does not require a Stackdriver account. + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatProjectName("[PROJECT]");
+   *   String filter = "";
+   *   ListMetricDescriptorsRequest request = ListMetricDescriptorsRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .setFilter(filter)
+   *     .build();
+   *   while (true) {
+   *     ListMetricDescriptorsResponse response = metricServiceApi.listMetricDescriptorsCallable().call(request);
+   *     for (MetricDescriptor element : response.getMetricDescriptorsList()) {
+   *       // doThingsWith(element);
+   *     }
+   *     String nextPageToken = response.getNextPageToken();
+   *     if (!Strings.isNullOrEmpty(nextPageToken)) {
+   *       request = request.toBuilder().setPageToken(nextPageToken).build();
+   *     } else {
+   *       break;
+   *     }
+   *   }
+   * }
+   * 
+ */ + public final ApiCallable + listMetricDescriptorsCallable() { + return listMetricDescriptorsCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Gets a single metric descriptor. This method does not require a Stackdriver account. + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatMetricDescriptorPathName("[PROJECT]", "[METRIC_DESCRIPTOR_PATH]");
+   *   MetricDescriptor response = metricServiceApi.getMetricDescriptor(formattedName);
+   * }
+   * 
+ * + * @param name The metric descriptor on which to execute the request. The format is + * `"projects/{project_id_or_number}/metricDescriptors/{metric_id}"`. + * An example value of `{metric_id}` is + * `"compute.googleapis.com/instance/disk/read_bytes_count"`. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final MetricDescriptor getMetricDescriptor(String name) { + METRIC_DESCRIPTOR_PATH_PATH_TEMPLATE.validate(name, "getMetricDescriptor"); + GetMetricDescriptorRequest request = + GetMetricDescriptorRequest.newBuilder().setName(name).build(); + return getMetricDescriptor(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Gets a single metric descriptor. This method does not require a Stackdriver account. + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatMetricDescriptorPathName("[PROJECT]", "[METRIC_DESCRIPTOR_PATH]");
+   *   GetMetricDescriptorRequest request = GetMetricDescriptorRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .build();
+   *   MetricDescriptor response = metricServiceApi.getMetricDescriptor(request);
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + private final MetricDescriptor getMetricDescriptor(GetMetricDescriptorRequest request) { + return getMetricDescriptorCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Gets a single metric descriptor. This method does not require a Stackdriver account. + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatMetricDescriptorPathName("[PROJECT]", "[METRIC_DESCRIPTOR_PATH]");
+   *   GetMetricDescriptorRequest request = GetMetricDescriptorRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .build();
+   *   ListenableFuture<MetricDescriptor> future = metricServiceApi.getMetricDescriptorCallable().futureCall(request);
+   *   // Do something
+   *   MetricDescriptor response = future.get();
+   * }
+   * 
+ */ + public final ApiCallable + getMetricDescriptorCallable() { + return getMetricDescriptorCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Creates a new metric descriptor. + * User-created metric descriptors define + * [custom metrics](/monitoring/custom-metrics). + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatProjectName("[PROJECT]");
+   *   MetricDescriptor metricDescriptor = MetricDescriptor.newBuilder().build();
+   *   MetricDescriptor response = metricServiceApi.createMetricDescriptor(formattedName, metricDescriptor);
+   * }
+   * 
+ * + * @param name The project on which to execute the request. The format is + * `"projects/{project_id_or_number}"`. + * @param metricDescriptor The new [custom metric](/monitoring/custom-metrics) + * descriptor. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final MetricDescriptor createMetricDescriptor( + String name, MetricDescriptor metricDescriptor) { + PROJECT_PATH_TEMPLATE.validate(name, "createMetricDescriptor"); + CreateMetricDescriptorRequest request = + CreateMetricDescriptorRequest.newBuilder() + .setName(name) + .setMetricDescriptor(metricDescriptor) + .build(); + return createMetricDescriptor(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Creates a new metric descriptor. + * User-created metric descriptors define + * [custom metrics](/monitoring/custom-metrics). + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatProjectName("[PROJECT]");
+   *   MetricDescriptor metricDescriptor = MetricDescriptor.newBuilder().build();
+   *   CreateMetricDescriptorRequest request = CreateMetricDescriptorRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .setMetricDescriptor(metricDescriptor)
+   *     .build();
+   *   MetricDescriptor response = metricServiceApi.createMetricDescriptor(request);
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final MetricDescriptor createMetricDescriptor(CreateMetricDescriptorRequest request) { + return createMetricDescriptorCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Creates a new metric descriptor. + * User-created metric descriptors define + * [custom metrics](/monitoring/custom-metrics). + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatProjectName("[PROJECT]");
+   *   MetricDescriptor metricDescriptor = MetricDescriptor.newBuilder().build();
+   *   CreateMetricDescriptorRequest request = CreateMetricDescriptorRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .setMetricDescriptor(metricDescriptor)
+   *     .build();
+   *   ListenableFuture<MetricDescriptor> future = metricServiceApi.createMetricDescriptorCallable().futureCall(request);
+   *   // Do something
+   *   MetricDescriptor response = future.get();
+   * }
+   * 
+ */ + public final ApiCallable + createMetricDescriptorCallable() { + return createMetricDescriptorCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Deletes a metric descriptor. Only user-created + * [custom metrics](/monitoring/custom-metrics) can be deleted. + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatMetricDescriptorPathName("[PROJECT]", "[METRIC_DESCRIPTOR_PATH]");
+   *   metricServiceApi.deleteMetricDescriptor(formattedName);
+   * }
+   * 
+ * + * @param name The metric descriptor on which to execute the request. The format is + * `"projects/{project_id_or_number}/metricDescriptors/{metric_id}"`. + * An example of `{metric_id}` is: + * `"custom.googleapis.com/my_test_metric"`. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final void deleteMetricDescriptor(String name) { + METRIC_DESCRIPTOR_PATH_PATH_TEMPLATE.validate(name, "deleteMetricDescriptor"); + DeleteMetricDescriptorRequest request = + DeleteMetricDescriptorRequest.newBuilder().setName(name).build(); + deleteMetricDescriptor(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Deletes a metric descriptor. Only user-created + * [custom metrics](/monitoring/custom-metrics) can be deleted. + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatMetricDescriptorPathName("[PROJECT]", "[METRIC_DESCRIPTOR_PATH]");
+   *   DeleteMetricDescriptorRequest request = DeleteMetricDescriptorRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .build();
+   *   metricServiceApi.deleteMetricDescriptor(request);
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + private final void deleteMetricDescriptor(DeleteMetricDescriptorRequest request) { + deleteMetricDescriptorCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Deletes a metric descriptor. Only user-created + * [custom metrics](/monitoring/custom-metrics) can be deleted. + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatMetricDescriptorPathName("[PROJECT]", "[METRIC_DESCRIPTOR_PATH]");
+   *   DeleteMetricDescriptorRequest request = DeleteMetricDescriptorRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .build();
+   *   ListenableFuture<Void> future = metricServiceApi.deleteMetricDescriptorCallable().futureCall(request);
+   *   // Do something
+   *   future.get();
+   * }
+   * 
+ */ + public final ApiCallable deleteMetricDescriptorCallable() { + return deleteMetricDescriptorCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Lists time series that match a filter. This method does not require a Stackdriver account. + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatProjectName("[PROJECT]");
+   *   String filter = "";
+   *   TimeInterval interval = TimeInterval.newBuilder().build();
+   *   Aggregation aggregation = Aggregation.newBuilder().build();
+   *   String orderBy = "";
+   *   TimeSeriesView view = TimeSeriesView.FULL;
+   *   ListTimeSeriesRequest request = ListTimeSeriesRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .setFilter(filter)
+   *     .setInterval(interval)
+   *     .setAggregation(aggregation)
+   *     .setOrderBy(orderBy)
+   *     .setView(view)
+   *     .build();
+   *   for (TimeSeries element : metricServiceApi.listTimeSeries(request)) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final PageAccessor listTimeSeries(ListTimeSeriesRequest request) { + return listTimeSeriesPagedCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Lists time series that match a filter. This method does not require a Stackdriver account. + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatProjectName("[PROJECT]");
+   *   String filter = "";
+   *   TimeInterval interval = TimeInterval.newBuilder().build();
+   *   Aggregation aggregation = Aggregation.newBuilder().build();
+   *   String orderBy = "";
+   *   TimeSeriesView view = TimeSeriesView.FULL;
+   *   ListTimeSeriesRequest request = ListTimeSeriesRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .setFilter(filter)
+   *     .setInterval(interval)
+   *     .setAggregation(aggregation)
+   *     .setOrderBy(orderBy)
+   *     .setView(view)
+   *     .build();
+   *   ListenableFuture<PageAccessor<TimeSeries>> future = metricServiceApi.listTimeSeriesPagedCallable().futureCall(request);
+   *   // Do something
+   *   for (TimeSeries element : future.get()) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * 
+ */ + public final ApiCallable> + listTimeSeriesPagedCallable() { + return listTimeSeriesPagedCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Lists time series that match a filter. This method does not require a Stackdriver account. + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatProjectName("[PROJECT]");
+   *   String filter = "";
+   *   TimeInterval interval = TimeInterval.newBuilder().build();
+   *   Aggregation aggregation = Aggregation.newBuilder().build();
+   *   String orderBy = "";
+   *   TimeSeriesView view = TimeSeriesView.FULL;
+   *   ListTimeSeriesRequest request = ListTimeSeriesRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .setFilter(filter)
+   *     .setInterval(interval)
+   *     .setAggregation(aggregation)
+   *     .setOrderBy(orderBy)
+   *     .setView(view)
+   *     .build();
+   *   while (true) {
+   *     ListTimeSeriesResponse response = metricServiceApi.listTimeSeriesCallable().call(request);
+   *     for (TimeSeries element : response.getTimeSeriesList()) {
+   *       // doThingsWith(element);
+   *     }
+   *     String nextPageToken = response.getNextPageToken();
+   *     if (!Strings.isNullOrEmpty(nextPageToken)) {
+   *       request = request.toBuilder().setPageToken(nextPageToken).build();
+   *     } else {
+   *       break;
+   *     }
+   *   }
+   * }
+   * 
+ */ + public final ApiCallable listTimeSeriesCallable() { + return listTimeSeriesCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Creates or adds data to one or more time series. + * The response is empty if all time series in the request were written. + * If any time series could not be written, a corresponding failure message is + * included in the error response. + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatProjectName("[PROJECT]");
+   *   List<TimeSeries> timeSeries = new ArrayList<>();
+   *   metricServiceApi.createTimeSeries(formattedName, timeSeries);
+   * }
+   * 
+ * + * @param name The project on which to execute the request. The format is + * `"projects/{project_id_or_number}"`. + * @param timeSeries The new data to be added to a list of time series. + * Adds at most one data point to each of several time series. The new data + * point must be more recent than any other point in its time series. Each + * `TimeSeries` value must fully specify a unique time series by supplying + * all label values for the metric and the monitored resource. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final void createTimeSeries(String name, List timeSeries) { + PROJECT_PATH_TEMPLATE.validate(name, "createTimeSeries"); + CreateTimeSeriesRequest request = + CreateTimeSeriesRequest.newBuilder().setName(name).addAllTimeSeries(timeSeries).build(); + createTimeSeries(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Creates or adds data to one or more time series. + * The response is empty if all time series in the request were written. + * If any time series could not be written, a corresponding failure message is + * included in the error response. + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatProjectName("[PROJECT]");
+   *   List<TimeSeries> timeSeries = new ArrayList<>();
+   *   CreateTimeSeriesRequest request = CreateTimeSeriesRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .addAllTimeSeries(timeSeries)
+   *     .build();
+   *   metricServiceApi.createTimeSeries(request);
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final void createTimeSeries(CreateTimeSeriesRequest request) { + createTimeSeriesCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Creates or adds data to one or more time series. + * The response is empty if all time series in the request were written. + * If any time series could not be written, a corresponding failure message is + * included in the error response. + * + * Sample code: + *

+   * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+   *   String formattedName = MetricServiceApi.formatProjectName("[PROJECT]");
+   *   List<TimeSeries> timeSeries = new ArrayList<>();
+   *   CreateTimeSeriesRequest request = CreateTimeSeriesRequest.newBuilder()
+   *     .setName(formattedName)
+   *     .addAllTimeSeries(timeSeries)
+   *     .build();
+   *   ListenableFuture<Void> future = metricServiceApi.createTimeSeriesCallable().futureCall(request);
+   *   // Do something
+   *   future.get();
+   * }
+   * 
+ */ + public final ApiCallable createTimeSeriesCallable() { + return createTimeSeriesCallable; + } + + /** + * Initiates an orderly shutdown in which preexisting calls continue but new calls are immediately + * cancelled. + */ + @Override + public final void close() throws Exception { + for (AutoCloseable closeable : closeables) { + closeable.close(); + } + } +} diff --git a/google-cloud-monitoring/src/main/java/com/google/cloud/monitoring/spi/v3/MetricServiceSettings.java b/google-cloud-monitoring/src/main/java/com/google/cloud/monitoring/spi/v3/MetricServiceSettings.java new file mode 100644 index 000000000000..27831a6e861e --- /dev/null +++ b/google-cloud-monitoring/src/main/java/com/google/cloud/monitoring/spi/v3/MetricServiceSettings.java @@ -0,0 +1,645 @@ +/* + * 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. + */ +package com.google.cloud.monitoring.spi.v3; + +import com.google.api.MetricDescriptor; +import com.google.api.MonitoredResourceDescriptor; +import com.google.api.gax.core.ConnectionSettings; +import com.google.api.gax.core.RetrySettings; +import com.google.api.gax.grpc.ApiCallSettings; +import com.google.api.gax.grpc.PageStreamingCallSettings; +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; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; +import com.google.monitoring.v3.CreateMetricDescriptorRequest; +import com.google.monitoring.v3.CreateTimeSeriesRequest; +import com.google.monitoring.v3.DeleteMetricDescriptorRequest; +import com.google.monitoring.v3.GetMetricDescriptorRequest; +import com.google.monitoring.v3.GetMonitoredResourceDescriptorRequest; +import com.google.monitoring.v3.ListMetricDescriptorsRequest; +import com.google.monitoring.v3.ListMetricDescriptorsResponse; +import com.google.monitoring.v3.ListMonitoredResourceDescriptorsRequest; +import com.google.monitoring.v3.ListMonitoredResourceDescriptorsResponse; +import com.google.monitoring.v3.ListTimeSeriesRequest; +import com.google.monitoring.v3.ListTimeSeriesResponse; +import com.google.monitoring.v3.MetricServiceGrpc; +import com.google.monitoring.v3.TimeSeries; +import com.google.protobuf.Empty; +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; + +// AUTO-GENERATED DOCUMENTATION AND CLASS +/** + * Settings class to configure an instance of {@link MetricServiceApi}. + * + *

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

    + *
  • The default service address (monitoring.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 getMonitoredResourceDescriptor to 30 seconds: + * + *

+ * 
+ * MetricServiceSettings.Builder metricServiceSettingsBuilder =
+ *     MetricServiceSettings.defaultBuilder();
+ * metricServiceSettingsBuilder.getMonitoredResourceDescriptorSettings().getRetrySettingsBuilder()
+ *     .setTotalTimeout(Duration.standardSeconds(30));
+ * MetricServiceSettings metricServiceSettings = metricServiceSettingsBuilder.build();
+ * 
+ * 
+ */ +@javax.annotation.Generated("by GAPIC") +public class MetricServiceSettings extends ServiceApiSettings { + /** + * The default address of the service. + */ + private static final String DEFAULT_SERVICE_ADDRESS = "monitoring.googleapis.com"; + + /** + * The default port of the service. + */ + private static final int DEFAULT_SERVICE_PORT = 443; + + /** + * The default scopes of the service. + */ + private static final ImmutableList DEFAULT_SERVICE_SCOPES = + ImmutableList.builder().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< + ListMonitoredResourceDescriptorsRequest, ListMonitoredResourceDescriptorsResponse, + MonitoredResourceDescriptor> + listMonitoredResourceDescriptorsSettings; + private final SimpleCallSettings< + GetMonitoredResourceDescriptorRequest, MonitoredResourceDescriptor> + getMonitoredResourceDescriptorSettings; + private final PageStreamingCallSettings< + ListMetricDescriptorsRequest, ListMetricDescriptorsResponse, MetricDescriptor> + listMetricDescriptorsSettings; + private final SimpleCallSettings + getMetricDescriptorSettings; + private final SimpleCallSettings + createMetricDescriptorSettings; + private final SimpleCallSettings + deleteMetricDescriptorSettings; + private final PageStreamingCallSettings + listTimeSeriesSettings; + private final SimpleCallSettings createTimeSeriesSettings; + + /** + * Returns the object with the settings used for calls to listMonitoredResourceDescriptors. + */ + public PageStreamingCallSettings< + ListMonitoredResourceDescriptorsRequest, ListMonitoredResourceDescriptorsResponse, + MonitoredResourceDescriptor> + listMonitoredResourceDescriptorsSettings() { + return listMonitoredResourceDescriptorsSettings; + } + + /** + * Returns the object with the settings used for calls to getMonitoredResourceDescriptor. + */ + public SimpleCallSettings + getMonitoredResourceDescriptorSettings() { + return getMonitoredResourceDescriptorSettings; + } + + /** + * Returns the object with the settings used for calls to listMetricDescriptors. + */ + public PageStreamingCallSettings< + ListMetricDescriptorsRequest, ListMetricDescriptorsResponse, MetricDescriptor> + listMetricDescriptorsSettings() { + return listMetricDescriptorsSettings; + } + + /** + * Returns the object with the settings used for calls to getMetricDescriptor. + */ + public SimpleCallSettings + getMetricDescriptorSettings() { + return getMetricDescriptorSettings; + } + + /** + * Returns the object with the settings used for calls to createMetricDescriptor. + */ + public SimpleCallSettings + createMetricDescriptorSettings() { + return createMetricDescriptorSettings; + } + + /** + * Returns the object with the settings used for calls to deleteMetricDescriptor. + */ + public SimpleCallSettings deleteMetricDescriptorSettings() { + return deleteMetricDescriptorSettings; + } + + /** + * Returns the object with the settings used for calls to listTimeSeries. + */ + public PageStreamingCallSettings + listTimeSeriesSettings() { + return listTimeSeriesSettings; + } + + /** + * Returns the object with the settings used for calls to createTimeSeries. + */ + public SimpleCallSettings createTimeSeriesSettings() { + return createTimeSeriesSettings; + } + + /** + * Returns the default service address. + */ + public static String getDefaultServiceAddress() { + return DEFAULT_SERVICE_ADDRESS; + } + + /** + * Returns the default service port. + */ + public static int getDefaultServicePort() { + return DEFAULT_SERVICE_PORT; + } + + /** + * Returns the default service scopes. + */ + public static ImmutableList getDefaultServiceScopes() { + return DEFAULT_SERVICE_SCOPES; + } + + /** + * 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 MetricServiceSettings(Builder settingsBuilder) throws IOException { + super( + settingsBuilder.getChannelProvider(), + settingsBuilder.getExecutorProvider(), + settingsBuilder.getGeneratorName(), + settingsBuilder.getGeneratorVersion(), + settingsBuilder.getClientLibName(), + settingsBuilder.getClientLibVersion()); + + listMonitoredResourceDescriptorsSettings = + settingsBuilder.listMonitoredResourceDescriptorsSettings().build(); + getMonitoredResourceDescriptorSettings = + settingsBuilder.getMonitoredResourceDescriptorSettings().build(); + listMetricDescriptorsSettings = settingsBuilder.listMetricDescriptorsSettings().build(); + getMetricDescriptorSettings = settingsBuilder.getMetricDescriptorSettings().build(); + createMetricDescriptorSettings = settingsBuilder.createMetricDescriptorSettings().build(); + deleteMetricDescriptorSettings = settingsBuilder.deleteMetricDescriptorSettings().build(); + listTimeSeriesSettings = settingsBuilder.listTimeSeriesSettings().build(); + createTimeSeriesSettings = settingsBuilder.createTimeSeriesSettings().build(); + } + + private static PageStreamingDescriptor< + ListMonitoredResourceDescriptorsRequest, ListMonitoredResourceDescriptorsResponse, + MonitoredResourceDescriptor> + LIST_MONITORED_RESOURCE_DESCRIPTORS_PAGE_STR_DESC = + new PageStreamingDescriptor< + ListMonitoredResourceDescriptorsRequest, ListMonitoredResourceDescriptorsResponse, + MonitoredResourceDescriptor>() { + @Override + public Object emptyToken() { + return ""; + } + + @Override + public ListMonitoredResourceDescriptorsRequest injectToken( + ListMonitoredResourceDescriptorsRequest payload, Object token) { + return ListMonitoredResourceDescriptorsRequest.newBuilder(payload) + .setPageToken((String) token) + .build(); + } + + @Override + public Object extractNextToken(ListMonitoredResourceDescriptorsResponse payload) { + return payload.getNextPageToken(); + } + + @Override + public Iterable extractResources( + ListMonitoredResourceDescriptorsResponse payload) { + return payload.getResourceDescriptorsList(); + } + }; + + private static PageStreamingDescriptor< + ListMetricDescriptorsRequest, ListMetricDescriptorsResponse, MetricDescriptor> + LIST_METRIC_DESCRIPTORS_PAGE_STR_DESC = + new PageStreamingDescriptor< + ListMetricDescriptorsRequest, ListMetricDescriptorsResponse, MetricDescriptor>() { + @Override + public Object emptyToken() { + return ""; + } + + @Override + public ListMetricDescriptorsRequest injectToken( + ListMetricDescriptorsRequest payload, Object token) { + return ListMetricDescriptorsRequest.newBuilder(payload) + .setPageToken((String) token) + .build(); + } + + @Override + public Object extractNextToken(ListMetricDescriptorsResponse payload) { + return payload.getNextPageToken(); + } + + @Override + public Iterable extractResources( + ListMetricDescriptorsResponse payload) { + return payload.getMetricDescriptorsList(); + } + }; + + private static PageStreamingDescriptor + LIST_TIME_SERIES_PAGE_STR_DESC = + new PageStreamingDescriptor() { + @Override + public Object emptyToken() { + return ""; + } + + @Override + public ListTimeSeriesRequest injectToken(ListTimeSeriesRequest payload, Object token) { + return ListTimeSeriesRequest.newBuilder(payload).setPageToken((String) token).build(); + } + + @Override + public Object extractNextToken(ListTimeSeriesResponse payload) { + return payload.getNextPageToken(); + } + + @Override + public Iterable extractResources(ListTimeSeriesResponse payload) { + return payload.getTimeSeriesList(); + } + }; + + /** + * Builder for MetricServiceSettings. + */ + public static class Builder extends ServiceApiSettings.Builder { + private final ImmutableList methodSettingsBuilders; + + private PageStreamingCallSettings.Builder< + ListMonitoredResourceDescriptorsRequest, ListMonitoredResourceDescriptorsResponse, + MonitoredResourceDescriptor> + listMonitoredResourceDescriptorsSettings; + private SimpleCallSettings.Builder< + GetMonitoredResourceDescriptorRequest, MonitoredResourceDescriptor> + getMonitoredResourceDescriptorSettings; + private PageStreamingCallSettings.Builder< + ListMetricDescriptorsRequest, ListMetricDescriptorsResponse, MetricDescriptor> + listMetricDescriptorsSettings; + private SimpleCallSettings.Builder + getMetricDescriptorSettings; + private SimpleCallSettings.Builder + createMetricDescriptorSettings; + private SimpleCallSettings.Builder + deleteMetricDescriptorSettings; + private PageStreamingCallSettings.Builder< + ListTimeSeriesRequest, ListTimeSeriesResponse, TimeSeries> + listTimeSeriesSettings; + private SimpleCallSettings.Builder createTimeSeriesSettings; + + private static final ImmutableMap> RETRYABLE_CODE_DEFINITIONS; + + static { + ImmutableMap.Builder> definitions = ImmutableMap.builder(); + definitions.put( + "idempotent", + Sets.immutableEnumSet( + Lists.newArrayList( + Status.Code.DEADLINE_EXCEEDED, Status.Code.UNAVAILABLE))); + definitions.put("non_idempotent", Sets.immutableEnumSet(Lists.newArrayList())); + RETRYABLE_CODE_DEFINITIONS = definitions.build(); + } + + private static final ImmutableMap RETRY_PARAM_DEFINITIONS; + + static { + ImmutableMap.Builder definitions = ImmutableMap.builder(); + RetrySettings.Builder settingsBuilder = null; + settingsBuilder = + RetrySettings.newBuilder() + .setInitialRetryDelay(Duration.millis(100L)) + .setRetryDelayMultiplier(1.3) + .setMaxRetryDelay(Duration.millis(60000L)) + .setInitialRpcTimeout(Duration.millis(20000L)) + .setRpcTimeoutMultiplier(1.0) + .setMaxRpcTimeout(Duration.millis(20000L)) + .setTotalTimeout(Duration.millis(600000L)); + definitions.put("default", settingsBuilder); + RETRY_PARAM_DEFINITIONS = definitions.build(); + } + + private Builder() { + super(DEFAULT_CONNECTION_SETTINGS); + + listMonitoredResourceDescriptorsSettings = + PageStreamingCallSettings.newBuilder( + MetricServiceGrpc.METHOD_LIST_MONITORED_RESOURCE_DESCRIPTORS, + LIST_MONITORED_RESOURCE_DESCRIPTORS_PAGE_STR_DESC); + + getMonitoredResourceDescriptorSettings = + SimpleCallSettings.newBuilder(MetricServiceGrpc.METHOD_GET_MONITORED_RESOURCE_DESCRIPTOR); + + listMetricDescriptorsSettings = + PageStreamingCallSettings.newBuilder( + MetricServiceGrpc.METHOD_LIST_METRIC_DESCRIPTORS, + LIST_METRIC_DESCRIPTORS_PAGE_STR_DESC); + + getMetricDescriptorSettings = + SimpleCallSettings.newBuilder(MetricServiceGrpc.METHOD_GET_METRIC_DESCRIPTOR); + + createMetricDescriptorSettings = + SimpleCallSettings.newBuilder(MetricServiceGrpc.METHOD_CREATE_METRIC_DESCRIPTOR); + + deleteMetricDescriptorSettings = + SimpleCallSettings.newBuilder(MetricServiceGrpc.METHOD_DELETE_METRIC_DESCRIPTOR); + + listTimeSeriesSettings = + PageStreamingCallSettings.newBuilder( + MetricServiceGrpc.METHOD_LIST_TIME_SERIES, LIST_TIME_SERIES_PAGE_STR_DESC); + + createTimeSeriesSettings = + SimpleCallSettings.newBuilder(MetricServiceGrpc.METHOD_CREATE_TIME_SERIES); + + methodSettingsBuilders = + ImmutableList.of( + listMonitoredResourceDescriptorsSettings, + getMonitoredResourceDescriptorSettings, + listMetricDescriptorsSettings, + getMetricDescriptorSettings, + createMetricDescriptorSettings, + deleteMetricDescriptorSettings, + listTimeSeriesSettings, + createTimeSeriesSettings); + } + + private static Builder createDefault() { + Builder builder = new Builder(); + + builder + .listMonitoredResourceDescriptorsSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .getMonitoredResourceDescriptorSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .listMetricDescriptorsSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .getMetricDescriptorSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .createMetricDescriptorSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .deleteMetricDescriptorSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .listTimeSeriesSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .createTimeSeriesSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + return builder; + } + + private Builder(MetricServiceSettings settings) { + super(settings); + + listMonitoredResourceDescriptorsSettings = + settings.listMonitoredResourceDescriptorsSettings.toBuilder(); + getMonitoredResourceDescriptorSettings = + settings.getMonitoredResourceDescriptorSettings.toBuilder(); + listMetricDescriptorsSettings = settings.listMetricDescriptorsSettings.toBuilder(); + getMetricDescriptorSettings = settings.getMetricDescriptorSettings.toBuilder(); + createMetricDescriptorSettings = settings.createMetricDescriptorSettings.toBuilder(); + deleteMetricDescriptorSettings = settings.deleteMetricDescriptorSettings.toBuilder(); + listTimeSeriesSettings = settings.listTimeSeriesSettings.toBuilder(); + createTimeSeriesSettings = settings.createTimeSeriesSettings.toBuilder(); + + methodSettingsBuilders = + ImmutableList.of( + listMonitoredResourceDescriptorsSettings, + getMonitoredResourceDescriptorSettings, + listMetricDescriptorsSettings, + getMetricDescriptorSettings, + createMetricDescriptorSettings, + deleteMetricDescriptorSettings, + listTimeSeriesSettings, + createTimeSeriesSettings); + } + + @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); + return this; + } + + @Override + public Builder provideChannelWith(ConnectionSettings settings) { + super.provideChannelWith(settings); + return this; + } + + @Override + public Builder provideChannelWith(Credentials credentials) { + super.provideChannelWith(credentials); + return this; + } + + @Override + public Builder provideChannelWith(List scopes) { + super.provideChannelWith(scopes); + return this; + } + + @Override + public Builder setGeneratorHeader(String name, String version) { + super.setGeneratorHeader(name, version); + return this; + } + + @Override + public Builder setClientLibHeader(String name, String version) { + super.setClientLibHeader(name, 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 listMonitoredResourceDescriptors. + */ + public PageStreamingCallSettings.Builder< + ListMonitoredResourceDescriptorsRequest, ListMonitoredResourceDescriptorsResponse, + MonitoredResourceDescriptor> + listMonitoredResourceDescriptorsSettings() { + return listMonitoredResourceDescriptorsSettings; + } + + /** + * Returns the builder for the settings used for calls to getMonitoredResourceDescriptor. + */ + public SimpleCallSettings.Builder< + GetMonitoredResourceDescriptorRequest, MonitoredResourceDescriptor> + getMonitoredResourceDescriptorSettings() { + return getMonitoredResourceDescriptorSettings; + } + + /** + * Returns the builder for the settings used for calls to listMetricDescriptors. + */ + public PageStreamingCallSettings.Builder< + ListMetricDescriptorsRequest, ListMetricDescriptorsResponse, MetricDescriptor> + listMetricDescriptorsSettings() { + return listMetricDescriptorsSettings; + } + + /** + * Returns the builder for the settings used for calls to getMetricDescriptor. + */ + public SimpleCallSettings.Builder + getMetricDescriptorSettings() { + return getMetricDescriptorSettings; + } + + /** + * Returns the builder for the settings used for calls to createMetricDescriptor. + */ + public SimpleCallSettings.Builder + createMetricDescriptorSettings() { + return createMetricDescriptorSettings; + } + + /** + * Returns the builder for the settings used for calls to deleteMetricDescriptor. + */ + public SimpleCallSettings.Builder + deleteMetricDescriptorSettings() { + return deleteMetricDescriptorSettings; + } + + /** + * Returns the builder for the settings used for calls to listTimeSeries. + */ + public PageStreamingCallSettings.Builder< + ListTimeSeriesRequest, ListTimeSeriesResponse, TimeSeries> + listTimeSeriesSettings() { + return listTimeSeriesSettings; + } + + /** + * Returns the builder for the settings used for calls to createTimeSeries. + */ + public SimpleCallSettings.Builder createTimeSeriesSettings() { + return createTimeSeriesSettings; + } + + @Override + public MetricServiceSettings build() throws IOException { + return new MetricServiceSettings(this); + } + } +} diff --git a/google-cloud-monitoring/src/main/java/com/google/cloud/monitoring/spi/v3/package-info.java b/google-cloud-monitoring/src/main/java/com/google/cloud/monitoring/spi/v3/package-info.java new file mode 100644 index 000000000000..1185776418d4 --- /dev/null +++ b/google-cloud-monitoring/src/main/java/com/google/cloud/monitoring/spi/v3/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. + */ + +/** + * A client to Google Monitoring API. + * + * The interfaces provided are listed below, along with a usage sample + * + * =============== + * GroupServiceApi + * =============== + * + * Service Description: The Group API lets you inspect and manage your + * [groups](google.monitoring.v3.Group). + * + * A group is a named filter that is used to identify + * a collection of monitored resources. Groups are typically used to + * mirror the physical and/or logical topology of the environment. + * Because group membership is computed dynamically, monitored + * resources that are started in the future are automatically placed + * in matching groups. By using a group to name monitored resources in, + * for example, an alert policy, the target of that alert policy is + * updated automatically as monitored resources are added and removed + * from the infrastructure. + * + * Sample for GroupServiceApi: + *
+ * 
+ * try (GroupServiceApi groupServiceApi = GroupServiceApi.create()) {
+ *   String formattedName = GroupServiceApi.formatGroupName("[PROJECT]", "[GROUP]");
+ *   Group response = groupServiceApi.getGroup(formattedName);
+ * }
+ * 
+ * 
+ * + * ================ + * MetricServiceApi + * ================ + * + * Service Description: Manages metric descriptors, monitored resource descriptors, and + * time series data. + * + * Sample for MetricServiceApi: + *
+ * 
+ * try (MetricServiceApi metricServiceApi = MetricServiceApi.create()) {
+ *   String formattedName = MetricServiceApi.formatMonitoredResourceDescriptorName("[PROJECT]", "[MONITORED_RESOURCE_DESCRIPTOR]");
+ *   MonitoredResourceDescriptor response = metricServiceApi.getMonitoredResourceDescriptor(formattedName);
+ * }
+ * 
+ * 
+ * + * ========================== + * AgentTranslationServiceApi + * ========================== + * + * Service Description: The AgentTranslation API allows `collectd`-based agents to + * write time series data to Cloud Monitoring. + * See [google.monitoring.v3.MetricService.CreateTimeSeries] instead. + * + * Sample for AgentTranslationServiceApi: + *
+ * 
+ * try (AgentTranslationServiceApi agentTranslationServiceApi = AgentTranslationServiceApi.create()) {
+ *   String formattedName = AgentTranslationServiceApi.formatProjectName("[PROJECT]");
+ *   MonitoredResource resource = MonitoredResource.newBuilder().build();
+ *   String collectdVersion = "";
+ *   List<CollectdPayload> collectdPayloads = new ArrayList<>();
+ *   agentTranslationServiceApi.createCollectdTimeSeries(formattedName, resource, collectdVersion, collectdPayloads);
+ * }
+ * 
+ * 
+ * + */ +package com.google.cloud.monitoring.spi.v3; diff --git a/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/AgentTranslationServiceTest.java b/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/AgentTranslationServiceTest.java new file mode 100644 index 000000000000..62852c768a7a --- /dev/null +++ b/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/AgentTranslationServiceTest.java @@ -0,0 +1,103 @@ +/* + * 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. + */ + +package com.google.cloud.monitoring.spi.v3; + +import com.google.api.MonitoredResource; +import com.google.api.gax.core.PageAccessor; +import com.google.api.gax.testing.MockGrpcService; +import com.google.api.gax.testing.MockServiceHelper; +import com.google.common.collect.Lists; +import com.google.monitoring.v3.CollectdPayload; +import com.google.monitoring.v3.CreateCollectdTimeSeriesRequest; +import com.google.protobuf.Empty; +import com.google.protobuf.GeneratedMessage; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +@javax.annotation.Generated("by GAPIC") +public class AgentTranslationServiceTest { + private static MockGroupService mockGroupService; + private static MockMetricService mockMetricService; + private static MockAgentTranslationService mockAgentTranslationService; + private static MockServiceHelper serviceHelper; + private AgentTranslationServiceApi api; + + @BeforeClass + public static void startStaticServer() { + mockGroupService = new MockGroupService(); + mockMetricService = new MockMetricService(); + mockAgentTranslationService = new MockAgentTranslationService(); + serviceHelper = + new MockServiceHelper( + "in-process-1", + Arrays.asList( + mockGroupService, mockMetricService, mockAgentTranslationService)); + serviceHelper.start(); + } + + @AfterClass + public static void stopServer() { + serviceHelper.stop(); + } + + @Before + public void setUp() throws IOException { + serviceHelper.reset(); + AgentTranslationServiceSettings settings = + AgentTranslationServiceSettings.defaultBuilder() + .provideChannelWith(serviceHelper.createChannel(), true) + .build(); + api = AgentTranslationServiceApi.create(settings); + } + + @After + public void tearDown() throws Exception { + api.close(); + } + + @Test + @SuppressWarnings("all") + public void createCollectdTimeSeriesTest() { + Empty expectedResponse = Empty.newBuilder().build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockAgentTranslationService.setResponses(expectedResponses); + + String formattedName = AgentTranslationServiceApi.formatProjectName("[PROJECT]"); + MonitoredResource resource = MonitoredResource.newBuilder().build(); + String collectdVersion = "collectdVersion-177050093"; + List collectdPayloads = new ArrayList<>(); + + api.createCollectdTimeSeries(formattedName, resource, collectdVersion, collectdPayloads); + + List actualRequests = mockAgentTranslationService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + CreateCollectdTimeSeriesRequest actualRequest = + (CreateCollectdTimeSeriesRequest) actualRequests.get(0); + + Assert.assertEquals(formattedName, actualRequest.getName()); + Assert.assertEquals(resource, actualRequest.getResource()); + Assert.assertEquals(collectdVersion, actualRequest.getCollectdVersion()); + Assert.assertEquals(collectdPayloads, actualRequest.getCollectdPayloadsList()); + } +} diff --git a/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/GroupServiceTest.java b/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/GroupServiceTest.java new file mode 100644 index 000000000000..ab06da6415c2 --- /dev/null +++ b/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/GroupServiceTest.java @@ -0,0 +1,243 @@ +/* + * 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. + */ + +package com.google.cloud.monitoring.spi.v3; + +import com.google.api.MonitoredResource; +import com.google.api.gax.core.PageAccessor; +import com.google.api.gax.testing.MockGrpcService; +import com.google.api.gax.testing.MockServiceHelper; +import com.google.common.collect.Lists; +import com.google.monitoring.v3.CreateGroupRequest; +import com.google.monitoring.v3.DeleteGroupRequest; +import com.google.monitoring.v3.GetGroupRequest; +import com.google.monitoring.v3.Group; +import com.google.monitoring.v3.ListGroupMembersRequest; +import com.google.monitoring.v3.ListGroupMembersResponse; +import com.google.monitoring.v3.TimeInterval; +import com.google.monitoring.v3.UpdateGroupRequest; +import com.google.protobuf.Empty; +import com.google.protobuf.GeneratedMessage; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +@javax.annotation.Generated("by GAPIC") +public class GroupServiceTest { + private static MockGroupService mockGroupService; + private static MockMetricService mockMetricService; + private static MockAgentTranslationService mockAgentTranslationService; + private static MockServiceHelper serviceHelper; + private GroupServiceApi api; + + @BeforeClass + public static void startStaticServer() { + mockGroupService = new MockGroupService(); + mockMetricService = new MockMetricService(); + mockAgentTranslationService = new MockAgentTranslationService(); + serviceHelper = + new MockServiceHelper( + "in-process-1", + Arrays.asList( + mockGroupService, mockMetricService, mockAgentTranslationService)); + serviceHelper.start(); + } + + @AfterClass + public static void stopServer() { + serviceHelper.stop(); + } + + @Before + public void setUp() throws IOException { + serviceHelper.reset(); + GroupServiceSettings settings = + GroupServiceSettings.defaultBuilder() + .provideChannelWith(serviceHelper.createChannel(), true) + .build(); + api = GroupServiceApi.create(settings); + } + + @After + public void tearDown() throws Exception { + api.close(); + } + + @Test + @SuppressWarnings("all") + public void getGroupTest() { + String formattedName2 = GroupServiceApi.formatGroupName("[PROJECT]", "[GROUP]"); + String displayName = "displayName1615086568"; + String parentName = "parentName1015022848"; + String filter = "filter-1274492040"; + boolean isCluster = false; + Group expectedResponse = + Group.newBuilder() + .setName(formattedName2) + .setDisplayName(displayName) + .setParentName(parentName) + .setFilter(filter) + .setIsCluster(isCluster) + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockGroupService.setResponses(expectedResponses); + + String formattedName = GroupServiceApi.formatGroupName("[PROJECT]", "[GROUP]"); + + Group actualResponse = api.getGroup(formattedName); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockGroupService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + GetGroupRequest actualRequest = (GetGroupRequest) actualRequests.get(0); + + Assert.assertEquals(formattedName, actualRequest.getName()); + } + + @Test + @SuppressWarnings("all") + public void createGroupTest() { + String formattedName2 = GroupServiceApi.formatProjectName("[PROJECT]"); + String displayName = "displayName1615086568"; + String parentName = "parentName1015022848"; + String filter = "filter-1274492040"; + boolean isCluster = false; + Group expectedResponse = + Group.newBuilder() + .setName(formattedName2) + .setDisplayName(displayName) + .setParentName(parentName) + .setFilter(filter) + .setIsCluster(isCluster) + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockGroupService.setResponses(expectedResponses); + + String formattedName = GroupServiceApi.formatProjectName("[PROJECT]"); + Group group = Group.newBuilder().build(); + boolean validateOnly = false; + + Group actualResponse = api.createGroup(formattedName, group, validateOnly); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockGroupService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + CreateGroupRequest actualRequest = (CreateGroupRequest) actualRequests.get(0); + + Assert.assertEquals(formattedName, actualRequest.getName()); + Assert.assertEquals(group, actualRequest.getGroup()); + Assert.assertEquals(validateOnly, actualRequest.getValidateOnly()); + } + + @Test + @SuppressWarnings("all") + public void updateGroupTest() { + String name = "name3373707"; + String displayName = "displayName1615086568"; + String parentName = "parentName1015022848"; + String filter = "filter-1274492040"; + boolean isCluster = false; + Group expectedResponse = + Group.newBuilder() + .setName(name) + .setDisplayName(displayName) + .setParentName(parentName) + .setFilter(filter) + .setIsCluster(isCluster) + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockGroupService.setResponses(expectedResponses); + + Group group = Group.newBuilder().build(); + boolean validateOnly = false; + + Group actualResponse = api.updateGroup(group, validateOnly); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockGroupService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + UpdateGroupRequest actualRequest = (UpdateGroupRequest) actualRequests.get(0); + + Assert.assertEquals(group, actualRequest.getGroup()); + Assert.assertEquals(validateOnly, actualRequest.getValidateOnly()); + } + + @Test + @SuppressWarnings("all") + public void deleteGroupTest() { + Empty expectedResponse = Empty.newBuilder().build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockGroupService.setResponses(expectedResponses); + + String formattedName = GroupServiceApi.formatGroupName("[PROJECT]", "[GROUP]"); + + api.deleteGroup(formattedName); + + List actualRequests = mockGroupService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + DeleteGroupRequest actualRequest = (DeleteGroupRequest) actualRequests.get(0); + + Assert.assertEquals(formattedName, actualRequest.getName()); + } + + @Test + @SuppressWarnings("all") + public void listGroupMembersTest() { + MonitoredResource membersElement = MonitoredResource.newBuilder().build(); + List members = Arrays.asList(membersElement); + String nextPageToken = "nextPageToken-1530815211"; + int totalSize = -705419236; + ListGroupMembersResponse expectedResponse = + ListGroupMembersResponse.newBuilder() + .addAllMembers(members) + .setNextPageToken(nextPageToken) + .setTotalSize(totalSize) + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockGroupService.setResponses(expectedResponses); + + String formattedName = GroupServiceApi.formatGroupName("[PROJECT]", "[GROUP]"); + String filter = "filter-1274492040"; + TimeInterval interval = TimeInterval.newBuilder().build(); + + PageAccessor pageAccessor = + api.listGroupMembers(formattedName, filter, interval); + + // PageAccessor will not make actual request until it is being used. + // Add all the pages here in order to make grpc requests. + List resources = Lists.newArrayList(pageAccessor.getPageValues()); + Assert.assertEquals(1, resources.size()); + Assert.assertEquals(expectedResponse.getMembersList().get(0), resources.get(0)); + + List actualRequests = mockGroupService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + ListGroupMembersRequest actualRequest = (ListGroupMembersRequest) actualRequests.get(0); + + Assert.assertEquals(formattedName, actualRequest.getName()); + Assert.assertEquals(filter, actualRequest.getFilter()); + Assert.assertEquals(interval, actualRequest.getInterval()); + } +} diff --git a/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/MetricServiceTest.java b/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/MetricServiceTest.java new file mode 100644 index 000000000000..77ae6be6ffb6 --- /dev/null +++ b/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/MetricServiceTest.java @@ -0,0 +1,305 @@ +/* + * 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. + */ + +package com.google.cloud.monitoring.spi.v3; + +import com.google.api.MetricDescriptor; +import com.google.api.MonitoredResourceDescriptor; +import com.google.api.gax.core.PageAccessor; +import com.google.api.gax.testing.MockGrpcService; +import com.google.api.gax.testing.MockServiceHelper; +import com.google.common.collect.Lists; +import com.google.monitoring.v3.CreateMetricDescriptorRequest; +import com.google.monitoring.v3.CreateTimeSeriesRequest; +import com.google.monitoring.v3.DeleteMetricDescriptorRequest; +import com.google.monitoring.v3.GetMetricDescriptorRequest; +import com.google.monitoring.v3.GetMonitoredResourceDescriptorRequest; +import com.google.monitoring.v3.ListMetricDescriptorsRequest; +import com.google.monitoring.v3.ListMetricDescriptorsResponse; +import com.google.monitoring.v3.ListMonitoredResourceDescriptorsRequest; +import com.google.monitoring.v3.ListMonitoredResourceDescriptorsResponse; +import com.google.monitoring.v3.TimeSeries; +import com.google.protobuf.Empty; +import com.google.protobuf.GeneratedMessage; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +@javax.annotation.Generated("by GAPIC") +public class MetricServiceTest { + private static MockGroupService mockGroupService; + private static MockMetricService mockMetricService; + private static MockAgentTranslationService mockAgentTranslationService; + private static MockServiceHelper serviceHelper; + private MetricServiceApi api; + + @BeforeClass + public static void startStaticServer() { + mockGroupService = new MockGroupService(); + mockMetricService = new MockMetricService(); + mockAgentTranslationService = new MockAgentTranslationService(); + serviceHelper = + new MockServiceHelper( + "in-process-1", + Arrays.asList( + mockGroupService, mockMetricService, mockAgentTranslationService)); + serviceHelper.start(); + } + + @AfterClass + public static void stopServer() { + serviceHelper.stop(); + } + + @Before + public void setUp() throws IOException { + serviceHelper.reset(); + MetricServiceSettings settings = + MetricServiceSettings.defaultBuilder() + .provideChannelWith(serviceHelper.createChannel(), true) + .build(); + api = MetricServiceApi.create(settings); + } + + @After + public void tearDown() throws Exception { + api.close(); + } + + @Test + @SuppressWarnings("all") + public void listMonitoredResourceDescriptorsTest() { + MonitoredResourceDescriptor resourceDescriptorsElement = + MonitoredResourceDescriptor.newBuilder().build(); + List resourceDescriptors = + Arrays.asList(resourceDescriptorsElement); + String nextPageToken = "nextPageToken-1530815211"; + ListMonitoredResourceDescriptorsResponse expectedResponse = + ListMonitoredResourceDescriptorsResponse.newBuilder() + .addAllResourceDescriptors(resourceDescriptors) + .setNextPageToken(nextPageToken) + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockMetricService.setResponses(expectedResponses); + + String formattedName = MetricServiceApi.formatProjectName("[PROJECT]"); + String filter = "filter-1274492040"; + + PageAccessor pageAccessor = + api.listMonitoredResourceDescriptors(formattedName, filter); + + // PageAccessor will not make actual request until it is being used. + // Add all the pages here in order to make grpc requests. + List resources = Lists.newArrayList(pageAccessor.getPageValues()); + Assert.assertEquals(1, resources.size()); + Assert.assertEquals(expectedResponse.getResourceDescriptorsList().get(0), resources.get(0)); + + List actualRequests = mockMetricService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + ListMonitoredResourceDescriptorsRequest actualRequest = + (ListMonitoredResourceDescriptorsRequest) actualRequests.get(0); + + Assert.assertEquals(formattedName, actualRequest.getName()); + Assert.assertEquals(filter, actualRequest.getFilter()); + } + + @Test + @SuppressWarnings("all") + public void getMonitoredResourceDescriptorTest() { + String formattedName2 = + MetricServiceApi.formatMonitoredResourceDescriptorName( + "[PROJECT]", "[MONITORED_RESOURCE_DESCRIPTOR]"); + String type = "type3575610"; + String displayName = "displayName1615086568"; + String description = "description-1724546052"; + MonitoredResourceDescriptor expectedResponse = + MonitoredResourceDescriptor.newBuilder() + .setName(formattedName2) + .setType(type) + .setDisplayName(displayName) + .setDescription(description) + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockMetricService.setResponses(expectedResponses); + + String formattedName = + MetricServiceApi.formatMonitoredResourceDescriptorName( + "[PROJECT]", "[MONITORED_RESOURCE_DESCRIPTOR]"); + + MonitoredResourceDescriptor actualResponse = api.getMonitoredResourceDescriptor(formattedName); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockMetricService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + GetMonitoredResourceDescriptorRequest actualRequest = + (GetMonitoredResourceDescriptorRequest) actualRequests.get(0); + + Assert.assertEquals(formattedName, actualRequest.getName()); + } + + @Test + @SuppressWarnings("all") + public void listMetricDescriptorsTest() { + MetricDescriptor metricDescriptorsElement = MetricDescriptor.newBuilder().build(); + List metricDescriptors = Arrays.asList(metricDescriptorsElement); + String nextPageToken = "nextPageToken-1530815211"; + ListMetricDescriptorsResponse expectedResponse = + ListMetricDescriptorsResponse.newBuilder() + .addAllMetricDescriptors(metricDescriptors) + .setNextPageToken(nextPageToken) + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockMetricService.setResponses(expectedResponses); + + String formattedName = MetricServiceApi.formatProjectName("[PROJECT]"); + String filter = "filter-1274492040"; + + PageAccessor pageAccessor = api.listMetricDescriptors(formattedName, filter); + + // PageAccessor will not make actual request until it is being used. + // Add all the pages here in order to make grpc requests. + List resources = Lists.newArrayList(pageAccessor.getPageValues()); + Assert.assertEquals(1, resources.size()); + Assert.assertEquals(expectedResponse.getMetricDescriptorsList().get(0), resources.get(0)); + + List actualRequests = mockMetricService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + ListMetricDescriptorsRequest actualRequest = + (ListMetricDescriptorsRequest) actualRequests.get(0); + + Assert.assertEquals(formattedName, actualRequest.getName()); + Assert.assertEquals(filter, actualRequest.getFilter()); + } + + @Test + @SuppressWarnings("all") + public void getMetricDescriptorTest() { + String formattedName2 = + MetricServiceApi.formatMetricDescriptorPathName("[PROJECT]", "[METRIC_DESCRIPTOR_PATH]"); + String type = "type3575610"; + String unit = "unit3594628"; + String description = "description-1724546052"; + String displayName = "displayName1615086568"; + MetricDescriptor expectedResponse = + MetricDescriptor.newBuilder() + .setName(formattedName2) + .setType(type) + .setUnit(unit) + .setDescription(description) + .setDisplayName(displayName) + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockMetricService.setResponses(expectedResponses); + + String formattedName = + MetricServiceApi.formatMetricDescriptorPathName("[PROJECT]", "[METRIC_DESCRIPTOR_PATH]"); + + MetricDescriptor actualResponse = api.getMetricDescriptor(formattedName); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockMetricService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + GetMetricDescriptorRequest actualRequest = (GetMetricDescriptorRequest) actualRequests.get(0); + + Assert.assertEquals(formattedName, actualRequest.getName()); + } + + @Test + @SuppressWarnings("all") + public void createMetricDescriptorTest() { + String formattedName2 = MetricServiceApi.formatProjectName("[PROJECT]"); + String type = "type3575610"; + String unit = "unit3594628"; + String description = "description-1724546052"; + String displayName = "displayName1615086568"; + MetricDescriptor expectedResponse = + MetricDescriptor.newBuilder() + .setName(formattedName2) + .setType(type) + .setUnit(unit) + .setDescription(description) + .setDisplayName(displayName) + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockMetricService.setResponses(expectedResponses); + + String formattedName = MetricServiceApi.formatProjectName("[PROJECT]"); + MetricDescriptor metricDescriptor = MetricDescriptor.newBuilder().build(); + + MetricDescriptor actualResponse = api.createMetricDescriptor(formattedName, metricDescriptor); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockMetricService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + CreateMetricDescriptorRequest actualRequest = + (CreateMetricDescriptorRequest) actualRequests.get(0); + + Assert.assertEquals(formattedName, actualRequest.getName()); + Assert.assertEquals(metricDescriptor, actualRequest.getMetricDescriptor()); + } + + @Test + @SuppressWarnings("all") + public void deleteMetricDescriptorTest() { + Empty expectedResponse = Empty.newBuilder().build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockMetricService.setResponses(expectedResponses); + + String formattedName = + MetricServiceApi.formatMetricDescriptorPathName("[PROJECT]", "[METRIC_DESCRIPTOR_PATH]"); + + api.deleteMetricDescriptor(formattedName); + + List actualRequests = mockMetricService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + DeleteMetricDescriptorRequest actualRequest = + (DeleteMetricDescriptorRequest) actualRequests.get(0); + + Assert.assertEquals(formattedName, actualRequest.getName()); + } + + @Test + @SuppressWarnings("all") + public void createTimeSeriesTest() { + Empty expectedResponse = Empty.newBuilder().build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockMetricService.setResponses(expectedResponses); + + String formattedName = MetricServiceApi.formatProjectName("[PROJECT]"); + List timeSeries = new ArrayList<>(); + + api.createTimeSeries(formattedName, timeSeries); + + List actualRequests = mockMetricService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + CreateTimeSeriesRequest actualRequest = (CreateTimeSeriesRequest) actualRequests.get(0); + + Assert.assertEquals(formattedName, actualRequest.getName()); + Assert.assertEquals(timeSeries, actualRequest.getTimeSeriesList()); + } +} diff --git a/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/MockAgentTranslationService.java b/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/MockAgentTranslationService.java new file mode 100644 index 000000000000..40e641cbebe5 --- /dev/null +++ b/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/MockAgentTranslationService.java @@ -0,0 +1,50 @@ +/* + * 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. + */ + +package com.google.cloud.monitoring.spi.v3; + +import com.google.api.gax.testing.MockGrpcService; +import com.google.monitoring.v3.AgentTranslationServiceGrpc; +import com.google.protobuf.GeneratedMessage; +import io.grpc.ServerServiceDefinition; +import java.util.List; + +@javax.annotation.Generated("by GAPIC") +public class MockAgentTranslationService implements MockGrpcService { + private MockAgentTranslationServiceImpl serviceImpl; + + public MockAgentTranslationService() { + serviceImpl = new MockAgentTranslationServiceImpl(); + } + + @Override + public List getRequests() { + return serviceImpl.getRequests(); + } + + @Override + public void setResponses(List responses) { + serviceImpl.setResponses(responses); + } + + @Override + public ServerServiceDefinition getServiceDefinition() { + return AgentTranslationServiceGrpc.bindService(serviceImpl); + } + + @Override + public void reset() { + serviceImpl.reset(); + } +} diff --git a/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/MockAgentTranslationServiceImpl.java b/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/MockAgentTranslationServiceImpl.java new file mode 100644 index 000000000000..79eefb710584 --- /dev/null +++ b/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/MockAgentTranslationServiceImpl.java @@ -0,0 +1,59 @@ +/* + * 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. + */ + +package com.google.cloud.monitoring.spi.v3; + +import com.google.common.collect.Lists; +import com.google.monitoring.v3.AgentTranslationServiceGrpc.AgentTranslationService; +import com.google.monitoring.v3.CreateCollectdTimeSeriesRequest; +import com.google.protobuf.Empty; +import com.google.protobuf.GeneratedMessage; +import io.grpc.stub.StreamObserver; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +@javax.annotation.Generated("by GAPIC") +public class MockAgentTranslationServiceImpl implements AgentTranslationService { + private ArrayList requests; + private Queue responses; + + public MockAgentTranslationServiceImpl() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } + + public List getRequests() { + return requests; + } + + public void setResponses(List responses) { + this.responses = Lists.newLinkedList(responses); + } + + public void reset() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } + + @Override + public void createCollectdTimeSeries( + CreateCollectdTimeSeriesRequest request, StreamObserver responseObserver) { + Empty response = (Empty) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } +} diff --git a/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/MockGroupService.java b/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/MockGroupService.java new file mode 100644 index 000000000000..58f02392f3a6 --- /dev/null +++ b/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/MockGroupService.java @@ -0,0 +1,50 @@ +/* + * 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. + */ + +package com.google.cloud.monitoring.spi.v3; + +import com.google.api.gax.testing.MockGrpcService; +import com.google.monitoring.v3.GroupServiceGrpc; +import com.google.protobuf.GeneratedMessage; +import io.grpc.ServerServiceDefinition; +import java.util.List; + +@javax.annotation.Generated("by GAPIC") +public class MockGroupService implements MockGrpcService { + private MockGroupServiceImpl serviceImpl; + + public MockGroupService() { + serviceImpl = new MockGroupServiceImpl(); + } + + @Override + public List getRequests() { + return serviceImpl.getRequests(); + } + + @Override + public void setResponses(List responses) { + serviceImpl.setResponses(responses); + } + + @Override + public ServerServiceDefinition getServiceDefinition() { + return GroupServiceGrpc.bindService(serviceImpl); + } + + @Override + public void reset() { + serviceImpl.reset(); + } +} diff --git a/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/MockGroupServiceImpl.java b/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/MockGroupServiceImpl.java new file mode 100644 index 000000000000..e68220eb18f2 --- /dev/null +++ b/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/MockGroupServiceImpl.java @@ -0,0 +1,108 @@ +/* + * 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. + */ + +package com.google.cloud.monitoring.spi.v3; + +import com.google.common.collect.Lists; +import com.google.monitoring.v3.CreateGroupRequest; +import com.google.monitoring.v3.DeleteGroupRequest; +import com.google.monitoring.v3.GetGroupRequest; +import com.google.monitoring.v3.Group; +import com.google.monitoring.v3.GroupServiceGrpc.GroupService; +import com.google.monitoring.v3.ListGroupMembersRequest; +import com.google.monitoring.v3.ListGroupMembersResponse; +import com.google.monitoring.v3.ListGroupsRequest; +import com.google.monitoring.v3.ListGroupsResponse; +import com.google.monitoring.v3.UpdateGroupRequest; +import com.google.protobuf.Empty; +import com.google.protobuf.GeneratedMessage; +import io.grpc.stub.StreamObserver; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +@javax.annotation.Generated("by GAPIC") +public class MockGroupServiceImpl implements GroupService { + private ArrayList requests; + private Queue responses; + + public MockGroupServiceImpl() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } + + public List getRequests() { + return requests; + } + + public void setResponses(List responses) { + this.responses = Lists.newLinkedList(responses); + } + + public void reset() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } + + @Override + public void listGroups( + ListGroupsRequest request, StreamObserver responseObserver) { + ListGroupsResponse response = (ListGroupsResponse) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void getGroup(GetGroupRequest request, StreamObserver responseObserver) { + Group response = (Group) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void createGroup(CreateGroupRequest request, StreamObserver responseObserver) { + Group response = (Group) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void updateGroup(UpdateGroupRequest request, StreamObserver responseObserver) { + Group response = (Group) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void deleteGroup(DeleteGroupRequest request, StreamObserver responseObserver) { + Empty response = (Empty) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void listGroupMembers( + ListGroupMembersRequest request, StreamObserver responseObserver) { + ListGroupMembersResponse response = (ListGroupMembersResponse) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } +} diff --git a/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/MockMetricService.java b/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/MockMetricService.java new file mode 100644 index 000000000000..6168d6176eb1 --- /dev/null +++ b/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/MockMetricService.java @@ -0,0 +1,50 @@ +/* + * 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. + */ + +package com.google.cloud.monitoring.spi.v3; + +import com.google.api.gax.testing.MockGrpcService; +import com.google.monitoring.v3.MetricServiceGrpc; +import com.google.protobuf.GeneratedMessage; +import io.grpc.ServerServiceDefinition; +import java.util.List; + +@javax.annotation.Generated("by GAPIC") +public class MockMetricService implements MockGrpcService { + private MockMetricServiceImpl serviceImpl; + + public MockMetricService() { + serviceImpl = new MockMetricServiceImpl(); + } + + @Override + public List getRequests() { + return serviceImpl.getRequests(); + } + + @Override + public void setResponses(List responses) { + serviceImpl.setResponses(responses); + } + + @Override + public ServerServiceDefinition getServiceDefinition() { + return MetricServiceGrpc.bindService(serviceImpl); + } + + @Override + public void reset() { + serviceImpl.reset(); + } +} diff --git a/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/MockMetricServiceImpl.java b/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/MockMetricServiceImpl.java new file mode 100644 index 000000000000..74669c6f8eaf --- /dev/null +++ b/google-cloud-monitoring/src/test/java/com/google/cloud/monitoring/spi/v3/MockMetricServiceImpl.java @@ -0,0 +1,138 @@ +/* + * 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. + */ + +package com.google.cloud.monitoring.spi.v3; + +import com.google.api.MetricDescriptor; +import com.google.api.MonitoredResourceDescriptor; +import com.google.common.collect.Lists; +import com.google.monitoring.v3.CreateMetricDescriptorRequest; +import com.google.monitoring.v3.CreateTimeSeriesRequest; +import com.google.monitoring.v3.DeleteMetricDescriptorRequest; +import com.google.monitoring.v3.GetMetricDescriptorRequest; +import com.google.monitoring.v3.GetMonitoredResourceDescriptorRequest; +import com.google.monitoring.v3.ListMetricDescriptorsRequest; +import com.google.monitoring.v3.ListMetricDescriptorsResponse; +import com.google.monitoring.v3.ListMonitoredResourceDescriptorsRequest; +import com.google.monitoring.v3.ListMonitoredResourceDescriptorsResponse; +import com.google.monitoring.v3.ListTimeSeriesRequest; +import com.google.monitoring.v3.ListTimeSeriesResponse; +import com.google.monitoring.v3.MetricServiceGrpc.MetricService; +import com.google.protobuf.Empty; +import com.google.protobuf.GeneratedMessage; +import io.grpc.stub.StreamObserver; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +@javax.annotation.Generated("by GAPIC") +public class MockMetricServiceImpl implements MetricService { + private ArrayList requests; + private Queue responses; + + public MockMetricServiceImpl() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } + + public List getRequests() { + return requests; + } + + public void setResponses(List responses) { + this.responses = Lists.newLinkedList(responses); + } + + public void reset() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } + + @Override + public void listMonitoredResourceDescriptors( + ListMonitoredResourceDescriptorsRequest request, + StreamObserver responseObserver) { + ListMonitoredResourceDescriptorsResponse response = + (ListMonitoredResourceDescriptorsResponse) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void getMonitoredResourceDescriptor( + GetMonitoredResourceDescriptorRequest request, + StreamObserver responseObserver) { + MonitoredResourceDescriptor response = (MonitoredResourceDescriptor) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void listMetricDescriptors( + ListMetricDescriptorsRequest request, + StreamObserver responseObserver) { + ListMetricDescriptorsResponse response = (ListMetricDescriptorsResponse) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void getMetricDescriptor( + GetMetricDescriptorRequest request, StreamObserver responseObserver) { + MetricDescriptor response = (MetricDescriptor) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void createMetricDescriptor( + CreateMetricDescriptorRequest request, StreamObserver responseObserver) { + MetricDescriptor response = (MetricDescriptor) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void deleteMetricDescriptor( + DeleteMetricDescriptorRequest request, StreamObserver responseObserver) { + Empty response = (Empty) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void listTimeSeries( + ListTimeSeriesRequest request, StreamObserver responseObserver) { + ListTimeSeriesResponse response = (ListTimeSeriesResponse) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void createTimeSeries( + CreateTimeSeriesRequest request, StreamObserver responseObserver) { + Empty response = (Empty) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } +} diff --git a/pom.xml b/pom.xml index b936baf7bbb7..acded1092e9b 100644 --- a/pom.xml +++ b/pom.xml @@ -104,6 +104,7 @@ google-cloud-examples google-cloud-language google-cloud-logging + google-cloud-monitoring google-cloud-pubsub google-cloud-resourcemanager google-cloud-speech