• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "tensorflow/core/platform/cloud/compute_engine_zone_provider.h"
17 
18 #include <utility>
19 #include "tensorflow/core/lib/strings/str_util.h"
20 namespace tensorflow {
21 
22 namespace {
23 constexpr char kGceMetadataZonePath[] = "instance/zone";
24 }  // namespace
25 
ComputeEngineZoneProvider(std::shared_ptr<ComputeEngineMetadataClient> google_metadata_client)26 ComputeEngineZoneProvider::ComputeEngineZoneProvider(
27     std::shared_ptr<ComputeEngineMetadataClient> google_metadata_client)
28     : google_metadata_client_(std::move(google_metadata_client)) {}
29 
GetZone(string * zone)30 Status ComputeEngineZoneProvider::GetZone(string* zone) {
31   if (!cached_zone.empty()) {
32     *zone = cached_zone;
33     return Status::OK();
34   }
35   std::vector<char> response_buffer;
36   TF_RETURN_IF_ERROR(google_metadata_client_->GetMetadata(kGceMetadataZonePath,
37                                                           &response_buffer));
38   StringPiece location(&response_buffer[0], response_buffer.size());
39 
40   std::vector<string> elems = str_util::Split(location, "/");
41   if (elems.size() == 4) {
42     cached_zone = elems.back();
43     *zone = cached_zone;
44   } else {
45     LOG(ERROR) << "Failed to parse the zone name from location: "
46                << string(location);
47   }
48 
49   return Status::OK();
50 }
~ComputeEngineZoneProvider()51 ComputeEngineZoneProvider::~ComputeEngineZoneProvider() {}
52 
53 }  // namespace tensorflow
54