From dc0fb066777f2b5563a3b89e80acf66eaddacbd7 Mon Sep 17 00:00:00 2001 From: Darren Bolduc Date: Wed, 13 Jul 2022 10:14:46 -0400 Subject: [PATCH] feat(run): generate library (#9460) --- BUILD.bazel | 2 + CHANGELOG.md | 8 + ci/etc/expected_install_directories | 5 + ci/etc/full_feature_list | 1 + cmake/CompileProtos.cmake | 25 +- external/googleapis/protodeps/run.deps | 13 + external/googleapis/protolists/run.list | 7 + external/googleapis/update_libraries.sh | 1 + generator/generator_config.textproto | 16 + google/cloud/run/BUILD.bazel | 59 +++ google/cloud/run/CMakeLists.txt | 182 +++++++++ google/cloud/run/README.md | 100 +++++ google/cloud/run/config.cmake.in | 22 ++ google/cloud/run/doc/main.dox | 112 ++++++ .../run/internal/revisions_auth_decorator.cc | 106 ++++++ .../run/internal/revisions_auth_decorator.h | 75 ++++ .../run/internal/revisions_connection_impl.cc | 119 ++++++ .../run/internal/revisions_connection_impl.h | 108 ++++++ .../internal/revisions_logging_decorator.cc | 104 ++++++ .../internal/revisions_logging_decorator.h | 76 ++++ .../internal/revisions_metadata_decorator.cc | 159 ++++++++ .../internal/revisions_metadata_decorator.h | 75 ++++ .../run/internal/revisions_option_defaults.cc | 70 ++++ .../run/internal/revisions_option_defaults.h | 37 ++ .../run/internal/revisions_retry_traits.h | 43 +++ google/cloud/run/internal/revisions_stub.cc | 104 ++++++ google/cloud/run/internal/revisions_stub.h | 105 ++++++ .../run/internal/revisions_stub_factory.cc | 64 ++++ .../run/internal/revisions_stub_factory.h | 42 +++ .../run/internal/services_auth_decorator.cc | 169 +++++++++ .../run/internal/services_auth_decorator.h | 97 +++++ .../run/internal/services_connection_impl.cc | 210 +++++++++++ .../run/internal/services_connection_impl.h | 123 ++++++ .../internal/services_logging_decorator.cc | 166 +++++++++ .../run/internal/services_logging_decorator.h | 98 +++++ .../internal/services_metadata_decorator.cc | 239 ++++++++++++ .../internal/services_metadata_decorator.h | 97 +++++ .../run/internal/services_option_defaults.cc | 70 ++++ .../run/internal/services_option_defaults.h | 37 ++ .../run/internal/services_retry_traits.h | 43 +++ google/cloud/run/internal/services_stub.cc | 167 +++++++++ google/cloud/run/internal/services_stub.h | 150 ++++++++ .../run/internal/services_stub_factory.cc | 64 ++++ .../run/internal/services_stub_factory.h | 42 +++ .../run/mocks/mock_revisions_connection.h | 68 ++++ .../run/mocks/mock_services_connection.h | 88 +++++ google/cloud/run/quickstart/.bazelrc | 31 ++ google/cloud/run/quickstart/BUILD.bazel | 25 ++ google/cloud/run/quickstart/CMakeLists.txt | 32 ++ google/cloud/run/quickstart/Makefile | 35 ++ google/cloud/run/quickstart/README.md | 166 +++++++++ google/cloud/run/quickstart/WORKSPACE.bazel | 51 +++ google/cloud/run/quickstart/quickstart.cc | 39 ++ google/cloud/run/revisions_client.cc | 83 +++++ google/cloud/run/revisions_client.h | 212 +++++++++++ google/cloud/run/revisions_connection.cc | 90 +++++ google/cloud/run/revisions_connection.h | 122 ++++++ ...revisions_connection_idempotency_policy.cc | 70 ++++ .../revisions_connection_idempotency_policy.h | 59 +++ google/cloud/run/revisions_options.h | 64 ++++ google/cloud/run/services_client.cc | 131 +++++++ google/cloud/run/services_client.h | 350 ++++++++++++++++++ google/cloud/run/services_connection.cc | 121 ++++++ google/cloud/run/services_connection.h | 137 +++++++ .../services_connection_idempotency_policy.cc | 95 +++++ .../services_connection_idempotency_policy.h | 74 ++++ google/cloud/run/services_options.h | 64 ++++ 67 files changed, 5808 insertions(+), 11 deletions(-) create mode 100644 external/googleapis/protodeps/run.deps create mode 100644 external/googleapis/protolists/run.list create mode 100644 google/cloud/run/BUILD.bazel create mode 100644 google/cloud/run/CMakeLists.txt create mode 100644 google/cloud/run/README.md create mode 100644 google/cloud/run/config.cmake.in create mode 100644 google/cloud/run/doc/main.dox create mode 100644 google/cloud/run/internal/revisions_auth_decorator.cc create mode 100644 google/cloud/run/internal/revisions_auth_decorator.h create mode 100644 google/cloud/run/internal/revisions_connection_impl.cc create mode 100644 google/cloud/run/internal/revisions_connection_impl.h create mode 100644 google/cloud/run/internal/revisions_logging_decorator.cc create mode 100644 google/cloud/run/internal/revisions_logging_decorator.h create mode 100644 google/cloud/run/internal/revisions_metadata_decorator.cc create mode 100644 google/cloud/run/internal/revisions_metadata_decorator.h create mode 100644 google/cloud/run/internal/revisions_option_defaults.cc create mode 100644 google/cloud/run/internal/revisions_option_defaults.h create mode 100644 google/cloud/run/internal/revisions_retry_traits.h create mode 100644 google/cloud/run/internal/revisions_stub.cc create mode 100644 google/cloud/run/internal/revisions_stub.h create mode 100644 google/cloud/run/internal/revisions_stub_factory.cc create mode 100644 google/cloud/run/internal/revisions_stub_factory.h create mode 100644 google/cloud/run/internal/services_auth_decorator.cc create mode 100644 google/cloud/run/internal/services_auth_decorator.h create mode 100644 google/cloud/run/internal/services_connection_impl.cc create mode 100644 google/cloud/run/internal/services_connection_impl.h create mode 100644 google/cloud/run/internal/services_logging_decorator.cc create mode 100644 google/cloud/run/internal/services_logging_decorator.h create mode 100644 google/cloud/run/internal/services_metadata_decorator.cc create mode 100644 google/cloud/run/internal/services_metadata_decorator.h create mode 100644 google/cloud/run/internal/services_option_defaults.cc create mode 100644 google/cloud/run/internal/services_option_defaults.h create mode 100644 google/cloud/run/internal/services_retry_traits.h create mode 100644 google/cloud/run/internal/services_stub.cc create mode 100644 google/cloud/run/internal/services_stub.h create mode 100644 google/cloud/run/internal/services_stub_factory.cc create mode 100644 google/cloud/run/internal/services_stub_factory.h create mode 100644 google/cloud/run/mocks/mock_revisions_connection.h create mode 100644 google/cloud/run/mocks/mock_services_connection.h create mode 100644 google/cloud/run/quickstart/.bazelrc create mode 100644 google/cloud/run/quickstart/BUILD.bazel create mode 100644 google/cloud/run/quickstart/CMakeLists.txt create mode 100644 google/cloud/run/quickstart/Makefile create mode 100644 google/cloud/run/quickstart/README.md create mode 100644 google/cloud/run/quickstart/WORKSPACE.bazel create mode 100644 google/cloud/run/quickstart/quickstart.cc create mode 100644 google/cloud/run/revisions_client.cc create mode 100644 google/cloud/run/revisions_client.h create mode 100644 google/cloud/run/revisions_connection.cc create mode 100644 google/cloud/run/revisions_connection.h create mode 100644 google/cloud/run/revisions_connection_idempotency_policy.cc create mode 100644 google/cloud/run/revisions_connection_idempotency_policy.h create mode 100644 google/cloud/run/revisions_options.h create mode 100644 google/cloud/run/services_client.cc create mode 100644 google/cloud/run/services_client.h create mode 100644 google/cloud/run/services_connection.cc create mode 100644 google/cloud/run/services_connection.h create mode 100644 google/cloud/run/services_connection_idempotency_policy.cc create mode 100644 google/cloud/run/services_connection_idempotency_policy.h create mode 100644 google/cloud/run/services_options.h diff --git a/BUILD.bazel b/BUILD.bazel index eb4d8e49c6c5b..4c559175347b3 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -21,6 +21,8 @@ exports_files([ ]) EXPERIMENTAL_LIBRARIES = [ + # Introduced circa 2022-07-12 + "run", # Introduced circa 2022-06-22 "video", # Introduced in 2022-05 diff --git a/CHANGELOG.md b/CHANGELOG.md index 69d77509e3b94..94e1a88a00e3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -97,6 +97,14 @@ For status on this, see https://github.com/googleapis/google-cloud-cpp/issues/88 ## v2.1.0 - TBD +### New Libraries + +We are introducing a new client library for [Cloud Run]. While we do not +anticipate any API changes to this library before declaring it GA, we are +releasing it early in case it elicits some feedback that requires changes. + +[Cloud Run]: https://github.com/googleapis/google-cloud-cpp/blob/main/google/cloud/run/README.md + ## v2.0.0 - 2022-07 **BREAKING CHANGES** diff --git a/ci/etc/expected_install_directories b/ci/etc/expected_install_directories index 2e28b401ee92f..028c778714ab9 100644 --- a/ci/etc/expected_install_directories +++ b/ci/etc/expected_install_directories @@ -259,6 +259,10 @@ ./include/google/cloud/retail/internal ./include/google/cloud/retail/mocks ./include/google/cloud/retail/v2 +./include/google/cloud/run +./include/google/cloud/run/internal +./include/google/cloud/run/mocks +./include/google/cloud/run/v2 ./include/google/cloud/scheduler ./include/google/cloud/scheduler/internal ./include/google/cloud/scheduler/mocks @@ -471,6 +475,7 @@ ./lib64/cmake/google_cloud_cpp_resourcemanager ./lib64/cmake/google_cloud_cpp_resourcesettings ./lib64/cmake/google_cloud_cpp_retail +./lib64/cmake/google_cloud_cpp_run ./lib64/cmake/google_cloud_cpp_scheduler ./lib64/cmake/google_cloud_cpp_secretmanager ./lib64/cmake/google_cloud_cpp_securitycenter diff --git a/ci/etc/full_feature_list b/ci/etc/full_feature_list index 04a48312f770a..9ab1bde261d42 100644 --- a/ci/etc/full_feature_list +++ b/ci/etc/full_feature_list @@ -57,6 +57,7 @@ redis resourcemanager resourcesettings retail +run scheduler secretmanager securitycenter diff --git a/cmake/CompileProtos.cmake b/cmake/CompileProtos.cmake index 071673acacd3a..e7a98cd1427db 100644 --- a/cmake/CompileProtos.cmake +++ b/cmake/CompileProtos.cmake @@ -80,9 +80,10 @@ function (google_cloud_cpp_generate_proto SRCS) foreach (file_path ${_opt_UNPARSED_ARGUMENTS}) get_filename_component(file_directory "${file_path}" DIRECTORY) get_filename_component(file_name "${file_path}" NAME) - # This gets the filename without the extension, analogous to $(basename - # "${file_path}" .proto) - get_filename_component(file_stem "${file_path}" NAME_WE) + # This gets the file name without the ".proto" extension. We would like + # to use get_filename_component with the option NAME_WLE, but that is + # not available until CMake 3.14 + string(REPLACE ".proto" "" file_stem "${file_name}") # Strip all the prefixes in ${_opt_PROTO_PATH_DIRECTORIES} from the # source proto directory @@ -175,11 +176,13 @@ function (google_cloud_cpp_generate_grpcpp SRCS) endforeach () set(${SRCS}) - foreach (filename ${_opt_UNPARSED_ARGUMENTS}) - get_filename_component(file_directory "${filename}" DIRECTORY) - # This gets the filename without the extension, analogous to $(basename - # "${filename}" .proto) - get_filename_component(file_stem "${filename}" NAME_WE) + foreach (file_path ${_opt_UNPARSED_ARGUMENTS}) + get_filename_component(file_directory "${file_path}" DIRECTORY) + get_filename_component(file_name "${file_path}" NAME) + # This gets the file name without the ".proto" extension. We would like + # to use get_filename_component with the option NAME_WLE, but that is + # not available until CMake 3.14 + string(REPLACE ".proto" "" file_stem "${file_name}") # Strip all the prefixes in ${_opt_PROTO_PATH_DIRECTORIES} from the # source proto directory @@ -201,9 +204,9 @@ function (google_cloud_cpp_generate_grpcpp SRCS) --plugin=protoc-gen-grpc=$ "--grpc_out=${CMAKE_CURRENT_BINARY_DIR}" "--cpp_out=${CMAKE_CURRENT_BINARY_DIR}" ${protobuf_include_path} - "${filename}" - DEPENDS "${filename}" protobuf::protoc gRPC::grpc_cpp_plugin - COMMENT "Running gRPC C++ protocol buffer compiler on ${filename}" + "${file_path}" + DEPENDS "${file_path}" protobuf::protoc gRPC::grpc_cpp_plugin + COMMENT "Running gRPC C++ protocol buffer compiler on ${file_path}" VERBATIM) endforeach () diff --git a/external/googleapis/protodeps/run.deps b/external/googleapis/protodeps/run.deps new file mode 100644 index 0000000000000..8d1d5337c5398 --- /dev/null +++ b/external/googleapis/protodeps/run.deps @@ -0,0 +1,13 @@ +@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:launch_stage_proto +@com_google_googleapis//google/api:resource_proto +@com_google_googleapis//google/api:routing_proto +@com_google_googleapis//google/iam/v1:iam_policy_proto +@com_google_googleapis//google/iam/v1:options_proto +@com_google_googleapis//google/iam/v1:policy_proto +@com_google_googleapis//google/longrunning:operations_proto +@com_google_googleapis//google/rpc:status_proto +@com_google_googleapis//google/type:expr_proto diff --git a/external/googleapis/protolists/run.list b/external/googleapis/protolists/run.list new file mode 100644 index 0000000000000..0749ed6b2132d --- /dev/null +++ b/external/googleapis/protolists/run.list @@ -0,0 +1,7 @@ +@com_google_googleapis//google/cloud/run/v2:condition.proto +@com_google_googleapis//google/cloud/run/v2:k8s.min.proto +@com_google_googleapis//google/cloud/run/v2:revision.proto +@com_google_googleapis//google/cloud/run/v2:revision_template.proto +@com_google_googleapis//google/cloud/run/v2:service.proto +@com_google_googleapis//google/cloud/run/v2:traffic_target.proto +@com_google_googleapis//google/cloud/run/v2:vendor_settings.proto diff --git a/external/googleapis/update_libraries.sh b/external/googleapis/update_libraries.sh index fc84bd2cd2f34..953f473a6a78d 100755 --- a/external/googleapis/update_libraries.sh +++ b/external/googleapis/update_libraries.sh @@ -147,6 +147,7 @@ declare -A -r LIBRARIES=( ["resourcemanager"]="@com_google_googleapis//google/cloud/resourcemanager/v3:resourcemanager_cc_grpc" ["resourcesettings"]="@com_google_googleapis//google/cloud/resourcesettings/v1:resourcesettings_cc_grpc" ["retail"]="@com_google_googleapis//google/cloud/retail/v2:retail_cc_grpc" + ["run"]="@com_google_googleapis//google/cloud/run/v2:run_cc_grpc" ["scheduler"]="@com_google_googleapis//google/cloud/scheduler/v1:scheduler_cc_grpc" ["secretmanager"]="$( printf ",%s" \ diff --git a/generator/generator_config.textproto b/generator/generator_config.textproto index 37a0ffb6405ed..b3b90e5ab28f9 100644 --- a/generator/generator_config.textproto +++ b/generator/generator_config.textproto @@ -1082,6 +1082,22 @@ service { retryable_status_codes: ["kUnavailable"] } +# Cloud Run +service { + service_proto_path: "google/cloud/run/v2/revision.proto" + product_path: "google/cloud/run" + initial_copyright_year: "2022" + retryable_status_codes: ["kUnavailable"] +} + +service { + service_proto_path: "google/cloud/run/v2/service.proto" + product_path: "google/cloud/run" + initial_copyright_year: "2022" + service_endpoint_env_var: "GOOGLE_CLOUD_CPP_RUN_SERVICES_ENDPOINT" + retryable_status_codes: ["kUnavailable"] +} + # Scheduler service { service_proto_path: "google/cloud/scheduler/v1/cloudscheduler.proto" diff --git a/google/cloud/run/BUILD.bazel b/google/cloud/run/BUILD.bazel new file mode 100644 index 0000000000000..3abd6720ac2bd --- /dev/null +++ b/google/cloud/run/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_run", + 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/cloud/run/v2:run_cc_grpc", + ], +) + +cc_library( + name = "google_cloud_cpp_run_mocks", + srcs = glob( + include = [MOCK_SOURCE_GLOB], + ), + hdrs = glob( + include = [MOCK_HEADER_GLOB], + ), + visibility = ["//:__pkg__"], + deps = [ + ":google_cloud_cpp_run", + "//:common", + "//:grpc_utils", + ], +) diff --git a/google/cloud/run/CMakeLists.txt b/google/cloud/run/CMakeLists.txt new file mode 100644 index 0000000000000..803a165fd79c8 --- /dev/null +++ b/google/cloud/run/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 "Cloud Run Admin API C++ Client") +set(DOXYGEN_PROJECT_BRIEF "A C++ Client Library for the Cloud Run Admin API") +set(DOXYGEN_PROJECT_NUMBER "${PROJECT_VERSION} (Experimental)") +set(DOXYGEN_EXCLUDE_SYMBOLS "internal" "run_internal" "run_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::run_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/run.list") +google_cloud_cpp_load_protodeps( + proto_deps "${PROJECT_SOURCE_DIR}/external/googleapis/protodeps/run.deps") +google_cloud_cpp_grpcpp_library( + google_cloud_cpp_run_protos # cmake-format: sort + ${proto_list} PROTO_PATH_DIRECTORIES "${EXTERNAL_GOOGLEAPIS_SOURCE}" + "${PROTO_INCLUDE_DIR}") +external_googleapis_set_version_and_alias(run_protos) +target_link_libraries(google_cloud_cpp_run_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_run ${source_files}) +target_include_directories( + google_cloud_cpp_run + PUBLIC $ + $ + $) +target_link_libraries( + google_cloud_cpp_run + PUBLIC google-cloud-cpp::grpc_utils google-cloud-cpp::common + google-cloud-cpp::run_protos) +google_cloud_cpp_add_common_options(google_cloud_cpp_run) +set_target_properties( + google_cloud_cpp_run + PROPERTIES EXPORT_NAME google-cloud-cpp::experimental-run + VERSION "${PROJECT_VERSION}" + SOVERSION "${PROJECT_VERSION_MAJOR}") +target_compile_options(google_cloud_cpp_run + PUBLIC ${GOOGLE_CLOUD_CPP_EXCEPTIONS_FLAG}) + +add_library(google-cloud-cpp::experimental-run ALIAS google_cloud_cpp_run) + +# 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_run_mocks INTERFACE) +target_sources(google_cloud_cpp_run_mocks INTERFACE ${mock_files}) +target_link_libraries( + google_cloud_cpp_run_mocks + INTERFACE google-cloud-cpp::experimental-run GTest::gmock_main GTest::gmock + GTest::gtest) +set_target_properties( + google_cloud_cpp_run_mocks + PROPERTIES EXPORT_NAME google-cloud-cpp::experimental-run_mocks) +target_include_directories( + google_cloud_cpp_run_mocks + INTERFACE $ + $ + $) +target_compile_options(google_cloud_cpp_run_mocks + INTERFACE ${GOOGLE_CLOUD_CPP_EXCEPTIONS_FLAG}) + +include(CTest) +if (BUILD_TESTING) + add_executable(run_quickstart "quickstart/quickstart.cc") + target_link_libraries(run_quickstart + PRIVATE google-cloud-cpp::experimental-run) + google_cloud_cpp_add_common_options(run_quickstart) + add_test( + NAME run_quickstart + COMMAND + cmake -P "${PROJECT_SOURCE_DIR}/cmake/quickstart-runner.cmake" + $ GOOGLE_CLOUD_PROJECT + GOOGLE_CLOUD_CPP_TEST_REGION) + set_tests_properties(run_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_run-targets + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/google_cloud_cpp_run" + COMPONENT google_cloud_cpp_development) + +# Install the libraries and headers in the locations determined by +# GNUInstallDirs +install( + TARGETS google_cloud_cpp_run google_cloud_cpp_run_protos + EXPORT google_cloud_cpp_run-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_run google_cloud_cpp_run_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_run_protos" + "${EXTERNAL_GOOGLEAPIS_SOURCE}") +google_cloud_cpp_install_proto_library_headers("google_cloud_cpp_run_protos") +google_cloud_cpp_install_headers("google_cloud_cpp_run" + "include/google/cloud/run") +google_cloud_cpp_install_headers("google_cloud_cpp_run_mocks" + "include/google/cloud/run") + +google_cloud_cpp_add_pkgconfig( + run + "The Cloud Run Admin API C++ Client Library" + "Provides C++ APIs to use the Cloud Run Admin API." + "google_cloud_cpp_grpc_utils" + " google_cloud_cpp_common" + " google_cloud_cpp_run_protos") + +# Create and install the CMake configuration files. +include(CMakePackageConfigHelpers) +configure_file("config.cmake.in" "google_cloud_cpp_run-config.cmake" @ONLY) +write_basic_package_version_file( + "google_cloud_cpp_run-config-version.cmake" + VERSION ${PROJECT_VERSION} + COMPATIBILITY ExactVersion) + +install( + FILES + "${CMAKE_CURRENT_BINARY_DIR}/google_cloud_cpp_run-config.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/google_cloud_cpp_run-config-version.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/google_cloud_cpp_run" + COMPONENT google_cloud_cpp_development) + +external_googleapis_install_pc("google_cloud_cpp_run_protos") diff --git a/google/cloud/run/README.md b/google/cloud/run/README.md new file mode 100644 index 0000000000000..57be29887815f --- /dev/null +++ b/google/cloud/run/README.md @@ -0,0 +1,100 @@ +# Cloud Run Admin API C++ Client Library + +:construction: + +This directory contains an idiomatic C++ client library for +[Cloud Run][cloud-service-root], a managed compute platform that lets +you run containers directly on top of Google's scalable infrastructure. + +Note that this library only provides tools to **manage** Cloud Run resources. To +actually deploy a C++ function to Cloud Run, see the +[Cloud Run for C++ Hello World][hello-world] example, which uses the +[C++ Functions Framework][functions-framework] library. + +[hello-world]: https://github.com/GoogleCloudPlatform/cpp-samples/tree/main/cloud-run-hello-world +[functions-framework]: https://github.com/GoogleCloudPlatform/functions-framework-cpp + +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 [Cloud Run Admin 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-root]: https://cloud.google.com/run +[cloud-service-docs]: https://cloud.google.com/run/docs +[doxygen-link]: https://googleapis.dev/cpp/google-cloud-run/latest/ +[source-link]: https://github.com/googleapis/google-cloud-cpp/tree/main/google/cloud/run + +## 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/run/services_client.h" +#include +#include + +int main(int argc, char* argv[]) try { + if (argc != 3) { + std::cerr << "Usage: " << argv[0] << " project-id location-id\n"; + return 1; + } + + namespace run = ::google::cloud::run; + auto client = run::ServicesClient(run::MakeServicesConnection()); + + auto const parent = + std::string{"projects/"} + argv[1] + "/locations/" + argv[2]; + for (auto r : client.ListServices(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/run/config.cmake.in b/google/cloud/run/config.cmake.in new file mode 100644 index 0000000000000..68c6849c72674 --- /dev/null +++ b/google/cloud/run/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_run-targets.cmake") diff --git a/google/cloud/run/doc/main.dox b/google/cloud/run/doc/main.dox new file mode 100644 index 0000000000000..9fe412d1a0cd4 --- /dev/null +++ b/google/cloud/run/doc/main.dox @@ -0,0 +1,112 @@ +/*! + +@mainpage Cloud Run Admin API C++ Client Library + +This directory contains an idiomatic C++ client library for +[Cloud Run][cloud-service-docs], a managed compute platform that lets +you run containers directly on top of Google's scalable infrastructure. + +Note that this library only provides tools to **manage** Cloud Run resources. To +actually deploy a C++ function to Cloud Run, see the +[Cloud Run for C++ Hello World][hello-world] example, which uses the +[C++ Functions Framework][functions-framework] library. + +[hello-world]: https://github.com/GoogleCloudPlatform/cpp-samples/tree/main/cloud-run-hello-world +[functions-framework]: https://github.com/GoogleCloudPlatform/functions-framework-cpp + +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. + +### Setting up your repo + +In order to use the Cloud Run Admin 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/run/quickstart +@endcode + +@par Example: Quickstart + +The following shows the code that you'll run in the +`google/cloud/run/quickstart/` directory, +which should give you a taste of the Cloud Run Admin API C++ client library API. + +@include quickstart.cc + +## Environment Variables + + + +- `GOOGLE_CLOUD_CPP_REVISIONS_ENDPOINT=...` overrides the + `EndpointOption` (which defaults to "run.googleapis.com") + used by `MakeRevisionsConnection()`. + +- `GOOGLE_CLOUD_CPP_RUN_SERVICES_ENDPOINT=...` overrides the + `EndpointOption` (which defaults to "run.googleapis.com") + used by `MakeServicesConnection()`. + + + +- `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/run +[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/run/README%2Emd +[github-quickstart]: https://github.com/googleapis/google-cloud-cpp/blob/main/google/cloud/run/quickstart/README%2Emd + +*/ diff --git a/google/cloud/run/internal/revisions_auth_decorator.cc b/google/cloud/run/internal/revisions_auth_decorator.cc new file mode 100644 index 0000000000000..351a39f4c61a3 --- /dev/null +++ b/google/cloud/run/internal/revisions_auth_decorator.cc @@ -0,0 +1,106 @@ +// 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/cloud/run/v2/revision.proto + +#include "google/cloud/run/internal/revisions_auth_decorator.h" +#include +#include + +namespace google { +namespace cloud { +namespace run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +RevisionsAuth::RevisionsAuth( + std::shared_ptr auth, + std::shared_ptr child) + : auth_(std::move(auth)), child_(std::move(child)) {} + +StatusOr RevisionsAuth::GetRevision( + grpc::ClientContext& context, + google::cloud::run::v2::GetRevisionRequest const& request) { + auto status = auth_->ConfigureContext(context); + if (!status.ok()) return status; + return child_->GetRevision(context, request); +} + +StatusOr +RevisionsAuth::ListRevisions( + grpc::ClientContext& context, + google::cloud::run::v2::ListRevisionsRequest const& request) { + auto status = auth_->ConfigureContext(context); + if (!status.ok()) return status; + return child_->ListRevisions(context, request); +} + +future> +RevisionsAuth::AsyncDeleteRevision( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::DeleteRevisionRequest 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->AsyncDeleteRevision(cq, *std::move(context), request); + }); +} + +future> +RevisionsAuth::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 RevisionsAuth::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 run_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/run/internal/revisions_auth_decorator.h b/google/cloud/run/internal/revisions_auth_decorator.h new file mode 100644 index 0000000000000..156a35238c7a1 --- /dev/null +++ b/google/cloud/run/internal/revisions_auth_decorator.h @@ -0,0 +1,75 @@ +// 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/cloud/run/v2/revision.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_REVISIONS_AUTH_DECORATOR_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_REVISIONS_AUTH_DECORATOR_H + +#include "google/cloud/run/internal/revisions_stub.h" +#include "google/cloud/internal/unified_grpc_credentials.h" +#include "google/cloud/version.h" +#include +#include +#include +#include + +namespace google { +namespace cloud { +namespace run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +class RevisionsAuth : public RevisionsStub { + public: + ~RevisionsAuth() override = default; + RevisionsAuth( + std::shared_ptr auth, + std::shared_ptr child); + + StatusOr GetRevision( + grpc::ClientContext& context, + google::cloud::run::v2::GetRevisionRequest const& request) override; + + StatusOr ListRevisions( + grpc::ClientContext& context, + google::cloud::run::v2::ListRevisionsRequest const& request) override; + + future> AsyncDeleteRevision( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::DeleteRevisionRequest 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 run_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_REVISIONS_AUTH_DECORATOR_H diff --git a/google/cloud/run/internal/revisions_connection_impl.cc b/google/cloud/run/internal/revisions_connection_impl.cc new file mode 100644 index 0000000000000..ebca07e6aa9ad --- /dev/null +++ b/google/cloud/run/internal/revisions_connection_impl.cc @@ -0,0 +1,119 @@ +// 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/cloud/run/v2/revision.proto + +#include "google/cloud/run/internal/revisions_connection_impl.h" +#include "google/cloud/run/internal/revisions_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 run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +RevisionsConnectionImpl::RevisionsConnectionImpl( + std::unique_ptr background, + std::shared_ptr stub, Options options) + : background_(std::move(background)), + stub_(std::move(stub)), + options_(internal::MergeOptions(std::move(options), + run_internal::RevisionsDefaultOptions( + RevisionsConnection::options()))) {} + +StatusOr RevisionsConnectionImpl::GetRevision( + google::cloud::run::v2::GetRevisionRequest const& request) { + return google::cloud::internal::RetryLoop( + retry_policy(), backoff_policy(), + idempotency_policy()->GetRevision(request), + [this](grpc::ClientContext& context, + google::cloud::run::v2::GetRevisionRequest const& request) { + return stub_->GetRevision(context, request); + }, + request, __func__); +} + +StreamRange +RevisionsConnectionImpl::ListRevisions( + google::cloud::run::v2::ListRevisionsRequest 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()->ListRevisions(request); + char const* function_name = __func__; + return google::cloud::internal::MakePaginationRange< + StreamRange>( + std::move(request), + [stub, retry, backoff, idempotency, + function_name](google::cloud::run::v2::ListRevisionsRequest const& r) { + return google::cloud::internal::RetryLoop( + retry->clone(), backoff->clone(), idempotency, + [stub]( + grpc::ClientContext& context, + google::cloud::run::v2::ListRevisionsRequest const& request) { + return stub->ListRevisions(context, request); + }, + r, function_name); + }, + [](google::cloud::run::v2::ListRevisionsResponse r) { + std::vector result( + r.revisions().size()); + auto& messages = *r.mutable_revisions(); + std::move(messages.begin(), messages.end(), result.begin()); + return result; + }); +} + +future> +RevisionsConnectionImpl::DeleteRevision( + google::cloud::run::v2::DeleteRevisionRequest const& request) { + auto& stub = stub_; + return google::cloud::internal::AsyncLongRunningOperation< + google::cloud::run::v2::Revision>( + background_->cq(), request, + [stub](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::DeleteRevisionRequest const& request) { + return stub->AsyncDeleteRevision(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::cloud::run::v2::Revision>, + retry_policy(), backoff_policy(), + idempotency_policy()->DeleteRevision(request), polling_policy(), + __func__); +} + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/run/internal/revisions_connection_impl.h b/google/cloud/run/internal/revisions_connection_impl.h new file mode 100644 index 0000000000000..9865ba801b7cd --- /dev/null +++ b/google/cloud/run/internal/revisions_connection_impl.h @@ -0,0 +1,108 @@ +// 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/cloud/run/v2/revision.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_REVISIONS_CONNECTION_IMPL_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_REVISIONS_CONNECTION_IMPL_H + +#include "google/cloud/run/internal/revisions_retry_traits.h" +#include "google/cloud/run/internal/revisions_stub.h" +#include "google/cloud/run/revisions_connection.h" +#include "google/cloud/run/revisions_connection_idempotency_policy.h" +#include "google/cloud/run/revisions_options.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 run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +class RevisionsConnectionImpl : public run::RevisionsConnection { + public: + ~RevisionsConnectionImpl() override = default; + + RevisionsConnectionImpl( + std::unique_ptr background, + std::shared_ptr stub, Options options); + + Options options() override { return options_; } + + StatusOr GetRevision( + google::cloud::run::v2::GetRevisionRequest const& request) override; + + StreamRange ListRevisions( + google::cloud::run::v2::ListRevisionsRequest request) override; + + future> DeleteRevision( + google::cloud::run::v2::DeleteRevisionRequest 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 run_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_REVISIONS_CONNECTION_IMPL_H diff --git a/google/cloud/run/internal/revisions_logging_decorator.cc b/google/cloud/run/internal/revisions_logging_decorator.cc new file mode 100644 index 0000000000000..c2c0408c23557 --- /dev/null +++ b/google/cloud/run/internal/revisions_logging_decorator.cc @@ -0,0 +1,104 @@ +// 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/cloud/run/v2/revision.proto + +#include "google/cloud/run/internal/revisions_logging_decorator.h" +#include "google/cloud/internal/log_wrapper.h" +#include "google/cloud/status_or.h" +#include +#include + +namespace google { +namespace cloud { +namespace run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +RevisionsLogging::RevisionsLogging(std::shared_ptr child, + TracingOptions tracing_options, + std::set components) + : child_(std::move(child)), + tracing_options_(std::move(tracing_options)), + components_(std::move(components)) {} + +StatusOr RevisionsLogging::GetRevision( + grpc::ClientContext& context, + google::cloud::run::v2::GetRevisionRequest const& request) { + return google::cloud::internal::LogWrapper( + [this](grpc::ClientContext& context, + google::cloud::run::v2::GetRevisionRequest const& request) { + return child_->GetRevision(context, request); + }, + context, request, __func__, tracing_options_); +} + +StatusOr +RevisionsLogging::ListRevisions( + grpc::ClientContext& context, + google::cloud::run::v2::ListRevisionsRequest const& request) { + return google::cloud::internal::LogWrapper( + [this](grpc::ClientContext& context, + google::cloud::run::v2::ListRevisionsRequest const& request) { + return child_->ListRevisions(context, request); + }, + context, request, __func__, tracing_options_); +} + +future> +RevisionsLogging::AsyncDeleteRevision( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::DeleteRevisionRequest const& request) { + return google::cloud::internal::LogWrapper( + [this](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::DeleteRevisionRequest const& request) { + return child_->AsyncDeleteRevision(cq, std::move(context), request); + }, + cq, std::move(context), request, __func__, tracing_options_); +} + +future> +RevisionsLogging::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 RevisionsLogging::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 run_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/run/internal/revisions_logging_decorator.h b/google/cloud/run/internal/revisions_logging_decorator.h new file mode 100644 index 0000000000000..d9ced7fb5ff50 --- /dev/null +++ b/google/cloud/run/internal/revisions_logging_decorator.h @@ -0,0 +1,76 @@ +// 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/cloud/run/v2/revision.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_REVISIONS_LOGGING_DECORATOR_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_REVISIONS_LOGGING_DECORATOR_H + +#include "google/cloud/run/internal/revisions_stub.h" +#include "google/cloud/tracing_options.h" +#include "google/cloud/version.h" +#include +#include +#include +#include + +namespace google { +namespace cloud { +namespace run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +class RevisionsLogging : public RevisionsStub { + public: + ~RevisionsLogging() override = default; + RevisionsLogging(std::shared_ptr child, + TracingOptions tracing_options, + std::set components); + + StatusOr GetRevision( + grpc::ClientContext& context, + google::cloud::run::v2::GetRevisionRequest const& request) override; + + StatusOr ListRevisions( + grpc::ClientContext& context, + google::cloud::run::v2::ListRevisionsRequest const& request) override; + + future> AsyncDeleteRevision( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::DeleteRevisionRequest 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_; +}; // RevisionsLogging + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_REVISIONS_LOGGING_DECORATOR_H diff --git a/google/cloud/run/internal/revisions_metadata_decorator.cc b/google/cloud/run/internal/revisions_metadata_decorator.cc new file mode 100644 index 0000000000000..cbd0dbdc53690 --- /dev/null +++ b/google/cloud/run/internal/revisions_metadata_decorator.cc @@ -0,0 +1,159 @@ +// 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/cloud/run/v2/revision.proto + +#include "google/cloud/run/internal/revisions_metadata_decorator.h" +#include "google/cloud/common_options.h" +#include "google/cloud/internal/absl_str_join_quiet.h" +#include "google/cloud/internal/api_client_header.h" +#include "google/cloud/internal/routing_matcher.h" +#include "google/cloud/status_or.h" +#include +#include + +namespace google { +namespace cloud { +namespace run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +RevisionsMetadata::RevisionsMetadata(std::shared_ptr child) + : child_(std::move(child)), + api_client_header_( + google::cloud::internal::ApiClientHeader("generator")) {} + +StatusOr RevisionsMetadata::GetRevision( + grpc::ClientContext& context, + google::cloud::run::v2::GetRevisionRequest const& request) { + std::vector params; + params.reserve(1); + + static auto* location_matcher = [] { + return new google::cloud::internal::RoutingMatcher< + google::cloud::run::v2::GetRevisionRequest>{ + "location=", + { + {[](google::cloud::run::v2::GetRevisionRequest const& request) + -> std::string const& { return request.name(); }, + std::regex{"projects/[^/]+/locations/([^/]+)/.*", + std::regex::optimize}}, + }}; + }(); + location_matcher->AppendParam(request, params); + + if (params.empty()) { + SetMetadata(context); + } else { + SetMetadata(context, absl::StrJoin(params, "&")); + } + return child_->GetRevision(context, request); +} + +StatusOr +RevisionsMetadata::ListRevisions( + grpc::ClientContext& context, + google::cloud::run::v2::ListRevisionsRequest const& request) { + std::vector params; + params.reserve(1); + + static auto* location_matcher = [] { + return new google::cloud::internal::RoutingMatcher< + google::cloud::run::v2::ListRevisionsRequest>{ + "location=", + { + {[](google::cloud::run::v2::ListRevisionsRequest const& request) + -> std::string const& { return request.parent(); }, + std::regex{"projects/[^/]+/locations/([^/]+)/.*", + std::regex::optimize}}, + }}; + }(); + location_matcher->AppendParam(request, params); + + if (params.empty()) { + SetMetadata(context); + } else { + SetMetadata(context, absl::StrJoin(params, "&")); + } + return child_->ListRevisions(context, request); +} + +future> +RevisionsMetadata::AsyncDeleteRevision( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::DeleteRevisionRequest const& request) { + std::vector params; + params.reserve(1); + + static auto* location_matcher = [] { + return new google::cloud::internal::RoutingMatcher< + google::cloud::run::v2::DeleteRevisionRequest>{ + "location=", + { + {[](google::cloud::run::v2::DeleteRevisionRequest const& request) + -> std::string const& { return request.name(); }, + std::regex{"projects/[^/]+/locations/([^/]+)/.*", + std::regex::optimize}}, + }}; + }(); + location_matcher->AppendParam(request, params); + + if (params.empty()) { + SetMetadata(*context); + } else { + SetMetadata(*context, absl::StrJoin(params, "&")); + } + return child_->AsyncDeleteRevision(cq, std::move(context), request); +} + +future> +RevisionsMetadata::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 RevisionsMetadata::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 RevisionsMetadata::SetMetadata(grpc::ClientContext& context, + std::string const& request_params) { + context.AddMetadata("x-goog-request-params", request_params); + SetMetadata(context); +} + +void RevisionsMetadata::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 run_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/run/internal/revisions_metadata_decorator.h b/google/cloud/run/internal/revisions_metadata_decorator.h new file mode 100644 index 0000000000000..1dac4554ce66f --- /dev/null +++ b/google/cloud/run/internal/revisions_metadata_decorator.h @@ -0,0 +1,75 @@ +// 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/cloud/run/v2/revision.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_REVISIONS_METADATA_DECORATOR_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_REVISIONS_METADATA_DECORATOR_H + +#include "google/cloud/run/internal/revisions_stub.h" +#include "google/cloud/version.h" +#include +#include +#include + +namespace google { +namespace cloud { +namespace run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +class RevisionsMetadata : public RevisionsStub { + public: + ~RevisionsMetadata() override = default; + explicit RevisionsMetadata(std::shared_ptr child); + + StatusOr GetRevision( + grpc::ClientContext& context, + google::cloud::run::v2::GetRevisionRequest const& request) override; + + StatusOr ListRevisions( + grpc::ClientContext& context, + google::cloud::run::v2::ListRevisionsRequest const& request) override; + + future> AsyncDeleteRevision( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::DeleteRevisionRequest 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 run_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_REVISIONS_METADATA_DECORATOR_H diff --git a/google/cloud/run/internal/revisions_option_defaults.cc b/google/cloud/run/internal/revisions_option_defaults.cc new file mode 100644 index 0000000000000..9e9374e86d7a7 --- /dev/null +++ b/google/cloud/run/internal/revisions_option_defaults.cc @@ -0,0 +1,70 @@ +// 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/cloud/run/v2/revision.proto + +#include "google/cloud/run/internal/revisions_option_defaults.h" +#include "google/cloud/run/revisions_connection.h" +#include "google/cloud/run/revisions_options.h" +#include "google/cloud/internal/populate_common_options.h" +#include "google/cloud/internal/populate_grpc_options.h" +#include + +namespace google { +namespace cloud { +namespace run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +namespace { +auto constexpr kBackoffScaling = 2.0; +} // namespace + +Options RevisionsDefaultOptions(Options options) { + options = google::cloud::internal::PopulateCommonOptions( + std::move(options), "GOOGLE_CLOUD_CPP_REVISIONS_ENDPOINT", "", + "GOOGLE_CLOUD_CPP_REVISIONS_AUTHORITY", "run.googleapis.com"); + options = + google::cloud::internal::PopulateGrpcOptions(std::move(options), ""); + if (!options.has()) { + options.set( + run::RevisionsLimitedTimeRetryPolicy(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( + run::MakeDefaultRevisionsConnectionIdempotencyPolicy()); + } + + return options; +} + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/run/internal/revisions_option_defaults.h b/google/cloud/run/internal/revisions_option_defaults.h new file mode 100644 index 0000000000000..edd6e59461628 --- /dev/null +++ b/google/cloud/run/internal/revisions_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/cloud/run/v2/revision.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_REVISIONS_OPTION_DEFAULTS_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_REVISIONS_OPTION_DEFAULTS_H + +#include "google/cloud/options.h" +#include "google/cloud/version.h" + +namespace google { +namespace cloud { +namespace run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +Options RevisionsDefaultOptions(Options options); + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_REVISIONS_OPTION_DEFAULTS_H diff --git a/google/cloud/run/internal/revisions_retry_traits.h b/google/cloud/run/internal/revisions_retry_traits.h new file mode 100644 index 0000000000000..e46ac51be6542 --- /dev/null +++ b/google/cloud/run/internal/revisions_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/cloud/run/v2/revision.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_REVISIONS_RETRY_TRAITS_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_REVISIONS_RETRY_TRAITS_H + +#include "google/cloud/status.h" +#include "google/cloud/version.h" + +namespace google { +namespace cloud { +namespace run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +/// Define the gRPC status code semantics for retrying requests. +struct RevisionsRetryTraits { + 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 run_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_REVISIONS_RETRY_TRAITS_H diff --git a/google/cloud/run/internal/revisions_stub.cc b/google/cloud/run/internal/revisions_stub.cc new file mode 100644 index 0000000000000..8f86663b5186a --- /dev/null +++ b/google/cloud/run/internal/revisions_stub.cc @@ -0,0 +1,104 @@ +// 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/cloud/run/v2/revision.proto + +#include "google/cloud/run/internal/revisions_stub.h" +#include "google/cloud/grpc_error_delegate.h" +#include "google/cloud/status_or.h" +#include +#include +#include + +namespace google { +namespace cloud { +namespace run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +RevisionsStub::~RevisionsStub() = default; + +StatusOr DefaultRevisionsStub::GetRevision( + grpc::ClientContext& client_context, + google::cloud::run::v2::GetRevisionRequest const& request) { + google::cloud::run::v2::Revision response; + auto status = grpc_stub_->GetRevision(&client_context, request, &response); + if (!status.ok()) { + return google::cloud::MakeStatusFromRpcError(status); + } + return response; +} + +StatusOr +DefaultRevisionsStub::ListRevisions( + grpc::ClientContext& client_context, + google::cloud::run::v2::ListRevisionsRequest const& request) { + google::cloud::run::v2::ListRevisionsResponse response; + auto status = grpc_stub_->ListRevisions(&client_context, request, &response); + if (!status.ok()) { + return google::cloud::MakeStatusFromRpcError(status); + } + return response; +} + +future> +DefaultRevisionsStub::AsyncDeleteRevision( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::DeleteRevisionRequest const& request) { + return cq.MakeUnaryRpc( + [this](grpc::ClientContext* context, + google::cloud::run::v2::DeleteRevisionRequest const& request, + grpc::CompletionQueue* cq) { + return grpc_stub_->AsyncDeleteRevision(context, request, cq); + }, + request, std::move(context)); +} + +future> +DefaultRevisionsStub::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 DefaultRevisionsStub::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 run_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/run/internal/revisions_stub.h b/google/cloud/run/internal/revisions_stub.h new file mode 100644 index 0000000000000..fddaebb30061f --- /dev/null +++ b/google/cloud/run/internal/revisions_stub.h @@ -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/cloud/run/v2/revision.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_REVISIONS_STUB_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_REVISIONS_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 run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +class RevisionsStub { + public: + virtual ~RevisionsStub() = 0; + + virtual StatusOr GetRevision( + grpc::ClientContext& context, + google::cloud::run::v2::GetRevisionRequest const& request) = 0; + + virtual StatusOr ListRevisions( + grpc::ClientContext& context, + google::cloud::run::v2::ListRevisionsRequest const& request) = 0; + + virtual future> AsyncDeleteRevision( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::DeleteRevisionRequest 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 DefaultRevisionsStub : public RevisionsStub { + public: + DefaultRevisionsStub( + std::unique_ptr + grpc_stub, + std::unique_ptr + operations) + : grpc_stub_(std::move(grpc_stub)), operations_(std::move(operations)) {} + + StatusOr GetRevision( + grpc::ClientContext& client_context, + google::cloud::run::v2::GetRevisionRequest const& request) override; + + StatusOr ListRevisions( + grpc::ClientContext& client_context, + google::cloud::run::v2::ListRevisionsRequest const& request) override; + + future> AsyncDeleteRevision( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::DeleteRevisionRequest 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 run_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_REVISIONS_STUB_H diff --git a/google/cloud/run/internal/revisions_stub_factory.cc b/google/cloud/run/internal/revisions_stub_factory.cc new file mode 100644 index 0000000000000..fbe7202e46539 --- /dev/null +++ b/google/cloud/run/internal/revisions_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/cloud/run/v2/revision.proto + +#include "google/cloud/run/internal/revisions_stub_factory.h" +#include "google/cloud/run/internal/revisions_auth_decorator.h" +#include "google/cloud/run/internal/revisions_logging_decorator.h" +#include "google/cloud/run/internal/revisions_metadata_decorator.h" +#include "google/cloud/run/internal/revisions_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 run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +std::shared_ptr CreateDefaultRevisionsStub( + 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::cloud::run::v2::Revisions::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 run_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/run/internal/revisions_stub_factory.h b/google/cloud/run/internal/revisions_stub_factory.h new file mode 100644 index 0000000000000..961ecbc0b6055 --- /dev/null +++ b/google/cloud/run/internal/revisions_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/cloud/run/v2/revision.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_REVISIONS_STUB_FACTORY_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_REVISIONS_STUB_FACTORY_H + +#include "google/cloud/run/internal/revisions_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 run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +std::shared_ptr CreateDefaultRevisionsStub( + google::cloud::CompletionQueue cq, Options const& options); + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_REVISIONS_STUB_FACTORY_H diff --git a/google/cloud/run/internal/services_auth_decorator.cc b/google/cloud/run/internal/services_auth_decorator.cc new file mode 100644 index 0000000000000..b1e593b6f4664 --- /dev/null +++ b/google/cloud/run/internal/services_auth_decorator.cc @@ -0,0 +1,169 @@ +// 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/cloud/run/v2/service.proto + +#include "google/cloud/run/internal/services_auth_decorator.h" +#include +#include + +namespace google { +namespace cloud { +namespace run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +ServicesAuth::ServicesAuth( + std::shared_ptr auth, + std::shared_ptr child) + : auth_(std::move(auth)), child_(std::move(child)) {} + +future> +ServicesAuth::AsyncCreateService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::CreateServiceRequest 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->AsyncCreateService(cq, *std::move(context), request); + }); +} + +StatusOr ServicesAuth::GetService( + grpc::ClientContext& context, + google::cloud::run::v2::GetServiceRequest const& request) { + auto status = auth_->ConfigureContext(context); + if (!status.ok()) return status; + return child_->GetService(context, request); +} + +StatusOr +ServicesAuth::ListServices( + grpc::ClientContext& context, + google::cloud::run::v2::ListServicesRequest const& request) { + auto status = auth_->ConfigureContext(context); + if (!status.ok()) return status; + return child_->ListServices(context, request); +} + +future> +ServicesAuth::AsyncUpdateService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::UpdateServiceRequest 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->AsyncUpdateService(cq, *std::move(context), request); + }); +} + +future> +ServicesAuth::AsyncDeleteService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::DeleteServiceRequest 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->AsyncDeleteService(cq, *std::move(context), request); + }); +} + +StatusOr ServicesAuth::GetIamPolicy( + grpc::ClientContext& context, + google::iam::v1::GetIamPolicyRequest const& request) { + auto status = auth_->ConfigureContext(context); + if (!status.ok()) return status; + return child_->GetIamPolicy(context, request); +} + +StatusOr ServicesAuth::SetIamPolicy( + grpc::ClientContext& context, + google::iam::v1::SetIamPolicyRequest const& request) { + auto status = auth_->ConfigureContext(context); + if (!status.ok()) return status; + return child_->SetIamPolicy(context, request); +} + +StatusOr +ServicesAuth::TestIamPermissions( + grpc::ClientContext& context, + google::iam::v1::TestIamPermissionsRequest const& request) { + auto status = auth_->ConfigureContext(context); + if (!status.ok()) return status; + return child_->TestIamPermissions(context, request); +} + +future> +ServicesAuth::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 ServicesAuth::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 run_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/run/internal/services_auth_decorator.h b/google/cloud/run/internal/services_auth_decorator.h new file mode 100644 index 0000000000000..539a181680d5c --- /dev/null +++ b/google/cloud/run/internal/services_auth_decorator.h @@ -0,0 +1,97 @@ +// 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/cloud/run/v2/service.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_SERVICES_AUTH_DECORATOR_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_SERVICES_AUTH_DECORATOR_H + +#include "google/cloud/run/internal/services_stub.h" +#include "google/cloud/internal/unified_grpc_credentials.h" +#include "google/cloud/version.h" +#include +#include +#include +#include + +namespace google { +namespace cloud { +namespace run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +class ServicesAuth : public ServicesStub { + public: + ~ServicesAuth() override = default; + ServicesAuth( + std::shared_ptr auth, + std::shared_ptr child); + + future> AsyncCreateService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::CreateServiceRequest const& request) override; + + StatusOr GetService( + grpc::ClientContext& context, + google::cloud::run::v2::GetServiceRequest const& request) override; + + StatusOr ListServices( + grpc::ClientContext& context, + google::cloud::run::v2::ListServicesRequest const& request) override; + + future> AsyncUpdateService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::UpdateServiceRequest const& request) override; + + future> AsyncDeleteService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::DeleteServiceRequest const& request) override; + + StatusOr GetIamPolicy( + grpc::ClientContext& context, + google::iam::v1::GetIamPolicyRequest const& request) override; + + StatusOr SetIamPolicy( + grpc::ClientContext& context, + google::iam::v1::SetIamPolicyRequest const& request) override; + + StatusOr TestIamPermissions( + grpc::ClientContext& context, + google::iam::v1::TestIamPermissionsRequest 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 run_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_SERVICES_AUTH_DECORATOR_H diff --git a/google/cloud/run/internal/services_connection_impl.cc b/google/cloud/run/internal/services_connection_impl.cc new file mode 100644 index 0000000000000..8cd40e4f049da --- /dev/null +++ b/google/cloud/run/internal/services_connection_impl.cc @@ -0,0 +1,210 @@ +// 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/cloud/run/v2/service.proto + +#include "google/cloud/run/internal/services_connection_impl.h" +#include "google/cloud/run/internal/services_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 run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +ServicesConnectionImpl::ServicesConnectionImpl( + std::unique_ptr background, + std::shared_ptr stub, Options options) + : background_(std::move(background)), + stub_(std::move(stub)), + options_(internal::MergeOptions(std::move(options), + run_internal::ServicesDefaultOptions( + ServicesConnection::options()))) {} + +future> +ServicesConnectionImpl::CreateService( + google::cloud::run::v2::CreateServiceRequest const& request) { + auto& stub = stub_; + return google::cloud::internal::AsyncLongRunningOperation< + google::cloud::run::v2::Service>( + background_->cq(), request, + [stub](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::CreateServiceRequest const& request) { + return stub->AsyncCreateService(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::cloud::run::v2::Service>, + retry_policy(), backoff_policy(), + idempotency_policy()->CreateService(request), polling_policy(), __func__); +} + +StatusOr ServicesConnectionImpl::GetService( + google::cloud::run::v2::GetServiceRequest const& request) { + return google::cloud::internal::RetryLoop( + retry_policy(), backoff_policy(), + idempotency_policy()->GetService(request), + [this](grpc::ClientContext& context, + google::cloud::run::v2::GetServiceRequest const& request) { + return stub_->GetService(context, request); + }, + request, __func__); +} + +StreamRange +ServicesConnectionImpl::ListServices( + google::cloud::run::v2::ListServicesRequest 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()->ListServices(request); + char const* function_name = __func__; + return google::cloud::internal::MakePaginationRange< + StreamRange>( + std::move(request), + [stub, retry, backoff, idempotency, + function_name](google::cloud::run::v2::ListServicesRequest const& r) { + return google::cloud::internal::RetryLoop( + retry->clone(), backoff->clone(), idempotency, + [stub](grpc::ClientContext& context, + google::cloud::run::v2::ListServicesRequest const& request) { + return stub->ListServices(context, request); + }, + r, function_name); + }, + [](google::cloud::run::v2::ListServicesResponse r) { + std::vector result( + r.services().size()); + auto& messages = *r.mutable_services(); + std::move(messages.begin(), messages.end(), result.begin()); + return result; + }); +} + +future> +ServicesConnectionImpl::UpdateService( + google::cloud::run::v2::UpdateServiceRequest const& request) { + auto& stub = stub_; + return google::cloud::internal::AsyncLongRunningOperation< + google::cloud::run::v2::Service>( + background_->cq(), request, + [stub](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::UpdateServiceRequest const& request) { + return stub->AsyncUpdateService(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::cloud::run::v2::Service>, + retry_policy(), backoff_policy(), + idempotency_policy()->UpdateService(request), polling_policy(), __func__); +} + +future> +ServicesConnectionImpl::DeleteService( + google::cloud::run::v2::DeleteServiceRequest const& request) { + auto& stub = stub_; + return google::cloud::internal::AsyncLongRunningOperation< + google::cloud::run::v2::Service>( + background_->cq(), request, + [stub](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::DeleteServiceRequest const& request) { + return stub->AsyncDeleteService(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::cloud::run::v2::Service>, + retry_policy(), backoff_policy(), + idempotency_policy()->DeleteService(request), polling_policy(), __func__); +} + +StatusOr ServicesConnectionImpl::GetIamPolicy( + google::iam::v1::GetIamPolicyRequest const& request) { + return google::cloud::internal::RetryLoop( + retry_policy(), backoff_policy(), + idempotency_policy()->GetIamPolicy(request), + [this](grpc::ClientContext& context, + google::iam::v1::GetIamPolicyRequest const& request) { + return stub_->GetIamPolicy(context, request); + }, + request, __func__); +} + +StatusOr ServicesConnectionImpl::SetIamPolicy( + google::iam::v1::SetIamPolicyRequest const& request) { + return google::cloud::internal::RetryLoop( + retry_policy(), backoff_policy(), + idempotency_policy()->SetIamPolicy(request), + [this](grpc::ClientContext& context, + google::iam::v1::SetIamPolicyRequest const& request) { + return stub_->SetIamPolicy(context, request); + }, + request, __func__); +} + +StatusOr +ServicesConnectionImpl::TestIamPermissions( + google::iam::v1::TestIamPermissionsRequest const& request) { + return google::cloud::internal::RetryLoop( + retry_policy(), backoff_policy(), + idempotency_policy()->TestIamPermissions(request), + [this](grpc::ClientContext& context, + google::iam::v1::TestIamPermissionsRequest const& request) { + return stub_->TestIamPermissions(context, request); + }, + request, __func__); +} + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/run/internal/services_connection_impl.h b/google/cloud/run/internal/services_connection_impl.h new file mode 100644 index 0000000000000..41884b1812819 --- /dev/null +++ b/google/cloud/run/internal/services_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/cloud/run/v2/service.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_SERVICES_CONNECTION_IMPL_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_SERVICES_CONNECTION_IMPL_H + +#include "google/cloud/run/internal/services_retry_traits.h" +#include "google/cloud/run/internal/services_stub.h" +#include "google/cloud/run/services_connection.h" +#include "google/cloud/run/services_connection_idempotency_policy.h" +#include "google/cloud/run/services_options.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 run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +class ServicesConnectionImpl : public run::ServicesConnection { + public: + ~ServicesConnectionImpl() override = default; + + ServicesConnectionImpl( + std::unique_ptr background, + std::shared_ptr stub, Options options); + + Options options() override { return options_; } + + future> CreateService( + google::cloud::run::v2::CreateServiceRequest const& request) override; + + StatusOr GetService( + google::cloud::run::v2::GetServiceRequest const& request) override; + + StreamRange ListServices( + google::cloud::run::v2::ListServicesRequest request) override; + + future> UpdateService( + google::cloud::run::v2::UpdateServiceRequest const& request) override; + + future> DeleteService( + google::cloud::run::v2::DeleteServiceRequest const& request) override; + + StatusOr GetIamPolicy( + google::iam::v1::GetIamPolicyRequest const& request) override; + + StatusOr SetIamPolicy( + google::iam::v1::SetIamPolicyRequest const& request) override; + + StatusOr TestIamPermissions( + google::iam::v1::TestIamPermissionsRequest 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 run_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_SERVICES_CONNECTION_IMPL_H diff --git a/google/cloud/run/internal/services_logging_decorator.cc b/google/cloud/run/internal/services_logging_decorator.cc new file mode 100644 index 0000000000000..01e68c6f6956a --- /dev/null +++ b/google/cloud/run/internal/services_logging_decorator.cc @@ -0,0 +1,166 @@ +// 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/cloud/run/v2/service.proto + +#include "google/cloud/run/internal/services_logging_decorator.h" +#include "google/cloud/internal/log_wrapper.h" +#include "google/cloud/status_or.h" +#include +#include + +namespace google { +namespace cloud { +namespace run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +ServicesLogging::ServicesLogging(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> +ServicesLogging::AsyncCreateService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::CreateServiceRequest const& request) { + return google::cloud::internal::LogWrapper( + [this](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::CreateServiceRequest const& request) { + return child_->AsyncCreateService(cq, std::move(context), request); + }, + cq, std::move(context), request, __func__, tracing_options_); +} + +StatusOr ServicesLogging::GetService( + grpc::ClientContext& context, + google::cloud::run::v2::GetServiceRequest const& request) { + return google::cloud::internal::LogWrapper( + [this](grpc::ClientContext& context, + google::cloud::run::v2::GetServiceRequest const& request) { + return child_->GetService(context, request); + }, + context, request, __func__, tracing_options_); +} + +StatusOr +ServicesLogging::ListServices( + grpc::ClientContext& context, + google::cloud::run::v2::ListServicesRequest const& request) { + return google::cloud::internal::LogWrapper( + [this](grpc::ClientContext& context, + google::cloud::run::v2::ListServicesRequest const& request) { + return child_->ListServices(context, request); + }, + context, request, __func__, tracing_options_); +} + +future> +ServicesLogging::AsyncUpdateService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::UpdateServiceRequest const& request) { + return google::cloud::internal::LogWrapper( + [this](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::UpdateServiceRequest const& request) { + return child_->AsyncUpdateService(cq, std::move(context), request); + }, + cq, std::move(context), request, __func__, tracing_options_); +} + +future> +ServicesLogging::AsyncDeleteService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::DeleteServiceRequest const& request) { + return google::cloud::internal::LogWrapper( + [this](google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::DeleteServiceRequest const& request) { + return child_->AsyncDeleteService(cq, std::move(context), request); + }, + cq, std::move(context), request, __func__, tracing_options_); +} + +StatusOr ServicesLogging::GetIamPolicy( + grpc::ClientContext& context, + google::iam::v1::GetIamPolicyRequest const& request) { + return google::cloud::internal::LogWrapper( + [this](grpc::ClientContext& context, + google::iam::v1::GetIamPolicyRequest const& request) { + return child_->GetIamPolicy(context, request); + }, + context, request, __func__, tracing_options_); +} + +StatusOr ServicesLogging::SetIamPolicy( + grpc::ClientContext& context, + google::iam::v1::SetIamPolicyRequest const& request) { + return google::cloud::internal::LogWrapper( + [this](grpc::ClientContext& context, + google::iam::v1::SetIamPolicyRequest const& request) { + return child_->SetIamPolicy(context, request); + }, + context, request, __func__, tracing_options_); +} + +StatusOr +ServicesLogging::TestIamPermissions( + grpc::ClientContext& context, + google::iam::v1::TestIamPermissionsRequest const& request) { + return google::cloud::internal::LogWrapper( + [this](grpc::ClientContext& context, + google::iam::v1::TestIamPermissionsRequest const& request) { + return child_->TestIamPermissions(context, request); + }, + context, request, __func__, tracing_options_); +} + +future> +ServicesLogging::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 ServicesLogging::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 run_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/run/internal/services_logging_decorator.h b/google/cloud/run/internal/services_logging_decorator.h new file mode 100644 index 0000000000000..dc2d5d256f91d --- /dev/null +++ b/google/cloud/run/internal/services_logging_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/cloud/run/v2/service.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_SERVICES_LOGGING_DECORATOR_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_SERVICES_LOGGING_DECORATOR_H + +#include "google/cloud/run/internal/services_stub.h" +#include "google/cloud/tracing_options.h" +#include "google/cloud/version.h" +#include +#include +#include +#include + +namespace google { +namespace cloud { +namespace run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +class ServicesLogging : public ServicesStub { + public: + ~ServicesLogging() override = default; + ServicesLogging(std::shared_ptr child, + TracingOptions tracing_options, + std::set components); + + future> AsyncCreateService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::CreateServiceRequest const& request) override; + + StatusOr GetService( + grpc::ClientContext& context, + google::cloud::run::v2::GetServiceRequest const& request) override; + + StatusOr ListServices( + grpc::ClientContext& context, + google::cloud::run::v2::ListServicesRequest const& request) override; + + future> AsyncUpdateService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::UpdateServiceRequest const& request) override; + + future> AsyncDeleteService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::DeleteServiceRequest const& request) override; + + StatusOr GetIamPolicy( + grpc::ClientContext& context, + google::iam::v1::GetIamPolicyRequest const& request) override; + + StatusOr SetIamPolicy( + grpc::ClientContext& context, + google::iam::v1::SetIamPolicyRequest const& request) override; + + StatusOr TestIamPermissions( + grpc::ClientContext& context, + google::iam::v1::TestIamPermissionsRequest 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_; +}; // ServicesLogging + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_SERVICES_LOGGING_DECORATOR_H diff --git a/google/cloud/run/internal/services_metadata_decorator.cc b/google/cloud/run/internal/services_metadata_decorator.cc new file mode 100644 index 0000000000000..7c37253e497b2 --- /dev/null +++ b/google/cloud/run/internal/services_metadata_decorator.cc @@ -0,0 +1,239 @@ +// 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/cloud/run/v2/service.proto + +#include "google/cloud/run/internal/services_metadata_decorator.h" +#include "google/cloud/common_options.h" +#include "google/cloud/internal/absl_str_join_quiet.h" +#include "google/cloud/internal/api_client_header.h" +#include "google/cloud/internal/routing_matcher.h" +#include "google/cloud/status_or.h" +#include +#include + +namespace google { +namespace cloud { +namespace run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +ServicesMetadata::ServicesMetadata(std::shared_ptr child) + : child_(std::move(child)), + api_client_header_( + google::cloud::internal::ApiClientHeader("generator")) {} + +future> +ServicesMetadata::AsyncCreateService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::CreateServiceRequest const& request) { + std::vector params; + params.reserve(1); + + static auto* location_matcher = [] { + return new google::cloud::internal::RoutingMatcher< + google::cloud::run::v2::CreateServiceRequest>{ + "location=", + { + {[](google::cloud::run::v2::CreateServiceRequest const& request) + -> std::string const& { return request.parent(); }, + std::regex{"projects/[^/]+/locations/([^/]+)", + std::regex::optimize}}, + }}; + }(); + location_matcher->AppendParam(request, params); + + if (params.empty()) { + SetMetadata(*context); + } else { + SetMetadata(*context, absl::StrJoin(params, "&")); + } + return child_->AsyncCreateService(cq, std::move(context), request); +} + +StatusOr ServicesMetadata::GetService( + grpc::ClientContext& context, + google::cloud::run::v2::GetServiceRequest const& request) { + std::vector params; + params.reserve(1); + + static auto* location_matcher = [] { + return new google::cloud::internal::RoutingMatcher< + google::cloud::run::v2::GetServiceRequest>{ + "location=", + { + {[](google::cloud::run::v2::GetServiceRequest const& request) + -> std::string const& { return request.name(); }, + std::regex{"projects/[^/]+/locations/([^/]+)/.*", + std::regex::optimize}}, + }}; + }(); + location_matcher->AppendParam(request, params); + + if (params.empty()) { + SetMetadata(context); + } else { + SetMetadata(context, absl::StrJoin(params, "&")); + } + return child_->GetService(context, request); +} + +StatusOr +ServicesMetadata::ListServices( + grpc::ClientContext& context, + google::cloud::run::v2::ListServicesRequest const& request) { + std::vector params; + params.reserve(1); + + static auto* location_matcher = [] { + return new google::cloud::internal::RoutingMatcher< + google::cloud::run::v2::ListServicesRequest>{ + "location=", + { + {[](google::cloud::run::v2::ListServicesRequest const& request) + -> std::string const& { return request.parent(); }, + std::regex{"projects/[^/]+/locations/([^/]+)", + std::regex::optimize}}, + }}; + }(); + location_matcher->AppendParam(request, params); + + if (params.empty()) { + SetMetadata(context); + } else { + SetMetadata(context, absl::StrJoin(params, "&")); + } + return child_->ListServices(context, request); +} + +future> +ServicesMetadata::AsyncUpdateService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::UpdateServiceRequest const& request) { + std::vector params; + params.reserve(1); + + static auto* location_matcher = [] { + return new google::cloud::internal::RoutingMatcher< + google::cloud::run::v2::UpdateServiceRequest>{ + "location=", + { + {[](google::cloud::run::v2::UpdateServiceRequest const& request) + -> std::string const& { return request.service().name(); }, + std::regex{"projects/[^/]+/locations/([^/]+)/.*", + std::regex::optimize}}, + }}; + }(); + location_matcher->AppendParam(request, params); + + if (params.empty()) { + SetMetadata(*context); + } else { + SetMetadata(*context, absl::StrJoin(params, "&")); + } + return child_->AsyncUpdateService(cq, std::move(context), request); +} + +future> +ServicesMetadata::AsyncDeleteService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::DeleteServiceRequest const& request) { + std::vector params; + params.reserve(1); + + static auto* location_matcher = [] { + return new google::cloud::internal::RoutingMatcher< + google::cloud::run::v2::DeleteServiceRequest>{ + "location=", + { + {[](google::cloud::run::v2::DeleteServiceRequest const& request) + -> std::string const& { return request.name(); }, + std::regex{"projects/[^/]+/locations/([^/]+)/.*", + std::regex::optimize}}, + }}; + }(); + location_matcher->AppendParam(request, params); + + if (params.empty()) { + SetMetadata(*context); + } else { + SetMetadata(*context, absl::StrJoin(params, "&")); + } + return child_->AsyncDeleteService(cq, std::move(context), request); +} + +StatusOr ServicesMetadata::GetIamPolicy( + grpc::ClientContext& context, + google::iam::v1::GetIamPolicyRequest const& request) { + SetMetadata(context, "resource=" + request.resource()); + return child_->GetIamPolicy(context, request); +} + +StatusOr ServicesMetadata::SetIamPolicy( + grpc::ClientContext& context, + google::iam::v1::SetIamPolicyRequest const& request) { + SetMetadata(context, "resource=" + request.resource()); + return child_->SetIamPolicy(context, request); +} + +StatusOr +ServicesMetadata::TestIamPermissions( + grpc::ClientContext& context, + google::iam::v1::TestIamPermissionsRequest const& request) { + SetMetadata(context, "resource=" + request.resource()); + return child_->TestIamPermissions(context, request); +} + +future> +ServicesMetadata::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 ServicesMetadata::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 ServicesMetadata::SetMetadata(grpc::ClientContext& context, + std::string const& request_params) { + context.AddMetadata("x-goog-request-params", request_params); + SetMetadata(context); +} + +void ServicesMetadata::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 run_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/run/internal/services_metadata_decorator.h b/google/cloud/run/internal/services_metadata_decorator.h new file mode 100644 index 0000000000000..496f6b41bae73 --- /dev/null +++ b/google/cloud/run/internal/services_metadata_decorator.h @@ -0,0 +1,97 @@ +// 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/cloud/run/v2/service.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_SERVICES_METADATA_DECORATOR_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_SERVICES_METADATA_DECORATOR_H + +#include "google/cloud/run/internal/services_stub.h" +#include "google/cloud/version.h" +#include +#include +#include + +namespace google { +namespace cloud { +namespace run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +class ServicesMetadata : public ServicesStub { + public: + ~ServicesMetadata() override = default; + explicit ServicesMetadata(std::shared_ptr child); + + future> AsyncCreateService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::CreateServiceRequest const& request) override; + + StatusOr GetService( + grpc::ClientContext& context, + google::cloud::run::v2::GetServiceRequest const& request) override; + + StatusOr ListServices( + grpc::ClientContext& context, + google::cloud::run::v2::ListServicesRequest const& request) override; + + future> AsyncUpdateService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::UpdateServiceRequest const& request) override; + + future> AsyncDeleteService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::DeleteServiceRequest const& request) override; + + StatusOr GetIamPolicy( + grpc::ClientContext& context, + google::iam::v1::GetIamPolicyRequest const& request) override; + + StatusOr SetIamPolicy( + grpc::ClientContext& context, + google::iam::v1::SetIamPolicyRequest const& request) override; + + StatusOr TestIamPermissions( + grpc::ClientContext& context, + google::iam::v1::TestIamPermissionsRequest 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 run_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_SERVICES_METADATA_DECORATOR_H diff --git a/google/cloud/run/internal/services_option_defaults.cc b/google/cloud/run/internal/services_option_defaults.cc new file mode 100644 index 0000000000000..cf90d72fce5fd --- /dev/null +++ b/google/cloud/run/internal/services_option_defaults.cc @@ -0,0 +1,70 @@ +// 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/cloud/run/v2/service.proto + +#include "google/cloud/run/internal/services_option_defaults.h" +#include "google/cloud/run/services_connection.h" +#include "google/cloud/run/services_options.h" +#include "google/cloud/internal/populate_common_options.h" +#include "google/cloud/internal/populate_grpc_options.h" +#include + +namespace google { +namespace cloud { +namespace run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +namespace { +auto constexpr kBackoffScaling = 2.0; +} // namespace + +Options ServicesDefaultOptions(Options options) { + options = google::cloud::internal::PopulateCommonOptions( + std::move(options), "GOOGLE_CLOUD_CPP_RUN_SERVICES_ENDPOINT", "", + "GOOGLE_CLOUD_CPP_RUN_SERVICES_AUTHORITY", "run.googleapis.com"); + options = + google::cloud::internal::PopulateGrpcOptions(std::move(options), ""); + if (!options.has()) { + options.set( + run::ServicesLimitedTimeRetryPolicy(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( + run::MakeDefaultServicesConnectionIdempotencyPolicy()); + } + + return options; +} + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/run/internal/services_option_defaults.h b/google/cloud/run/internal/services_option_defaults.h new file mode 100644 index 0000000000000..b179fe6c144d4 --- /dev/null +++ b/google/cloud/run/internal/services_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/cloud/run/v2/service.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_SERVICES_OPTION_DEFAULTS_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_SERVICES_OPTION_DEFAULTS_H + +#include "google/cloud/options.h" +#include "google/cloud/version.h" + +namespace google { +namespace cloud { +namespace run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +Options ServicesDefaultOptions(Options options); + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_SERVICES_OPTION_DEFAULTS_H diff --git a/google/cloud/run/internal/services_retry_traits.h b/google/cloud/run/internal/services_retry_traits.h new file mode 100644 index 0000000000000..b4237cc6ca8ca --- /dev/null +++ b/google/cloud/run/internal/services_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/cloud/run/v2/service.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_SERVICES_RETRY_TRAITS_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_SERVICES_RETRY_TRAITS_H + +#include "google/cloud/status.h" +#include "google/cloud/version.h" + +namespace google { +namespace cloud { +namespace run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +/// Define the gRPC status code semantics for retrying requests. +struct ServicesRetryTraits { + 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 run_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_SERVICES_RETRY_TRAITS_H diff --git a/google/cloud/run/internal/services_stub.cc b/google/cloud/run/internal/services_stub.cc new file mode 100644 index 0000000000000..e2ceb6ffd7941 --- /dev/null +++ b/google/cloud/run/internal/services_stub.cc @@ -0,0 +1,167 @@ +// 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/cloud/run/v2/service.proto + +#include "google/cloud/run/internal/services_stub.h" +#include "google/cloud/grpc_error_delegate.h" +#include "google/cloud/status_or.h" +#include +#include +#include + +namespace google { +namespace cloud { +namespace run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +ServicesStub::~ServicesStub() = default; + +future> +DefaultServicesStub::AsyncCreateService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::CreateServiceRequest const& request) { + return cq.MakeUnaryRpc( + [this](grpc::ClientContext* context, + google::cloud::run::v2::CreateServiceRequest const& request, + grpc::CompletionQueue* cq) { + return grpc_stub_->AsyncCreateService(context, request, cq); + }, + request, std::move(context)); +} + +StatusOr DefaultServicesStub::GetService( + grpc::ClientContext& client_context, + google::cloud::run::v2::GetServiceRequest const& request) { + google::cloud::run::v2::Service response; + auto status = grpc_stub_->GetService(&client_context, request, &response); + if (!status.ok()) { + return google::cloud::MakeStatusFromRpcError(status); + } + return response; +} + +StatusOr +DefaultServicesStub::ListServices( + grpc::ClientContext& client_context, + google::cloud::run::v2::ListServicesRequest const& request) { + google::cloud::run::v2::ListServicesResponse response; + auto status = grpc_stub_->ListServices(&client_context, request, &response); + if (!status.ok()) { + return google::cloud::MakeStatusFromRpcError(status); + } + return response; +} + +future> +DefaultServicesStub::AsyncUpdateService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::UpdateServiceRequest const& request) { + return cq.MakeUnaryRpc( + [this](grpc::ClientContext* context, + google::cloud::run::v2::UpdateServiceRequest const& request, + grpc::CompletionQueue* cq) { + return grpc_stub_->AsyncUpdateService(context, request, cq); + }, + request, std::move(context)); +} + +future> +DefaultServicesStub::AsyncDeleteService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::DeleteServiceRequest const& request) { + return cq.MakeUnaryRpc( + [this](grpc::ClientContext* context, + google::cloud::run::v2::DeleteServiceRequest const& request, + grpc::CompletionQueue* cq) { + return grpc_stub_->AsyncDeleteService(context, request, cq); + }, + request, std::move(context)); +} + +StatusOr DefaultServicesStub::GetIamPolicy( + grpc::ClientContext& client_context, + google::iam::v1::GetIamPolicyRequest const& request) { + google::iam::v1::Policy response; + auto status = grpc_stub_->GetIamPolicy(&client_context, request, &response); + if (!status.ok()) { + return google::cloud::MakeStatusFromRpcError(status); + } + return response; +} + +StatusOr DefaultServicesStub::SetIamPolicy( + grpc::ClientContext& client_context, + google::iam::v1::SetIamPolicyRequest const& request) { + google::iam::v1::Policy response; + auto status = grpc_stub_->SetIamPolicy(&client_context, request, &response); + if (!status.ok()) { + return google::cloud::MakeStatusFromRpcError(status); + } + return response; +} + +StatusOr +DefaultServicesStub::TestIamPermissions( + grpc::ClientContext& client_context, + google::iam::v1::TestIamPermissionsRequest const& request) { + google::iam::v1::TestIamPermissionsResponse response; + auto status = + grpc_stub_->TestIamPermissions(&client_context, request, &response); + if (!status.ok()) { + return google::cloud::MakeStatusFromRpcError(status); + } + return response; +} + +future> +DefaultServicesStub::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 DefaultServicesStub::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 run_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/run/internal/services_stub.h b/google/cloud/run/internal/services_stub.h new file mode 100644 index 0000000000000..bbd0080a1893f --- /dev/null +++ b/google/cloud/run/internal/services_stub.h @@ -0,0 +1,150 @@ +// 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/cloud/run/v2/service.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_SERVICES_STUB_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_SERVICES_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 run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +class ServicesStub { + public: + virtual ~ServicesStub() = 0; + + virtual future> AsyncCreateService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::CreateServiceRequest const& request) = 0; + + virtual StatusOr GetService( + grpc::ClientContext& context, + google::cloud::run::v2::GetServiceRequest const& request) = 0; + + virtual StatusOr ListServices( + grpc::ClientContext& context, + google::cloud::run::v2::ListServicesRequest const& request) = 0; + + virtual future> AsyncUpdateService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::UpdateServiceRequest const& request) = 0; + + virtual future> AsyncDeleteService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::DeleteServiceRequest const& request) = 0; + + virtual StatusOr GetIamPolicy( + grpc::ClientContext& context, + google::iam::v1::GetIamPolicyRequest const& request) = 0; + + virtual StatusOr SetIamPolicy( + grpc::ClientContext& context, + google::iam::v1::SetIamPolicyRequest const& request) = 0; + + virtual StatusOr + TestIamPermissions( + grpc::ClientContext& context, + google::iam::v1::TestIamPermissionsRequest 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 DefaultServicesStub : public ServicesStub { + public: + DefaultServicesStub( + std::unique_ptr + grpc_stub, + std::unique_ptr + operations) + : grpc_stub_(std::move(grpc_stub)), operations_(std::move(operations)) {} + + future> AsyncCreateService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::CreateServiceRequest const& request) override; + + StatusOr GetService( + grpc::ClientContext& client_context, + google::cloud::run::v2::GetServiceRequest const& request) override; + + StatusOr ListServices( + grpc::ClientContext& client_context, + google::cloud::run::v2::ListServicesRequest const& request) override; + + future> AsyncUpdateService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::UpdateServiceRequest const& request) override; + + future> AsyncDeleteService( + google::cloud::CompletionQueue& cq, + std::unique_ptr context, + google::cloud::run::v2::DeleteServiceRequest const& request) override; + + StatusOr GetIamPolicy( + grpc::ClientContext& client_context, + google::iam::v1::GetIamPolicyRequest const& request) override; + + StatusOr SetIamPolicy( + grpc::ClientContext& client_context, + google::iam::v1::SetIamPolicyRequest const& request) override; + + StatusOr TestIamPermissions( + grpc::ClientContext& client_context, + google::iam::v1::TestIamPermissionsRequest 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 run_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_SERVICES_STUB_H diff --git a/google/cloud/run/internal/services_stub_factory.cc b/google/cloud/run/internal/services_stub_factory.cc new file mode 100644 index 0000000000000..ae92d99ff4449 --- /dev/null +++ b/google/cloud/run/internal/services_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/cloud/run/v2/service.proto + +#include "google/cloud/run/internal/services_stub_factory.h" +#include "google/cloud/run/internal/services_auth_decorator.h" +#include "google/cloud/run/internal/services_logging_decorator.h" +#include "google/cloud/run/internal/services_metadata_decorator.h" +#include "google/cloud/run/internal/services_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 run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +std::shared_ptr CreateDefaultServicesStub( + 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::cloud::run::v2::Services::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 run_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/run/internal/services_stub_factory.h b/google/cloud/run/internal/services_stub_factory.h new file mode 100644 index 0000000000000..3e64d3da6685d --- /dev/null +++ b/google/cloud/run/internal/services_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/cloud/run/v2/service.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_SERVICES_STUB_FACTORY_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_SERVICES_STUB_FACTORY_H + +#include "google/cloud/run/internal/services_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 run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +std::shared_ptr CreateDefaultServicesStub( + google::cloud::CompletionQueue cq, Options const& options); + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_INTERNAL_SERVICES_STUB_FACTORY_H diff --git a/google/cloud/run/mocks/mock_revisions_connection.h b/google/cloud/run/mocks/mock_revisions_connection.h new file mode 100644 index 0000000000000..9eac4c1287838 --- /dev/null +++ b/google/cloud/run/mocks/mock_revisions_connection.h @@ -0,0 +1,68 @@ +// 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/cloud/run/v2/revision.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_MOCKS_MOCK_REVISIONS_CONNECTION_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_MOCKS_MOCK_REVISIONS_CONNECTION_H + +#include "google/cloud/run/revisions_connection.h" +#include + +namespace google { +namespace cloud { +namespace run_mocks { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +/** + * A class to mock `RevisionsConnection`. + * + * Application developers may want to test their code with simulated responses, + * including errors, from an object of type `RevisionsClient`. To do so, + * construct an object of type `RevisionsClient` 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 MockRevisionsConnection : public run::RevisionsConnection { + public: + MOCK_METHOD(Options, options, (), (override)); + + MOCK_METHOD(StatusOr, GetRevision, + (google::cloud::run::v2::GetRevisionRequest const& request), + (override)); + + MOCK_METHOD(StreamRange, ListRevisions, + (google::cloud::run::v2::ListRevisionsRequest request), + (override)); + + MOCK_METHOD(future>, + DeleteRevision, + (google::cloud::run::v2::DeleteRevisionRequest const& request), + (override)); +}; + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run_mocks +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_MOCKS_MOCK_REVISIONS_CONNECTION_H diff --git a/google/cloud/run/mocks/mock_services_connection.h b/google/cloud/run/mocks/mock_services_connection.h new file mode 100644 index 0000000000000..480b604d9e626 --- /dev/null +++ b/google/cloud/run/mocks/mock_services_connection.h @@ -0,0 +1,88 @@ +// 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/cloud/run/v2/service.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_MOCKS_MOCK_SERVICES_CONNECTION_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_MOCKS_MOCK_SERVICES_CONNECTION_H + +#include "google/cloud/run/services_connection.h" +#include + +namespace google { +namespace cloud { +namespace run_mocks { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +/** + * A class to mock `ServicesConnection`. + * + * Application developers may want to test their code with simulated responses, + * including errors, from an object of type `ServicesClient`. To do so, + * construct an object of type `ServicesClient` 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 MockServicesConnection : public run::ServicesConnection { + public: + MOCK_METHOD(Options, options, (), (override)); + + MOCK_METHOD(future>, CreateService, + (google::cloud::run::v2::CreateServiceRequest const& request), + (override)); + + MOCK_METHOD(StatusOr, GetService, + (google::cloud::run::v2::GetServiceRequest const& request), + (override)); + + MOCK_METHOD(StreamRange, ListServices, + (google::cloud::run::v2::ListServicesRequest request), + (override)); + + MOCK_METHOD(future>, UpdateService, + (google::cloud::run::v2::UpdateServiceRequest const& request), + (override)); + + MOCK_METHOD(future>, DeleteService, + (google::cloud::run::v2::DeleteServiceRequest const& request), + (override)); + + MOCK_METHOD(StatusOr, GetIamPolicy, + (google::iam::v1::GetIamPolicyRequest const& request), + (override)); + + MOCK_METHOD(StatusOr, SetIamPolicy, + (google::iam::v1::SetIamPolicyRequest const& request), + (override)); + + MOCK_METHOD(StatusOr, + TestIamPermissions, + (google::iam::v1::TestIamPermissionsRequest const& request), + (override)); +}; + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run_mocks +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_MOCKS_MOCK_SERVICES_CONNECTION_H diff --git a/google/cloud/run/quickstart/.bazelrc b/google/cloud/run/quickstart/.bazelrc new file mode 100644 index 0000000000000..77d763d38194e --- /dev/null +++ b/google/cloud/run/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/run/quickstart/BUILD.bazel b/google/cloud/run/quickstart/BUILD.bazel new file mode 100644 index 0000000000000..378052419bbd8 --- /dev/null +++ b/google/cloud/run/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-run", + ], +) diff --git a/google/cloud/run/quickstart/CMakeLists.txt b/google/cloud/run/quickstart/CMakeLists.txt new file mode 100644 index 0000000000000..94cee43565d75 --- /dev/null +++ b/google/cloud/run/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 Cloud Run Admin API C++ client library from a +# larger CMake project. + +cmake_minimum_required(VERSION 3.10) +project(google-cloud-cpp-run-quickstart CXX) + +find_package(google_cloud_cpp_run 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-run) diff --git a/google/cloud/run/quickstart/Makefile b/google/cloud/run/quickstart/Makefile new file mode 100644 index 0000000000000..e16df73ef98e1 --- /dev/null +++ b/google/cloud/run/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 Cloud Run Admin 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 Cloud Run Admin API C++ +# client library. +CLIENT_MODULE := google_cloud_cpp_run +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/run/quickstart/README.md b/google/cloud/run/quickstart/README.md new file mode 100644 index 0000000000000..d27461c08064e --- /dev/null +++ b/google/cloud/run/quickstart/README.md @@ -0,0 +1,166 @@ +# HOWTO: using the Cloud Run Admin API C++ client in your project + +This directory contains small examples showing how to use the Cloud Run Admin 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, Cloud Run Admin 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/run/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,run] + ``` + + 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/run/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/run/docs/quickstarts +[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/run/quickstart/WORKSPACE.bazel b/google/cloud/run/quickstart/WORKSPACE.bazel new file mode 100644 index 0000000000000..eeeced84d7e10 --- /dev/null +++ b/google/cloud/run/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 Cloud Run Admin API +# C++ client library in Bazel-based projects. +workspace(name = "run_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 = "a7e51bfffb95a377094b2ae7e3b9f715a68ed931c48992c7273b2fae989c029c", + strip_prefix = "google-cloud-cpp-1.36.0", + url = "https://github.com/googleapis/google-cloud-cpp/archive/v1.36.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/run/quickstart/quickstart.cc b/google/cloud/run/quickstart/quickstart.cc new file mode 100644 index 0000000000000..f0cde8ad2fad0 --- /dev/null +++ b/google/cloud/run/quickstart/quickstart.cc @@ -0,0 +1,39 @@ +// 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/run/services_client.h" +#include +#include + +int main(int argc, char* argv[]) try { + if (argc != 3) { + std::cerr << "Usage: " << argv[0] << " project-id location-id\n"; + return 1; + } + + namespace run = ::google::cloud::run; + auto client = run::ServicesClient(run::MakeServicesConnection()); + + auto const parent = + std::string{"projects/"} + argv[1] + "/locations/" + argv[2]; + for (auto r : client.ListServices(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; +} diff --git a/google/cloud/run/revisions_client.cc b/google/cloud/run/revisions_client.cc new file mode 100644 index 0000000000000..2cb79ffadf33e --- /dev/null +++ b/google/cloud/run/revisions_client.cc @@ -0,0 +1,83 @@ +// 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/cloud/run/v2/revision.proto + +#include "google/cloud/run/revisions_client.h" +#include "google/cloud/run/internal/revisions_option_defaults.h" +#include + +namespace google { +namespace cloud { +namespace run { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +RevisionsClient::RevisionsClient( + std::shared_ptr connection, Options opts) + : connection_(std::move(connection)), + options_(internal::MergeOptions( + std::move(opts), + run_internal::RevisionsDefaultOptions(connection_->options()))) {} +RevisionsClient::~RevisionsClient() = default; + +StatusOr RevisionsClient::GetRevision( + std::string const& name, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + google::cloud::run::v2::GetRevisionRequest request; + request.set_name(name); + return connection_->GetRevision(request); +} + +StatusOr RevisionsClient::GetRevision( + google::cloud::run::v2::GetRevisionRequest const& request, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + return connection_->GetRevision(request); +} + +StreamRange RevisionsClient::ListRevisions( + std::string const& parent, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + google::cloud::run::v2::ListRevisionsRequest request; + request.set_parent(parent); + return connection_->ListRevisions(request); +} + +StreamRange RevisionsClient::ListRevisions( + google::cloud::run::v2::ListRevisionsRequest request, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + return connection_->ListRevisions(std::move(request)); +} + +future> +RevisionsClient::DeleteRevision(std::string const& name, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + google::cloud::run::v2::DeleteRevisionRequest request; + request.set_name(name); + return connection_->DeleteRevision(request); +} + +future> +RevisionsClient::DeleteRevision( + google::cloud::run::v2::DeleteRevisionRequest const& request, + Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + return connection_->DeleteRevision(request); +} + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run +} // namespace cloud +} // namespace google diff --git a/google/cloud/run/revisions_client.h b/google/cloud/run/revisions_client.h new file mode 100644 index 0000000000000..d7c248abf8abb --- /dev/null +++ b/google/cloud/run/revisions_client.h @@ -0,0 +1,212 @@ +// 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/cloud/run/v2/revision.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_REVISIONS_CLIENT_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_REVISIONS_CLIENT_H + +#include "google/cloud/run/revisions_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 run { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +/// +/// Cloud Run Revision Control Plane API. +/// +/// @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 RevisionsClient { + public: + explicit RevisionsClient(std::shared_ptr connection, + Options opts = {}); + ~RevisionsClient(); + + //@{ + // @name Copy and move support + RevisionsClient(RevisionsClient const&) = default; + RevisionsClient& operator=(RevisionsClient const&) = default; + RevisionsClient(RevisionsClient&&) = default; + RevisionsClient& operator=(RevisionsClient&&) = default; + //@} + + //@{ + // @name Equality + friend bool operator==(RevisionsClient const& a, RevisionsClient const& b) { + return a.connection_ == b.connection_; + } + friend bool operator!=(RevisionsClient const& a, RevisionsClient const& b) { + return !(a == b); + } + //@} + + /// + /// Gets information about a Revision. + /// + /// @param name Required. The full name of the Revision. + /// Format: + /// projects/{project}/locations/{location}/services/{service}/revisions/{revision} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::cloud::run::v2::Revision,google/cloud/run/v2/revision.proto#L162} + /// + /// [google.cloud.run.v2.GetRevisionRequest]: + /// @googleapis_reference_link{google/cloud/run/v2/revision.proto#L90} + /// [google.cloud.run.v2.Revision]: + /// @googleapis_reference_link{google/cloud/run/v2/revision.proto#L162} + /// + StatusOr GetRevision( + std::string const& name, Options opts = {}); + + /// + /// Gets information about a Revision. + /// + /// @param request + /// @googleapis_link{google::cloud::run::v2::GetRevisionRequest,google/cloud/run/v2/revision.proto#L90} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::cloud::run::v2::Revision,google/cloud/run/v2/revision.proto#L162} + /// + /// [google.cloud.run.v2.GetRevisionRequest]: + /// @googleapis_reference_link{google/cloud/run/v2/revision.proto#L90} + /// [google.cloud.run.v2.Revision]: + /// @googleapis_reference_link{google/cloud/run/v2/revision.proto#L162} + /// + StatusOr GetRevision( + google::cloud::run::v2::GetRevisionRequest const& request, + Options opts = {}); + + /// + /// List Revisions from a given Service, or from a given location. + /// + /// @param parent Required. The Service from which the Revisions should be + /// listed. + /// To list all Revisions across Services, use "-" instead of Service name. + /// Format: + /// projects/{project}/locations/{location}/services/{service} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::cloud::run::v2::Revision,google/cloud/run/v2/revision.proto#L162} + /// + /// [google.cloud.run.v2.ListRevisionsRequest]: + /// @googleapis_reference_link{google/cloud/run/v2/revision.proto#L103} + /// [google.cloud.run.v2.Revision]: + /// @googleapis_reference_link{google/cloud/run/v2/revision.proto#L162} + /// + StreamRange ListRevisions( + std::string const& parent, Options opts = {}); + + /// + /// List Revisions from a given Service, or from a given location. + /// + /// @param request + /// @googleapis_link{google::cloud::run::v2::ListRevisionsRequest,google/cloud/run/v2/revision.proto#L103} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::cloud::run::v2::Revision,google/cloud/run/v2/revision.proto#L162} + /// + /// [google.cloud.run.v2.ListRevisionsRequest]: + /// @googleapis_reference_link{google/cloud/run/v2/revision.proto#L103} + /// [google.cloud.run.v2.Revision]: + /// @googleapis_reference_link{google/cloud/run/v2/revision.proto#L162} + /// + StreamRange ListRevisions( + google::cloud::run::v2::ListRevisionsRequest request, Options opts = {}); + + /// + /// Delete a Revision. + /// + /// @param name Required. The name of the Revision to delete. + /// Format: + /// projects/{project}/locations/{location}/services/{service}/revisions/{revision} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::cloud::run::v2::Revision,google/cloud/run/v2/revision.proto#L162} + /// + /// [google.cloud.run.v2.DeleteRevisionRequest]: + /// @googleapis_reference_link{google/cloud/run/v2/revision.proto#L139} + /// [google.cloud.run.v2.Revision]: + /// @googleapis_reference_link{google/cloud/run/v2/revision.proto#L162} + /// + future> DeleteRevision( + std::string const& name, Options opts = {}); + + /// + /// Delete a Revision. + /// + /// @param request + /// @googleapis_link{google::cloud::run::v2::DeleteRevisionRequest,google/cloud/run/v2/revision.proto#L139} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::cloud::run::v2::Revision,google/cloud/run/v2/revision.proto#L162} + /// + /// [google.cloud.run.v2.DeleteRevisionRequest]: + /// @googleapis_reference_link{google/cloud/run/v2/revision.proto#L139} + /// [google.cloud.run.v2.Revision]: + /// @googleapis_reference_link{google/cloud/run/v2/revision.proto#L162} + /// + future> DeleteRevision( + google::cloud::run::v2::DeleteRevisionRequest const& request, + Options opts = {}); + + private: + std::shared_ptr connection_; + Options options_; +}; + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_REVISIONS_CLIENT_H diff --git a/google/cloud/run/revisions_connection.cc b/google/cloud/run/revisions_connection.cc new file mode 100644 index 0000000000000..3719627f70d68 --- /dev/null +++ b/google/cloud/run/revisions_connection.cc @@ -0,0 +1,90 @@ +// 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/cloud/run/v2/revision.proto + +#include "google/cloud/run/revisions_connection.h" +#include "google/cloud/run/internal/revisions_connection_impl.h" +#include "google/cloud/run/internal/revisions_option_defaults.h" +#include "google/cloud/run/internal/revisions_stub_factory.h" +#include "google/cloud/run/revisions_options.h" +#include "google/cloud/background_threads.h" +#include "google/cloud/common_options.h" +#include "google/cloud/grpc_options.h" +#include "google/cloud/internal/pagination_range.h" +#include + +namespace google { +namespace cloud { +namespace run { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +RevisionsConnection::~RevisionsConnection() = default; + +StatusOr RevisionsConnection::GetRevision( + google::cloud::run::v2::GetRevisionRequest const&) { + return Status(StatusCode::kUnimplemented, "not implemented"); +} + +StreamRange +RevisionsConnection::ListRevisions( + google::cloud::run::v2:: + ListRevisionsRequest) { // NOLINT(performance-unnecessary-value-param) + return google::cloud::internal::MakeUnimplementedPaginationRange< + StreamRange>(); +} + +future> +RevisionsConnection::DeleteRevision( + google::cloud::run::v2::DeleteRevisionRequest const&) { + return google::cloud::make_ready_future< + StatusOr>( + Status(StatusCode::kUnimplemented, "not implemented")); +} + +std::shared_ptr MakeRevisionsConnection(Options options) { + internal::CheckExpectedOptions(options, __func__); + options = run_internal::RevisionsDefaultOptions(std::move(options)); + auto background = internal::MakeBackgroundThreadsFactory(options)(); + auto stub = + run_internal::CreateDefaultRevisionsStub(background->cq(), options); + return std::make_shared( + std::move(background), std::move(stub), std::move(options)); +} + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run +} // namespace cloud +} // namespace google + +namespace google { +namespace cloud { +namespace run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +std::shared_ptr MakeRevisionsConnection( + std::shared_ptr stub, Options options) { + options = RevisionsDefaultOptions(std::move(options)); + auto background = internal::MakeBackgroundThreadsFactory(options)(); + return std::make_shared( + std::move(background), std::move(stub), std::move(options)); +} + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/run/revisions_connection.h b/google/cloud/run/revisions_connection.h new file mode 100644 index 0000000000000..3886a512684d2 --- /dev/null +++ b/google/cloud/run/revisions_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/cloud/run/v2/revision.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_REVISIONS_CONNECTION_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_REVISIONS_CONNECTION_H + +#include "google/cloud/run/internal/revisions_retry_traits.h" +#include "google/cloud/run/internal/revisions_stub.h" +#include "google/cloud/run/revisions_connection_idempotency_policy.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 run { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +using RevisionsRetryPolicy = ::google::cloud::internal::TraitBasedRetryPolicy< + run_internal::RevisionsRetryTraits>; + +using RevisionsLimitedTimeRetryPolicy = + ::google::cloud::internal::LimitedTimeRetryPolicy< + run_internal::RevisionsRetryTraits>; + +using RevisionsLimitedErrorCountRetryPolicy = + ::google::cloud::internal::LimitedErrorCountRetryPolicy< + run_internal::RevisionsRetryTraits>; + +/** + * The `RevisionsConnection` object for `RevisionsClient`. + * + * This interface defines virtual methods for each of the user-facing overload + * sets in `RevisionsClient`. This allows users to inject custom behavior + * (e.g., with a Google Mock object) when writing tests that use objects of type + * `RevisionsClient`. + * + * To create a concrete instance, see `MakeRevisionsConnection()`. + * + * For mocking, see `run_mocks::MockRevisionsConnection`. + */ +class RevisionsConnection { + public: + virtual ~RevisionsConnection() = 0; + + virtual Options options() { return Options{}; } + + virtual StatusOr GetRevision( + google::cloud::run::v2::GetRevisionRequest const& request); + + virtual StreamRange ListRevisions( + google::cloud::run::v2::ListRevisionsRequest request); + + virtual future> DeleteRevision( + google::cloud::run::v2::DeleteRevisionRequest const& request); +}; + +/** + * A factory function to construct an object of type `RevisionsConnection`. + * + * The returned connection object should not be used directly; instead it + * should be passed as an argument to the constructor of RevisionsClient, + * and that class used instead. + * + * The optional @p opts argument may be used to configure aspects of the + * returned `RevisionsConnection`. Expected options are any of the types in + * the following option lists: + * + * - `google::cloud::CommonOptionList` + * - `google::cloud::GrpcOptionList` + * - `google::cloud::run::RevisionsPolicyOptionList` + * + * @note Unrecognized options will be ignored. To debug issues with options set + * `GOOGLE_CLOUD_CPP_ENABLE_CLOG=yes` in the environment and unexpected + * options will be logged. + * + * @param options (optional) Configure the `RevisionsConnection` created by + * this function. + */ +std::shared_ptr MakeRevisionsConnection( + Options options = {}); + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run +} // namespace cloud +} // namespace google + +namespace google { +namespace cloud { +namespace run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +std::shared_ptr MakeRevisionsConnection( + std::shared_ptr stub, Options options); + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_REVISIONS_CONNECTION_H diff --git a/google/cloud/run/revisions_connection_idempotency_policy.cc b/google/cloud/run/revisions_connection_idempotency_policy.cc new file mode 100644 index 0000000000000..5b3b69317fa2d --- /dev/null +++ b/google/cloud/run/revisions_connection_idempotency_policy.cc @@ -0,0 +1,70 @@ +// 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/cloud/run/v2/revision.proto + +#include "google/cloud/run/revisions_connection_idempotency_policy.h" +#include "absl/memory/memory.h" +#include + +namespace google { +namespace cloud { +namespace run { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +using ::google::cloud::Idempotency; + +RevisionsConnectionIdempotencyPolicy::~RevisionsConnectionIdempotencyPolicy() = + default; + +namespace { +class DefaultRevisionsConnectionIdempotencyPolicy + : public RevisionsConnectionIdempotencyPolicy { + public: + ~DefaultRevisionsConnectionIdempotencyPolicy() override = default; + + /// Create a new copy of this object. + std::unique_ptr clone() const override { + return absl::make_unique( + *this); + } + + Idempotency GetRevision( + google::cloud::run::v2::GetRevisionRequest const&) override { + return Idempotency::kIdempotent; + } + + Idempotency ListRevisions( + google::cloud::run::v2::ListRevisionsRequest) override { + return Idempotency::kIdempotent; + } + + Idempotency DeleteRevision( + google::cloud::run::v2::DeleteRevisionRequest const&) override { + return Idempotency::kNonIdempotent; + } +}; +} // namespace + +std::unique_ptr +MakeDefaultRevisionsConnectionIdempotencyPolicy() { + return absl::make_unique(); +} + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run +} // namespace cloud +} // namespace google diff --git a/google/cloud/run/revisions_connection_idempotency_policy.h b/google/cloud/run/revisions_connection_idempotency_policy.h new file mode 100644 index 0000000000000..218bab464748a --- /dev/null +++ b/google/cloud/run/revisions_connection_idempotency_policy.h @@ -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. + +// Generated by the Codegen C++ plugin. +// If you make any local changes, they will be lost. +// source: google/cloud/run/v2/revision.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_REVISIONS_CONNECTION_IDEMPOTENCY_POLICY_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_REVISIONS_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 run { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +class RevisionsConnectionIdempotencyPolicy { + public: + virtual ~RevisionsConnectionIdempotencyPolicy() = 0; + + /// Create a new copy of this object. + virtual std::unique_ptr clone() + const = 0; + + virtual google::cloud::Idempotency GetRevision( + google::cloud::run::v2::GetRevisionRequest const& request) = 0; + + virtual google::cloud::Idempotency ListRevisions( + google::cloud::run::v2::ListRevisionsRequest request) = 0; + + virtual google::cloud::Idempotency DeleteRevision( + google::cloud::run::v2::DeleteRevisionRequest const& request) = 0; +}; + +std::unique_ptr +MakeDefaultRevisionsConnectionIdempotencyPolicy(); + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_REVISIONS_CONNECTION_IDEMPOTENCY_POLICY_H diff --git a/google/cloud/run/revisions_options.h b/google/cloud/run/revisions_options.h new file mode 100644 index 0000000000000..807f5a8ac99dc --- /dev/null +++ b/google/cloud/run/revisions_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/cloud/run/v2/revision.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_REVISIONS_OPTIONS_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_REVISIONS_OPTIONS_H + +#include "google/cloud/run/revisions_connection.h" +#include "google/cloud/run/revisions_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 run { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +/// Option to use with `google::cloud::Options`. +struct RevisionsRetryPolicyOption { + using Type = std::shared_ptr; +}; + +/// Option to use with `google::cloud::Options`. +struct RevisionsBackoffPolicyOption { + using Type = std::shared_ptr; +}; + +/// Option to use with `google::cloud::Options`. +struct RevisionsPollingPolicyOption { + using Type = std::shared_ptr; +}; + +/// Option to use with `google::cloud::Options`. +struct RevisionsConnectionIdempotencyPolicyOption { + using Type = std::shared_ptr; +}; + +using RevisionsPolicyOptionList = + OptionList; + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_REVISIONS_OPTIONS_H diff --git a/google/cloud/run/services_client.cc b/google/cloud/run/services_client.cc new file mode 100644 index 0000000000000..f44589c796aca --- /dev/null +++ b/google/cloud/run/services_client.cc @@ -0,0 +1,131 @@ +// 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/cloud/run/v2/service.proto + +#include "google/cloud/run/services_client.h" +#include "google/cloud/run/internal/services_option_defaults.h" +#include + +namespace google { +namespace cloud { +namespace run { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +ServicesClient::ServicesClient(std::shared_ptr connection, + Options opts) + : connection_(std::move(connection)), + options_(internal::MergeOptions( + std::move(opts), + run_internal::ServicesDefaultOptions(connection_->options()))) {} +ServicesClient::~ServicesClient() = default; + +future> ServicesClient::CreateService( + std::string const& parent, google::cloud::run::v2::Service const& service, + std::string const& service_id, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + google::cloud::run::v2::CreateServiceRequest request; + request.set_parent(parent); + *request.mutable_service() = service; + request.set_service_id(service_id); + return connection_->CreateService(request); +} + +future> ServicesClient::CreateService( + google::cloud::run::v2::CreateServiceRequest const& request, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + return connection_->CreateService(request); +} + +StatusOr ServicesClient::GetService( + std::string const& name, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + google::cloud::run::v2::GetServiceRequest request; + request.set_name(name); + return connection_->GetService(request); +} + +StatusOr ServicesClient::GetService( + google::cloud::run::v2::GetServiceRequest const& request, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + return connection_->GetService(request); +} + +StreamRange ServicesClient::ListServices( + std::string const& parent, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + google::cloud::run::v2::ListServicesRequest request; + request.set_parent(parent); + return connection_->ListServices(request); +} + +StreamRange ServicesClient::ListServices( + google::cloud::run::v2::ListServicesRequest request, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + return connection_->ListServices(std::move(request)); +} + +future> ServicesClient::UpdateService( + google::cloud::run::v2::Service const& service, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + google::cloud::run::v2::UpdateServiceRequest request; + *request.mutable_service() = service; + return connection_->UpdateService(request); +} + +future> ServicesClient::UpdateService( + google::cloud::run::v2::UpdateServiceRequest const& request, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + return connection_->UpdateService(request); +} + +future> ServicesClient::DeleteService( + std::string const& name, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + google::cloud::run::v2::DeleteServiceRequest request; + request.set_name(name); + return connection_->DeleteService(request); +} + +future> ServicesClient::DeleteService( + google::cloud::run::v2::DeleteServiceRequest const& request, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + return connection_->DeleteService(request); +} + +StatusOr ServicesClient::GetIamPolicy( + google::iam::v1::GetIamPolicyRequest const& request, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + return connection_->GetIamPolicy(request); +} + +StatusOr ServicesClient::SetIamPolicy( + google::iam::v1::SetIamPolicyRequest const& request, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + return connection_->SetIamPolicy(request); +} + +StatusOr +ServicesClient::TestIamPermissions( + google::iam::v1::TestIamPermissionsRequest const& request, Options opts) { + internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_)); + return connection_->TestIamPermissions(request); +} + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run +} // namespace cloud +} // namespace google diff --git a/google/cloud/run/services_client.h b/google/cloud/run/services_client.h new file mode 100644 index 0000000000000..0563f24884f23 --- /dev/null +++ b/google/cloud/run/services_client.h @@ -0,0 +1,350 @@ +// 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/cloud/run/v2/service.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_SERVICES_CLIENT_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_SERVICES_CLIENT_H + +#include "google/cloud/run/services_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 run { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +/// +/// Cloud Run Service Control Plane API +/// +/// @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 ServicesClient { + public: + explicit ServicesClient(std::shared_ptr connection, + Options opts = {}); + ~ServicesClient(); + + //@{ + // @name Copy and move support + ServicesClient(ServicesClient const&) = default; + ServicesClient& operator=(ServicesClient const&) = default; + ServicesClient(ServicesClient&&) = default; + ServicesClient& operator=(ServicesClient&&) = default; + //@} + + //@{ + // @name Equality + friend bool operator==(ServicesClient const& a, ServicesClient const& b) { + return a.connection_ == b.connection_; + } + friend bool operator!=(ServicesClient const& a, ServicesClient const& b) { + return !(a == b); + } + //@} + + /// + /// Creates a new Service in a given project and location. + /// + /// @param parent Required. The location and project in which this service + /// should be created. + /// Format: projects/{projectnumber}/locations/{location} + /// @param service Required. The Service instance to create. + /// @param service_id Required. The unique identifier for the Service. The + /// name of the service becomes + /// {parent}/services/{service_id}. + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::cloud::run::v2::Service,google/cloud/run/v2/service.proto#L266} + /// + /// [google.cloud.run.v2.CreateServiceRequest]: + /// @googleapis_reference_link{google/cloud/run/v2/service.proto#L159} + /// [google.cloud.run.v2.Service]: + /// @googleapis_reference_link{google/cloud/run/v2/service.proto#L266} + /// + future> CreateService( + std::string const& parent, google::cloud::run::v2::Service const& service, + std::string const& service_id, Options opts = {}); + + /// + /// Creates a new Service in a given project and location. + /// + /// @param request + /// @googleapis_link{google::cloud::run::v2::CreateServiceRequest,google/cloud/run/v2/service.proto#L159} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::cloud::run::v2::Service,google/cloud/run/v2/service.proto#L266} + /// + /// [google.cloud.run.v2.CreateServiceRequest]: + /// @googleapis_reference_link{google/cloud/run/v2/service.proto#L159} + /// [google.cloud.run.v2.Service]: + /// @googleapis_reference_link{google/cloud/run/v2/service.proto#L266} + /// + future> CreateService( + google::cloud::run::v2::CreateServiceRequest const& request, + Options opts = {}); + + /// + /// Gets information about a Service. + /// + /// @param name Required. The full name of the Service. + /// Format: projects/{projectnumber}/locations/{location}/services/{service} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::cloud::run::v2::Service,google/cloud/run/v2/service.proto#L266} + /// + /// [google.cloud.run.v2.GetServiceRequest]: + /// @googleapis_reference_link{google/cloud/run/v2/service.proto#L230} + /// [google.cloud.run.v2.Service]: + /// @googleapis_reference_link{google/cloud/run/v2/service.proto#L266} + /// + StatusOr GetService(std::string const& name, + Options opts = {}); + + /// + /// Gets information about a Service. + /// + /// @param request + /// @googleapis_link{google::cloud::run::v2::GetServiceRequest,google/cloud/run/v2/service.proto#L230} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::cloud::run::v2::Service,google/cloud/run/v2/service.proto#L266} + /// + /// [google.cloud.run.v2.GetServiceRequest]: + /// @googleapis_reference_link{google/cloud/run/v2/service.proto#L230} + /// [google.cloud.run.v2.Service]: + /// @googleapis_reference_link{google/cloud/run/v2/service.proto#L266} + /// + StatusOr GetService( + google::cloud::run::v2::GetServiceRequest const& request, + Options opts = {}); + + /// + /// List Services. + /// + /// @param parent Required. The location and project to list resources on. + /// Location must be a valid GCP region, and may not be the "-" wildcard. + /// Format: projects/{projectnumber}/locations/{location} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::cloud::run::v2::Service,google/cloud/run/v2/service.proto#L266} + /// + /// [google.cloud.run.v2.ListServicesRequest]: + /// @googleapis_reference_link{google/cloud/run/v2/service.proto#L197} + /// [google.cloud.run.v2.Service]: + /// @googleapis_reference_link{google/cloud/run/v2/service.proto#L266} + /// + StreamRange ListServices( + std::string const& parent, Options opts = {}); + + /// + /// List Services. + /// + /// @param request + /// @googleapis_link{google::cloud::run::v2::ListServicesRequest,google/cloud/run/v2/service.proto#L197} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::cloud::run::v2::Service,google/cloud/run/v2/service.proto#L266} + /// + /// [google.cloud.run.v2.ListServicesRequest]: + /// @googleapis_reference_link{google/cloud/run/v2/service.proto#L197} + /// [google.cloud.run.v2.Service]: + /// @googleapis_reference_link{google/cloud/run/v2/service.proto#L266} + /// + StreamRange ListServices( + google::cloud::run::v2::ListServicesRequest request, Options opts = {}); + + /// + /// Updates a Service. + /// + /// @param service Required. The Service to be updated. + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::cloud::run::v2::Service,google/cloud/run/v2/service.proto#L266} + /// + /// [google.cloud.run.v2.UpdateServiceRequest]: + /// @googleapis_reference_link{google/cloud/run/v2/service.proto#L182} + /// [google.cloud.run.v2.Service]: + /// @googleapis_reference_link{google/cloud/run/v2/service.proto#L266} + /// + future> UpdateService( + google::cloud::run::v2::Service const& service, Options opts = {}); + + /// + /// Updates a Service. + /// + /// @param request + /// @googleapis_link{google::cloud::run::v2::UpdateServiceRequest,google/cloud/run/v2/service.proto#L182} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::cloud::run::v2::Service,google/cloud/run/v2/service.proto#L266} + /// + /// [google.cloud.run.v2.UpdateServiceRequest]: + /// @googleapis_reference_link{google/cloud/run/v2/service.proto#L182} + /// [google.cloud.run.v2.Service]: + /// @googleapis_reference_link{google/cloud/run/v2/service.proto#L266} + /// + future> UpdateService( + google::cloud::run::v2::UpdateServiceRequest const& request, + Options opts = {}); + + /// + /// Deletes a Service. + /// This will cause the Service to stop serving traffic and will delete all + /// revisions. + /// + /// @param name Required. The full name of the Service. + /// Format: projects/{projectnumber}/locations/{location}/services/{service} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::cloud::run::v2::Service,google/cloud/run/v2/service.proto#L266} + /// + /// [google.cloud.run.v2.DeleteServiceRequest]: + /// @googleapis_reference_link{google/cloud/run/v2/service.proto#L242} + /// [google.cloud.run.v2.Service]: + /// @googleapis_reference_link{google/cloud/run/v2/service.proto#L266} + /// + future> DeleteService( + std::string const& name, Options opts = {}); + + /// + /// Deletes a Service. + /// This will cause the Service to stop serving traffic and will delete all + /// revisions. + /// + /// @param request + /// @googleapis_link{google::cloud::run::v2::DeleteServiceRequest,google/cloud/run/v2/service.proto#L242} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::cloud::run::v2::Service,google/cloud/run/v2/service.proto#L266} + /// + /// [google.cloud.run.v2.DeleteServiceRequest]: + /// @googleapis_reference_link{google/cloud/run/v2/service.proto#L242} + /// [google.cloud.run.v2.Service]: + /// @googleapis_reference_link{google/cloud/run/v2/service.proto#L266} + /// + future> DeleteService( + google::cloud::run::v2::DeleteServiceRequest const& request, + Options opts = {}); + + /// + /// Get the IAM Access Control policy currently in effect for the given + /// Cloud Run Service. This result does not include any inherited policies. + /// + /// @param request + /// @googleapis_link{google::iam::v1::GetIamPolicyRequest,google/iam/v1/iam_policy.proto#L123} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::iam::v1::Policy,google/iam/v1/policy.proto#L96} + /// + /// [google.iam.v1.GetIamPolicyRequest]: + /// @googleapis_reference_link{google/iam/v1/iam_policy.proto#L123} + /// [google.iam.v1.Policy]: + /// @googleapis_reference_link{google/iam/v1/policy.proto#L96} + /// + StatusOr GetIamPolicy( + google::iam::v1::GetIamPolicyRequest const& request, Options opts = {}); + + /// + /// Sets the IAM Access control policy for the specified Service. Overwrites + /// any existing policy. + /// + /// @param request + /// @googleapis_link{google::iam::v1::SetIamPolicyRequest,google/iam/v1/iam_policy.proto#L101} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::iam::v1::Policy,google/iam/v1/policy.proto#L96} + /// + /// [google.iam.v1.SetIamPolicyRequest]: + /// @googleapis_reference_link{google/iam/v1/iam_policy.proto#L101} + /// [google.iam.v1.Policy]: + /// @googleapis_reference_link{google/iam/v1/policy.proto#L96} + /// + StatusOr SetIamPolicy( + google::iam::v1::SetIamPolicyRequest const& request, Options opts = {}); + + /// + /// Returns permissions that a caller has on the specified Project. + /// + /// There are no permissions required for making this API call. + /// + /// @param request + /// @googleapis_link{google::iam::v1::TestIamPermissionsRequest,google/iam/v1/iam_policy.proto#L136} + /// @param opts Optional. Override the class-level options, such as retry and + /// backoff policies. + /// @return + /// @googleapis_link{google::iam::v1::TestIamPermissionsResponse,google/iam/v1/iam_policy.proto#L151} + /// + /// [google.iam.v1.TestIamPermissionsRequest]: + /// @googleapis_reference_link{google/iam/v1/iam_policy.proto#L136} + /// [google.iam.v1.TestIamPermissionsResponse]: + /// @googleapis_reference_link{google/iam/v1/iam_policy.proto#L151} + /// + StatusOr TestIamPermissions( + google::iam::v1::TestIamPermissionsRequest const& request, + Options opts = {}); + + private: + std::shared_ptr connection_; + Options options_; +}; + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_SERVICES_CLIENT_H diff --git a/google/cloud/run/services_connection.cc b/google/cloud/run/services_connection.cc new file mode 100644 index 0000000000000..c05fbcdb06909 --- /dev/null +++ b/google/cloud/run/services_connection.cc @@ -0,0 +1,121 @@ +// 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/cloud/run/v2/service.proto + +#include "google/cloud/run/services_connection.h" +#include "google/cloud/run/internal/services_connection_impl.h" +#include "google/cloud/run/internal/services_option_defaults.h" +#include "google/cloud/run/internal/services_stub_factory.h" +#include "google/cloud/run/services_options.h" +#include "google/cloud/background_threads.h" +#include "google/cloud/common_options.h" +#include "google/cloud/grpc_options.h" +#include "google/cloud/internal/pagination_range.h" +#include + +namespace google { +namespace cloud { +namespace run { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +ServicesConnection::~ServicesConnection() = default; + +future> +ServicesConnection::CreateService( + google::cloud::run::v2::CreateServiceRequest const&) { + return google::cloud::make_ready_future< + StatusOr>( + Status(StatusCode::kUnimplemented, "not implemented")); +} + +StatusOr ServicesConnection::GetService( + google::cloud::run::v2::GetServiceRequest const&) { + return Status(StatusCode::kUnimplemented, "not implemented"); +} + +StreamRange ServicesConnection::ListServices( + google::cloud::run::v2:: + ListServicesRequest) { // NOLINT(performance-unnecessary-value-param) + return google::cloud::internal::MakeUnimplementedPaginationRange< + StreamRange>(); +} + +future> +ServicesConnection::UpdateService( + google::cloud::run::v2::UpdateServiceRequest const&) { + return google::cloud::make_ready_future< + StatusOr>( + Status(StatusCode::kUnimplemented, "not implemented")); +} + +future> +ServicesConnection::DeleteService( + google::cloud::run::v2::DeleteServiceRequest const&) { + return google::cloud::make_ready_future< + StatusOr>( + Status(StatusCode::kUnimplemented, "not implemented")); +} + +StatusOr ServicesConnection::GetIamPolicy( + google::iam::v1::GetIamPolicyRequest const&) { + return Status(StatusCode::kUnimplemented, "not implemented"); +} + +StatusOr ServicesConnection::SetIamPolicy( + google::iam::v1::SetIamPolicyRequest const&) { + return Status(StatusCode::kUnimplemented, "not implemented"); +} + +StatusOr +ServicesConnection::TestIamPermissions( + google::iam::v1::TestIamPermissionsRequest const&) { + return Status(StatusCode::kUnimplemented, "not implemented"); +} + +std::shared_ptr MakeServicesConnection(Options options) { + internal::CheckExpectedOptions(options, __func__); + options = run_internal::ServicesDefaultOptions(std::move(options)); + auto background = internal::MakeBackgroundThreadsFactory(options)(); + auto stub = + run_internal::CreateDefaultServicesStub(background->cq(), options); + return std::make_shared( + std::move(background), std::move(stub), std::move(options)); +} + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run +} // namespace cloud +} // namespace google + +namespace google { +namespace cloud { +namespace run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +std::shared_ptr MakeServicesConnection( + std::shared_ptr stub, Options options) { + options = ServicesDefaultOptions(std::move(options)); + auto background = internal::MakeBackgroundThreadsFactory(options)(); + return std::make_shared( + std::move(background), std::move(stub), std::move(options)); +} + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/run/services_connection.h b/google/cloud/run/services_connection.h new file mode 100644 index 0000000000000..0a49484a93445 --- /dev/null +++ b/google/cloud/run/services_connection.h @@ -0,0 +1,137 @@ +// 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/cloud/run/v2/service.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_SERVICES_CONNECTION_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_SERVICES_CONNECTION_H + +#include "google/cloud/run/internal/services_retry_traits.h" +#include "google/cloud/run/internal/services_stub.h" +#include "google/cloud/run/services_connection_idempotency_policy.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 run { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +using ServicesRetryPolicy = ::google::cloud::internal::TraitBasedRetryPolicy< + run_internal::ServicesRetryTraits>; + +using ServicesLimitedTimeRetryPolicy = + ::google::cloud::internal::LimitedTimeRetryPolicy< + run_internal::ServicesRetryTraits>; + +using ServicesLimitedErrorCountRetryPolicy = + ::google::cloud::internal::LimitedErrorCountRetryPolicy< + run_internal::ServicesRetryTraits>; + +/** + * The `ServicesConnection` object for `ServicesClient`. + * + * This interface defines virtual methods for each of the user-facing overload + * sets in `ServicesClient`. This allows users to inject custom behavior + * (e.g., with a Google Mock object) when writing tests that use objects of type + * `ServicesClient`. + * + * To create a concrete instance, see `MakeServicesConnection()`. + * + * For mocking, see `run_mocks::MockServicesConnection`. + */ +class ServicesConnection { + public: + virtual ~ServicesConnection() = 0; + + virtual Options options() { return Options{}; } + + virtual future> CreateService( + google::cloud::run::v2::CreateServiceRequest const& request); + + virtual StatusOr GetService( + google::cloud::run::v2::GetServiceRequest const& request); + + virtual StreamRange ListServices( + google::cloud::run::v2::ListServicesRequest request); + + virtual future> UpdateService( + google::cloud::run::v2::UpdateServiceRequest const& request); + + virtual future> DeleteService( + google::cloud::run::v2::DeleteServiceRequest const& request); + + virtual StatusOr GetIamPolicy( + google::iam::v1::GetIamPolicyRequest const& request); + + virtual StatusOr SetIamPolicy( + google::iam::v1::SetIamPolicyRequest const& request); + + virtual StatusOr + TestIamPermissions(google::iam::v1::TestIamPermissionsRequest const& request); +}; + +/** + * A factory function to construct an object of type `ServicesConnection`. + * + * The returned connection object should not be used directly; instead it + * should be passed as an argument to the constructor of ServicesClient, + * and that class used instead. + * + * The optional @p opts argument may be used to configure aspects of the + * returned `ServicesConnection`. Expected options are any of the types in + * the following option lists: + * + * - `google::cloud::CommonOptionList` + * - `google::cloud::GrpcOptionList` + * - `google::cloud::run::ServicesPolicyOptionList` + * + * @note Unrecognized options will be ignored. To debug issues with options set + * `GOOGLE_CLOUD_CPP_ENABLE_CLOG=yes` in the environment and unexpected + * options will be logged. + * + * @param options (optional) Configure the `ServicesConnection` created by + * this function. + */ +std::shared_ptr MakeServicesConnection( + Options options = {}); + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run +} // namespace cloud +} // namespace google + +namespace google { +namespace cloud { +namespace run_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +std::shared_ptr MakeServicesConnection( + std::shared_ptr stub, Options options); + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_SERVICES_CONNECTION_H diff --git a/google/cloud/run/services_connection_idempotency_policy.cc b/google/cloud/run/services_connection_idempotency_policy.cc new file mode 100644 index 0000000000000..08396aed6d83c --- /dev/null +++ b/google/cloud/run/services_connection_idempotency_policy.cc @@ -0,0 +1,95 @@ +// 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/cloud/run/v2/service.proto + +#include "google/cloud/run/services_connection_idempotency_policy.h" +#include "absl/memory/memory.h" +#include + +namespace google { +namespace cloud { +namespace run { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +using ::google::cloud::Idempotency; + +ServicesConnectionIdempotencyPolicy::~ServicesConnectionIdempotencyPolicy() = + default; + +namespace { +class DefaultServicesConnectionIdempotencyPolicy + : public ServicesConnectionIdempotencyPolicy { + public: + ~DefaultServicesConnectionIdempotencyPolicy() override = default; + + /// Create a new copy of this object. + std::unique_ptr clone() const override { + return absl::make_unique(*this); + } + + Idempotency CreateService( + google::cloud::run::v2::CreateServiceRequest const&) override { + return Idempotency::kNonIdempotent; + } + + Idempotency GetService( + google::cloud::run::v2::GetServiceRequest const&) override { + return Idempotency::kIdempotent; + } + + Idempotency ListServices( + google::cloud::run::v2::ListServicesRequest) override { + return Idempotency::kIdempotent; + } + + Idempotency UpdateService( + google::cloud::run::v2::UpdateServiceRequest const&) override { + return Idempotency::kNonIdempotent; + } + + Idempotency DeleteService( + google::cloud::run::v2::DeleteServiceRequest const&) override { + return Idempotency::kNonIdempotent; + } + + Idempotency GetIamPolicy( + google::iam::v1::GetIamPolicyRequest const&) override { + return Idempotency::kIdempotent; + } + + Idempotency SetIamPolicy( + google::iam::v1::SetIamPolicyRequest const& request) override { + return request.policy().etag().empty() ? Idempotency::kNonIdempotent + : Idempotency::kIdempotent; + } + + Idempotency TestIamPermissions( + google::iam::v1::TestIamPermissionsRequest const&) override { + return Idempotency::kNonIdempotent; + } +}; +} // namespace + +std::unique_ptr +MakeDefaultServicesConnectionIdempotencyPolicy() { + return absl::make_unique(); +} + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run +} // namespace cloud +} // namespace google diff --git a/google/cloud/run/services_connection_idempotency_policy.h b/google/cloud/run/services_connection_idempotency_policy.h new file mode 100644 index 0000000000000..1956c1f6c3b19 --- /dev/null +++ b/google/cloud/run/services_connection_idempotency_policy.h @@ -0,0 +1,74 @@ +// 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/cloud/run/v2/service.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_SERVICES_CONNECTION_IDEMPOTENCY_POLICY_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_SERVICES_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 run { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +class ServicesConnectionIdempotencyPolicy { + public: + virtual ~ServicesConnectionIdempotencyPolicy() = 0; + + /// Create a new copy of this object. + virtual std::unique_ptr clone() + const = 0; + + virtual google::cloud::Idempotency CreateService( + google::cloud::run::v2::CreateServiceRequest const& request) = 0; + + virtual google::cloud::Idempotency GetService( + google::cloud::run::v2::GetServiceRequest const& request) = 0; + + virtual google::cloud::Idempotency ListServices( + google::cloud::run::v2::ListServicesRequest request) = 0; + + virtual google::cloud::Idempotency UpdateService( + google::cloud::run::v2::UpdateServiceRequest const& request) = 0; + + virtual google::cloud::Idempotency DeleteService( + google::cloud::run::v2::DeleteServiceRequest const& request) = 0; + + virtual google::cloud::Idempotency GetIamPolicy( + google::iam::v1::GetIamPolicyRequest const& request) = 0; + + virtual google::cloud::Idempotency SetIamPolicy( + google::iam::v1::SetIamPolicyRequest const& request) = 0; + + virtual google::cloud::Idempotency TestIamPermissions( + google::iam::v1::TestIamPermissionsRequest const& request) = 0; +}; + +std::unique_ptr +MakeDefaultServicesConnectionIdempotencyPolicy(); + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_SERVICES_CONNECTION_IDEMPOTENCY_POLICY_H diff --git a/google/cloud/run/services_options.h b/google/cloud/run/services_options.h new file mode 100644 index 0000000000000..31bb29e4ed214 --- /dev/null +++ b/google/cloud/run/services_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/cloud/run/v2/service.proto + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_SERVICES_OPTIONS_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_SERVICES_OPTIONS_H + +#include "google/cloud/run/services_connection.h" +#include "google/cloud/run/services_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 run { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +/// Option to use with `google::cloud::Options`. +struct ServicesRetryPolicyOption { + using Type = std::shared_ptr; +}; + +/// Option to use with `google::cloud::Options`. +struct ServicesBackoffPolicyOption { + using Type = std::shared_ptr; +}; + +/// Option to use with `google::cloud::Options`. +struct ServicesPollingPolicyOption { + using Type = std::shared_ptr; +}; + +/// Option to use with `google::cloud::Options`. +struct ServicesConnectionIdempotencyPolicyOption { + using Type = std::shared_ptr; +}; + +using ServicesPolicyOptionList = + OptionList; + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace run +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_RUN_SERVICES_OPTIONS_H