From 015c398fa87dbc4abdddb2d63b001a0c770c4342 Mon Sep 17 00:00:00 2001 From: Michael Darakananda Date: Tue, 15 Dec 2015 15:00:25 -0800 Subject: [PATCH] Add Logging APIs generated by VKit. Some tests are written but definitely not completed yet. It shows, however, that the API is not dead on arrival. --- .../logging/spi/v2/ConfigServiceV2Api.java | 533 ++++++++++++++++ .../logging/spi/v2/LoggingServiceV2Api.java | 583 ++++++++++++++++++ .../logging/spi/v2/MetricsServiceV2Api.java | 533 ++++++++++++++++ .../spi/v2/testing/LocalLoggingImpl.java | 92 +++ .../logging/testing/LocalLoggingHelper.java | 79 +++ .../spi/v2/LoggingServiceV2ApiTest.java | 107 ++++ 6 files changed, 1927 insertions(+) create mode 100644 gcloud-java-logging/src/main/java/com/google/gcloud/logging/spi/v2/ConfigServiceV2Api.java create mode 100644 gcloud-java-logging/src/main/java/com/google/gcloud/logging/spi/v2/LoggingServiceV2Api.java create mode 100644 gcloud-java-logging/src/main/java/com/google/gcloud/logging/spi/v2/MetricsServiceV2Api.java create mode 100644 gcloud-java-logging/src/main/java/com/google/gcloud/logging/spi/v2/testing/LocalLoggingImpl.java create mode 100644 gcloud-java-logging/src/main/java/com/google/gcloud/logging/testing/LocalLoggingHelper.java create mode 100644 gcloud-java-logging/src/test/java/com/google/gcloud/logging/spi/v2/LoggingServiceV2ApiTest.java diff --git a/gcloud-java-logging/src/main/java/com/google/gcloud/logging/spi/v2/ConfigServiceV2Api.java b/gcloud-java-logging/src/main/java/com/google/gcloud/logging/spi/v2/ConfigServiceV2Api.java new file mode 100644 index 000000000000..51bbbc34f2a6 --- /dev/null +++ b/gcloud-java-logging/src/main/java/com/google/gcloud/logging/spi/v2/ConfigServiceV2Api.java @@ -0,0 +1,533 @@ +/* + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +/* + * EDITING INSTRUCTIONS + * This file was generated from the file + * https://github.com/google/googleapis/blob/master/google/logging/v2/logging_config.proto + * and updates to that file get reflected here through a refresh process. + * For the short term, the refresh process will only be runnable by Google engineers. + * Manual additions are allowed because the refresh process performs + * a 3-way merge in order to preserve those manual additions. In order to not + * break the refresh process, only certain types of modifications are + * allowed. + * + * Allowed modifications - currently these are the only types allowed: + * 1. New methods (these should be added to the end of the class) + * 2. New imports + * 3. Additional documentation between "manual edit" demarcations + * + * Happy editing! + */ +package com.google.gcloud.logging.spi.v2; + +import com.google.logging.v2.ConfigServiceV2Grpc; +import com.google.logging.v2.CreateSinkRequest; +import com.google.logging.v2.DeleteSinkRequest; +import com.google.logging.v2.GetSinkRequest; +import com.google.logging.v2.ListSinksRequest; +import com.google.logging.v2.ListSinksResponse; +import com.google.logging.v2.LogSink; +import com.google.logging.v2.UpdateSinkRequest; +import com.google.protobuf.Empty; +import io.gapi.gax.grpc.ApiCallable; +import io.gapi.gax.grpc.PageDescriptor; +import io.gapi.gax.grpc.ServiceApiSettings; +import io.gapi.gax.internal.ApiUtils; +import io.gapi.gax.protobuf.PathTemplate; +import io.grpc.ManagedChannel; +import java.io.IOException; + +// Manually-added imports: add custom (non-generated) imports after this point. + + + +// AUTO-GENERATED DOCUMENTATION AND SERVICE - see instructions at the top of the file for editing. +/** + * See //google/logging/v2/logging.proto for documentation + * + * + * + */ +@javax.annotation.Generated("by API code generation") +public class ConfigServiceV2Api implements AutoCloseable { + + // ========= + // Constants + // ========= + + /** + * The default address of the service. + * + * + * + */ + public static final String SERVICE_ADDRESS = "logging.googleapis.com"; + + /** + * The default port of the service. + * + * + * + */ + public static final int DEFAULT_SERVICE_PORT = 443; + + + private static final ApiCallable + LIST_SINKS = ApiCallable.create(ConfigServiceV2Grpc.METHOD_LIST_SINKS); + private static final ApiCallable + GET_SINK = ApiCallable.create(ConfigServiceV2Grpc.METHOD_GET_SINK); + private static final ApiCallable + CREATE_SINK = ApiCallable.create(ConfigServiceV2Grpc.METHOD_CREATE_SINK); + private static final ApiCallable + UPDATE_SINK = ApiCallable.create(ConfigServiceV2Grpc.METHOD_UPDATE_SINK); + private static final ApiCallable + DELETE_SINK = ApiCallable.create(ConfigServiceV2Grpc.METHOD_DELETE_SINK); + + private static PageDescriptor LIST_SINKS_PAGE_DESC = + new PageDescriptor() { + @Override + public Object emptyToken() { + return ""; + } + @Override + public ListSinksRequest injectToken( + ListSinksRequest payload, Object token) { + return ListSinksRequest + .newBuilder(payload) + .setPageToken((String) token) + .build(); + } + @Override + public Object extractNextToken(ListSinksResponse payload) { + return payload.getNextPageToken(); + } + @Override + public Iterable extractResources(ListSinksResponse payload) { + return payload.getSinksList(); + } + }; + + private static String ALL_SCOPES[] = { + "https://www.googleapis.com/auth/logging.read", + "https://www.googleapis.com/auth/logging.write", + "https://www.googleapis.com/auth/logging.admin" + }; + + /** + * A PathTemplate representing the fully-qualified path to represent + * a project_name resource. + * + * + * + */ + private static final PathTemplate PROJECT_NAME_PATH_TEMPLATE = + PathTemplate.create("/projects/{project}"); + /** + * A PathTemplate representing the fully-qualified path to represent + * a sink_name resource. + * + * + * + */ + private static final PathTemplate SINK_NAME_PATH_TEMPLATE = + PathTemplate.create("/projects/{project}/sinks/{sink}"); + + // ======== + // Members + // ======== + + private final ManagedChannel channel; + private final ServiceApiSettings settings; + + // =============== + // Factory Methods + // =============== + + /** + * Constructs an instance of ConfigServiceV2Api with default settings. + * + * + * + */ + public static ConfigServiceV2Api create() throws IOException { + return create(new ServiceApiSettings()); + } + + /** + * Constructs an instance of ConfigServiceV2Api, 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 ConfigServiceV2Api create(ServiceApiSettings settings) throws IOException { + return new ConfigServiceV2Api(settings); + } + + /** + * Constructs an instance of ConfigServiceV2Api, 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 ConfigServiceV2Api(ServiceApiSettings settings) throws IOException { + ServiceApiSettings internalSettings = ApiUtils.populateSettings(settings, + SERVICE_ADDRESS, DEFAULT_SERVICE_PORT, ALL_SCOPES); + this.settings = internalSettings; + this.channel = internalSettings.getChannel(); + } + + // ============================== + // Resource Name Helper Functions + // ============================== + + /** + * Creates a string containing the fully-qualified path to represent + * a project_name resource. + * + * + * + */ + public static final String createProjectNamePath(String project) { + return PROJECT_NAME_PATH_TEMPLATE.instantiate( + "project", project); + } + + /** + * Creates a string containing the fully-qualified path to represent + * a sink_name resource. + * + * + * + */ + public static final String createSinkNamePath(String project, String sink) { + return SINK_NAME_PATH_TEMPLATE.instantiate( + "project", project,"sink", sink); + } + + + /** + * Extracts the project from the given fully-qualified path which + * represents a projectName resource. + * + * + * + */ + public static final String extractProjectFromProjectNamePath(String projectNamePath) { + return PROJECT_NAME_PATH_TEMPLATE.parse(projectNamePath).get("project"); + } + + /** + * Extracts the project from the given fully-qualified path which + * represents a sinkName resource. + * + * + * + */ + public static final String extractProjectFromSinkNamePath(String sinkNamePath) { + return SINK_NAME_PATH_TEMPLATE.parse(sinkNamePath).get("project"); + } + + /** + * Extracts the sink from the given fully-qualified path which + * represents a sinkName resource. + * + * + * + */ + public static final String extractSinkFromSinkNamePath(String sinkNamePath) { + return SINK_NAME_PATH_TEMPLATE.parse(sinkNamePath).get("sink"); + } + + + // ============= + // Service Calls + // ============= + + // ----- listSinks ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists sinks. + * + * + * + */ + public Iterable listSinks(String projectName) { + ListSinksRequest request = + ListSinksRequest.newBuilder() + .setProjectName(projectName) + .build(); + return listSinks(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists sinks. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public Iterable listSinks(ListSinksRequest request) { + return listSinksStreamingCallable() + .iterableResponseStreamCall(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists sinks. + * + * + * + */ + public ApiCallable listSinksStreamingCallable() { + return listSinksCallable().pageStreaming(LIST_SINKS_PAGE_DESC); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists sinks. + * + * + * + */ + public ApiCallable listSinksCallable() { + return ApiUtils.prepareIdempotentCallable(LIST_SINKS, settings).bind(channel); + } + + // ----- getSink ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Gets a sink. + * + * + * + * + * @param sinkName The resource name of the sink to return. + * Example: `"projects/my-project-id/sinks/my-sink-id"`. + */ + public LogSink getSink(String sinkName) { + GetSinkRequest request = + GetSinkRequest.newBuilder() + .setSinkName(sinkName) + .build(); + + return getSink(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Gets a sink. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public LogSink getSink(GetSinkRequest request) { + return getSinkCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Gets a sink. + * + * + * + */ + public ApiCallable getSinkCallable() { + return ApiUtils.prepareIdempotentCallable(GET_SINK, settings).bind(channel); + } + + // ----- createSink ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Creates a sink. + * + * + * + * + * @param projectName The resource name of the project in which to create the sink. + * Example: `"projects/my-project-id"`. + * + * The new sink must be provided in the request. + * @param sink The new sink, which must not have an identifier that already + * exists. + */ + public LogSink createSink(String projectName, LogSink sink) { + CreateSinkRequest request = + CreateSinkRequest.newBuilder() + .setProjectName(projectName) + .setSink(sink) + .build(); + + return createSink(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Creates a sink. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public LogSink createSink(CreateSinkRequest request) { + return createSinkCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Creates a sink. + * + * + * + */ + public ApiCallable createSinkCallable() { + return CREATE_SINK.bind(channel); + } + + // ----- updateSink ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Creates or updates a sink. + * + * + * + * + * @param sinkName The resource name of the sink to update. + * Example: `"projects/my-project-id/sinks/my-sink-id"`. + * + * The updated sink must be provided in the request and have the + * same name that is specified in `sinkName`. If the sink does not + * exist, it is created. + * @param sink The updated sink, whose name must be the same as the sink + * identifier in `sinkName`. If `sinkName` does not exist, then + * this method creates a new sink. + */ + public LogSink updateSink(String sinkName, LogSink sink) { + UpdateSinkRequest request = + UpdateSinkRequest.newBuilder() + .setSinkName(sinkName) + .setSink(sink) + .build(); + + return updateSink(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Creates or updates a sink. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public LogSink updateSink(UpdateSinkRequest request) { + return updateSinkCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Creates or updates a sink. + * + * + * + */ + public ApiCallable updateSinkCallable() { + return ApiUtils.prepareIdempotentCallable(UPDATE_SINK, settings).bind(channel); + } + + // ----- deleteSink ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Deletes a sink. + * + * + * + * + * @param sinkName The resource name of the sink to delete. + * Example: `"projects/my-project-id/sinks/my-sink-id"`. + */ + public void deleteSink(String sinkName) { + DeleteSinkRequest request = + DeleteSinkRequest.newBuilder() + .setSinkName(sinkName) + .build(); + + deleteSink(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Deletes a sink. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public void deleteSink(DeleteSinkRequest request) { + deleteSinkCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Deletes a sink. + * + * + * + */ + public ApiCallable deleteSinkCallable() { + return ApiUtils.prepareIdempotentCallable(DELETE_SINK, settings).bind(channel); + } + + + // ======== + // Cleanup + // ======== + + /** + * Initiates an orderly shutdown in which preexisting calls continue but new calls are immediately + * cancelled. + * + * + * + */ + @Override + public void close() { + // Manually-added shutdown code + + // Auto-generated shutdown code + channel.shutdown(); + + // Manually-added shutdown code + } + + + // ======== + // Manually-added methods: add custom (non-generated) methods after this point. + // ======== + +} diff --git a/gcloud-java-logging/src/main/java/com/google/gcloud/logging/spi/v2/LoggingServiceV2Api.java b/gcloud-java-logging/src/main/java/com/google/gcloud/logging/spi/v2/LoggingServiceV2Api.java new file mode 100644 index 000000000000..86285b90bfdc --- /dev/null +++ b/gcloud-java-logging/src/main/java/com/google/gcloud/logging/spi/v2/LoggingServiceV2Api.java @@ -0,0 +1,583 @@ +/* + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +/* + * EDITING INSTRUCTIONS + * This file was generated from the file + * https://github.com/google/googleapis/blob/master/google/logging/v2/logging.proto + * and updates to that file get reflected here through a refresh process. + * For the short term, the refresh process will only be runnable by Google engineers. + * Manual additions are allowed because the refresh process performs + * a 3-way merge in order to preserve those manual additions. In order to not + * break the refresh process, only certain types of modifications are + * allowed. + * + * Allowed modifications - currently these are the only types allowed: + * 1. New methods (these should be added to the end of the class) + * 2. New imports + * 3. Additional documentation between "manual edit" demarcations + * + * Happy editing! + */ +package com.google.gcloud.logging.spi.v2; + +import com.google.api.MonitoredResource; +import com.google.api.MonitoredResourceDescriptor; +import com.google.logging.v2.DeleteLogRequest; +import com.google.logging.v2.ListLogEntriesRequest; +import com.google.logging.v2.ListLogEntriesResponse; +import com.google.logging.v2.ListMonitoredResourceDescriptorsRequest; +import com.google.logging.v2.ListMonitoredResourceDescriptorsResponse; +import com.google.logging.v2.LogEntry; +import com.google.logging.v2.LoggingServiceV2Grpc; +import com.google.logging.v2.ReadLogEntriesRequest; +import com.google.logging.v2.ReadLogEntriesResponse; +import com.google.logging.v2.WriteLogEntriesRequest; +import com.google.logging.v2.WriteLogEntriesResponse; +import com.google.protobuf.Empty; +import io.gapi.gax.grpc.ApiCallable; +import io.gapi.gax.grpc.PageDescriptor; +import io.gapi.gax.grpc.ServiceApiSettings; +import io.gapi.gax.internal.ApiUtils; +import io.gapi.gax.protobuf.PathTemplate; +import io.grpc.ManagedChannel; +import java.io.IOException; +import java.util.List; +import java.util.Map; + +// Manually-added imports: add custom (non-generated) imports after this point. + + + +// AUTO-GENERATED DOCUMENTATION AND SERVICE - see instructions at the top of the file for editing. +/** + * Service for ingesting and querying logs. + * + * + * + */ +@javax.annotation.Generated("by API code generation") +public class LoggingServiceV2Api implements AutoCloseable { + + // ========= + // Constants + // ========= + + /** + * The default address of the service. + * + * + * + */ + public static final String SERVICE_ADDRESS = "logging.googleapis.com"; + + /** + * The default port of the service. + * + * + * + */ + public static final int DEFAULT_SERVICE_PORT = 443; + + + private static final ApiCallable + DELETE_LOG = ApiCallable.create(LoggingServiceV2Grpc.METHOD_DELETE_LOG); + private static final ApiCallable + WRITE_LOG_ENTRIES = ApiCallable.create(LoggingServiceV2Grpc.METHOD_WRITE_LOG_ENTRIES); + private static final ApiCallable + LIST_LOG_ENTRIES = ApiCallable.create(LoggingServiceV2Grpc.METHOD_LIST_LOG_ENTRIES); + private static final ApiCallable + READ_LOG_ENTRIES = ApiCallable.create(LoggingServiceV2Grpc.METHOD_READ_LOG_ENTRIES); + private static final ApiCallable + LIST_MONITORED_RESOURCE_DESCRIPTORS = ApiCallable.create(LoggingServiceV2Grpc.METHOD_LIST_MONITORED_RESOURCE_DESCRIPTORS); + + private static PageDescriptor LIST_LOG_ENTRIES_PAGE_DESC = + new PageDescriptor() { + @Override + public Object emptyToken() { + return ""; + } + @Override + public ListLogEntriesRequest injectToken( + ListLogEntriesRequest payload, Object token) { + return ListLogEntriesRequest + .newBuilder(payload) + .setPageToken((String) token) + .build(); + } + @Override + public Object extractNextToken(ListLogEntriesResponse payload) { + return payload.getNextPageToken(); + } + @Override + public Iterable extractResources(ListLogEntriesResponse payload) { + return payload.getEntriesList(); + } + }; + + private static PageDescriptor READ_LOG_ENTRIES_PAGE_DESC = + new PageDescriptor() { + @Override + public Object emptyToken() { + return ""; + } + @Override + public ReadLogEntriesRequest injectToken( + ReadLogEntriesRequest payload, Object token) { + return ReadLogEntriesRequest + .newBuilder(payload) + .setResumeToken((String) token) + .build(); + } + @Override + public Object extractNextToken(ReadLogEntriesResponse payload) { + return payload.getResumeToken(); + } + @Override + public Iterable extractResources(ReadLogEntriesResponse payload) { + return payload.getEntriesList(); + } + }; + + private static PageDescriptor LIST_MONITORED_RESOURCE_DESCRIPTORS_PAGE_DESC = + new PageDescriptor() { + @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 String ALL_SCOPES[] = { + "https://www.googleapis.com/auth/logging.read", + "https://www.googleapis.com/auth/logging.write", + "https://www.googleapis.com/auth/logging.admin" + }; + + /** + * A PathTemplate representing the fully-qualified path to represent + * a log_name resource. + * + * + * + */ + private static final PathTemplate LOG_NAME_PATH_TEMPLATE = + PathTemplate.create("/projects/{project}/logs/{log}"); + + // ======== + // Members + // ======== + + private final ManagedChannel channel; + private final ServiceApiSettings settings; + + // =============== + // Factory Methods + // =============== + + /** + * Constructs an instance of LoggingServiceV2Api with default settings. + * + * + * + */ + public static LoggingServiceV2Api create() throws IOException { + return create(new ServiceApiSettings()); + } + + /** + * Constructs an instance of LoggingServiceV2Api, 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 LoggingServiceV2Api create(ServiceApiSettings settings) throws IOException { + return new LoggingServiceV2Api(settings); + } + + /** + * Constructs an instance of LoggingServiceV2Api, 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 LoggingServiceV2Api(ServiceApiSettings settings) throws IOException { + ServiceApiSettings internalSettings = ApiUtils.populateSettings(settings, + SERVICE_ADDRESS, DEFAULT_SERVICE_PORT, ALL_SCOPES); + this.settings = internalSettings; + this.channel = internalSettings.getChannel(); + } + + // ============================== + // Resource Name Helper Functions + // ============================== + + /** + * Creates a string containing the fully-qualified path to represent + * a log_name resource. + * + * + * + */ + public static final String createLogNamePath(String project, String log) { + return LOG_NAME_PATH_TEMPLATE.instantiate( + "project", project,"log", log); + } + + + /** + * Extracts the project from the given fully-qualified path which + * represents a logName resource. + * + * + * + */ + public static final String extractProjectFromLogNamePath(String logNamePath) { + return LOG_NAME_PATH_TEMPLATE.parse(logNamePath).get("project"); + } + + /** + * Extracts the log from the given fully-qualified path which + * represents a logName resource. + * + * + * + */ + public static final String extractLogFromLogNamePath(String logNamePath) { + return LOG_NAME_PATH_TEMPLATE.parse(logNamePath).get("log"); + } + + + // ============= + // Service Calls + // ============= + + // ----- deleteLog ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Deletes a log and all its log entries. + * The log will reappear if it receives new entries. + * + * + * + * + * @param logName Required. The resource name of the log to delete. Example: + * `"projects/my-project/logs/syslog"`. + */ + public void deleteLog(String logName) { + DeleteLogRequest request = + DeleteLogRequest.newBuilder() + .setLogName(logName) + .build(); + + deleteLog(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Deletes a log and all its log entries. + * The log will reappear if it receives new entries. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public void deleteLog(DeleteLogRequest request) { + deleteLogCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Deletes a log and all its log entries. + * The log will reappear if it receives new entries. + * + * + * + */ + public ApiCallable deleteLogCallable() { + return ApiUtils.prepareIdempotentCallable(DELETE_LOG, settings).bind(channel); + } + + // ----- writeLogEntries ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Writes log entries to Cloud Logging. + * All log entries in Cloud Logging are written by this method. + * + * + * + * + * @param logName Optional. A default log resource name for those log entries in `entries` + * that do not specify their own `logName`. Example: + * `"projects/my-project/logs/syslog"`. See + * [LogEntry][google.logging.v2.LogEntry]. + * @param resource Optional. A default monitored resource for those log entries in `entries` + * that do not specify their own `resource`. + * @param labels Optional. User-defined `key:value` items that are added to + * the `labels` field of each log entry in `entries`, except when a log + * entry specifies its own 'key:value' item with the same key. + * Example: `{ "size": "large", "color":"red" }` + * @param entries Required. The log entries to write. The log entries must have values for + * all required fields. + */ + public WriteLogEntriesResponse writeLogEntries(String logName, MonitoredResource resource, Map labels, List entries) { + WriteLogEntriesRequest request = + WriteLogEntriesRequest.newBuilder() + .setLogName(logName) + .setResource(resource) + .putAllLabels(labels) + .addAllEntries(entries) + .build(); + + return writeLogEntries(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Writes log entries to Cloud Logging. + * All log entries in Cloud Logging are written by this method. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public WriteLogEntriesResponse writeLogEntries(WriteLogEntriesRequest request) { + return writeLogEntriesCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Writes log entries to Cloud Logging. + * All log entries in Cloud Logging are written by this method. + * + * + * + */ + public ApiCallable writeLogEntriesCallable() { + return WRITE_LOG_ENTRIES.bind(channel); + } + + // ----- listLogEntries ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists log entries. + * Use this method to examine log entries from Cloud Logging. + * See [Exporting Logs](/logging/docs/export) for other ways to copy + * log entries out of Cloud Logging. + * + * + * + */ + public Iterable listLogEntries(List projectIds, String filter, String orderBy) { + ListLogEntriesRequest request = + ListLogEntriesRequest.newBuilder() + .addAllProjectIds(projectIds) + .setFilter(filter) + .setOrderBy(orderBy) + .build(); + return listLogEntries(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists log entries. + * Use this method to examine log entries from Cloud Logging. + * See [Exporting Logs](/logging/docs/export) for other ways to copy + * log entries out of Cloud Logging. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public Iterable listLogEntries(ListLogEntriesRequest request) { + return listLogEntriesStreamingCallable() + .iterableResponseStreamCall(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists log entries. + * Use this method to examine log entries from Cloud Logging. + * See [Exporting Logs](/logging/docs/export) for other ways to copy + * log entries out of Cloud Logging. + * + * + * + */ + public ApiCallable listLogEntriesStreamingCallable() { + return listLogEntriesCallable().pageStreaming(LIST_LOG_ENTRIES_PAGE_DESC); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists log entries. + * Use this method to examine log entries from Cloud Logging. + * See [Exporting Logs](/logging/docs/export) for other ways to copy + * log entries out of Cloud Logging. + * + * + * + */ + public ApiCallable listLogEntriesCallable() { + return LIST_LOG_ENTRIES.bind(channel); + } + + // ----- readLogEntries ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Streaming read of log entries. Similar to `List`, this method is intended + * for a large volume of log entries. + * + * + * + */ + public Iterable readLogEntries(List projectIds, String filter, String orderBy) { + ReadLogEntriesRequest request = + ReadLogEntriesRequest.newBuilder() + .addAllProjectIds(projectIds) + .setFilter(filter) + .setOrderBy(orderBy) + .build(); + return readLogEntries(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Streaming read of log entries. Similar to `List`, this method is intended + * for a large volume of log entries. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public Iterable readLogEntries(ReadLogEntriesRequest request) { + return readLogEntriesStreamingCallable() + .iterableResponseStreamCall(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Streaming read of log entries. Similar to `List`, this method is intended + * for a large volume of log entries. + * + * + * + */ + public ApiCallable readLogEntriesStreamingCallable() { + return readLogEntriesCallable().pageStreaming(READ_LOG_ENTRIES_PAGE_DESC); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Streaming read of log entries. Similar to `List`, this method is intended + * for a large volume of log entries. + * + * + * + */ + public ApiCallable readLogEntriesCallable() { + return READ_LOG_ENTRIES.bind(channel); + } + + // ----- listMonitoredResourceDescriptors ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists monitored resource descriptors that are used by Cloud Logging. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public Iterable listMonitoredResourceDescriptors(ListMonitoredResourceDescriptorsRequest request) { + return listMonitoredResourceDescriptorsStreamingCallable() + .iterableResponseStreamCall(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists monitored resource descriptors that are used by Cloud Logging. + * + * + * + */ + public ApiCallable listMonitoredResourceDescriptorsStreamingCallable() { + return listMonitoredResourceDescriptorsCallable().pageStreaming(LIST_MONITORED_RESOURCE_DESCRIPTORS_PAGE_DESC); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists monitored resource descriptors that are used by Cloud Logging. + * + * + * + */ + public ApiCallable listMonitoredResourceDescriptorsCallable() { + return ApiUtils.prepareIdempotentCallable(LIST_MONITORED_RESOURCE_DESCRIPTORS, settings).bind(channel); + } + + + // ======== + // Cleanup + // ======== + + /** + * Initiates an orderly shutdown in which preexisting calls continue but new calls are immediately + * cancelled. + * + * + * + */ + @Override + public void close() { + // Manually-added shutdown code + + // Auto-generated shutdown code + channel.shutdown(); + + // Manually-added shutdown code + } + + + // ======== + // Manually-added methods: add custom (non-generated) methods after this point. + // ======== + +} diff --git a/gcloud-java-logging/src/main/java/com/google/gcloud/logging/spi/v2/MetricsServiceV2Api.java b/gcloud-java-logging/src/main/java/com/google/gcloud/logging/spi/v2/MetricsServiceV2Api.java new file mode 100644 index 000000000000..78d408550ffb --- /dev/null +++ b/gcloud-java-logging/src/main/java/com/google/gcloud/logging/spi/v2/MetricsServiceV2Api.java @@ -0,0 +1,533 @@ +/* + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +/* + * EDITING INSTRUCTIONS + * This file was generated from the file + * https://github.com/google/googleapis/blob/master/google/logging/v2/logging_metrics.proto + * and updates to that file get reflected here through a refresh process. + * For the short term, the refresh process will only be runnable by Google engineers. + * Manual additions are allowed because the refresh process performs + * a 3-way merge in order to preserve those manual additions. In order to not + * break the refresh process, only certain types of modifications are + * allowed. + * + * Allowed modifications - currently these are the only types allowed: + * 1. New methods (these should be added to the end of the class) + * 2. New imports + * 3. Additional documentation between "manual edit" demarcations + * + * Happy editing! + */ +package com.google.gcloud.logging.spi.v2; + +import com.google.logging.v2.CreateLogMetricRequest; +import com.google.logging.v2.DeleteLogMetricRequest; +import com.google.logging.v2.GetLogMetricRequest; +import com.google.logging.v2.ListLogMetricsRequest; +import com.google.logging.v2.ListLogMetricsResponse; +import com.google.logging.v2.LogMetric; +import com.google.logging.v2.MetricsServiceV2Grpc; +import com.google.logging.v2.UpdateLogMetricRequest; +import com.google.protobuf.Empty; +import io.gapi.gax.grpc.ApiCallable; +import io.gapi.gax.grpc.PageDescriptor; +import io.gapi.gax.grpc.ServiceApiSettings; +import io.gapi.gax.internal.ApiUtils; +import io.gapi.gax.protobuf.PathTemplate; +import io.grpc.ManagedChannel; +import java.io.IOException; + +// Manually-added imports: add custom (non-generated) imports after this point. + + + +// AUTO-GENERATED DOCUMENTATION AND SERVICE - see instructions at the top of the file for editing. +/** + * See //google/logging/v1/logging.proto for documentation + * + * + * + */ +@javax.annotation.Generated("by API code generation") +public class MetricsServiceV2Api implements AutoCloseable { + + // ========= + // Constants + // ========= + + /** + * The default address of the service. + * + * + * + */ + public static final String SERVICE_ADDRESS = "logging.googleapis.com"; + + /** + * The default port of the service. + * + * + * + */ + public static final int DEFAULT_SERVICE_PORT = 443; + + + private static final ApiCallable + LIST_LOG_METRICS = ApiCallable.create(MetricsServiceV2Grpc.METHOD_LIST_LOG_METRICS); + private static final ApiCallable + GET_LOG_METRIC = ApiCallable.create(MetricsServiceV2Grpc.METHOD_GET_LOG_METRIC); + private static final ApiCallable + CREATE_LOG_METRIC = ApiCallable.create(MetricsServiceV2Grpc.METHOD_CREATE_LOG_METRIC); + private static final ApiCallable + UPDATE_LOG_METRIC = ApiCallable.create(MetricsServiceV2Grpc.METHOD_UPDATE_LOG_METRIC); + private static final ApiCallable + DELETE_LOG_METRIC = ApiCallable.create(MetricsServiceV2Grpc.METHOD_DELETE_LOG_METRIC); + + private static PageDescriptor LIST_LOG_METRICS_PAGE_DESC = + new PageDescriptor() { + @Override + public Object emptyToken() { + return ""; + } + @Override + public ListLogMetricsRequest injectToken( + ListLogMetricsRequest payload, Object token) { + return ListLogMetricsRequest + .newBuilder(payload) + .setPageToken((String) token) + .build(); + } + @Override + public Object extractNextToken(ListLogMetricsResponse payload) { + return payload.getNextPageToken(); + } + @Override + public Iterable extractResources(ListLogMetricsResponse payload) { + return payload.getMetricsList(); + } + }; + + private static String ALL_SCOPES[] = { + "https://www.googleapis.com/auth/logging.read", + "https://www.googleapis.com/auth/logging.write", + "https://www.googleapis.com/auth/logging.admin" + }; + + /** + * A PathTemplate representing the fully-qualified path to represent + * a project_name resource. + * + * + * + */ + private static final PathTemplate PROJECT_NAME_PATH_TEMPLATE = + PathTemplate.create("/projects/{project}"); + /** + * A PathTemplate representing the fully-qualified path to represent + * a metric_name resource. + * + * + * + */ + private static final PathTemplate METRIC_NAME_PATH_TEMPLATE = + PathTemplate.create("/projects/{project}/metrics/{metric}"); + + // ======== + // Members + // ======== + + private final ManagedChannel channel; + private final ServiceApiSettings settings; + + // =============== + // Factory Methods + // =============== + + /** + * Constructs an instance of MetricsServiceV2Api with default settings. + * + * + * + */ + public static MetricsServiceV2Api create() throws IOException { + return create(new ServiceApiSettings()); + } + + /** + * Constructs an instance of MetricsServiceV2Api, 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 MetricsServiceV2Api create(ServiceApiSettings settings) throws IOException { + return new MetricsServiceV2Api(settings); + } + + /** + * Constructs an instance of MetricsServiceV2Api, 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 MetricsServiceV2Api(ServiceApiSettings settings) throws IOException { + ServiceApiSettings internalSettings = ApiUtils.populateSettings(settings, + SERVICE_ADDRESS, DEFAULT_SERVICE_PORT, ALL_SCOPES); + this.settings = internalSettings; + this.channel = internalSettings.getChannel(); + } + + // ============================== + // Resource Name Helper Functions + // ============================== + + /** + * Creates a string containing the fully-qualified path to represent + * a project_name resource. + * + * + * + */ + public static final String createProjectNamePath(String project) { + return PROJECT_NAME_PATH_TEMPLATE.instantiate( + "project", project); + } + + /** + * Creates a string containing the fully-qualified path to represent + * a metric_name resource. + * + * + * + */ + public static final String createMetricNamePath(String project, String metric) { + return METRIC_NAME_PATH_TEMPLATE.instantiate( + "project", project,"metric", metric); + } + + + /** + * Extracts the project from the given fully-qualified path which + * represents a projectName resource. + * + * + * + */ + public static final String extractProjectFromProjectNamePath(String projectNamePath) { + return PROJECT_NAME_PATH_TEMPLATE.parse(projectNamePath).get("project"); + } + + /** + * Extracts the project from the given fully-qualified path which + * represents a metricName resource. + * + * + * + */ + public static final String extractProjectFromMetricNamePath(String metricNamePath) { + return METRIC_NAME_PATH_TEMPLATE.parse(metricNamePath).get("project"); + } + + /** + * Extracts the metric from the given fully-qualified path which + * represents a metricName resource. + * + * + * + */ + public static final String extractMetricFromMetricNamePath(String metricNamePath) { + return METRIC_NAME_PATH_TEMPLATE.parse(metricNamePath).get("metric"); + } + + + // ============= + // Service Calls + // ============= + + // ----- listLogMetrics ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists logs-based metrics. + * + * + * + */ + public Iterable listLogMetrics(String projectName) { + ListLogMetricsRequest request = + ListLogMetricsRequest.newBuilder() + .setProjectName(projectName) + .build(); + return listLogMetrics(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists logs-based metrics. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public Iterable listLogMetrics(ListLogMetricsRequest request) { + return listLogMetricsStreamingCallable() + .iterableResponseStreamCall(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists logs-based metrics. + * + * + * + */ + public ApiCallable listLogMetricsStreamingCallable() { + return listLogMetricsCallable().pageStreaming(LIST_LOG_METRICS_PAGE_DESC); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists logs-based metrics. + * + * + * + */ + public ApiCallable listLogMetricsCallable() { + return ApiUtils.prepareIdempotentCallable(LIST_LOG_METRICS, settings).bind(channel); + } + + // ----- getLogMetric ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Gets a logs-based metric. + * + * + * + * + * @param metricName The resource name of the desired metric. + * Example: `"projects/my-project-id/metrics/my-metric-id"`. + */ + public LogMetric getLogMetric(String metricName) { + GetLogMetricRequest request = + GetLogMetricRequest.newBuilder() + .setMetricName(metricName) + .build(); + + return getLogMetric(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Gets a logs-based metric. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public LogMetric getLogMetric(GetLogMetricRequest request) { + return getLogMetricCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Gets a logs-based metric. + * + * + * + */ + public ApiCallable getLogMetricCallable() { + return ApiUtils.prepareIdempotentCallable(GET_LOG_METRIC, settings).bind(channel); + } + + // ----- createLogMetric ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Creates a logs-based metric. + * + * + * + * + * @param projectName The resource name of the project in which to create the metric. + * Example: `"projects/my-project-id"`. + * + * The new metric must be provided in the request. + * @param metric The new logs-based metric, which must not have an identifier that + * already exists. + */ + public LogMetric createLogMetric(String projectName, LogMetric metric) { + CreateLogMetricRequest request = + CreateLogMetricRequest.newBuilder() + .setProjectName(projectName) + .setMetric(metric) + .build(); + + return createLogMetric(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Creates a logs-based metric. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public LogMetric createLogMetric(CreateLogMetricRequest request) { + return createLogMetricCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Creates a logs-based metric. + * + * + * + */ + public ApiCallable createLogMetricCallable() { + return CREATE_LOG_METRIC.bind(channel); + } + + // ----- updateLogMetric ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Creates or updates a logs-based metric. + * + * + * + * + * @param metricName The resource name of the metric to update. + * Example: `"projects/my-project-id/metrics/my-metric-id"`. + * + * The updated metric must be provided in the request and have the + * same identifier that is specified in `metricName`. + * If the metric does not exist, it is created. + * @param metric The updated metric, whose name must be the same as the + * metric identifier in `metricName`. If `metricName` does not + * exist, then a new metric is created. + */ + public LogMetric updateLogMetric(String metricName, LogMetric metric) { + UpdateLogMetricRequest request = + UpdateLogMetricRequest.newBuilder() + .setMetricName(metricName) + .setMetric(metric) + .build(); + + return updateLogMetric(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Creates or updates a logs-based metric. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public LogMetric updateLogMetric(UpdateLogMetricRequest request) { + return updateLogMetricCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Creates or updates a logs-based metric. + * + * + * + */ + public ApiCallable updateLogMetricCallable() { + return ApiUtils.prepareIdempotentCallable(UPDATE_LOG_METRIC, settings).bind(channel); + } + + // ----- deleteLogMetric ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Deletes a logs-based metric. + * + * + * + * + * @param metricName The resource name of the metric to delete. + * Example: `"projects/my-project-id/metrics/my-metric-id"`. + */ + public void deleteLogMetric(String metricName) { + DeleteLogMetricRequest request = + DeleteLogMetricRequest.newBuilder() + .setMetricName(metricName) + .build(); + + deleteLogMetric(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Deletes a logs-based metric. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public void deleteLogMetric(DeleteLogMetricRequest request) { + deleteLogMetricCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Deletes a logs-based metric. + * + * + * + */ + public ApiCallable deleteLogMetricCallable() { + return ApiUtils.prepareIdempotentCallable(DELETE_LOG_METRIC, settings).bind(channel); + } + + + // ======== + // Cleanup + // ======== + + /** + * Initiates an orderly shutdown in which preexisting calls continue but new calls are immediately + * cancelled. + * + * + * + */ + @Override + public void close() { + // Manually-added shutdown code + + // Auto-generated shutdown code + channel.shutdown(); + + // Manually-added shutdown code + } + + + // ======== + // Manually-added methods: add custom (non-generated) methods after this point. + // ======== + +} diff --git a/gcloud-java-logging/src/main/java/com/google/gcloud/logging/spi/v2/testing/LocalLoggingImpl.java b/gcloud-java-logging/src/main/java/com/google/gcloud/logging/spi/v2/testing/LocalLoggingImpl.java new file mode 100644 index 000000000000..3144389a5a09 --- /dev/null +++ b/gcloud-java-logging/src/main/java/com/google/gcloud/logging/spi/v2/testing/LocalLoggingImpl.java @@ -0,0 +1,92 @@ +package com.google.gcloud.pubsub.spi.v2.testing; + +import com.google.logging.v2.DeleteLogRequest; +import com.google.logging.v2.ListLogEntriesRequest; +import com.google.logging.v2.ListLogEntriesResponse; +import com.google.logging.v2.ListMonitoredResourceDescriptorsRequest; +import com.google.logging.v2.ListMonitoredResourceDescriptorsResponse; +import com.google.logging.v2.LogEntry; +import com.google.logging.v2.LoggingServiceV2Grpc; +import com.google.logging.v2.ReadLogEntriesRequest; +import com.google.logging.v2.ReadLogEntriesResponse; +import com.google.logging.v2.WriteLogEntriesRequest; +import com.google.logging.v2.WriteLogEntriesResponse; +import com.google.protobuf.ByteString; +import com.google.protobuf.Empty; + +import io.grpc.stub.StreamObserver; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class LocalLoggingImpl implements LoggingServiceV2Grpc.LoggingServiceV2 { + + private Map> logs = new HashMap<>(); + + @Override + public void deleteLog(DeleteLogRequest request, StreamObserver responseObserver) { + logs.remove(request.getLogName()); + responseObserver.onNext(Empty.getDefaultInstance()); + responseObserver.onCompleted(); + } + + @Override + public void writeLogEntries( + WriteLogEntriesRequest request, StreamObserver responseObserver) { + List entries = logs.get(request.getLogName()); + if (entries == null) { + entries = new ArrayList(); + logs.put(request.getLogName(), entries); + } + entries.addAll(request.getEntriesList()); + // Response is empty + responseObserver.onNext(WriteLogEntriesResponse.newBuilder().build()); + responseObserver.onCompleted(); + } + + @Override + public void listLogEntries( + ListLogEntriesRequest request, StreamObserver responseObserver) { + List entries = new ArrayList<>(); + + for (ByteString proj : request.getProjectIdsList().asByteStringList()) { + String prefix = "projects/" + proj.toStringUtf8() + "/"; + for (Map.Entry> entry : logs.entrySet()) { + if (entry.getKey().startsWith(prefix)) { + entries.addAll(entry.getValue()); + } + } + } + responseObserver.onNext(ListLogEntriesResponse.newBuilder().addAllEntries(entries).build()); + responseObserver.onCompleted(); + } + + @Override + public void readLogEntries( + ReadLogEntriesRequest request, StreamObserver responseObserver) { + List entries = new ArrayList<>(); + for (ByteString proj : request.getProjectIdsList().asByteStringList()) { + String prefix = "projects/" + proj.toStringUtf8() + "/"; + for (Map.Entry> entry : logs.entrySet()) { + if (entry.getKey().startsWith(prefix)) { + entries.addAll(entry.getValue()); + } + } + } + responseObserver.onNext(ReadLogEntriesResponse.newBuilder().addAllEntries(entries).build()); + responseObserver.onCompleted(); + } + + @Override + public void listMonitoredResourceDescriptors( + ListMonitoredResourceDescriptorsRequest request, + StreamObserver responseObserver) { + throw new UnsupportedOperationException(); + } + + public void reset() { + logs.clear(); + } +} diff --git a/gcloud-java-logging/src/main/java/com/google/gcloud/logging/testing/LocalLoggingHelper.java b/gcloud-java-logging/src/main/java/com/google/gcloud/logging/testing/LocalLoggingHelper.java new file mode 100644 index 000000000000..4653112fed52 --- /dev/null +++ b/gcloud-java-logging/src/main/java/com/google/gcloud/logging/testing/LocalLoggingHelper.java @@ -0,0 +1,79 @@ +package com.google.gcloud.logging.testing; + +import com.google.gcloud.pubsub.spi.v2.testing.LocalLoggingImpl; +import com.google.logging.v2.LoggingServiceV2Grpc; + +import io.grpc.ManagedChannel; +import io.grpc.Server; +import io.grpc.netty.NegotiationType; +import io.grpc.netty.NettyChannelBuilder; +import io.grpc.netty.NettyServerBuilder; +import io.netty.channel.local.LocalAddress; +import io.netty.channel.local.LocalChannel; +import io.netty.channel.local.LocalServerChannel; + +import java.io.IOException; +import java.net.SocketAddress; + +/** + * LocalLoggingHelper runs an in-memory Logging server for use in tests. + */ +public class LocalLoggingHelper { + private static final int FLOW_CONTROL_WINDOW = 65 * 1024; + + private final SocketAddress address; + private final Server server; + private final LocalLoggingImpl loggingImpl; + + /** + * Constructs a new LocalPubsubHelper. The method start() must + * be called before it is used. + */ + public LocalLoggingHelper(String addressString) { + address = new LocalAddress(addressString); + loggingImpl = new LocalLoggingImpl(); + NettyServerBuilder builder = + NettyServerBuilder.forAddress(address) + .flowControlWindow(FLOW_CONTROL_WINDOW) + .channelType(LocalServerChannel.class); + builder.addService(LoggingServiceV2Grpc.bindService(loggingImpl)); + server = builder.build(); + } + /** + * Starts the in-memory service. + */ + public void start() { + try { + server.start(); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + /** + * Resets the state of the in-memory service. + */ + public void reset() { + loggingImpl.reset(); + } + /** + * Returns the internal in-memory service. + */ + public LocalLoggingImpl getLoggingImpl() { + return loggingImpl; + } + /** + * Creates a channel for making requests to the in-memory service. + */ + public ManagedChannel createChannel() { + return NettyChannelBuilder.forAddress(address) + .negotiationType(NegotiationType.PLAINTEXT) + .channelType(LocalChannel.class) + .build(); + } + /** + * Shuts down the in-memory service. + */ + public void shutdownNow() { + server.shutdownNow(); + } +} diff --git a/gcloud-java-logging/src/test/java/com/google/gcloud/logging/spi/v2/LoggingServiceV2ApiTest.java b/gcloud-java-logging/src/test/java/com/google/gcloud/logging/spi/v2/LoggingServiceV2ApiTest.java new file mode 100644 index 000000000000..fb55edc053b4 --- /dev/null +++ b/gcloud-java-logging/src/test/java/com/google/gcloud/logging/spi/v2/LoggingServiceV2ApiTest.java @@ -0,0 +1,107 @@ +/* + * Copyright 2015 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.gcloud.logging.spi.v2; + +import com.google.api.MonitoredResource; +import com.google.common.collect.Iterables; +import com.google.gcloud.logging.testing.LocalLoggingHelper; +import com.google.logging.v2.LogEntry; + +import io.gapi.gax.grpc.ServiceApiSettings; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +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; + +public class LoggingServiceV2ApiTest { + private static LocalLoggingHelper loggingHelper; + private LoggingServiceV2Api loggingApi; + + @BeforeClass + public static void startStaticServer() { + loggingHelper = new LocalLoggingHelper("in-process-1"); + loggingHelper.start(); + } + + @AfterClass + public static void stopServer() { + loggingHelper.shutdownNow(); + } + + @Before + public void setUp() throws IOException { + loggingHelper.reset(); + ServiceApiSettings settings = new ServiceApiSettings(); + settings.setChannel(loggingHelper.createChannel()); + loggingApi = LoggingServiceV2Api.create(settings); + } + + @After + public void tearDown() { + loggingApi.close(); + } + + @Test + public void testWriteLog() { + String logName = LoggingServiceV2Api.createLogNamePath("my-project", "my-log"); + MonitoredResource resource = MonitoredResource.newBuilder().build(); + List entries = new ArrayList<>(); + entries.add(LogEntry.newBuilder().setLogName(logName).setTextPayload("foobar").build()); + loggingApi.writeLogEntries(logName, resource, Collections.emptyMap(), entries); + } + + @Test + public void testListLog() { + String logName = LoggingServiceV2Api.createLogNamePath("my-project", "my-log"); + MonitoredResource resource = MonitoredResource.newBuilder().build(); + List entries = new ArrayList<>(); + entries.add(LogEntry.newBuilder().setLogName(logName).setTextPayload("foobar").build()); + loggingApi.writeLogEntries(logName, resource, Collections.emptyMap(), entries); + + Iterable gotEntries = + loggingApi.listLogEntries(Collections.singletonList("my-project"), "", ""); + Assert.assertTrue(Iterables.elementsEqual(entries, gotEntries)); + } + + @Test + public void testListNoLog() { + Iterable entries = + loggingApi.listLogEntries(Collections.singletonList("my-project"), "", ""); + Assert.assertTrue(Iterables.isEmpty(entries)); + } + + @Test + public void testDeleteLog() { + String logName = LoggingServiceV2Api.createLogNamePath("my-project", "my-log"); + MonitoredResource resource = MonitoredResource.newBuilder().build(); + List entries = new ArrayList<>(); + entries.add(LogEntry.newBuilder().setLogName(logName).setTextPayload("foobar").build()); + loggingApi.writeLogEntries(logName, resource, Collections.emptyMap(), entries); + + loggingApi.deleteLog(logName); + + Iterable gotEntries = + loggingApi.listLogEntries(Collections.singletonList("my-project"), "", ""); + Assert.assertTrue(Iterables.isEmpty(gotEntries)); + } +}