Skip to content

Commit 56b4c94

Browse files
authored
impl(GCS+gRPC): determine monitoring project (#14093)
1 parent 992d59d commit 56b4c94

File tree

6 files changed

+132
-0
lines changed

6 files changed

+132
-0
lines changed

google/cloud/storage/google_cloud_cpp_storage_grpc.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ google_cloud_cpp_storage_grpc_hdrs = [
6767
"internal/grpc/hmac_key_metadata_parser.h",
6868
"internal/grpc/hmac_key_request_parser.h",
6969
"internal/grpc/make_cord.h",
70+
"internal/grpc/monitoring_project.h",
7071
"internal/grpc/notification_metadata_parser.h",
7172
"internal/grpc/notification_request_parser.h",
7273
"internal/grpc/object_access_control_parser.h",
@@ -127,6 +128,7 @@ google_cloud_cpp_storage_grpc_srcs = [
127128
"internal/grpc/hmac_key_metadata_parser.cc",
128129
"internal/grpc/hmac_key_request_parser.cc",
129130
"internal/grpc/make_cord.cc",
131+
"internal/grpc/monitoring_project.cc",
130132
"internal/grpc/notification_metadata_parser.cc",
131133
"internal/grpc/notification_request_parser.cc",
132134
"internal/grpc/object_access_control_parser.cc",

google/cloud/storage/google_cloud_cpp_storage_grpc.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ else ()
156156
internal/grpc/hmac_key_request_parser.h
157157
internal/grpc/make_cord.cc
158158
internal/grpc/make_cord.h
159+
internal/grpc/monitoring_project.cc
160+
internal/grpc/monitoring_project.h
159161
internal/grpc/notification_metadata_parser.cc
160162
internal/grpc/notification_metadata_parser.h
161163
internal/grpc/notification_request_parser.cc
@@ -402,6 +404,7 @@ if (BUILD_TESTING AND GOOGLE_CLOUD_CPP_STORAGE_ENABLE_GRPC)
402404
internal/grpc/hmac_key_metadata_parser_test.cc
403405
internal/grpc/hmac_key_request_parser_test.cc
404406
internal/grpc/make_cord_test.cc
407+
internal/grpc/monitoring_project_test.cc
405408
internal/grpc/notification_metadata_parser_test.cc
406409
internal/grpc/notification_request_parser_test.cc
407410
internal/grpc/object_access_control_parser_test.cc
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "google/cloud/storage/internal/grpc/monitoring_project.h"
16+
#include "google/cloud/storage/options.h"
17+
18+
namespace google {
19+
namespace cloud {
20+
namespace storage_internal {
21+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
22+
23+
absl::optional<Project> MonitoringProject(Options const& options) {
24+
if (!options.has<storage::ProjectIdOption>()) return absl::nullopt;
25+
auto project_id = options.get<storage::ProjectIdOption>();
26+
if (project_id.empty()) return absl::nullopt;
27+
return Project(std::move(project_id));
28+
}
29+
30+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
31+
} // namespace storage_internal
32+
} // namespace cloud
33+
} // namespace google
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_GRPC_MONITORING_PROJECT_H
16+
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_GRPC_MONITORING_PROJECT_H
17+
18+
#include "google/cloud/options.h"
19+
#include "google/cloud/project.h"
20+
#include "google/cloud/version.h"
21+
#include "absl/types/optional.h"
22+
23+
namespace google {
24+
namespace cloud {
25+
namespace storage_internal {
26+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
27+
28+
/**
29+
* Returns the monitoring project given the (fully populated) options.
30+
*/
31+
absl::optional<Project> MonitoringProject(Options const& options);
32+
33+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
34+
} // namespace storage_internal
35+
} // namespace cloud
36+
} // namespace google
37+
38+
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_GRPC_MONITORING_PROJECT_H
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "google/cloud/storage/internal/grpc/monitoring_project.h"
16+
#include "google/cloud/storage/internal/grpc/default_options.h"
17+
#include "google/cloud/storage/options.h"
18+
#include "google/cloud/options.h"
19+
#include "google/cloud/project.h"
20+
#include "google/cloud/testing_util/scoped_environment.h"
21+
#include "absl/types/optional.h"
22+
#include <gmock/gmock.h>
23+
24+
namespace google {
25+
namespace cloud {
26+
namespace storage_internal {
27+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
28+
namespace {
29+
30+
using ::google::cloud::testing_util::ScopedEnvironment;
31+
using ::testing::Optional;
32+
33+
TEST(MonitoringProject, Default) {
34+
ScopedEnvironment pr("GOOGLE_CLOUD_PROJECT", absl::nullopt);
35+
auto storage_options = DefaultOptionsGrpc(Options{});
36+
EXPECT_EQ(MonitoringProject(storage_options),
37+
absl::optional<Project>(absl::nullopt));
38+
}
39+
40+
TEST(MonitoringProject, WithExplicitProject) {
41+
// The cases where the project is set in the environment, or in both the
42+
// environment and the application-provided options are already tested. Here
43+
// we (un)set the environment only to prevent flakes.
44+
ScopedEnvironment pr("GOOGLE_CLOUD_PROJECT", absl::nullopt);
45+
auto storage_options = DefaultOptionsGrpc(
46+
Options{}.set<storage::ProjectIdOption>("test-only-project"));
47+
EXPECT_THAT(MonitoringProject(storage_options),
48+
Optional<Project>(Project("test-only-project")));
49+
}
50+
51+
} // namespace
52+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
53+
} // namespace storage_internal
54+
} // namespace cloud
55+
} // namespace google

google/cloud/storage/storage_client_grpc_unit_tests.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ storage_client_grpc_unit_tests = [
5959
"internal/grpc/hmac_key_metadata_parser_test.cc",
6060
"internal/grpc/hmac_key_request_parser_test.cc",
6161
"internal/grpc/make_cord_test.cc",
62+
"internal/grpc/monitoring_project_test.cc",
6263
"internal/grpc/notification_metadata_parser_test.cc",
6364
"internal/grpc/notification_request_parser_test.cc",
6465
"internal/grpc/object_access_control_parser_test.cc",

0 commit comments

Comments
 (0)