• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <json/json.h>
23 
24 #include "common/libs/utils/result.h"
25 #include "host/libs/web/credential_source.h"
26 #include "host/libs/web/http_client/http_client.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> Network() const;
62   GceNetworkInterface& Network(const std::string&) &;
63   GceNetworkInterface Network(const std::string&) &&;
64 
65   std::optional<std::string> Subnetwork() const;
66   GceNetworkInterface& Subnetwork(const std::string&) &;
67   GceNetworkInterface Subnetwork(const std::string&) &&;
68 
69   std::optional<std::string> ExternalIp() const;
70   std::optional<std::string> InternalIp() const;
71 
72   const Json::Value& AsJson() const;
73 
74  private:
75   Json::Value data_;
76 };
77 
78 class GceInstanceInfo {
79  public:
80   GceInstanceInfo() = default;
81   explicit GceInstanceInfo(const Json::Value&);
82 
83   std::optional<std::string> Zone() const;
84   GceInstanceInfo& Zone(const std::string&) &;
85   GceInstanceInfo Zone(const std::string&) &&;
86 
87   std::optional<std::string> Name() const;
88   GceInstanceInfo& Name(const std::string&) &;
89   GceInstanceInfo Name(const std::string&) &&;
90 
91   std::optional<std::string> MachineType() const;
92   GceInstanceInfo& MachineType(const std::string&) &;
93   GceInstanceInfo MachineType(const std::string&) &&;
94 
95   GceInstanceInfo& AddDisk(const GceInstanceDisk&) &;
96   GceInstanceInfo AddDisk(const GceInstanceDisk&) &&;
97 
98   GceInstanceInfo& AddNetworkInterface(const GceNetworkInterface&) &;
99   GceInstanceInfo AddNetworkInterface(const GceNetworkInterface&) &&;
100   std::vector<GceNetworkInterface> NetworkInterfaces() const;
101 
102   GceInstanceInfo& AddMetadata(const std::string&, const std::string&) &;
103   GceInstanceInfo AddMetadata(const std::string&, const std::string&) &&;
104 
105   GceInstanceInfo& AddScope(const std::string&) &;
106   GceInstanceInfo AddScope(const std::string&) &&;
107 
108   const Json::Value& AsJson() const;
109 
110  private:
111   Json::Value data_;
112 };
113 
114 class GceApi {
115  public:
116   class Operation {
117    public:
118     ~Operation();
119     void StopWaiting();
120     /// `true` means it waited to completion, `false` means it was cancelled
121     std::future<Result<bool>>& Future();
122 
123    private:
124     class Impl;
125     std::unique_ptr<Impl> impl_;
126     Operation(std::unique_ptr<Impl>);
127     friend class GceApi;
128   };
129 
130   GceApi(HttpClient&, CredentialSource& credentials,
131          const std::string& project);
132 
133   std::future<Result<GceInstanceInfo>> Get(const GceInstanceInfo&);
134   std::future<Result<GceInstanceInfo>> Get(const std::string& zone,
135                                            const std::string& name);
136 
137   Operation Insert(const Json::Value&);
138   Operation Insert(const GceInstanceInfo&);
139 
140   Operation Reset(const std::string& zone, const std::string& name);
141   Operation Reset(const GceInstanceInfo&);
142 
143   Operation Delete(const std::string& zone, const std::string& name);
144   Operation Delete(const GceInstanceInfo&);
145 
146  private:
147   Result<std::vector<std::string>> Headers();
148 
149   HttpClient& http_client_;
150   CredentialSource& credentials_;
151   std::string project_;
152 };
153 
154 }  // namespace cuttlefish
155