From ab44cf336097076879b8fa4b83cc442282d5a693 Mon Sep 17 00:00:00 2001 From: Carlos O'Ryan Date: Wed, 17 Aug 2022 15:28:09 -0700 Subject: [PATCH] feat(apikeys): generate library (#9699) --- BUILD.bazel | 2 + CHANGELOG.md | 8 + ci/etc/expected_install_directories | 6 + ci/etc/full_feature_list | 1 + external/googleapis/protodeps/apikeys.deps | 7 + external/googleapis/protolists/apikeys.list | 2 + external/googleapis/update_libraries.sh | 1 + generator/generator_config.textproto | 8 + google/cloud/apikeys/BUILD.bazel | 59 +++ google/cloud/apikeys/CMakeLists.txt | 182 ++++++++ google/cloud/apikeys/README.md | 90 ++++ google/cloud/apikeys/api_keys_client.cc | 140 ++++++ google/cloud/apikeys/api_keys_client.h | 425 ++++++++++++++++++ google/cloud/apikeys/api_keys_connection.cc | 105 +++++ google/cloud/apikeys/api_keys_connection.h | 122 +++++ .../api_keys_connection_idempotency_policy.cc | 92 ++++ .../api_keys_connection_idempotency_policy.h | 73 +++ google/cloud/apikeys/api_keys_options.h | 64 +++ google/cloud/apikeys/config.cmake.in | 22 + google/cloud/apikeys/doc/main.dox | 101 +++++ .../internal/api_keys_auth_decorator.cc | 174 +++++++ .../internal/api_keys_auth_decorator.h | 98 ++++ .../internal/api_keys_connection_impl.cc | 224 +++++++++ .../internal/api_keys_connection_impl.h | 123 +++++ .../internal/api_keys_logging_decorator.cc | 165 +++++++ .../internal/api_keys_logging_decorator.h | 99 ++++ .../internal/api_keys_metadata_decorator.cc | 139 ++++++ .../internal/api_keys_metadata_decorator.h | 98 ++++ .../internal/api_keys_option_defaults.cc | 71 +++ .../internal/api_keys_option_defaults.h | 37 ++ .../apikeys/internal/api_keys_retry_traits.h | 43 ++ .../cloud/apikeys/internal/api_keys_stub.cc | 170 +++++++ google/cloud/apikeys/internal/api_keys_stub.h | 151 +++++++ .../apikeys/internal/api_keys_stub_factory.cc | 64 +++ .../apikeys/internal/api_keys_stub_factory.h | 42 ++ .../apikeys/mocks/mock_api_keys_connection.h | 87 ++++ google/cloud/apikeys/quickstart/.bazelrc | 31 ++ google/cloud/apikeys/quickstart/BUILD.bazel | 25 ++ .../cloud/apikeys/quickstart/CMakeLists.txt | 32 ++ google/cloud/apikeys/quickstart/Makefile | 35 ++ google/cloud/apikeys/quickstart/README.md | 166 +++++++ .../cloud/apikeys/quickstart/WORKSPACE.bazel | 51 +++ google/cloud/apikeys/quickstart/quickstart.cc | 38 ++ 43 files changed, 3673 insertions(+) create mode 100644 external/googleapis/protodeps/apikeys.deps create mode 100644 external/googleapis/protolists/apikeys.list create mode 100644 google/cloud/apikeys/BUILD.bazel create mode 100644 google/cloud/apikeys/CMakeLists.txt create mode 100644 google/cloud/apikeys/README.md create mode 100644 google/cloud/apikeys/api_keys_client.cc create mode 100644 google/cloud/apikeys/api_keys_client.h create mode 100644 google/cloud/apikeys/api_keys_connection.cc create mode 100644 google/cloud/apikeys/api_keys_connection.h create mode 100644 google/cloud/apikeys/api_keys_connection_idempotency_policy.cc create mode 100644 google/cloud/apikeys/api_keys_connection_idempotency_policy.h create mode 100644 google/cloud/apikeys/api_keys_options.h create mode 100644 google/cloud/apikeys/config.cmake.in create mode 100644 google/cloud/apikeys/doc/main.dox create mode 100644 google/cloud/apikeys/internal/api_keys_auth_decorator.cc create mode 100644 google/cloud/apikeys/internal/api_keys_auth_decorator.h create mode 100644 google/cloud/apikeys/internal/api_keys_connection_impl.cc create mode 100644 google/cloud/apikeys/internal/api_keys_connection_impl.h create mode 100644 google/cloud/apikeys/internal/api_keys_logging_decorator.cc create mode 100644 google/cloud/apikeys/internal/api_keys_logging_decorator.h create mode 100644 google/cloud/apikeys/internal/api_keys_metadata_decorator.cc create mode 100644 google/cloud/apikeys/internal/api_keys_metadata_decorator.h create mode 100644 google/cloud/apikeys/internal/api_keys_option_defaults.cc create mode 100644 google/cloud/apikeys/internal/api_keys_option_defaults.h create mode 100644 google/cloud/apikeys/internal/api_keys_retry_traits.h create mode 100644 google/cloud/apikeys/internal/api_keys_stub.cc create mode 100644 google/cloud/apikeys/internal/api_keys_stub.h create mode 100644 google/cloud/apikeys/internal/api_keys_stub_factory.cc create mode 100644 google/cloud/apikeys/internal/api_keys_stub_factory.h create mode 100644 google/cloud/apikeys/mocks/mock_api_keys_connection.h create mode 100644 google/cloud/apikeys/quickstart/.bazelrc create mode 100644 google/cloud/apikeys/quickstart/BUILD.bazel create mode 100644 google/cloud/apikeys/quickstart/CMakeLists.txt create mode 100644 google/cloud/apikeys/quickstart/Makefile create mode 100644 google/cloud/apikeys/quickstart/README.md create mode 100644 google/cloud/apikeys/quickstart/WORKSPACE.bazel create mode 100644 google/cloud/apikeys/quickstart/quickstart.cc diff --git a/BUILD.bazel b/BUILD.bazel index b6ea8da1e290..77932bb52137 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -21,6 +21,8 @@ exports_files([ ]) EXPERIMENTAL_LIBRARIES = [ + # Introduced circa 2022-08-17 + "apikeys", # Introduced circa 2022-07-22 "beyondcorp", # Introduced circa 2022-07-12 diff --git a/CHANGELOG.md b/CHANGELOG.md index 379c0c2c56b7..cff04eb2bab7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -126,6 +126,14 @@ may elicit a deprecation warning. See [#9086] for more details. ## v2.2.0 - TBD +### New Libraries + +We are introducing new client libraries for GCP services. While we do not +anticipate any API changes to these libraries before declaring them GA, we are +releasing them early in case they elicit some feedback that requires changes. + +* [API Keys](/google/cloud/apikeys/README.md) + ### [Storage](/google/cloud/storage/README.md) * We have a new implementation for HTTP requests. This new implementation diff --git a/ci/etc/expected_install_directories b/ci/etc/expected_install_directories index ad934e29fe6f..50cdf66d07a2 100644 --- a/ci/etc/expected_install_directories +++ b/ci/etc/expected_install_directories @@ -2,6 +2,8 @@ ./include ./include/google ./include/google/api +./include/google/api/apikeys +./include/google/api/apikeys/v2 ./include/google/api/servicecontrol ./include/google/api/servicecontrol/v1 ./include/google/api/servicemanagement @@ -30,6 +32,9 @@ ./include/google/cloud/apigeeconnect/internal ./include/google/cloud/apigeeconnect/mocks ./include/google/cloud/apigeeconnect/v1 +./include/google/cloud/apikeys +./include/google/cloud/apikeys/internal +./include/google/cloud/apikeys/mocks ./include/google/cloud/appengine ./include/google/cloud/appengine/internal ./include/google/cloud/appengine/mocks @@ -437,6 +442,7 @@ ./lib64/cmake/google_cloud_cpp_accesscontextmanager ./lib64/cmake/google_cloud_cpp_apigateway ./lib64/cmake/google_cloud_cpp_apigeeconnect +./lib64/cmake/google_cloud_cpp_apikeys ./lib64/cmake/google_cloud_cpp_appengine ./lib64/cmake/google_cloud_cpp_artifactregistry ./lib64/cmake/google_cloud_cpp_asset diff --git a/ci/etc/full_feature_list b/ci/etc/full_feature_list index 28c1f32864d7..4379e5c2de52 100644 --- a/ci/etc/full_feature_list +++ b/ci/etc/full_feature_list @@ -2,6 +2,7 @@ accessapproval accesscontextmanager apigateway apigeeconnect +apikeys appengine artifactregistry asset diff --git a/external/googleapis/protodeps/apikeys.deps b/external/googleapis/protodeps/apikeys.deps new file mode 100644 index 000000000000..005db452bd44 --- /dev/null +++ b/external/googleapis/protodeps/apikeys.deps @@ -0,0 +1,7 @@ +@com_google_googleapis//google/api:annotations_proto +@com_google_googleapis//google/api:client_proto +@com_google_googleapis//google/api:field_behavior_proto +@com_google_googleapis//google/api:http_proto +@com_google_googleapis//google/api:resource_proto +@com_google_googleapis//google/longrunning:operations_proto +@com_google_googleapis//google/rpc:status_proto diff --git a/external/googleapis/protolists/apikeys.list b/external/googleapis/protolists/apikeys.list new file mode 100644 index 000000000000..f8d04a268cb2 --- /dev/null +++ b/external/googleapis/protolists/apikeys.list @@ -0,0 +1,2 @@ +@com_google_googleapis//google/api/apikeys/v2:apikeys.proto +@com_google_googleapis//google/api/apikeys/v2:resources.proto diff --git a/external/googleapis/update_libraries.sh b/external/googleapis/update_libraries.sh index 9ebae1fc1935..705b16d2dd0a 100755 --- a/external/googleapis/update_libraries.sh +++ b/external/googleapis/update_libraries.sh @@ -24,6 +24,7 @@ declare -A -r LIBRARIES=( )" ["apigateway"]="@com_google_googleapis//google/cloud/apigateway/v1:apigateway_cc_grpc" ["apigeeconnect"]="@com_google_googleapis//google/cloud/apigeeconnect/v1:apigeeconnect_cc_grpc" + ["apikeys"]="@com_google_googleapis//google/api/apikeys/v2:apikeys_cc_grpc" ["appengine"]="$( printf ",%s" \ "@com_google_googleapis//google/appengine/v1:appengine_cc_grpc" \ diff --git a/generator/generator_config.textproto b/generator/generator_config.textproto index 8d457dd6c349..fa75f7ad2540 100644 --- a/generator/generator_config.textproto +++ b/generator/generator_config.textproto @@ -47,6 +47,14 @@ service { retryable_status_codes: ["kUnavailable", "kUnknown"] } +# API Keys +service { + service_proto_path: "google/api/apikeys/v2/apikeys.proto" + product_path: "google/cloud/apikeys" + initial_copyright_year: "2022" + retryable_status_codes: ["kUnavailable"] +} + # App Engine Admin service { service_proto_path: "google/appengine/v1/appengine.proto" diff --git a/google/cloud/apikeys/BUILD.bazel b/google/cloud/apikeys/BUILD.bazel new file mode 100644 index 000000000000..b8663f4469ab --- /dev/null +++ b/google/cloud/apikeys/BUILD.bazel @@ -0,0 +1,59 @@ +# Copyright 2022 Google LLC +# +# 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 +# +# https://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(default_visibility = ["//visibility:private"]) + +licenses(["notice"]) # Apache 2.0 + +SOURCE_GLOB = "**/*.cc" + +MOCK_SOURCE_GLOB = "mocks/*.cc" + +HEADER_GLOB = "**/*.h" + +MOCK_HEADER_GLOB = "mocks/*.h" + +cc_library( + name = "google_cloud_cpp_apikeys", + srcs = glob( + include = [SOURCE_GLOB], + exclude = [MOCK_SOURCE_GLOB], + ), + hdrs = glob( + include = [HEADER_GLOB], + exclude = [MOCK_HEADER_GLOB], + ), + visibility = ["//:__pkg__"], + deps = [ + "//:common", + "//:grpc_utils", + "@com_google_googleapis//google/api/apikeys/v2:apikeys_cc_grpc", + ], +) + +cc_library( + name = "google_cloud_cpp_apikeys_mocks", + srcs = glob( + include = [MOCK_SOURCE_GLOB], + ), + hdrs = glob( + include = [MOCK_HEADER_GLOB], + ), + visibility = ["//:__pkg__"], + deps = [ + ":google_cloud_cpp_apikeys", + "//:common", + "//:grpc_utils", + ], +) diff --git a/google/cloud/apikeys/CMakeLists.txt b/google/cloud/apikeys/CMakeLists.txt new file mode 100644 index 000000000000..b5f94d6be0d0 --- /dev/null +++ b/google/cloud/apikeys/CMakeLists.txt @@ -0,0 +1,182 @@ +# ~~~ +# Copyright 2022 Google LLC +# +# 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 +# +# https://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. +# ~~~ + +include(GoogleapisConfig) +set(DOXYGEN_PROJECT_NAME "API Keys API C++ Client") +set(DOXYGEN_PROJECT_BRIEF "A C++ Client Library for the API Keys API") +set(DOXYGEN_PROJECT_NUMBER "${PROJECT_VERSION} (Experimental)") +set(DOXYGEN_EXCLUDE_SYMBOLS "internal" "apikeys_internal" "apikeys_testing" + "examples") +set(DOXYGEN_EXAMPLE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/quickstart) + +# Creates the proto headers needed by doxygen. +set(GOOGLE_CLOUD_CPP_DOXYGEN_DEPS google-cloud-cpp::apikeys_protos) + +include(GoogleCloudCppCommon) + +set(EXTERNAL_GOOGLEAPIS_SOURCE + "${PROJECT_BINARY_DIR}/external/googleapis/src/googleapis_download") +find_path(PROTO_INCLUDE_DIR google/protobuf/descriptor.proto) +if (PROTO_INCLUDE_DIR) + list(INSERT PROTOBUF_IMPORT_DIRS 0 "${PROTO_INCLUDE_DIR}") +endif () + +include(CompileProtos) +google_cloud_cpp_load_protolist( + proto_list + "${PROJECT_SOURCE_DIR}/external/googleapis/protolists/apikeys.list") +google_cloud_cpp_load_protodeps( + proto_deps + "${PROJECT_SOURCE_DIR}/external/googleapis/protodeps/apikeys.deps") +google_cloud_cpp_grpcpp_library( + google_cloud_cpp_apikeys_protos # cmake-format: sort + ${proto_list} PROTO_PATH_DIRECTORIES "${EXTERNAL_GOOGLEAPIS_SOURCE}" + "${PROTO_INCLUDE_DIR}") +external_googleapis_set_version_and_alias(apikeys_protos) +target_link_libraries(google_cloud_cpp_apikeys_protos PUBLIC ${proto_deps}) + +file( + GLOB source_files + RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" + "*.h" "*.cc" "internal/*.h" "internal/*.cc") +list(SORT source_files) +add_library(google_cloud_cpp_apikeys ${source_files}) +target_include_directories( + google_cloud_cpp_apikeys + PUBLIC $ + $ + $) +target_link_libraries( + google_cloud_cpp_apikeys + PUBLIC google-cloud-cpp::grpc_utils google-cloud-cpp::common + google-cloud-cpp::apikeys_protos) +google_cloud_cpp_add_common_options(google_cloud_cpp_apikeys) +set_target_properties( + google_cloud_cpp_apikeys + PROPERTIES EXPORT_NAME google-cloud-cpp::experimental-apikeys + VERSION "${PROJECT_VERSION}" + SOVERSION "${PROJECT_VERSION_MAJOR}") +target_compile_options(google_cloud_cpp_apikeys + PUBLIC ${GOOGLE_CLOUD_CPP_EXCEPTIONS_FLAG}) + +add_library(google-cloud-cpp::experimental-apikeys ALIAS + google_cloud_cpp_apikeys) + +# Create a header-only library for the mocks. We use a CMake `INTERFACE` library +# for these, a regular library would not work on macOS (where the library needs +# at least one .o file). Unfortunately INTERFACE libraries are a bit weird in +# that they need absolute paths for their sources. +file( + GLOB relative_mock_files + RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" + "mocks/*.h") +list(SORT relative_mock_files) +set(mock_files) +foreach (file IN LISTS relative_mock_files) + list(APPEND mock_files "${CMAKE_CURRENT_SOURCE_DIR}/${file}") +endforeach () +add_library(google_cloud_cpp_apikeys_mocks INTERFACE) +target_sources(google_cloud_cpp_apikeys_mocks INTERFACE ${mock_files}) +target_link_libraries( + google_cloud_cpp_apikeys_mocks + INTERFACE google-cloud-cpp::experimental-apikeys GTest::gmock_main + GTest::gmock GTest::gtest) +set_target_properties( + google_cloud_cpp_apikeys_mocks + PROPERTIES EXPORT_NAME google-cloud-cpp::experimental-apikeys_mocks) +target_include_directories( + google_cloud_cpp_apikeys_mocks + INTERFACE $ + $ + $) +target_compile_options(google_cloud_cpp_apikeys_mocks + INTERFACE ${GOOGLE_CLOUD_CPP_EXCEPTIONS_FLAG}) + +include(CTest) +if (BUILD_TESTING) + add_executable(apikeys_quickstart "quickstart/quickstart.cc") + target_link_libraries(apikeys_quickstart + PRIVATE google-cloud-cpp::experimental-apikeys) + google_cloud_cpp_add_common_options(apikeys_quickstart) + add_test( + NAME apikeys_quickstart + COMMAND cmake -P "${PROJECT_SOURCE_DIR}/cmake/quickstart-runner.cmake" + $ GOOGLE_CLOUD_PROJECT) + set_tests_properties(apikeys_quickstart + PROPERTIES LABELS "integration-test;quickstart") +endif () + +# Get the destination directories based on the GNU recommendations. +include(GNUInstallDirs) + +# Export the CMake targets to make it easy to create configuration files. +install( + EXPORT google_cloud_cpp_apikeys-targets + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/google_cloud_cpp_apikeys" + COMPONENT google_cloud_cpp_development) + +# Install the libraries and headers in the locations determined by +# GNUInstallDirs +install( + TARGETS google_cloud_cpp_apikeys google_cloud_cpp_apikeys_protos + EXPORT google_cloud_cpp_apikeys-targets + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + COMPONENT google_cloud_cpp_runtime + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + COMPONENT google_cloud_cpp_runtime + NAMELINK_SKIP + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + COMPONENT google_cloud_cpp_development) +# With CMake-3.12 and higher we could avoid this separate command (and the +# duplication). +install( + TARGETS google_cloud_cpp_apikeys google_cloud_cpp_apikeys_protos + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + COMPONENT google_cloud_cpp_development + NAMELINK_ONLY + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + COMPONENT google_cloud_cpp_development) + +google_cloud_cpp_install_proto_library_protos("google_cloud_cpp_apikeys_protos" + "${EXTERNAL_GOOGLEAPIS_SOURCE}") +google_cloud_cpp_install_proto_library_headers( + "google_cloud_cpp_apikeys_protos") +google_cloud_cpp_install_headers("google_cloud_cpp_apikeys" + "include/google/cloud/apikeys") +google_cloud_cpp_install_headers("google_cloud_cpp_apikeys_mocks" + "include/google/cloud/apikeys") + +google_cloud_cpp_add_pkgconfig( + apikeys "The API Keys API C++ Client Library" + "Provides C++ APIs to use the API Keys API." "google_cloud_cpp_grpc_utils" + " google_cloud_cpp_common" " google_cloud_cpp_apikeys_protos") + +# Create and install the CMake configuration files. +include(CMakePackageConfigHelpers) +configure_file("config.cmake.in" "google_cloud_cpp_apikeys-config.cmake" @ONLY) +write_basic_package_version_file( + "google_cloud_cpp_apikeys-config-version.cmake" + VERSION ${PROJECT_VERSION} + COMPATIBILITY ExactVersion) + +install( + FILES + "${CMAKE_CURRENT_BINARY_DIR}/google_cloud_cpp_apikeys-config.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/google_cloud_cpp_apikeys-config-version.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/google_cloud_cpp_apikeys" + COMPONENT google_cloud_cpp_development) + +external_googleapis_install_pc("google_cloud_cpp_apikeys_protos") diff --git a/google/cloud/apikeys/README.md b/google/cloud/apikeys/README.md new file mode 100644 index 000000000000..55a97e47d5dd --- /dev/null +++ b/google/cloud/apikeys/README.md @@ -0,0 +1,90 @@ +# API Keys API C++ Client Library + +:construction: + +This directory contains an idiomatic C++ client library for the +[API Keys API][cloud-service-docs], a service to manage the API keys associated +with developer projects. + +This library is **experimental**. Its APIs are subject to change without notice. + +Please note that the Google Cloud C++ client libraries do **not** follow +[Semantic Versioning](https://semver.org/). + +## Supported Platforms + +* Windows, macOS, Linux +* C++14 (and higher) compilers (we test with GCC >= 7.3, Clang >= 6.0, and + MSVC >= 2017) +* Environments with or without exceptions +* Bazel (>= 4.0) and CMake (>= 3.5) builds + +## Documentation + +* Official documentation about the [API Keys API][cloud-service-docs] service +* [Reference doxygen documentation][doxygen-link] for each release of this + client library +* Detailed header comments in our [public `.h`][source-link] files + +[cloud-service-docs]: https://cloud.google.com/api-keys +[doxygen-link]: https://googleapis.dev/cpp/google-cloud-apikeys/latest/ +[source-link]: https://github.com/googleapis/google-cloud-cpp/tree/main/google/cloud/apikeys + +## Quickstart + +The [quickstart/](quickstart/README.md) directory contains a minimal environment +to get started using this client library in a larger project. The following +"Hello World" program is used in this quickstart, and should give you a taste of +this library. + + +```cc +#include "google/cloud/apikeys/api_keys_client.h" +#include +#include + +int main(int argc, char* argv[]) try { + if (argc != 2) { + std::cerr << "Usage: " << argv[0] << " project-id\n"; + return 1; + } + + namespace apikeys = ::google::cloud::apikeys; + auto client = apikeys::ApiKeysClient(apikeys::MakeApiKeysConnection()); + + auto const parent = std::string{"projects/"} + argv[1] + "/locations/global"; + for (auto r : client.ListKeys(parent)) { + if (!r) throw std::runtime_error(r.status().message()); + std::cout << r->DebugString() << "\n"; + } + + return 0; +} catch (std::exception const& ex) { + std::cerr << "Standard exception raised: " << ex.what() << "\n"; + return 1; +} +``` + + +* Packaging maintainers or developers who prefer to install the library in a + fixed directory (such as `/usr/local` or `/opt`) should consult the + [packaging guide](/doc/packaging.md). +* Developers wanting to use the libraries as part of a larger CMake or Bazel + project should consult the [quickstart guides](#quickstart) for the library + or libraries they want to use. +* Developers wanting to compile the library just to run some of the examples or + tests should read the current document. +* Contributors and developers to `google-cloud-cpp` should consult the guide to + [setup a development workstation][howto-setup-dev-workstation]. + +[howto-setup-dev-workstation]: /doc/contributor/howto-guide-setup-development-workstation.md + +## Contributing changes + +See [`CONTRIBUTING.md`](/CONTRIBUTING.md) for details on how to +contribute to this project, including how to build and test your changes +as well as how to properly format your code. + +## Licensing + +Apache 2.0; see [`LICENSE`](/LICENSE) for details. diff --git a/google/cloud/apikeys/api_keys_client.cc b/google/cloud/apikeys/api_keys_client.cc new file mode 100644 index 000000000000..2b5238bd877e --- /dev/null +++ b/google/cloud/apikeys/api_keys_client.cc @@ -0,0 +1,140 @@ +// Copyright 2022 Google LLC +// +// 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 +// +// https://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. + +// Generated by the Codegen C++ plugin. +// If you make any local changes, they will be lost. +// source: google/api/apikeys/v2/apikeys.proto + +#include "google/cloud/apikeys/api_keys_client.h" +#include + +namespace google { +namespace cloud { +namespace apikeys { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +ApiKeysClient::ApiKeysClient(std::shared_ptr connection, + Options opts) + : connection_(std::move(connection)), + options_( + internal::MergeOptions(std::move(opts), connection_->options())) {} +ApiKeysClient::~ApiKeysClient() = default; + +future> ApiKeysClient::CreateKey( + std::string const& parent, google::api::apikeys::v2::Key const& key, + std::string const& key_id, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + google::api::apikeys::v2::CreateKeyRequest request; + request.set_parent(parent); + *request.mutable_key() = key; + request.set_key_id(key_id); + return connection_->CreateKey(request); +} + +future> ApiKeysClient::CreateKey( + google::api::apikeys::v2::CreateKeyRequest const& request, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + return connection_->CreateKey(request); +} + +StreamRange ApiKeysClient::ListKeys( + std::string const& parent, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + google::api::apikeys::v2::ListKeysRequest request; + request.set_parent(parent); + return connection_->ListKeys(request); +} + +StreamRange ApiKeysClient::ListKeys( + google::api::apikeys::v2::ListKeysRequest request, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + return connection_->ListKeys(std::move(request)); +} + +StatusOr ApiKeysClient::GetKey( + std::string const& name, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + google::api::apikeys::v2::GetKeyRequest request; + request.set_name(name); + return connection_->GetKey(request); +} + +StatusOr ApiKeysClient::GetKey( + google::api::apikeys::v2::GetKeyRequest const& request, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + return connection_->GetKey(request); +} + +StatusOr +ApiKeysClient::GetKeyString(std::string const& name, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + google::api::apikeys::v2::GetKeyStringRequest request; + request.set_name(name); + return connection_->GetKeyString(request); +} + +StatusOr +ApiKeysClient::GetKeyString( + google::api::apikeys::v2::GetKeyStringRequest const& request, + Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + return connection_->GetKeyString(request); +} + +future> ApiKeysClient::UpdateKey( + google::api::apikeys::v2::Key const& key, + google::protobuf::FieldMask const& update_mask, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + google::api::apikeys::v2::UpdateKeyRequest request; + *request.mutable_key() = key; + *request.mutable_update_mask() = update_mask; + return connection_->UpdateKey(request); +} + +future> ApiKeysClient::UpdateKey( + google::api::apikeys::v2::UpdateKeyRequest const& request, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + return connection_->UpdateKey(request); +} + +future> ApiKeysClient::DeleteKey( + std::string const& name, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + google::api::apikeys::v2::DeleteKeyRequest request; + request.set_name(name); + return connection_->DeleteKey(request); +} + +future> ApiKeysClient::DeleteKey( + google::api::apikeys::v2::DeleteKeyRequest const& request, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + return connection_->DeleteKey(request); +} + +future> ApiKeysClient::UndeleteKey( + google::api::apikeys::v2::UndeleteKeyRequest const& request, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + return connection_->UndeleteKey(request); +} + +StatusOr ApiKeysClient::LookupKey( + google::api::apikeys::v2::LookupKeyRequest const& request, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + return connection_->LookupKey(request); +} + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace apikeys +} // namespace cloud +} // namespace google diff --git a/google/cloud/apikeys/api_keys_client.h b/google/cloud/apikeys/api_keys_client.h new file mode 100644 index 000000000000..685c7b22d534 --- /dev/null +++ b/google/cloud/apikeys/api_keys_client.h @@ -0,0 +1,425 @@ +// Copyright 2022 Google LLC +// +// 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 +// +// https://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. + +// Generated by the Codegen C++ plugin. +// If you make any local changes, they will be lost. +// source: google/api/apikeys/v2/apikeys.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_API_KEYS_CLIENT_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_API_KEYS_CLIENT_H + +#include "google/cloud/apikeys/api_keys_connection.h" +#include "google/cloud/future.h" +#include "google/cloud/options.h" +#include "google/cloud/polling_policy.h" +#include "google/cloud/status_or.h" +#include "google/cloud/version.h" +#include +#include +#include + +namespace google { +namespace cloud { +namespace apikeys { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +/// +/// Manages the API keys associated with projects. +/// +/// @par Equality +/// +/// Instances of this class created via copy-construction or copy-assignment +/// always compare equal. Instances created with equal +/// `std::shared_ptr<*Connection>` objects compare equal. Objects that compare +/// equal share the same underlying resources. +/// +/// @par Performance +/// +/// Creating a new instance of this class is a relatively expensive operation, +/// new objects establish new connections to the service. In contrast, +/// copy-construction, move-construction, and the corresponding assignment +/// operations are relatively efficient as the copies share all underlying +/// resources. +/// +/// @par Thread Safety +/// +/// Concurrent access to different instances of this class, even if they compare +/// equal, is guaranteed to work. Two or more threads operating on the same +/// instance of this class is not guaranteed to work. Since copy-construction +/// and move-construction is a relatively efficient operation, consider using +/// such a copy when using this class from multiple threads. +/// +class ApiKeysClient { + public: + explicit ApiKeysClient(std::shared_ptr connection, + Options opts = {}); + ~ApiKeysClient(); + + //@{ + // @name Copy and move support + ApiKeysClient(ApiKeysClient const&) = default; + ApiKeysClient& operator=(ApiKeysClient const&) = default; + ApiKeysClient(ApiKeysClient&&) = default; + ApiKeysClient& operator=(ApiKeysClient&&) = default; + //@} + + //@{ + // @name Equality + friend bool operator==(ApiKeysClient const& a, ApiKeysClient const& b) { + return a.connection_ == b.connection_; + } + friend bool operator!=(ApiKeysClient const& a, ApiKeysClient const& b) { + return !(a == b); + } + //@} + + /// + /// Creates a new API key. + /// + /// NOTE: Key is a global resource; hence the only supported value for + /// location is `global`. + /// + /// @param parent Required. The project in which the API key is created. + /// @param key Required. The API key fields to set at creation time. + /// You can configure only the `display_name`, `restrictions`, and + /// `annotations` fields. + /// @param key_id User specified key id (optional). If specified, it will + /// become the final + /// component of the key resource name. + /// The id must be unique within the project, must conform with RFC-1034, + /// is restricted to lower-cased letters, and has a maximum length of 63 + /// characters. In another word, the id must match the regular + /// expression: `[a-z]([a-z0-9-]{0,61}[a-z0-9])?`. + /// The id must NOT be a UUID-like string. + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::api::apikeys::v2::Key,google/api/apikeys/v2/resources.proto#L32} + /// + /// [google.api.apikeys.v2.CreateKeyRequest]: + /// @googleapis_reference_link{google/api/apikeys/v2/apikeys.proto#L154} + /// [google.api.apikeys.v2.Key]: + /// @googleapis_reference_link{google/api/apikeys/v2/resources.proto#L32} + /// + future> CreateKey( + std::string const& parent, google::api::apikeys::v2::Key const& key, + std::string const& key_id, Options opts = {}); + + /// + /// Creates a new API key. + /// + /// NOTE: Key is a global resource; hence the only supported value for + /// location is `global`. + /// + /// @param request + /// @googleapis_link{google::api::apikeys::v2::CreateKeyRequest,google/api/apikeys/v2/apikeys.proto#L154} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::api::apikeys::v2::Key,google/api/apikeys/v2/resources.proto#L32} + /// + /// [google.api.apikeys.v2.CreateKeyRequest]: + /// @googleapis_reference_link{google/api/apikeys/v2/apikeys.proto#L154} + /// [google.api.apikeys.v2.Key]: + /// @googleapis_reference_link{google/api/apikeys/v2/resources.proto#L32} + /// + future> CreateKey( + google::api::apikeys::v2::CreateKeyRequest const& request, + Options opts = {}); + + /// + /// Lists the API keys owned by a project. The key string of the API key + /// isn't included in the response. + /// + /// NOTE: Key is a global resource; hence the only supported value for + /// location is `global`. + /// + /// @param parent Required. Lists all API keys associated with this project. + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::api::apikeys::v2::Key,google/api/apikeys/v2/resources.proto#L32} + /// + /// [google.api.apikeys.v2.ListKeysRequest]: + /// @googleapis_reference_link{google/api/apikeys/v2/apikeys.proto#L181} + /// [google.api.apikeys.v2.Key]: + /// @googleapis_reference_link{google/api/apikeys/v2/resources.proto#L32} + /// + StreamRange ListKeys(std::string const& parent, + Options opts = {}); + + /// + /// Lists the API keys owned by a project. The key string of the API key + /// isn't included in the response. + /// + /// NOTE: Key is a global resource; hence the only supported value for + /// location is `global`. + /// + /// @param request + /// @googleapis_link{google::api::apikeys::v2::ListKeysRequest,google/api/apikeys/v2/apikeys.proto#L181} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::api::apikeys::v2::Key,google/api/apikeys/v2/resources.proto#L32} + /// + /// [google.api.apikeys.v2.ListKeysRequest]: + /// @googleapis_reference_link{google/api/apikeys/v2/apikeys.proto#L181} + /// [google.api.apikeys.v2.Key]: + /// @googleapis_reference_link{google/api/apikeys/v2/resources.proto#L32} + /// + StreamRange ListKeys( + google::api::apikeys::v2::ListKeysRequest request, Options opts = {}); + + /// + /// Gets the metadata for an API key. The key string of the API key + /// isn't included in the response. + /// + /// NOTE: Key is a global resource; hence the only supported value for + /// location is `global`. + /// + /// @param name Required. The resource name of the API key to get. + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::api::apikeys::v2::Key,google/api/apikeys/v2/resources.proto#L32} + /// + /// [google.api.apikeys.v2.GetKeyRequest]: + /// @googleapis_reference_link{google/api/apikeys/v2/apikeys.proto#L211} + /// [google.api.apikeys.v2.Key]: + /// @googleapis_reference_link{google/api/apikeys/v2/resources.proto#L32} + /// + StatusOr GetKey(std::string const& name, + Options opts = {}); + + /// + /// Gets the metadata for an API key. The key string of the API key + /// isn't included in the response. + /// + /// NOTE: Key is a global resource; hence the only supported value for + /// location is `global`. + /// + /// @param request + /// @googleapis_link{google::api::apikeys::v2::GetKeyRequest,google/api/apikeys/v2/apikeys.proto#L211} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::api::apikeys::v2::Key,google/api/apikeys/v2/resources.proto#L32} + /// + /// [google.api.apikeys.v2.GetKeyRequest]: + /// @googleapis_reference_link{google/api/apikeys/v2/apikeys.proto#L211} + /// [google.api.apikeys.v2.Key]: + /// @googleapis_reference_link{google/api/apikeys/v2/resources.proto#L32} + /// + StatusOr GetKey( + google::api::apikeys::v2::GetKeyRequest const& request, + Options opts = {}); + + /// + /// Get the key string for an API key. + /// + /// NOTE: Key is a global resource; hence the only supported value for + /// location is `global`. + /// + /// @param name Required. The resource name of the API key to be retrieved. + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::api::apikeys::v2::GetKeyStringResponse,google/api/apikeys/v2/apikeys.proto#L229} + /// + /// [google.api.apikeys.v2.GetKeyStringRequest]: + /// @googleapis_reference_link{google/api/apikeys/v2/apikeys.proto#L220} + /// [google.api.apikeys.v2.GetKeyStringResponse]: + /// @googleapis_reference_link{google/api/apikeys/v2/apikeys.proto#L229} + /// + StatusOr GetKeyString( + std::string const& name, Options opts = {}); + + /// + /// Get the key string for an API key. + /// + /// NOTE: Key is a global resource; hence the only supported value for + /// location is `global`. + /// + /// @param request + /// @googleapis_link{google::api::apikeys::v2::GetKeyStringRequest,google/api/apikeys/v2/apikeys.proto#L220} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::api::apikeys::v2::GetKeyStringResponse,google/api/apikeys/v2/apikeys.proto#L229} + /// + /// [google.api.apikeys.v2.GetKeyStringRequest]: + /// @googleapis_reference_link{google/api/apikeys/v2/apikeys.proto#L220} + /// [google.api.apikeys.v2.GetKeyStringResponse]: + /// @googleapis_reference_link{google/api/apikeys/v2/apikeys.proto#L229} + /// + StatusOr GetKeyString( + google::api::apikeys::v2::GetKeyStringRequest const& request, + Options opts = {}); + + /// + /// Patches the modifiable fields of an API key. + /// The key string of the API key isn't included in the response. + /// + /// NOTE: Key is a global resource; hence the only supported value for + /// location is `global`. + /// + /// @param key Required. Set the `name` field to the resource name of the API + /// key to be + /// updated. You can update only the `display_name`, `restrictions`, and + /// `annotations` fields. + /// @param update_mask The field mask specifies which fields to be updated as + /// part of this + /// request. All other fields are ignored. + /// Mutable fields are: `display_name`, `restrictions`, and `annotations`. + /// If an update mask is not provided, the service treats it as an implied + /// mask equivalent to all allowed fields that are set on the wire. If the + /// field mask has a special value "*", the service treats it equivalent to + /// replace all allowed mutable fields. + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::api::apikeys::v2::Key,google/api/apikeys/v2/resources.proto#L32} + /// + /// [google.api.apikeys.v2.UpdateKeyRequest]: + /// @googleapis_reference_link{google/api/apikeys/v2/apikeys.proto#L235} + /// [google.api.apikeys.v2.Key]: + /// @googleapis_reference_link{google/api/apikeys/v2/resources.proto#L32} + /// + future> UpdateKey( + google::api::apikeys::v2::Key const& key, + google::protobuf::FieldMask const& update_mask, Options opts = {}); + + /// + /// Patches the modifiable fields of an API key. + /// The key string of the API key isn't included in the response. + /// + /// NOTE: Key is a global resource; hence the only supported value for + /// location is `global`. + /// + /// @param request + /// @googleapis_link{google::api::apikeys::v2::UpdateKeyRequest,google/api/apikeys/v2/apikeys.proto#L235} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::api::apikeys::v2::Key,google/api/apikeys/v2/resources.proto#L32} + /// + /// [google.api.apikeys.v2.UpdateKeyRequest]: + /// @googleapis_reference_link{google/api/apikeys/v2/apikeys.proto#L235} + /// [google.api.apikeys.v2.Key]: + /// @googleapis_reference_link{google/api/apikeys/v2/resources.proto#L32} + /// + future> UpdateKey( + google::api::apikeys::v2::UpdateKeyRequest const& request, + Options opts = {}); + + /// + /// Deletes an API key. Deleted key can be retrieved within 30 days of + /// deletion. Afterward, key will be purged from the project. + /// + /// NOTE: Key is a global resource; hence the only supported value for + /// location is `global`. + /// + /// @param name Required. The resource name of the API key to be deleted. + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::api::apikeys::v2::Key,google/api/apikeys/v2/resources.proto#L32} + /// + /// [google.api.apikeys.v2.DeleteKeyRequest]: + /// @googleapis_reference_link{google/api/apikeys/v2/apikeys.proto#L252} + /// [google.api.apikeys.v2.Key]: + /// @googleapis_reference_link{google/api/apikeys/v2/resources.proto#L32} + /// + future> DeleteKey( + std::string const& name, Options opts = {}); + + /// + /// Deletes an API key. Deleted key can be retrieved within 30 days of + /// deletion. Afterward, key will be purged from the project. + /// + /// NOTE: Key is a global resource; hence the only supported value for + /// location is `global`. + /// + /// @param request + /// @googleapis_link{google::api::apikeys::v2::DeleteKeyRequest,google/api/apikeys/v2/apikeys.proto#L252} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::api::apikeys::v2::Key,google/api/apikeys/v2/resources.proto#L32} + /// + /// [google.api.apikeys.v2.DeleteKeyRequest]: + /// @googleapis_reference_link{google/api/apikeys/v2/apikeys.proto#L252} + /// [google.api.apikeys.v2.Key]: + /// @googleapis_reference_link{google/api/apikeys/v2/resources.proto#L32} + /// + future> DeleteKey( + google::api::apikeys::v2::DeleteKeyRequest const& request, + Options opts = {}); + + /// + /// Undeletes an API key which was deleted within 30 days. + /// + /// NOTE: Key is a global resource; hence the only supported value for + /// location is `global`. + /// + /// @param request + /// @googleapis_link{google::api::apikeys::v2::UndeleteKeyRequest,google/api/apikeys/v2/apikeys.proto#L265} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::api::apikeys::v2::Key,google/api/apikeys/v2/resources.proto#L32} + /// + /// [google.api.apikeys.v2.UndeleteKeyRequest]: + /// @googleapis_reference_link{google/api/apikeys/v2/apikeys.proto#L265} + /// [google.api.apikeys.v2.Key]: + /// @googleapis_reference_link{google/api/apikeys/v2/resources.proto#L32} + /// + future> UndeleteKey( + google::api::apikeys::v2::UndeleteKeyRequest const& request, + Options opts = {}); + + /// + /// Find the parent project and resource name of the API + /// key that matches the key string in the request. If the API key has been + /// purged, resource name will not be set. + /// The service account must have the `apikeys.keys.lookup` permission + /// on the parent project. + /// + /// @param request + /// @googleapis_link{google::api::apikeys::v2::LookupKeyRequest,google/api/apikeys/v2/apikeys.proto#L274} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::api::apikeys::v2::LookupKeyResponse,google/api/apikeys/v2/apikeys.proto#L280} + /// + /// [google.api.apikeys.v2.LookupKeyRequest]: + /// @googleapis_reference_link{google/api/apikeys/v2/apikeys.proto#L274} + /// [google.api.apikeys.v2.LookupKeyResponse]: + /// @googleapis_reference_link{google/api/apikeys/v2/apikeys.proto#L280} + /// + StatusOr LookupKey( + google::api::apikeys::v2::LookupKeyRequest const& request, + Options opts = {}); + + private: + std::shared_ptr connection_; + Options options_; +}; + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace apikeys +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_API_KEYS_CLIENT_H diff --git a/google/cloud/apikeys/api_keys_connection.cc b/google/cloud/apikeys/api_keys_connection.cc new file mode 100644 index 000000000000..b036f239f078 --- /dev/null +++ b/google/cloud/apikeys/api_keys_connection.cc @@ -0,0 +1,105 @@ +// Copyright 2022 Google LLC +// +// 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 +// +// https://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. + +// Generated by the Codegen C++ plugin. +// If you make any local changes, they will be lost. +// source: google/api/apikeys/v2/apikeys.proto + +#include "google/cloud/apikeys/api_keys_connection.h" +#include "google/cloud/apikeys/api_keys_options.h" +#include "google/cloud/apikeys/internal/api_keys_connection_impl.h" +#include "google/cloud/apikeys/internal/api_keys_option_defaults.h" +#include "google/cloud/apikeys/internal/api_keys_stub_factory.h" +#include "google/cloud/background_threads.h" +#include "google/cloud/common_options.h" +#include "google/cloud/credentials.h" +#include "google/cloud/grpc_options.h" +#include "google/cloud/internal/pagination_range.h" +#include + +namespace google { +namespace cloud { +namespace apikeys { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +ApiKeysConnection::~ApiKeysConnection() = default; + +future> ApiKeysConnection::CreateKey( + google::api::apikeys::v2::CreateKeyRequest const&) { + return google::cloud::make_ready_future< + StatusOr>( + Status(StatusCode::kUnimplemented, "not implemented")); +} + +StreamRange ApiKeysConnection::ListKeys( + google::api::apikeys::v2:: + ListKeysRequest) { // NOLINT(performance-unnecessary-value-param) + return google::cloud::internal::MakeUnimplementedPaginationRange< + StreamRange>(); +} + +StatusOr ApiKeysConnection::GetKey( + google::api::apikeys::v2::GetKeyRequest const&) { + return Status(StatusCode::kUnimplemented, "not implemented"); +} + +StatusOr +ApiKeysConnection::GetKeyString( + google::api::apikeys::v2::GetKeyStringRequest const&) { + return Status(StatusCode::kUnimplemented, "not implemented"); +} + +future> ApiKeysConnection::UpdateKey( + google::api::apikeys::v2::UpdateKeyRequest const&) { + return google::cloud::make_ready_future< + StatusOr>( + Status(StatusCode::kUnimplemented, "not implemented")); +} + +future> ApiKeysConnection::DeleteKey( + google::api::apikeys::v2::DeleteKeyRequest const&) { + return google::cloud::make_ready_future< + StatusOr>( + Status(StatusCode::kUnimplemented, "not implemented")); +} + +future> ApiKeysConnection::UndeleteKey( + google::api::apikeys::v2::UndeleteKeyRequest const&) { + return google::cloud::make_ready_future< + StatusOr>( + Status(StatusCode::kUnimplemented, "not implemented")); +} + +StatusOr +ApiKeysConnection::LookupKey( + google::api::apikeys::v2::LookupKeyRequest const&) { + return Status(StatusCode::kUnimplemented, "not implemented"); +} + +std::shared_ptr MakeApiKeysConnection(Options options) { + internal::CheckExpectedOptions(options, __func__); + options = apikeys_internal::ApiKeysDefaultOptions(std::move(options)); + auto background = internal::MakeBackgroundThreadsFactory(options)(); + auto stub = + apikeys_internal::CreateDefaultApiKeysStub(background->cq(), options); + return std::make_shared( + std::move(background), std::move(stub), std::move(options)); +} + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace apikeys +} // namespace cloud +} // namespace google diff --git a/google/cloud/apikeys/api_keys_connection.h b/google/cloud/apikeys/api_keys_connection.h new file mode 100644 index 000000000000..9491a88e5d79 --- /dev/null +++ b/google/cloud/apikeys/api_keys_connection.h @@ -0,0 +1,122 @@ +// Copyright 2022 Google LLC +// +// 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 +// +// https://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. + +// Generated by the Codegen C++ plugin. +// If you make any local changes, they will be lost. +// source: google/api/apikeys/v2/apikeys.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_API_KEYS_CONNECTION_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_API_KEYS_CONNECTION_H + +#include "google/cloud/apikeys/api_keys_connection_idempotency_policy.h" +#include "google/cloud/apikeys/internal/api_keys_retry_traits.h" +#include "google/cloud/apikeys/internal/api_keys_stub.h" +#include "google/cloud/backoff_policy.h" +#include "google/cloud/future.h" +#include "google/cloud/options.h" +#include "google/cloud/polling_policy.h" +#include "google/cloud/status_or.h" +#include "google/cloud/stream_range.h" +#include "google/cloud/version.h" +#include +#include + +namespace google { +namespace cloud { +namespace apikeys { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +using ApiKeysRetryPolicy = ::google::cloud::internal::TraitBasedRetryPolicy< + apikeys_internal::ApiKeysRetryTraits>; + +using ApiKeysLimitedTimeRetryPolicy = + ::google::cloud::internal::LimitedTimeRetryPolicy< + apikeys_internal::ApiKeysRetryTraits>; + +using ApiKeysLimitedErrorCountRetryPolicy = + ::google::cloud::internal::LimitedErrorCountRetryPolicy< + apikeys_internal::ApiKeysRetryTraits>; + +/** + * The `ApiKeysConnection` object for `ApiKeysClient`. + * + * This interface defines virtual methods for each of the user-facing overload + * sets in `ApiKeysClient`. This allows users to inject custom behavior + * (e.g., with a Google Mock object) when writing tests that use objects of type + * `ApiKeysClient`. + * + * To create a concrete instance, see `MakeApiKeysConnection()`. + * + * For mocking, see `apikeys_mocks::MockApiKeysConnection`. + */ +class ApiKeysConnection { + public: + virtual ~ApiKeysConnection() = 0; + + virtual Options options() { return Options{}; } + + virtual future> CreateKey( + google::api::apikeys::v2::CreateKeyRequest const& request); + + virtual StreamRange ListKeys( + google::api::apikeys::v2::ListKeysRequest request); + + virtual StatusOr GetKey( + google::api::apikeys::v2::GetKeyRequest const& request); + + virtual StatusOr GetKeyString( + google::api::apikeys::v2::GetKeyStringRequest const& request); + + virtual future> UpdateKey( + google::api::apikeys::v2::UpdateKeyRequest const& request); + + virtual future> DeleteKey( + google::api::apikeys::v2::DeleteKeyRequest const& request); + + virtual future> UndeleteKey( + google::api::apikeys::v2::UndeleteKeyRequest const& request); + + virtual StatusOr LookupKey( + google::api::apikeys::v2::LookupKeyRequest const& request); +}; + +/** + * A factory function to construct an object of type `ApiKeysConnection`. + * + * The returned connection object should not be used directly; instead it + * should be passed as an argument to the constructor of ApiKeysClient. + * + * The optional @p options argument may be used to configure aspects of the + * returned `ApiKeysConnection`. Expected options are any of the types in + * the following option lists: + * + * - `google::cloud::CommonOptionList` + * - `google::cloud::GrpcOptionList` + * - `google::cloud::UnifiedCredentialsOptionList` + * - `google::cloud::apikeys::ApiKeysPolicyOptionList` + * + * @note Unexpected options will be ignored. To log unexpected options instead, + * set `GOOGLE_CLOUD_CPP_ENABLE_CLOG=yes` in the environment. + * + * @param options (optional) Configure the `ApiKeysConnection` created by + * this function. + */ +std::shared_ptr MakeApiKeysConnection(Options options = {}); + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace apikeys +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_API_KEYS_CONNECTION_H diff --git a/google/cloud/apikeys/api_keys_connection_idempotency_policy.cc b/google/cloud/apikeys/api_keys_connection_idempotency_policy.cc new file mode 100644 index 000000000000..d97fb1b62a8f --- /dev/null +++ b/google/cloud/apikeys/api_keys_connection_idempotency_policy.cc @@ -0,0 +1,92 @@ +// Copyright 2022 Google LLC +// +// 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 +// +// https://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. + +// Generated by the Codegen C++ plugin. +// If you make any local changes, they will be lost. +// source: google/api/apikeys/v2/apikeys.proto + +#include "google/cloud/apikeys/api_keys_connection_idempotency_policy.h" +#include "absl/memory/memory.h" +#include + +namespace google { +namespace cloud { +namespace apikeys { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +using ::google::cloud::Idempotency; + +ApiKeysConnectionIdempotencyPolicy::~ApiKeysConnectionIdempotencyPolicy() = + default; + +namespace { +class DefaultApiKeysConnectionIdempotencyPolicy + : public ApiKeysConnectionIdempotencyPolicy { + public: + ~DefaultApiKeysConnectionIdempotencyPolicy() override = default; + + /// Create a new copy of this object. + std::unique_ptr clone() const override { + return absl::make_unique(*this); + } + + Idempotency CreateKey( + google::api::apikeys::v2::CreateKeyRequest const&) override { + return Idempotency::kNonIdempotent; + } + + Idempotency ListKeys(google::api::apikeys::v2::ListKeysRequest) override { + return Idempotency::kIdempotent; + } + + Idempotency GetKey(google::api::apikeys::v2::GetKeyRequest const&) override { + return Idempotency::kIdempotent; + } + + Idempotency GetKeyString( + google::api::apikeys::v2::GetKeyStringRequest const&) override { + return Idempotency::kIdempotent; + } + + Idempotency UpdateKey( + google::api::apikeys::v2::UpdateKeyRequest const&) override { + return Idempotency::kNonIdempotent; + } + + Idempotency DeleteKey( + google::api::apikeys::v2::DeleteKeyRequest const&) override { + return Idempotency::kNonIdempotent; + } + + Idempotency UndeleteKey( + google::api::apikeys::v2::UndeleteKeyRequest const&) override { + return Idempotency::kNonIdempotent; + } + + Idempotency LookupKey( + google::api::apikeys::v2::LookupKeyRequest const&) override { + return Idempotency::kIdempotent; + } +}; +} // namespace + +std::unique_ptr +MakeDefaultApiKeysConnectionIdempotencyPolicy() { + return absl::make_unique(); +} + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace apikeys +} // namespace cloud +} // namespace google diff --git a/google/cloud/apikeys/api_keys_connection_idempotency_policy.h b/google/cloud/apikeys/api_keys_connection_idempotency_policy.h new file mode 100644 index 000000000000..4bc61aa7f27b --- /dev/null +++ b/google/cloud/apikeys/api_keys_connection_idempotency_policy.h @@ -0,0 +1,73 @@ +// Copyright 2022 Google LLC +// +// 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 +// +// https://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. + +// Generated by the Codegen C++ plugin. +// If you make any local changes, they will be lost. +// source: google/api/apikeys/v2/apikeys.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_API_KEYS_CONNECTION_IDEMPOTENCY_POLICY_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_API_KEYS_CONNECTION_IDEMPOTENCY_POLICY_H + +#include "google/cloud/idempotency.h" +#include "google/cloud/internal/retry_policy.h" +#include "google/cloud/version.h" +#include +#include + +namespace google { +namespace cloud { +namespace apikeys { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +class ApiKeysConnectionIdempotencyPolicy { + public: + virtual ~ApiKeysConnectionIdempotencyPolicy() = 0; + + /// Create a new copy of this object. + virtual std::unique_ptr clone() const = 0; + + virtual google::cloud::Idempotency CreateKey( + google::api::apikeys::v2::CreateKeyRequest const& request) = 0; + + virtual google::cloud::Idempotency ListKeys( + google::api::apikeys::v2::ListKeysRequest request) = 0; + + virtual google::cloud::Idempotency GetKey( + google::api::apikeys::v2::GetKeyRequest const& request) = 0; + + virtual google::cloud::Idempotency GetKeyString( + google::api::apikeys::v2::GetKeyStringRequest const& request) = 0; + + virtual google::cloud::Idempotency UpdateKey( + google::api::apikeys::v2::UpdateKeyRequest const& request) = 0; + + virtual google::cloud::Idempotency DeleteKey( + google::api::apikeys::v2::DeleteKeyRequest const& request) = 0; + + virtual google::cloud::Idempotency UndeleteKey( + google::api::apikeys::v2::UndeleteKeyRequest const& request) = 0; + + virtual google::cloud::Idempotency LookupKey( + google::api::apikeys::v2::LookupKeyRequest const& request) = 0; +}; + +std::unique_ptr +MakeDefaultApiKeysConnectionIdempotencyPolicy(); + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace apikeys +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_API_KEYS_CONNECTION_IDEMPOTENCY_POLICY_H diff --git a/google/cloud/apikeys/api_keys_options.h b/google/cloud/apikeys/api_keys_options.h new file mode 100644 index 000000000000..502efa849ff9 --- /dev/null +++ b/google/cloud/apikeys/api_keys_options.h @@ -0,0 +1,64 @@ +// Copyright 2022 Google LLC +// +// 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 +// +// https://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. + +// Generated by the Codegen C++ plugin. +// If you make any local changes, they will be lost. +// source: google/api/apikeys/v2/apikeys.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_API_KEYS_OPTIONS_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_API_KEYS_OPTIONS_H + +#include "google/cloud/apikeys/api_keys_connection.h" +#include "google/cloud/apikeys/api_keys_connection_idempotency_policy.h" +#include "google/cloud/backoff_policy.h" +#include "google/cloud/options.h" +#include "google/cloud/version.h" +#include + +namespace google { +namespace cloud { +namespace apikeys { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +/// Option to use with `google::cloud::Options`. +struct ApiKeysRetryPolicyOption { + using Type = std::shared_ptr; +}; + +/// Option to use with `google::cloud::Options`. +struct ApiKeysBackoffPolicyOption { + using Type = std::shared_ptr; +}; + +/// Option to use with `google::cloud::Options`. +struct ApiKeysPollingPolicyOption { + using Type = std::shared_ptr; +}; + +/// Option to use with `google::cloud::Options`. +struct ApiKeysConnectionIdempotencyPolicyOption { + using Type = std::shared_ptr; +}; + +using ApiKeysPolicyOptionList = + OptionList; + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace apikeys +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_API_KEYS_OPTIONS_H diff --git a/google/cloud/apikeys/config.cmake.in b/google/cloud/apikeys/config.cmake.in new file mode 100644 index 000000000000..13cee0055027 --- /dev/null +++ b/google/cloud/apikeys/config.cmake.in @@ -0,0 +1,22 @@ +# Copyright 2022 Google LLC +# +# 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 +# +# https://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. + +include(CMakeFindDependencyMacro) +# google_cloud_cpp_googleapis finds both gRPC and Protobuf, no need to load them here. +find_dependency(google_cloud_cpp_googleapis) +find_dependency(google_cloud_cpp_common) +find_dependency(google_cloud_cpp_grpc_utils) +find_dependency(absl) + +include("${CMAKE_CURRENT_LIST_DIR}/google_cloud_cpp_apikeys-targets.cmake") diff --git a/google/cloud/apikeys/doc/main.dox b/google/cloud/apikeys/doc/main.dox new file mode 100644 index 000000000000..ad2b80728368 --- /dev/null +++ b/google/cloud/apikeys/doc/main.dox @@ -0,0 +1,101 @@ +/*! + +@mainpage API Keys API C++ Client Library + +An idiomatic C++ client library for the [API Keys API][cloud-service-docs], a +service to manage the API keys associated with developer projects. + +This library is **experimental**. Its APIs are subject to change without notice. + +This library requires a C++14 compiler. It is supported (and tested) on multiple +Linux distributions, as well as Windows and macOS. The [README][github-readme] +on [GitHub][github-link] provides detailed instructions to install the necessary +dependencies, as well as how to compile the client library. + +@tableofcontents{HTML:2} + +## Setting up your repo + +In order to use the API Keys API C++ client library from your own code, +you'll need to configure your build system to discover and compile the Cloud +C++ client libraries. In some cases your build system or package manager may +need to download the libraries too. The Cloud C++ client libraries natively +support [Bazel](https://bazel.build/) and [CMake](https://cmake.org/) as build +systems. We've created a minimal, "Hello World", [quickstart][github-quickstart] +that includes detailed instructions on how to compile the library for use in +your application. You can fetch the source from [GitHub][github-link] as normal: + +@code{.sh} +git clone https://github.com/googleapis/google-cloud-cpp.git +cd google-cloud-cpp/google/cloud/apikeys/quickstart +@endcode + +@par Example: Quickstart + +The following shows the code that you'll run in the +`google/cloud/apikeys/quickstart/` directory, +which should give you a taste of the API Keys API C++ client library API. + +@include quickstart.cc + +## Environment Variables + + + +- `GOOGLE_CLOUD_CPP_API_KEYS_ENDPOINT=...` overrides the + `EndpointOption` (which defaults to "apikeys.googleapis.com") + used by `MakeApiKeysConnection()`. + + + +- `GOOGLE_CLOUD_CPP_ENABLE_TRACING=rpc` turns on tracing for most gRPC + calls. The library injects an additional Stub decorator that prints each gRPC + request and response. Unless you have configured your own logging backend, + you should also set `GOOGLE_CLOUD_CPP_ENABLE_CLOG` to produce any output on + the program's console. + +- `GOOGLE_CLOUD_CPP_ENABLE_TRACING=rpc-streams` turns on tracing for streaming + gRPC calls. This can produce a lot of output, so use with caution! + +- `GOOGLE_CLOUD_CPP_TRACING_OPTIONS=...` modifies the behavior of gRPC tracing, + including whether messages will be output on multiple lines, or whether + string/bytes fields will be truncated. + +- `GOOGLE_CLOUD_CPP_ENABLE_CLOG=yes` turns on logging in the library. Basically + the library always "logs" but the logging infrastructure has no backend to + actually print anything until the application sets a backend or it sets this + environment variable. + +## Error Handling + +[status-or-header]: https://github.com/googleapis/google-cloud-cpp/blob/main/google/cloud/status_or.h + +This library never throws exceptions to signal error, but you can use exceptions +to detect errors in the returned objects. In general, the library returns a +[`StatusOr`][status-or-header] if an error is possible. This is an "outcome" +type, when the operation is successful a `StatusOr` converts to `true` in +boolean context (and its `.ok()` member function returns `true`), the +application can then use `operator->` or `operator*` to access the `T` value. +When the operation fails a `StatusOr` converts to `false` (and `.ok()` +returns `false`). It is undefined behavior to use the value in this case. + +If you prefer to use exceptions on error, you can use the `.value()` accessor. +It will return the `T` value or throw on error. + +For operations that do not return a value the library simply returns +`google::cloud::Status`. + +## Retry, Backoff, and Idempotency Policies. + +The library automatically retries requests that fail with transient errors, and +uses [exponential backoff] to backoff between retries. Application developers +can override the default policies. + +[cloud-service-docs]: https://cloud.google.com/api-keys +[exponential backoff]: https://en.wikipedia.org/wiki/Exponential_backoff +[github-link]: https://github.com/googleapis/google-cloud-cpp 'GitHub Repository' + +[github-readme]: https://github.com/googleapis/google-cloud-cpp/blob/main/google/cloud/apikeys/README%2Emd +[github-quickstart]: https://github.com/googleapis/google-cloud-cpp/blob/main/google/cloud/apikeys/quickstart/README%2Emd + +*/ diff --git a/google/cloud/apikeys/internal/api_keys_auth_decorator.cc b/google/cloud/apikeys/internal/api_keys_auth_decorator.cc new file mode 100644 index 000000000000..c3f1e71b1429 --- /dev/null +++ b/google/cloud/apikeys/internal/api_keys_auth_decorator.cc @@ -0,0 +1,174 @@ +// Copyright 2022 Google LLC +// +// 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 +// +// https://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. + +// Generated by the Codegen C++ plugin. +// If you make any local changes, they will be lost. +// source: google/api/apikeys/v2/apikeys.proto + +#include "google/cloud/apikeys/internal/api_keys_auth_decorator.h" +#include +#include + +namespace google { +namespace cloud { +namespace apikeys_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +ApiKeysAuth::ApiKeysAuth( + std::shared_ptr auth, + std::shared_ptr child) + : auth_(std::move(auth)), child_(std::move(child)) {} + +future> ApiKeysAuth::AsyncCreateKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::CreateKeyRequest const& request) { + using ReturnType = StatusOr; + auto& child = child_; + return auth_->AsyncConfigureContext(std::move(context)) + .then([cq, child, + request](future>> + f) mutable { + auto context = f.get(); + if (!context) { + return make_ready_future(ReturnType(std::move(context).status())); + } + return child->AsyncCreateKey(cq, *std::move(context), request); + }); +} + +StatusOr ApiKeysAuth::ListKeys( + grpc::ClientContext& context, + google::api::apikeys::v2::ListKeysRequest const& request) { + auto status = auth_->ConfigureContext(context); + if (!status.ok()) return status; + return child_->ListKeys(context, request); +} + +StatusOr ApiKeysAuth::GetKey( + grpc::ClientContext& context, + google::api::apikeys::v2::GetKeyRequest const& request) { + auto status = auth_->ConfigureContext(context); + if (!status.ok()) return status; + return child_->GetKey(context, request); +} + +StatusOr +ApiKeysAuth::GetKeyString( + grpc::ClientContext& context, + google::api::apikeys::v2::GetKeyStringRequest const& request) { + auto status = auth_->ConfigureContext(context); + if (!status.ok()) return status; + return child_->GetKeyString(context, request); +} + +future> ApiKeysAuth::AsyncUpdateKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::UpdateKeyRequest const& request) { + using ReturnType = StatusOr; + auto& child = child_; + return auth_->AsyncConfigureContext(std::move(context)) + .then([cq, child, + request](future>> + f) mutable { + auto context = f.get(); + if (!context) { + return make_ready_future(ReturnType(std::move(context).status())); + } + return child->AsyncUpdateKey(cq, *std::move(context), request); + }); +} + +future> ApiKeysAuth::AsyncDeleteKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::DeleteKeyRequest const& request) { + using ReturnType = StatusOr; + auto& child = child_; + return auth_->AsyncConfigureContext(std::move(context)) + .then([cq, child, + request](future>> + f) mutable { + auto context = f.get(); + if (!context) { + return make_ready_future(ReturnType(std::move(context).status())); + } + return child->AsyncDeleteKey(cq, *std::move(context), request); + }); +} + +future> ApiKeysAuth::AsyncUndeleteKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::UndeleteKeyRequest const& request) { + using ReturnType = StatusOr; + auto& child = child_; + return auth_->AsyncConfigureContext(std::move(context)) + .then([cq, child, + request](future>> + f) mutable { + auto context = f.get(); + if (!context) { + return make_ready_future(ReturnType(std::move(context).status())); + } + return child->AsyncUndeleteKey(cq, *std::move(context), request); + }); +} + +StatusOr ApiKeysAuth::LookupKey( + grpc::ClientContext& context, + google::api::apikeys::v2::LookupKeyRequest const& request) { + auto status = auth_->ConfigureContext(context); + if (!status.ok()) return status; + return child_->LookupKey(context, request); +} + +future> ApiKeysAuth::AsyncGetOperation( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::GetOperationRequest const& request) { + using ReturnType = StatusOr; + auto& child = child_; + return auth_->AsyncConfigureContext(std::move(context)) + .then([cq, child, + request](future>> + f) mutable { + auto context = f.get(); + if (!context) { + return make_ready_future(ReturnType(std::move(context).status())); + } + return child->AsyncGetOperation(cq, *std::move(context), request); + }); +} + +future ApiKeysAuth::AsyncCancelOperation( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::CancelOperationRequest const& request) { + auto& child = child_; + return auth_->AsyncConfigureContext(std::move(context)) + .then([cq, child, + request](future>> + f) mutable { + auto context = f.get(); + if (!context) return make_ready_future(std::move(context).status()); + return child->AsyncCancelOperation(cq, *std::move(context), request); + }); +} + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace apikeys_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/apikeys/internal/api_keys_auth_decorator.h b/google/cloud/apikeys/internal/api_keys_auth_decorator.h new file mode 100644 index 000000000000..328779263597 --- /dev/null +++ b/google/cloud/apikeys/internal/api_keys_auth_decorator.h @@ -0,0 +1,98 @@ +// Copyright 2022 Google LLC +// +// 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 +// +// https://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. + +// Generated by the Codegen C++ plugin. +// If you make any local changes, they will be lost. +// source: google/api/apikeys/v2/apikeys.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_INTERNAL_API_KEYS_AUTH_DECORATOR_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_INTERNAL_API_KEYS_AUTH_DECORATOR_H + +#include "google/cloud/apikeys/internal/api_keys_stub.h" +#include "google/cloud/internal/unified_grpc_credentials.h" +#include "google/cloud/version.h" +#include +#include +#include +#include + +namespace google { +namespace cloud { +namespace apikeys_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +class ApiKeysAuth : public ApiKeysStub { + public: + ~ApiKeysAuth() override = default; + ApiKeysAuth( + std::shared_ptr auth, + std::shared_ptr child); + + future> AsyncCreateKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::CreateKeyRequest const& request) override; + + StatusOr ListKeys( + grpc::ClientContext& context, + google::api::apikeys::v2::ListKeysRequest const& request) override; + + StatusOr GetKey( + grpc::ClientContext& context, + google::api::apikeys::v2::GetKeyRequest const& request) override; + + StatusOr GetKeyString( + grpc::ClientContext& context, + google::api::apikeys::v2::GetKeyStringRequest const& request) override; + + future> AsyncUpdateKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::UpdateKeyRequest const& request) override; + + future> AsyncDeleteKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::DeleteKeyRequest const& request) override; + + future> AsyncUndeleteKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::UndeleteKeyRequest const& request) override; + + StatusOr LookupKey( + grpc::ClientContext& context, + google::api::apikeys::v2::LookupKeyRequest const& request) override; + + future> AsyncGetOperation( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::GetOperationRequest const& request) override; + + future AsyncCancelOperation( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::CancelOperationRequest const& request) override; + + private: + std::shared_ptr auth_; + std::shared_ptr child_; +}; + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace apikeys_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_INTERNAL_API_KEYS_AUTH_DECORATOR_H diff --git a/google/cloud/apikeys/internal/api_keys_connection_impl.cc b/google/cloud/apikeys/internal/api_keys_connection_impl.cc new file mode 100644 index 000000000000..1edbaa766ab2 --- /dev/null +++ b/google/cloud/apikeys/internal/api_keys_connection_impl.cc @@ -0,0 +1,224 @@ +// Copyright 2022 Google LLC +// +// 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 +// +// https://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. + +// Generated by the Codegen C++ plugin. +// If you make any local changes, they will be lost. +// source: google/api/apikeys/v2/apikeys.proto + +#include "google/cloud/apikeys/internal/api_keys_connection_impl.h" +#include "google/cloud/apikeys/internal/api_keys_option_defaults.h" +#include "google/cloud/background_threads.h" +#include "google/cloud/common_options.h" +#include "google/cloud/grpc_options.h" +#include "google/cloud/internal/async_long_running_operation.h" +#include "google/cloud/internal/pagination_range.h" +#include "google/cloud/internal/retry_loop.h" +#include + +namespace google { +namespace cloud { +namespace apikeys_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +ApiKeysConnectionImpl::ApiKeysConnectionImpl( + std::unique_ptr background, + std::shared_ptr stub, Options options) + : background_(std::move(background)), + stub_(std::move(stub)), + options_(internal::MergeOptions(std::move(options), + ApiKeysConnection::options())) {} + +future> +ApiKeysConnectionImpl::CreateKey( + google::api::apikeys::v2::CreateKeyRequest const& request) { + auto& stub = stub_; + return google::cloud::internal::AsyncLongRunningOperation< + google::api::apikeys::v2::Key>( + background_->cq(), request, + [stub](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::CreateKeyRequest const& request) { + return stub->AsyncCreateKey(cq, std::move(context), request); + }, + [stub](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::GetOperationRequest const& request) { + return stub->AsyncGetOperation(cq, std::move(context), request); + }, + [stub](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::CancelOperationRequest const& request) { + return stub->AsyncCancelOperation(cq, std::move(context), request); + }, + &google::cloud::internal::ExtractLongRunningResultResponse< + google::api::apikeys::v2::Key>, + retry_policy(), backoff_policy(), + idempotency_policy()->CreateKey(request), polling_policy(), __func__); +} + +StreamRange ApiKeysConnectionImpl::ListKeys( + google::api::apikeys::v2::ListKeysRequest request) { + request.clear_page_token(); + auto& stub = stub_; + auto retry = + std::shared_ptr(retry_policy()); + auto backoff = std::shared_ptr(backoff_policy()); + auto idempotency = idempotency_policy()->ListKeys(request); + char const* function_name = __func__; + return google::cloud::internal::MakePaginationRange< + StreamRange>( + std::move(request), + [stub, retry, backoff, idempotency, + function_name](google::api::apikeys::v2::ListKeysRequest const& r) { + return google::cloud::internal::RetryLoop( + retry->clone(), backoff->clone(), idempotency, + [stub](grpc::ClientContext& context, + google::api::apikeys::v2::ListKeysRequest const& request) { + return stub->ListKeys(context, request); + }, + r, function_name); + }, + [](google::api::apikeys::v2::ListKeysResponse r) { + std::vector result(r.keys().size()); + auto& messages = *r.mutable_keys(); + std::move(messages.begin(), messages.end(), result.begin()); + return result; + }); +} + +StatusOr ApiKeysConnectionImpl::GetKey( + google::api::apikeys::v2::GetKeyRequest const& request) { + return google::cloud::internal::RetryLoop( + retry_policy(), backoff_policy(), idempotency_policy()->GetKey(request), + [this](grpc::ClientContext& context, + google::api::apikeys::v2::GetKeyRequest const& request) { + return stub_->GetKey(context, request); + }, + request, __func__); +} + +StatusOr +ApiKeysConnectionImpl::GetKeyString( + google::api::apikeys::v2::GetKeyStringRequest const& request) { + return google::cloud::internal::RetryLoop( + retry_policy(), backoff_policy(), + idempotency_policy()->GetKeyString(request), + [this](grpc::ClientContext& context, + google::api::apikeys::v2::GetKeyStringRequest const& request) { + return stub_->GetKeyString(context, request); + }, + request, __func__); +} + +future> +ApiKeysConnectionImpl::UpdateKey( + google::api::apikeys::v2::UpdateKeyRequest const& request) { + auto& stub = stub_; + return google::cloud::internal::AsyncLongRunningOperation< + google::api::apikeys::v2::Key>( + background_->cq(), request, + [stub](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::UpdateKeyRequest const& request) { + return stub->AsyncUpdateKey(cq, std::move(context), request); + }, + [stub](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::GetOperationRequest const& request) { + return stub->AsyncGetOperation(cq, std::move(context), request); + }, + [stub](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::CancelOperationRequest const& request) { + return stub->AsyncCancelOperation(cq, std::move(context), request); + }, + &google::cloud::internal::ExtractLongRunningResultResponse< + google::api::apikeys::v2::Key>, + retry_policy(), backoff_policy(), + idempotency_policy()->UpdateKey(request), polling_policy(), __func__); +} + +future> +ApiKeysConnectionImpl::DeleteKey( + google::api::apikeys::v2::DeleteKeyRequest const& request) { + auto& stub = stub_; + return google::cloud::internal::AsyncLongRunningOperation< + google::api::apikeys::v2::Key>( + background_->cq(), request, + [stub](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::DeleteKeyRequest const& request) { + return stub->AsyncDeleteKey(cq, std::move(context), request); + }, + [stub](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::GetOperationRequest const& request) { + return stub->AsyncGetOperation(cq, std::move(context), request); + }, + [stub](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::CancelOperationRequest const& request) { + return stub->AsyncCancelOperation(cq, std::move(context), request); + }, + &google::cloud::internal::ExtractLongRunningResultResponse< + google::api::apikeys::v2::Key>, + retry_policy(), backoff_policy(), + idempotency_policy()->DeleteKey(request), polling_policy(), __func__); +} + +future> +ApiKeysConnectionImpl::UndeleteKey( + google::api::apikeys::v2::UndeleteKeyRequest const& request) { + auto& stub = stub_; + return google::cloud::internal::AsyncLongRunningOperation< + google::api::apikeys::v2::Key>( + background_->cq(), request, + [stub](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::UndeleteKeyRequest const& request) { + return stub->AsyncUndeleteKey(cq, std::move(context), request); + }, + [stub](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::GetOperationRequest const& request) { + return stub->AsyncGetOperation(cq, std::move(context), request); + }, + [stub](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::CancelOperationRequest const& request) { + return stub->AsyncCancelOperation(cq, std::move(context), request); + }, + &google::cloud::internal::ExtractLongRunningResultResponse< + google::api::apikeys::v2::Key>, + retry_policy(), backoff_policy(), + idempotency_policy()->UndeleteKey(request), polling_policy(), __func__); +} + +StatusOr +ApiKeysConnectionImpl::LookupKey( + google::api::apikeys::v2::LookupKeyRequest const& request) { + return google::cloud::internal::RetryLoop( + retry_policy(), backoff_policy(), + idempotency_policy()->LookupKey(request), + [this](grpc::ClientContext& context, + google::api::apikeys::v2::LookupKeyRequest const& request) { + return stub_->LookupKey(context, request); + }, + request, __func__); +} + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace apikeys_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/apikeys/internal/api_keys_connection_impl.h b/google/cloud/apikeys/internal/api_keys_connection_impl.h new file mode 100644 index 000000000000..86eee1fdcfa1 --- /dev/null +++ b/google/cloud/apikeys/internal/api_keys_connection_impl.h @@ -0,0 +1,123 @@ +// Copyright 2022 Google LLC +// +// 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 +// +// https://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. + +// Generated by the Codegen C++ plugin. +// If you make any local changes, they will be lost. +// source: google/api/apikeys/v2/apikeys.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_INTERNAL_API_KEYS_CONNECTION_IMPL_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_INTERNAL_API_KEYS_CONNECTION_IMPL_H + +#include "google/cloud/apikeys/api_keys_connection.h" +#include "google/cloud/apikeys/api_keys_connection_idempotency_policy.h" +#include "google/cloud/apikeys/api_keys_options.h" +#include "google/cloud/apikeys/internal/api_keys_retry_traits.h" +#include "google/cloud/apikeys/internal/api_keys_stub.h" +#include "google/cloud/background_threads.h" +#include "google/cloud/backoff_policy.h" +#include "google/cloud/future.h" +#include "google/cloud/options.h" +#include "google/cloud/polling_policy.h" +#include "google/cloud/status_or.h" +#include "google/cloud/stream_range.h" +#include "google/cloud/version.h" +#include +#include + +namespace google { +namespace cloud { +namespace apikeys_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +class ApiKeysConnectionImpl : public apikeys::ApiKeysConnection { + public: + ~ApiKeysConnectionImpl() override = default; + + ApiKeysConnectionImpl( + std::unique_ptr background, + std::shared_ptr stub, Options options); + + Options options() override { return options_; } + + future> CreateKey( + google::api::apikeys::v2::CreateKeyRequest const& request) override; + + StreamRange ListKeys( + google::api::apikeys::v2::ListKeysRequest request) override; + + StatusOr GetKey( + google::api::apikeys::v2::GetKeyRequest const& request) override; + + StatusOr GetKeyString( + google::api::apikeys::v2::GetKeyStringRequest const& request) override; + + future> UpdateKey( + google::api::apikeys::v2::UpdateKeyRequest const& request) override; + + future> DeleteKey( + google::api::apikeys::v2::DeleteKeyRequest const& request) override; + + future> UndeleteKey( + google::api::apikeys::v2::UndeleteKeyRequest const& request) override; + + StatusOr LookupKey( + google::api::apikeys::v2::LookupKeyRequest const& request) override; + + private: + std::unique_ptr retry_policy() { + auto const& options = internal::CurrentOptions(); + if (options.has()) { + return options.get()->clone(); + } + return options_.get()->clone(); + } + + std::unique_ptr backoff_policy() { + auto const& options = internal::CurrentOptions(); + if (options.has()) { + return options.get()->clone(); + } + return options_.get()->clone(); + } + + std::unique_ptr + idempotency_policy() { + auto const& options = internal::CurrentOptions(); + if (options.has()) { + return options.get() + ->clone(); + } + return options_.get() + ->clone(); + } + + std::unique_ptr polling_policy() { + auto const& options = internal::CurrentOptions(); + if (options.has()) { + return options.get()->clone(); + } + return options_.get()->clone(); + } + + std::unique_ptr background_; + std::shared_ptr stub_; + Options options_; +}; + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace apikeys_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_INTERNAL_API_KEYS_CONNECTION_IMPL_H diff --git a/google/cloud/apikeys/internal/api_keys_logging_decorator.cc b/google/cloud/apikeys/internal/api_keys_logging_decorator.cc new file mode 100644 index 000000000000..70427df6f2e8 --- /dev/null +++ b/google/cloud/apikeys/internal/api_keys_logging_decorator.cc @@ -0,0 +1,165 @@ +// Copyright 2022 Google LLC +// +// 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 +// +// https://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. + +// Generated by the Codegen C++ plugin. +// If you make any local changes, they will be lost. +// source: google/api/apikeys/v2/apikeys.proto + +#include "google/cloud/apikeys/internal/api_keys_logging_decorator.h" +#include "google/cloud/internal/log_wrapper.h" +#include "google/cloud/status_or.h" +#include +#include + +namespace google { +namespace cloud { +namespace apikeys_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +ApiKeysLogging::ApiKeysLogging(std::shared_ptr child, + TracingOptions tracing_options, + std::set components) + : child_(std::move(child)), + tracing_options_(std::move(tracing_options)), + components_(std::move(components)) {} + +future> ApiKeysLogging::AsyncCreateKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::CreateKeyRequest const& request) { + return google::cloud::internal::LogWrapper( + [this](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::CreateKeyRequest const& request) { + return child_->AsyncCreateKey(cq, std::move(context), request); + }, + cq, std::move(context), request, __func__, tracing_options_); +} + +StatusOr ApiKeysLogging::ListKeys( + grpc::ClientContext& context, + google::api::apikeys::v2::ListKeysRequest const& request) { + return google::cloud::internal::LogWrapper( + [this](grpc::ClientContext& context, + google::api::apikeys::v2::ListKeysRequest const& request) { + return child_->ListKeys(context, request); + }, + context, request, __func__, tracing_options_); +} + +StatusOr ApiKeysLogging::GetKey( + grpc::ClientContext& context, + google::api::apikeys::v2::GetKeyRequest const& request) { + return google::cloud::internal::LogWrapper( + [this](grpc::ClientContext& context, + google::api::apikeys::v2::GetKeyRequest const& request) { + return child_->GetKey(context, request); + }, + context, request, __func__, tracing_options_); +} + +StatusOr +ApiKeysLogging::GetKeyString( + grpc::ClientContext& context, + google::api::apikeys::v2::GetKeyStringRequest const& request) { + return google::cloud::internal::LogWrapper( + [this](grpc::ClientContext& context, + google::api::apikeys::v2::GetKeyStringRequest const& request) { + return child_->GetKeyString(context, request); + }, + context, request, __func__, tracing_options_); +} + +future> ApiKeysLogging::AsyncUpdateKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::UpdateKeyRequest const& request) { + return google::cloud::internal::LogWrapper( + [this](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::UpdateKeyRequest const& request) { + return child_->AsyncUpdateKey(cq, std::move(context), request); + }, + cq, std::move(context), request, __func__, tracing_options_); +} + +future> ApiKeysLogging::AsyncDeleteKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::DeleteKeyRequest const& request) { + return google::cloud::internal::LogWrapper( + [this](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::DeleteKeyRequest const& request) { + return child_->AsyncDeleteKey(cq, std::move(context), request); + }, + cq, std::move(context), request, __func__, tracing_options_); +} + +future> +ApiKeysLogging::AsyncUndeleteKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::UndeleteKeyRequest const& request) { + return google::cloud::internal::LogWrapper( + [this](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::UndeleteKeyRequest const& request) { + return child_->AsyncUndeleteKey(cq, std::move(context), request); + }, + cq, std::move(context), request, __func__, tracing_options_); +} + +StatusOr ApiKeysLogging::LookupKey( + grpc::ClientContext& context, + google::api::apikeys::v2::LookupKeyRequest const& request) { + return google::cloud::internal::LogWrapper( + [this](grpc::ClientContext& context, + google::api::apikeys::v2::LookupKeyRequest const& request) { + return child_->LookupKey(context, request); + }, + context, request, __func__, tracing_options_); +} + +future> +ApiKeysLogging::AsyncGetOperation( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::GetOperationRequest const& request) { + return google::cloud::internal::LogWrapper( + [this](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::GetOperationRequest const& request) { + return child_->AsyncGetOperation(cq, std::move(context), request); + }, + cq, std::move(context), request, __func__, tracing_options_); +} + +future ApiKeysLogging::AsyncCancelOperation( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::CancelOperationRequest const& request) { + return google::cloud::internal::LogWrapper( + [this](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::CancelOperationRequest const& request) { + return child_->AsyncCancelOperation(cq, std::move(context), request); + }, + cq, std::move(context), request, __func__, tracing_options_); +} + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace apikeys_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/apikeys/internal/api_keys_logging_decorator.h b/google/cloud/apikeys/internal/api_keys_logging_decorator.h new file mode 100644 index 000000000000..4cca96d7be79 --- /dev/null +++ b/google/cloud/apikeys/internal/api_keys_logging_decorator.h @@ -0,0 +1,99 @@ +// Copyright 2022 Google LLC +// +// 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 +// +// https://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. + +// Generated by the Codegen C++ plugin. +// If you make any local changes, they will be lost. +// source: google/api/apikeys/v2/apikeys.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_INTERNAL_API_KEYS_LOGGING_DECORATOR_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_INTERNAL_API_KEYS_LOGGING_DECORATOR_H + +#include "google/cloud/apikeys/internal/api_keys_stub.h" +#include "google/cloud/tracing_options.h" +#include "google/cloud/version.h" +#include +#include +#include +#include + +namespace google { +namespace cloud { +namespace apikeys_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +class ApiKeysLogging : public ApiKeysStub { + public: + ~ApiKeysLogging() override = default; + ApiKeysLogging(std::shared_ptr child, + TracingOptions tracing_options, + std::set components); + + future> AsyncCreateKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::CreateKeyRequest const& request) override; + + StatusOr ListKeys( + grpc::ClientContext& context, + google::api::apikeys::v2::ListKeysRequest const& request) override; + + StatusOr GetKey( + grpc::ClientContext& context, + google::api::apikeys::v2::GetKeyRequest const& request) override; + + StatusOr GetKeyString( + grpc::ClientContext& context, + google::api::apikeys::v2::GetKeyStringRequest const& request) override; + + future> AsyncUpdateKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::UpdateKeyRequest const& request) override; + + future> AsyncDeleteKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::DeleteKeyRequest const& request) override; + + future> AsyncUndeleteKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::UndeleteKeyRequest const& request) override; + + StatusOr LookupKey( + grpc::ClientContext& context, + google::api::apikeys::v2::LookupKeyRequest const& request) override; + + future> AsyncGetOperation( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::GetOperationRequest const& request) override; + + future AsyncCancelOperation( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::CancelOperationRequest const& request) override; + + private: + std::shared_ptr child_; + TracingOptions tracing_options_; + std::set components_; +}; // ApiKeysLogging + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace apikeys_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_INTERNAL_API_KEYS_LOGGING_DECORATOR_H diff --git a/google/cloud/apikeys/internal/api_keys_metadata_decorator.cc b/google/cloud/apikeys/internal/api_keys_metadata_decorator.cc new file mode 100644 index 000000000000..a0b76b438e3f --- /dev/null +++ b/google/cloud/apikeys/internal/api_keys_metadata_decorator.cc @@ -0,0 +1,139 @@ +// Copyright 2022 Google LLC +// +// 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 +// +// https://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. + +// Generated by the Codegen C++ plugin. +// If you make any local changes, they will be lost. +// source: google/api/apikeys/v2/apikeys.proto + +#include "google/cloud/apikeys/internal/api_keys_metadata_decorator.h" +#include "google/cloud/common_options.h" +#include "google/cloud/internal/api_client_header.h" +#include "google/cloud/status_or.h" +#include +#include + +namespace google { +namespace cloud { +namespace apikeys_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +ApiKeysMetadata::ApiKeysMetadata(std::shared_ptr child) + : child_(std::move(child)), + api_client_header_( + google::cloud::internal::ApiClientHeader("generator")) {} + +future> +ApiKeysMetadata::AsyncCreateKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::CreateKeyRequest const& request) { + SetMetadata(*context, "parent=" + request.parent()); + return child_->AsyncCreateKey(cq, std::move(context), request); +} + +StatusOr ApiKeysMetadata::ListKeys( + grpc::ClientContext& context, + google::api::apikeys::v2::ListKeysRequest const& request) { + SetMetadata(context, "parent=" + request.parent()); + return child_->ListKeys(context, request); +} + +StatusOr ApiKeysMetadata::GetKey( + grpc::ClientContext& context, + google::api::apikeys::v2::GetKeyRequest const& request) { + SetMetadata(context, "name=" + request.name()); + return child_->GetKey(context, request); +} + +StatusOr +ApiKeysMetadata::GetKeyString( + grpc::ClientContext& context, + google::api::apikeys::v2::GetKeyStringRequest const& request) { + SetMetadata(context, "name=" + request.name()); + return child_->GetKeyString(context, request); +} + +future> +ApiKeysMetadata::AsyncUpdateKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::UpdateKeyRequest const& request) { + SetMetadata(*context, "key.name=" + request.key().name()); + return child_->AsyncUpdateKey(cq, std::move(context), request); +} + +future> +ApiKeysMetadata::AsyncDeleteKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::DeleteKeyRequest const& request) { + SetMetadata(*context, "name=" + request.name()); + return child_->AsyncDeleteKey(cq, std::move(context), request); +} + +future> +ApiKeysMetadata::AsyncUndeleteKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::UndeleteKeyRequest const& request) { + SetMetadata(*context, "name=" + request.name()); + return child_->AsyncUndeleteKey(cq, std::move(context), request); +} + +StatusOr +ApiKeysMetadata::LookupKey( + grpc::ClientContext& context, + google::api::apikeys::v2::LookupKeyRequest const& request) { + SetMetadata(context); + return child_->LookupKey(context, request); +} + +future> +ApiKeysMetadata::AsyncGetOperation( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::GetOperationRequest const& request) { + SetMetadata(*context, "name=" + request.name()); + return child_->AsyncGetOperation(cq, std::move(context), request); +} + +future ApiKeysMetadata::AsyncCancelOperation( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::CancelOperationRequest const& request) { + SetMetadata(*context, "name=" + request.name()); + return child_->AsyncCancelOperation(cq, std::move(context), request); +} + +void ApiKeysMetadata::SetMetadata(grpc::ClientContext& context, + std::string const& request_params) { + context.AddMetadata("x-goog-request-params", request_params); + SetMetadata(context); +} + +void ApiKeysMetadata::SetMetadata(grpc::ClientContext& context) { + context.AddMetadata("x-goog-api-client", api_client_header_); + auto const& options = internal::CurrentOptions(); + if (options.has()) { + context.AddMetadata("x-goog-user-project", + options.get()); + } + auto const& authority = options.get(); + if (!authority.empty()) context.set_authority(authority); +} + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace apikeys_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/apikeys/internal/api_keys_metadata_decorator.h b/google/cloud/apikeys/internal/api_keys_metadata_decorator.h new file mode 100644 index 000000000000..e4288b256dcc --- /dev/null +++ b/google/cloud/apikeys/internal/api_keys_metadata_decorator.h @@ -0,0 +1,98 @@ +// Copyright 2022 Google LLC +// +// 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 +// +// https://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. + +// Generated by the Codegen C++ plugin. +// If you make any local changes, they will be lost. +// source: google/api/apikeys/v2/apikeys.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_INTERNAL_API_KEYS_METADATA_DECORATOR_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_INTERNAL_API_KEYS_METADATA_DECORATOR_H + +#include "google/cloud/apikeys/internal/api_keys_stub.h" +#include "google/cloud/version.h" +#include +#include +#include + +namespace google { +namespace cloud { +namespace apikeys_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +class ApiKeysMetadata : public ApiKeysStub { + public: + ~ApiKeysMetadata() override = default; + explicit ApiKeysMetadata(std::shared_ptr child); + + future> AsyncCreateKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::CreateKeyRequest const& request) override; + + StatusOr ListKeys( + grpc::ClientContext& context, + google::api::apikeys::v2::ListKeysRequest const& request) override; + + StatusOr GetKey( + grpc::ClientContext& context, + google::api::apikeys::v2::GetKeyRequest const& request) override; + + StatusOr GetKeyString( + grpc::ClientContext& context, + google::api::apikeys::v2::GetKeyStringRequest const& request) override; + + future> AsyncUpdateKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::UpdateKeyRequest const& request) override; + + future> AsyncDeleteKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::DeleteKeyRequest const& request) override; + + future> AsyncUndeleteKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::UndeleteKeyRequest const& request) override; + + StatusOr LookupKey( + grpc::ClientContext& context, + google::api::apikeys::v2::LookupKeyRequest const& request) override; + + future> AsyncGetOperation( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::GetOperationRequest const& request) override; + + future AsyncCancelOperation( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::CancelOperationRequest const& request) override; + + private: + void SetMetadata(grpc::ClientContext& context, + std::string const& request_params); + void SetMetadata(grpc::ClientContext& context); + + std::shared_ptr child_; + std::string api_client_header_; +}; + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace apikeys_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_INTERNAL_API_KEYS_METADATA_DECORATOR_H diff --git a/google/cloud/apikeys/internal/api_keys_option_defaults.cc b/google/cloud/apikeys/internal/api_keys_option_defaults.cc new file mode 100644 index 000000000000..b8254232ff78 --- /dev/null +++ b/google/cloud/apikeys/internal/api_keys_option_defaults.cc @@ -0,0 +1,71 @@ +// Copyright 2022 Google LLC +// +// 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 +// +// https://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. + +// Generated by the Codegen C++ plugin. +// If you make any local changes, they will be lost. +// source: google/api/apikeys/v2/apikeys.proto + +#include "google/cloud/apikeys/internal/api_keys_option_defaults.h" +#include "google/cloud/apikeys/api_keys_connection.h" +#include "google/cloud/apikeys/api_keys_options.h" +#include "google/cloud/internal/populate_common_options.h" +#include "google/cloud/internal/populate_grpc_options.h" +#include + +namespace google { +namespace cloud { +namespace apikeys_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +namespace { +auto constexpr kBackoffScaling = 2.0; +} // namespace + +Options ApiKeysDefaultOptions(Options options) { + options = google::cloud::internal::PopulateCommonOptions( + std::move(options), "GOOGLE_CLOUD_CPP_API_KEYS_ENDPOINT", "", + "GOOGLE_CLOUD_CPP_API_KEYS_AUTHORITY", "apikeys.googleapis.com"); + options = + google::cloud::internal::PopulateGrpcOptions(std::move(options), ""); + if (!options.has()) { + options.set( + apikeys::ApiKeysLimitedTimeRetryPolicy(std::chrono::minutes(30)) + .clone()); + } + if (!options.has()) { + options.set( + ExponentialBackoffPolicy(std::chrono::seconds(1), + std::chrono::minutes(5), kBackoffScaling) + .clone()); + } + if (!options.has()) { + options.set( + GenericPollingPolicy( + options.get()->clone(), + options.get()->clone()) + .clone()); + } + if (!options.has()) { + options.set( + apikeys::MakeDefaultApiKeysConnectionIdempotencyPolicy()); + } + + return options; +} + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace apikeys_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/apikeys/internal/api_keys_option_defaults.h b/google/cloud/apikeys/internal/api_keys_option_defaults.h new file mode 100644 index 000000000000..b7e04f71edfd --- /dev/null +++ b/google/cloud/apikeys/internal/api_keys_option_defaults.h @@ -0,0 +1,37 @@ +// Copyright 2022 Google LLC +// +// 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 +// +// https://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. + +// Generated by the Codegen C++ plugin. +// If you make any local changes, they will be lost. +// source: google/api/apikeys/v2/apikeys.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_INTERNAL_API_KEYS_OPTION_DEFAULTS_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_INTERNAL_API_KEYS_OPTION_DEFAULTS_H + +#include "google/cloud/options.h" +#include "google/cloud/version.h" + +namespace google { +namespace cloud { +namespace apikeys_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +Options ApiKeysDefaultOptions(Options options); + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace apikeys_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_INTERNAL_API_KEYS_OPTION_DEFAULTS_H diff --git a/google/cloud/apikeys/internal/api_keys_retry_traits.h b/google/cloud/apikeys/internal/api_keys_retry_traits.h new file mode 100644 index 000000000000..20f380be3255 --- /dev/null +++ b/google/cloud/apikeys/internal/api_keys_retry_traits.h @@ -0,0 +1,43 @@ +// Copyright 2022 Google LLC +// +// 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 +// +// https://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. + +// Generated by the Codegen C++ plugin. +// If you make any local changes, they will be lost. +// source: google/api/apikeys/v2/apikeys.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_INTERNAL_API_KEYS_RETRY_TRAITS_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_INTERNAL_API_KEYS_RETRY_TRAITS_H + +#include "google/cloud/status.h" +#include "google/cloud/version.h" + +namespace google { +namespace cloud { +namespace apikeys_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +/// Define the gRPC status code semantics for retrying requests. +struct ApiKeysRetryTraits { + static inline bool IsPermanentFailure(google::cloud::Status const& status) { + return status.code() != StatusCode::kOk && + status.code() != StatusCode::kUnavailable; + } +}; + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace apikeys_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_INTERNAL_API_KEYS_RETRY_TRAITS_H diff --git a/google/cloud/apikeys/internal/api_keys_stub.cc b/google/cloud/apikeys/internal/api_keys_stub.cc new file mode 100644 index 000000000000..4e5fcf69f021 --- /dev/null +++ b/google/cloud/apikeys/internal/api_keys_stub.cc @@ -0,0 +1,170 @@ +// Copyright 2022 Google LLC +// +// 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 +// +// https://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. + +// Generated by the Codegen C++ plugin. +// If you make any local changes, they will be lost. +// source: google/api/apikeys/v2/apikeys.proto + +#include "google/cloud/apikeys/internal/api_keys_stub.h" +#include "google/cloud/grpc_error_delegate.h" +#include "google/cloud/status_or.h" +#include +#include +#include + +namespace google { +namespace cloud { +namespace apikeys_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +ApiKeysStub::~ApiKeysStub() = default; + +future> +DefaultApiKeysStub::AsyncCreateKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::CreateKeyRequest const& request) { + return cq.MakeUnaryRpc( + [this](grpc::ClientContext* context, + google::api::apikeys::v2::CreateKeyRequest const& request, + grpc::CompletionQueue* cq) { + return grpc_stub_->AsyncCreateKey(context, request, cq); + }, + request, std::move(context)); +} + +StatusOr +DefaultApiKeysStub::ListKeys( + grpc::ClientContext& client_context, + google::api::apikeys::v2::ListKeysRequest const& request) { + google::api::apikeys::v2::ListKeysResponse response; + auto status = grpc_stub_->ListKeys(&client_context, request, &response); + if (!status.ok()) { + return google::cloud::MakeStatusFromRpcError(status); + } + return response; +} + +StatusOr DefaultApiKeysStub::GetKey( + grpc::ClientContext& client_context, + google::api::apikeys::v2::GetKeyRequest const& request) { + google::api::apikeys::v2::Key response; + auto status = grpc_stub_->GetKey(&client_context, request, &response); + if (!status.ok()) { + return google::cloud::MakeStatusFromRpcError(status); + } + return response; +} + +StatusOr +DefaultApiKeysStub::GetKeyString( + grpc::ClientContext& client_context, + google::api::apikeys::v2::GetKeyStringRequest const& request) { + google::api::apikeys::v2::GetKeyStringResponse response; + auto status = grpc_stub_->GetKeyString(&client_context, request, &response); + if (!status.ok()) { + return google::cloud::MakeStatusFromRpcError(status); + } + return response; +} + +future> +DefaultApiKeysStub::AsyncUpdateKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::UpdateKeyRequest const& request) { + return cq.MakeUnaryRpc( + [this](grpc::ClientContext* context, + google::api::apikeys::v2::UpdateKeyRequest const& request, + grpc::CompletionQueue* cq) { + return grpc_stub_->AsyncUpdateKey(context, request, cq); + }, + request, std::move(context)); +} + +future> +DefaultApiKeysStub::AsyncDeleteKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::DeleteKeyRequest const& request) { + return cq.MakeUnaryRpc( + [this](grpc::ClientContext* context, + google::api::apikeys::v2::DeleteKeyRequest const& request, + grpc::CompletionQueue* cq) { + return grpc_stub_->AsyncDeleteKey(context, request, cq); + }, + request, std::move(context)); +} + +future> +DefaultApiKeysStub::AsyncUndeleteKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::UndeleteKeyRequest const& request) { + return cq.MakeUnaryRpc( + [this](grpc::ClientContext* context, + google::api::apikeys::v2::UndeleteKeyRequest const& request, + grpc::CompletionQueue* cq) { + return grpc_stub_->AsyncUndeleteKey(context, request, cq); + }, + request, std::move(context)); +} + +StatusOr +DefaultApiKeysStub::LookupKey( + grpc::ClientContext& client_context, + google::api::apikeys::v2::LookupKeyRequest const& request) { + google::api::apikeys::v2::LookupKeyResponse response; + auto status = grpc_stub_->LookupKey(&client_context, request, &response); + if (!status.ok()) { + return google::cloud::MakeStatusFromRpcError(status); + } + return response; +} + +future> +DefaultApiKeysStub::AsyncGetOperation( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::GetOperationRequest const& request) { + return cq.MakeUnaryRpc( + [this](grpc::ClientContext* context, + google::longrunning::GetOperationRequest const& request, + grpc::CompletionQueue* cq) { + return operations_->AsyncGetOperation(context, request, cq); + }, + request, std::move(context)); +} + +future DefaultApiKeysStub::AsyncCancelOperation( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::CancelOperationRequest const& request) { + return cq + .MakeUnaryRpc( + [this](grpc::ClientContext* context, + google::longrunning::CancelOperationRequest const& request, + grpc::CompletionQueue* cq) { + return operations_->AsyncCancelOperation(context, request, cq); + }, + request, std::move(context)) + .then([](future> f) { + return f.get().status(); + }); +} + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace apikeys_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/apikeys/internal/api_keys_stub.h b/google/cloud/apikeys/internal/api_keys_stub.h new file mode 100644 index 000000000000..1cba8165920f --- /dev/null +++ b/google/cloud/apikeys/internal/api_keys_stub.h @@ -0,0 +1,151 @@ +// Copyright 2022 Google LLC +// +// 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 +// +// https://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. + +// Generated by the Codegen C++ plugin. +// If you make any local changes, they will be lost. +// source: google/api/apikeys/v2/apikeys.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_INTERNAL_API_KEYS_STUB_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_INTERNAL_API_KEYS_STUB_H + +#include "google/cloud/completion_queue.h" +#include "google/cloud/future.h" +#include "google/cloud/status_or.h" +#include "google/cloud/version.h" +#include +#include +#include + +namespace google { +namespace cloud { +namespace apikeys_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +class ApiKeysStub { + public: + virtual ~ApiKeysStub() = 0; + + virtual future> AsyncCreateKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::CreateKeyRequest const& request) = 0; + + virtual StatusOr ListKeys( + grpc::ClientContext& context, + google::api::apikeys::v2::ListKeysRequest const& request) = 0; + + virtual StatusOr GetKey( + grpc::ClientContext& context, + google::api::apikeys::v2::GetKeyRequest const& request) = 0; + + virtual StatusOr GetKeyString( + grpc::ClientContext& context, + google::api::apikeys::v2::GetKeyStringRequest const& request) = 0; + + virtual future> AsyncUpdateKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::UpdateKeyRequest const& request) = 0; + + virtual future> AsyncDeleteKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::DeleteKeyRequest const& request) = 0; + + virtual future> AsyncUndeleteKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::UndeleteKeyRequest const& request) = 0; + + virtual StatusOr LookupKey( + grpc::ClientContext& context, + google::api::apikeys::v2::LookupKeyRequest const& request) = 0; + + virtual future> AsyncGetOperation( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::GetOperationRequest const& request) = 0; + + virtual future AsyncCancelOperation( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::CancelOperationRequest const& request) = 0; +}; + +class DefaultApiKeysStub : public ApiKeysStub { + public: + DefaultApiKeysStub( + std::unique_ptr + grpc_stub, + std::unique_ptr + operations) + : grpc_stub_(std::move(grpc_stub)), operations_(std::move(operations)) {} + + future> AsyncCreateKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::CreateKeyRequest const& request) override; + + StatusOr ListKeys( + grpc::ClientContext& client_context, + google::api::apikeys::v2::ListKeysRequest const& request) override; + + StatusOr GetKey( + grpc::ClientContext& client_context, + google::api::apikeys::v2::GetKeyRequest const& request) override; + + StatusOr GetKeyString( + grpc::ClientContext& client_context, + google::api::apikeys::v2::GetKeyStringRequest const& request) override; + + future> AsyncUpdateKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::UpdateKeyRequest const& request) override; + + future> AsyncDeleteKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::DeleteKeyRequest const& request) override; + + future> AsyncUndeleteKey( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::api::apikeys::v2::UndeleteKeyRequest const& request) override; + + StatusOr LookupKey( + grpc::ClientContext& client_context, + google::api::apikeys::v2::LookupKeyRequest const& request) override; + + future> AsyncGetOperation( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::GetOperationRequest const& request) override; + + future AsyncCancelOperation( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::longrunning::CancelOperationRequest const& request) override; + + private: + std::unique_ptr grpc_stub_; + std::unique_ptr operations_; +}; + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace apikeys_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_INTERNAL_API_KEYS_STUB_H diff --git a/google/cloud/apikeys/internal/api_keys_stub_factory.cc b/google/cloud/apikeys/internal/api_keys_stub_factory.cc new file mode 100644 index 000000000000..da843abc87ff --- /dev/null +++ b/google/cloud/apikeys/internal/api_keys_stub_factory.cc @@ -0,0 +1,64 @@ +// Copyright 2022 Google LLC +// +// 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 +// +// https://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. + +// Generated by the Codegen C++ plugin. +// If you make any local changes, they will be lost. +// source: google/api/apikeys/v2/apikeys.proto + +#include "google/cloud/apikeys/internal/api_keys_stub_factory.h" +#include "google/cloud/apikeys/internal/api_keys_auth_decorator.h" +#include "google/cloud/apikeys/internal/api_keys_logging_decorator.h" +#include "google/cloud/apikeys/internal/api_keys_metadata_decorator.h" +#include "google/cloud/apikeys/internal/api_keys_stub.h" +#include "google/cloud/common_options.h" +#include "google/cloud/grpc_options.h" +#include "google/cloud/internal/algorithm.h" +#include "google/cloud/log.h" +#include "google/cloud/options.h" +#include +#include + +namespace google { +namespace cloud { +namespace apikeys_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +std::shared_ptr CreateDefaultApiKeysStub( + google::cloud::CompletionQueue cq, Options const& options) { + auto auth = google::cloud::internal::CreateAuthenticationStrategy( + std::move(cq), options); + auto channel = auth->CreateChannel(options.get(), + internal::MakeChannelArguments(options)); + auto service_grpc_stub = google::api::apikeys::v2::ApiKeys::NewStub(channel); + std::shared_ptr stub = std::make_shared( + std::move(service_grpc_stub), + google::longrunning::Operations::NewStub(channel)); + + if (auth->RequiresConfigureContext()) { + stub = std::make_shared(std::move(auth), std::move(stub)); + } + stub = std::make_shared(std::move(stub)); + if (internal::Contains(options.get(), "rpc")) { + GCP_LOG(INFO) << "Enabled logging for gRPC calls"; + stub = std::make_shared( + std::move(stub), options.get(), + options.get()); + } + return stub; +} + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace apikeys_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/apikeys/internal/api_keys_stub_factory.h b/google/cloud/apikeys/internal/api_keys_stub_factory.h new file mode 100644 index 000000000000..4bcb854e0fc5 --- /dev/null +++ b/google/cloud/apikeys/internal/api_keys_stub_factory.h @@ -0,0 +1,42 @@ +// Copyright 2022 Google LLC +// +// 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 +// +// https://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. + +// Generated by the Codegen C++ plugin. +// If you make any local changes, they will be lost. +// source: google/api/apikeys/v2/apikeys.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_INTERNAL_API_KEYS_STUB_FACTORY_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_INTERNAL_API_KEYS_STUB_FACTORY_H + +#include "google/cloud/apikeys/internal/api_keys_stub.h" +#include "google/cloud/completion_queue.h" +#include "google/cloud/credentials.h" +#include "google/cloud/internal/unified_grpc_credentials.h" +#include "google/cloud/version.h" +#include + +namespace google { +namespace cloud { +namespace apikeys_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +std::shared_ptr CreateDefaultApiKeysStub( + google::cloud::CompletionQueue cq, Options const& options); + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace apikeys_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_INTERNAL_API_KEYS_STUB_FACTORY_H diff --git a/google/cloud/apikeys/mocks/mock_api_keys_connection.h b/google/cloud/apikeys/mocks/mock_api_keys_connection.h new file mode 100644 index 000000000000..e5e8b31947dd --- /dev/null +++ b/google/cloud/apikeys/mocks/mock_api_keys_connection.h @@ -0,0 +1,87 @@ +// Copyright 2022 Google LLC +// +// 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 +// +// https://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. + +// Generated by the Codegen C++ plugin. +// If you make any local changes, they will be lost. +// source: google/api/apikeys/v2/apikeys.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_MOCKS_MOCK_API_KEYS_CONNECTION_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_MOCKS_MOCK_API_KEYS_CONNECTION_H + +#include "google/cloud/apikeys/api_keys_connection.h" +#include + +namespace google { +namespace cloud { +namespace apikeys_mocks { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +/** + * A class to mock `ApiKeysConnection`. + * + * Application developers may want to test their code with simulated responses, + * including errors, from an object of type `ApiKeysClient`. To do so, + * construct an object of type `ApiKeysClient` with an instance of this + * class. Then use the Google Test framework functions to program the behavior + * of this mock. + * + * @see [This example][bq-mock] for how to test your application with + * GoogleTest. While the example showcases types from the BigQuery library, the + * underlying principles apply for any pair of `*Client` and `*Connection`. + * + * [bq-mock]: @googleapis_dev_link{bigquery,bigquery-read-mock.html} + */ +class MockApiKeysConnection : public apikeys::ApiKeysConnection { + public: + MOCK_METHOD(Options, options, (), (override)); + + MOCK_METHOD(future>, CreateKey, + (google::api::apikeys::v2::CreateKeyRequest const& request), + (override)); + + MOCK_METHOD(StreamRange, ListKeys, + (google::api::apikeys::v2::ListKeysRequest request), (override)); + + MOCK_METHOD(StatusOr, GetKey, + (google::api::apikeys::v2::GetKeyRequest const& request), + (override)); + + MOCK_METHOD(StatusOr, + GetKeyString, + (google::api::apikeys::v2::GetKeyStringRequest const& request), + (override)); + + MOCK_METHOD(future>, UpdateKey, + (google::api::apikeys::v2::UpdateKeyRequest const& request), + (override)); + + MOCK_METHOD(future>, DeleteKey, + (google::api::apikeys::v2::DeleteKeyRequest const& request), + (override)); + + MOCK_METHOD(future>, UndeleteKey, + (google::api::apikeys::v2::UndeleteKeyRequest const& request), + (override)); + + MOCK_METHOD(StatusOr, LookupKey, + (google::api::apikeys::v2::LookupKeyRequest const& request), + (override)); +}; + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace apikeys_mocks +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_APIKEYS_MOCKS_MOCK_API_KEYS_CONNECTION_H diff --git a/google/cloud/apikeys/quickstart/.bazelrc b/google/cloud/apikeys/quickstart/.bazelrc new file mode 100644 index 000000000000..77d763d38194 --- /dev/null +++ b/google/cloud/apikeys/quickstart/.bazelrc @@ -0,0 +1,31 @@ +# Copyright 2022 Google LLC +# +# 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 +# +# https://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. + +# To workaround a bug in Bazel [1], gRPC requires this flag on macOS builds, +# and there is (AFAIK) no way to "inherit" their definitions. +# [1]: https://github.com/bazelbuild/bazel/issues/4341 +build --copt=-DGRPC_BAZEL_BUILD + +# Use host-OS-specific config lines from bazelrc files. +build --enable_platform_specific_config=true + +# The project requires C++ >= 14. By default Bazel adds `-std=c++0x` which +# disables C++14 features, even if the compilers defaults to C++ >= 14 +build:linux --cxxopt=-std=c++14 +build:macos --cxxopt=-std=c++14 + +# Do not create the convenience links. They are inconvenient when the build +# runs inside a docker image or if one builds a quickstart and then builds +# the project separately. +build --experimental_convenience_symlinks=ignore diff --git a/google/cloud/apikeys/quickstart/BUILD.bazel b/google/cloud/apikeys/quickstart/BUILD.bazel new file mode 100644 index 000000000000..7319866090cc --- /dev/null +++ b/google/cloud/apikeys/quickstart/BUILD.bazel @@ -0,0 +1,25 @@ +# Copyright 2022 Google LLC +# +# 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 +# +# https://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. + +licenses(["notice"]) # Apache 2.0 + +cc_binary( + name = "quickstart", + srcs = [ + "quickstart.cc", + ], + deps = [ + "@com_github_googleapis_google_cloud_cpp//:experimental-apikeys", + ], +) diff --git a/google/cloud/apikeys/quickstart/CMakeLists.txt b/google/cloud/apikeys/quickstart/CMakeLists.txt new file mode 100644 index 000000000000..b0b5c6839093 --- /dev/null +++ b/google/cloud/apikeys/quickstart/CMakeLists.txt @@ -0,0 +1,32 @@ +# Copyright 2022 Google LLC +# +# 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 +# +# https://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. + +# This file shows how to use the API Keys API C++ client library from a larger +# CMake project. + +cmake_minimum_required(VERSION 3.10) +project(google-cloud-cpp-apikeys-quickstart CXX) + +find_package(google_cloud_cpp_apikeys REQUIRED) + +# MSVC requires some additional code to select the correct runtime library +if (VCPKG_TARGET_TRIPLET MATCHES "-static$") + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") +else () + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>DLL") +endif () + +# Define your targets. +add_executable(quickstart quickstart.cc) +target_link_libraries(quickstart google-cloud-cpp::experimental-apikeys) diff --git a/google/cloud/apikeys/quickstart/Makefile b/google/cloud/apikeys/quickstart/Makefile new file mode 100644 index 000000000000..94f9b82bca19 --- /dev/null +++ b/google/cloud/apikeys/quickstart/Makefile @@ -0,0 +1,35 @@ +# Copyright 2022 Google LLC +# +# 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 +# +# https://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. + +# This is a minimal Makefile to show how to use the API Keys API C++ client +# for developers who use make(1) as their build system. + +# The CXX, CXXFLAGS and CXXLD variables are hard-coded. These values work for +# our tests, but applications would typically make them configurable parameters. +CXX=g++ +CXXFLAGS= +CXXLD=$(CXX) +BIN=. + +all: $(BIN)/quickstart + +# Configuration variables to compile and link against the API Keys API C++ +# client library. +CLIENT_MODULE := google_cloud_cpp_apikeys +CLIENT_CXXFLAGS := $(shell pkg-config $(CLIENT_MODULE) --cflags) +CLIENT_CXXLDFLAGS := $(shell pkg-config $(CLIENT_MODULE) --libs-only-L) +CLIENT_LIBS := $(shell pkg-config $(CLIENT_MODULE) --libs-only-l) + +$(BIN)/quickstart: quickstart.cc + $(CXXLD) $(CXXFLAGS) $(CLIENT_CXXFLAGS) $(CLIENT_CXXLDFLAGS) -o $@ $^ $(CLIENT_LIBS) diff --git a/google/cloud/apikeys/quickstart/README.md b/google/cloud/apikeys/quickstart/README.md new file mode 100644 index 000000000000..4193402a4a89 --- /dev/null +++ b/google/cloud/apikeys/quickstart/README.md @@ -0,0 +1,166 @@ +# HOWTO: using the API Keys API C++ client in your project + +This directory contains small examples showing how to use the API Keys API C++ +client library in your own project. These instructions assume that you have +some experience as a C++ developer and that you have a working C++ toolchain +(compiler, linker, etc.) installed on your platform. + +* Packaging maintainers or developers who prefer to install the library in a + fixed directory (such as `/usr/local` or `/opt`) should consult the + [packaging guide](/doc/packaging.md). +* Developers wanting to use the libraries as part of a larger CMake or Bazel + project should consult the current document. Note that there are similar + documents for each library in their corresponding directories. +* Developers wanting to compile the library just to run some of the examples or + tests should consult the + [building and installing](/README.md#building-and-installing) section of the + top-level README file. +* Contributors and developers to `google-cloud-cpp` should consult the guide to + [setup a development workstation][howto-setup-dev-workstation]. + +[howto-setup-dev-workstation]: /doc/contributor/howto-guide-setup-development-workstation.md + +## Before you begin + +To run the quickstart examples you will need a working Google Cloud Platform +(GCP) project. The [quickstart][quickstart-link] covers the necessary +steps in detail. + +## Configuring authentication for the C++ Client Library + +Like most Google Cloud Platform (GCP) services, API Keys API requires that +your application authenticates with the service before accessing any data. If +you are not familiar with GCP authentication please take this opportunity to +review the [Authentication Overview][authentication-quickstart]. This library +uses the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to find the +credentials file. For example: + +| Shell | Command | +| :----------------- | ---------------------------------------------- | +| Bash/zsh/ksh/etc. | `export GOOGLE_APPLICATION_CREDENTIALS=[PATH]` | +| sh | `GOOGLE_APPLICATION_CREDENTIALS=[PATH];`
`export GOOGLE_APPLICATION_CREDENTIALS` | +| csh/tsch | `setenv GOOGLE_APPLICATION_CREDENTIALS [PATH]` | +| Windows Powershell | `$env:GOOGLE_APPLICATION_CREDENTIALS=[PATH]` | +| Windows cmd.exe | `set GOOGLE_APPLICATION_CREDENTIALS=[PATH]` | + +Setting this environment variable is the recommended way to configure the +authentication preferences, though if the environment variable is not set, the +library searches for a credentials file in the same location as the [Cloud +SDK](https://cloud.google.com/sdk/). For more information about *Application +Default Credentials*, see +https://cloud.google.com/docs/authentication/production + +## Using with Bazel + +> :warning: If you are using Windows or macOS there are additional instructions +> at the end of this document. + +1. Install Bazel using [the instructions][bazel-install] from the `bazel.build` + website. + +2. Compile this example using Bazel: + + ```bash + cd $HOME/google-cloud-cpp/google/cloud/apikeys/quickstart + bazel build ... + ``` + + Note that Bazel automatically downloads and compiles all dependencies of the + project. As it is often the case with C++ libraries, compiling these + dependencies may take several minutes. + +3. Run the example, changing the placeholder(s) to appropriate values: + + ```bash + bazel run :quickstart -- [...] + ``` + +## Using with CMake + +> :warning: If you are using Windows or macOS there are additional instructions +> at the end of this document. + +1. Install CMake. The package managers for most Linux distributions include a + package for CMake. Likewise, you can install CMake on Windows using a package + manager such as [chocolatey][choco-cmake-link], and on macOS using + [homebrew][homebrew-cmake-link]. You can also obtain the software directly + from the [cmake.org](https://cmake.org/download/). + +2. Install the dependencies with your favorite tools. As an example, if you use + [vcpkg](https://github.com/Microsoft/vcpkg.git): + + ```bash + cd $HOME/vcpkg + ./vcpkg install google-cloud-cpp[core,apikeys] + ``` + + Note that, as it is often the case with C++ libraries, compiling these + dependencies may take several minutes. + +3. Configure CMake, if necessary, configure the directory where you installed + the dependencies: + + ```bash + cd $HOME/gooogle-cloud-cpp/google/cloud/apikeys/quickstart + cmake -H. -B.build -DCMAKE_TOOLCHAIN_FILE=$HOME/vcpkg/scripts/buildsystems/vcpkg.cmake + cmake --build .build + ``` + +4. Run the example, changing the placeholder(s) to appropriate values: + + ```bash + .build/quickstart [...] + ``` + +## Platform Specific Notes + +### macOS + +gRPC [requires][grpc-roots-pem-bug] an environment variable to configure the +trust store for SSL certificates, you can download and configure this using: + +```bash +curl -Lo roots.pem https://pki.google.com/roots.pem +export GRPC_DEFAULT_SSL_ROOTS_FILE_PATH="$PWD/roots.pem" +``` + +To workaround a [bug in Bazel][bazel-grpc-macos-bug], gRPC requires this flag on +macOS builds, you can add the option manually or include it in your `.bazelrc` +file: + +```bash +bazel build --copt=-DGRPC_BAZEL_BUILD ... +``` + +### Windows + +To correctly configure the MSVC runtime you should change the CMake minimum +required version to 3.15 or add `-DCMAKE_POLICY_DEFAULT_CMP0091=NEW` to the +CMake configuration step. + +Bazel tends to create very long file names and paths. You may need to use a +short directory to store the build output, such as `c:\b`, and instruct Bazel +to use it via: + +```shell +bazel --output_user_root=c:\b build ... +``` + +gRPC [requires][grpc-roots-pem-bug] an environment variable to configure the +trust store for SSL certificates, you can download and configure this using: + +```console +@powershell -NoProfile -ExecutionPolicy unrestricted -Command ^ + (new-object System.Net.WebClient).Downloadfile( ^ + 'https://pki.google.com/roots.pem', 'roots.pem') +set GRPC_DEFAULT_SSL_ROOTS_FILE_PATH=%cd%\roots.pem +``` + +[bazel-install]: https://docs.bazel.build/versions/main/install.html +[quickstart-link]: https://cloud.google.com/api-keys/docs/get-started-api-keys +[grpc-roots-pem-bug]: https://github.com/grpc/grpc/issues/16571 +[choco-cmake-link]: https://chocolatey.org/packages/cmake +[homebrew-cmake-link]: https://formulae.brew.sh/formula/cmake +[cmake-download-link]: https://cmake.org/download/ +[bazel-grpc-macos-bug]: https://github.com/bazelbuild/bazel/issues/4341 +[authentication-quickstart]: https://cloud.google.com/docs/authentication/getting-started 'Authentication Getting Started' diff --git a/google/cloud/apikeys/quickstart/WORKSPACE.bazel b/google/cloud/apikeys/quickstart/WORKSPACE.bazel new file mode 100644 index 000000000000..7a78f2dd8d60 --- /dev/null +++ b/google/cloud/apikeys/quickstart/WORKSPACE.bazel @@ -0,0 +1,51 @@ +# Copyright 2022 Google LLC +# +# 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 +# +# https://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 minimal WORKSPACE file showing how to use the API Keys API +# C++ client library in Bazel-based projects. +workspace(name = "apikeys_quickstart") + +# Add the necessary Starlark functions to fetch google-cloud-cpp. +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +# Fetch the Google Cloud C++ libraries. +# NOTE: Update this version and SHA256 as needed. +http_archive( + name = "com_github_googleapis_google_cloud_cpp", + sha256 = "168c38219feb5a2c6b81bec5960cd067f6cda3daa83cd9761fa04f27d2b78f17", + strip_prefix = "google-cloud-cpp-2.1.0", + url = "https://github.com/googleapis/google-cloud-cpp/archive/v2.1.0.tar.gz", +) + +# Load indirect dependencies due to +# https://github.com/bazelbuild/bazel/issues/1943 +load("@com_github_googleapis_google_cloud_cpp//bazel:google_cloud_cpp_deps.bzl", "google_cloud_cpp_deps") + +google_cloud_cpp_deps() + +load("@com_google_googleapis//:repository_rules.bzl", "switched_rules_by_language") + +switched_rules_by_language( + name = "com_google_googleapis_imports", + cc = True, + grpc = True, +) + +load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps") + +grpc_deps() + +load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps") + +grpc_extra_deps() diff --git a/google/cloud/apikeys/quickstart/quickstart.cc b/google/cloud/apikeys/quickstart/quickstart.cc new file mode 100644 index 000000000000..c8940f6f733a --- /dev/null +++ b/google/cloud/apikeys/quickstart/quickstart.cc @@ -0,0 +1,38 @@ +// Copyright 2022 Google LLC +// +// 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 +// +// https://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. + +#include "google/cloud/apikeys/api_keys_client.h" +#include +#include + +int main(int argc, char* argv[]) try { + if (argc != 2) { + std::cerr << "Usage: " << argv[0] << " project-id\n"; + return 1; + } + + namespace apikeys = ::google::cloud::apikeys; + auto client = apikeys::ApiKeysClient(apikeys::MakeApiKeysConnection()); + + auto const parent = std::string{"projects/"} + argv[1] + "/locations/global"; + for (auto r : client.ListKeys(parent)) { + if (!r) throw std::runtime_error(r.status().message()); + std::cout << r->DebugString() << "\n"; + } + + return 0; +} catch (std::exception const& ex) { + std::cerr << "Standard exception raised: " << ex.what() << "\n"; + return 1; +}