1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
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     http://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 
16 #ifndef TENSORFLOW_CORE_PLATFORM_CLOUD_COMPUTE_ENGINE_METADATA_CLIENT_H_
17 #define TENSORFLOW_CORE_PLATFORM_CLOUD_COMPUTE_ENGINE_METADATA_CLIENT_H_
18 
19 #include "tensorflow/core/lib/core/status.h"
20 #include "tensorflow/core/platform/cloud/http_request.h"
21 #include "tensorflow/core/platform/cloud/retrying_utils.h"
22 
23 namespace tensorflow {
24 
25 /// \brief A client that accesses to the metadata server running on GCE hosts.
26 ///
27 /// Uses the provided HttpRequest::Factory to make requests to the local
28 /// metadata service
29 /// (https://cloud.google.com/compute/docs/storing-retrieving-metadata).
30 /// Retries on recoverable failures using exponential backoff with the initial
31 /// retry wait configurable via initial_retry_delay_usec.
32 class ComputeEngineMetadataClient {
33  public:
34   explicit ComputeEngineMetadataClient(
35       std::shared_ptr<HttpRequest::Factory> http_request_factory,
36       const RetryConfig& config = RetryConfig(
37           10000,  /* init_delay_time_us = 1 ms */
38           1000000 /* max_delay_time_us = 1 s */
39           ));
~ComputeEngineMetadataClient()40   virtual ~ComputeEngineMetadataClient() {}
41 
42   /// \brief Get the metadata value for a given attribute of the metadata
43   /// service.
44   ///
45   /// Given a metadata path relative
46   /// to http://metadata.google.internal/computeMetadata/v1/,
47   /// fills response_buffer with the metadata. Returns OK if the server returns
48   /// the response for the given metadata path successfully.
49   ///
50   /// Example usage:
51   /// To get the zone of an instance:
52   ///   compute_engine_metadata_client.GetMetadata(
53   ///       "instance/zone", response_buffer);
54   virtual Status GetMetadata(const string& path,
55                              std::vector<char>* response_buffer);
56 
57  private:
58   std::shared_ptr<HttpRequest::Factory> http_request_factory_;
59   const RetryConfig retry_config_;
60 
61   TF_DISALLOW_COPY_AND_ASSIGN(ComputeEngineMetadataClient);
62 };
63 
64 }  // namespace tensorflow
65 
66 #endif  // TENSORFLOW_CORE_PLATFORM_CLOUD_COMPUTE_ENGINE_METADATA_CLIENT_H_
67