• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2023 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef GRPC_SRC_CORE_UTIL_GCP_METADATA_QUERY_H
18 #define GRPC_SRC_CORE_UTIL_GCP_METADATA_QUERY_H
19 
20 #include <grpc/support/port_platform.h>
21 
22 #include <string>
23 
24 #include "absl/functional/any_invocable.h"
25 #include "absl/status/statusor.h"
26 #include "src/core/lib/iomgr/closure.h"
27 #include "src/core/lib/iomgr/error.h"
28 #include "src/core/lib/iomgr/polling_entity.h"
29 #include "src/core/util/http_client/httpcli.h"
30 #include "src/core/util/http_client/parser.h"
31 #include "src/core/util/orphanable.h"
32 #include "src/core/util/time.h"
33 
34 namespace grpc_core {
35 
36 // Fetches the value of an attribute from the MetadataServer on a GCP
37 // environment.
38 class GcpMetadataQuery : public InternallyRefCounted<GcpMetadataQuery> {
39  public:
40   static constexpr const char kZoneAttribute[] =
41       "/computeMetadata/v1/instance/zone";
42   static constexpr const char kClusterNameAttribute[] =
43       "/computeMetadata/v1/instance/attributes/cluster-name";
44   static constexpr const char kRegionAttribute[] =
45       "/computeMetadata/v1/instance/region";
46   static constexpr const char kInstanceIdAttribute[] =
47       "/computeMetadata/v1/instance/id";
48   static constexpr const char kIPv6Attribute[] =
49       "/computeMetadata/v1/instance/network-interfaces/0/ipv6s";
50 
51   GcpMetadataQuery(
52       std::string attribute, grpc_polling_entity* pollent,
53       absl::AnyInvocable<void(std::string /* attribute */,
54                               absl::StatusOr<std::string> /* result */)>
55           callback,
56       Duration timeout);
57 
58   // Alternative ctor allows overriding the metadata server address, mainly
59   // to inject fakes in tests
60   GcpMetadataQuery(
61       std::string metadata_server_name, std::string attribute,
62       grpc_polling_entity* pollent,
63       absl::AnyInvocable<void(std::string /* attribute */,
64                               absl::StatusOr<std::string> /* result */)>
65           callback,
66       Duration timeout);
67 
68   ~GcpMetadataQuery() override;
69 
70   void Orphan() override;
71 
72  private:
73   static void OnDone(void* arg, grpc_error_handle error);
74 
75   grpc_closure on_done_;
76   std::string attribute_;
77   absl::AnyInvocable<void(std::string /* attribute */,
78                           absl::StatusOr<std::string> /* result */)>
79       callback_;
80   OrphanablePtr<HttpRequest> http_request_;
81   grpc_http_response response_;
82 };
83 
84 }  // namespace grpc_core
85 
86 #endif  // GRPC_SRC_CORE_UTIL_GCP_METADATA_QUERY_H
87