1 // 2 // Copyright (C) 2022 The Android Open Source Project 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 #pragma once 17 18 #include <future> 19 #include <optional> 20 #include <string> 21 22 #include <android-base/result.h> 23 #include <json/json.h> 24 25 #include "host/libs/web/credential_source.h" 26 #include "host/libs/web/curl_wrapper.h" 27 28 namespace cuttlefish { 29 30 class GceInstanceDisk { 31 public: 32 GceInstanceDisk() = default; 33 explicit GceInstanceDisk(const Json::Value&); 34 35 static GceInstanceDisk EphemeralBootDisk(); 36 37 std::optional<std::string> Name() const; 38 GceInstanceDisk& Name(const std::string&) &; 39 GceInstanceDisk Name(const std::string&) &&; 40 41 std::optional<std::string> SourceImage() const; 42 GceInstanceDisk& SourceImage(const std::string&) &; 43 GceInstanceDisk SourceImage(const std::string&) &&; 44 45 GceInstanceDisk& SizeGb(uint64_t gb) &; 46 GceInstanceDisk SizeGb(uint64_t gb) &&; 47 48 const Json::Value& AsJson() const; 49 50 private: 51 Json::Value data_; 52 }; 53 54 class GceNetworkInterface { 55 public: 56 GceNetworkInterface() = default; 57 explicit GceNetworkInterface(const Json::Value&); 58 59 static GceNetworkInterface Default(); 60 61 std::optional<std::string> ExternalIp() const; 62 std::optional<std::string> InternalIp() const; 63 64 const Json::Value& AsJson() const; 65 66 private: 67 Json::Value data_; 68 }; 69 70 class GceInstanceInfo { 71 public: 72 GceInstanceInfo() = default; 73 explicit GceInstanceInfo(const Json::Value&); 74 75 std::optional<std::string> Zone() const; 76 GceInstanceInfo& Zone(const std::string&) &; 77 GceInstanceInfo Zone(const std::string&) &&; 78 79 std::optional<std::string> Name() const; 80 GceInstanceInfo& Name(const std::string&) &; 81 GceInstanceInfo Name(const std::string&) &&; 82 83 std::optional<std::string> MachineType() const; 84 GceInstanceInfo& MachineType(const std::string&) &; 85 GceInstanceInfo MachineType(const std::string&) &&; 86 87 GceInstanceInfo& AddDisk(const GceInstanceDisk&) &; 88 GceInstanceInfo AddDisk(const GceInstanceDisk&) &&; 89 90 GceInstanceInfo& AddNetworkInterface(const GceNetworkInterface&) &; 91 GceInstanceInfo AddNetworkInterface(const GceNetworkInterface&) &&; 92 std::vector<GceNetworkInterface> NetworkInterfaces() const; 93 94 GceInstanceInfo& AddMetadata(const std::string&, const std::string&) &; 95 GceInstanceInfo AddMetadata(const std::string&, const std::string&) &&; 96 97 GceInstanceInfo& AddScope(const std::string&) &; 98 GceInstanceInfo AddScope(const std::string&) &&; 99 100 const Json::Value& AsJson() const; 101 102 private: 103 Json::Value data_; 104 }; 105 106 class GceApi { 107 public: 108 class Operation { 109 public: 110 ~Operation(); 111 void StopWaiting(); 112 /// `true` means it waited to completion, `false` means it was cancelled 113 std::future<android::base::Result<bool>>& Future(); 114 115 private: 116 class Impl; 117 std::unique_ptr<Impl> impl_; 118 Operation(std::unique_ptr<Impl>); 119 friend class GceApi; 120 }; 121 122 GceApi(CurlWrapper&, CredentialSource& credentials, 123 const std::string& project); 124 125 std::future<android::base::Result<GceInstanceInfo>> Get( 126 const GceInstanceInfo&); 127 std::future<android::base::Result<GceInstanceInfo>> Get( 128 const std::string& zone, const std::string& name); 129 130 Operation Insert(const Json::Value&); 131 Operation Insert(const GceInstanceInfo&); 132 133 Operation Reset(const std::string& zone, const std::string& name); 134 Operation Reset(const GceInstanceInfo&); 135 136 Operation Delete(const std::string& zone, const std::string& name); 137 Operation Delete(const GceInstanceInfo&); 138 139 private: 140 std::vector<std::string> Headers(); 141 142 CurlWrapper& curl_; 143 CredentialSource& credentials_; 144 std::string project_; 145 }; 146 147 } // namespace cuttlefish 148