1 //
2 // Copyright (C) 2019 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 <chrono>
19 #include <functional>
20 #include <memory>
21 #include <ostream>
22 #include <string>
23 #include <variant>
24 
25 #include "credential_source.h"
26 #include "curl_wrapper.h"
27 
28 namespace cuttlefish {
29 
30 class Artifact {
31   std::string name;
32   size_t size;
33   unsigned long last_modified_time;
34   std::string md5;
35   std::string content_type;
36   std::string revision;
37   unsigned long creation_time;
38   unsigned int crc32;
39 public:
40   Artifact(const Json::Value&);
Artifact(const std::string & name)41   Artifact(const std::string& name) : name(name) {}
42 
Name()43   const std::string& Name() const { return name; }
Size()44   size_t Size() const { return size; }
LastModifiedTime()45   unsigned long LastModifiedTime() const { return last_modified_time; }
Md5()46   const std::string& Md5() const { return md5; }
ContentType()47   const std::string& ContentType() const { return content_type; }
Revision()48   const std::string& Revision() const { return revision; }
CreationTime()49   unsigned long CreationTime() const { return creation_time; }
Crc32()50   unsigned int Crc32() const { return crc32; }
51 };
52 
53 struct DeviceBuild {
DeviceBuildDeviceBuild54   DeviceBuild(const std::string& id, const std::string& target) {
55     this->id = id;
56     this->target = target;
57   }
58 
59   std::string id;
60   std::string target;
61   std::string product;
62 };
63 
64 std::ostream& operator<<(std::ostream&, const DeviceBuild&);
65 
66 struct DirectoryBuild {
67   // TODO(schuffelen): Support local builds other than "eng"
68   DirectoryBuild(const std::vector<std::string>& paths,
69                  const std::string& target);
70 
71   std::vector<std::string> paths;
72   std::string target;
73   std::string id;
74   std::string product;
75 };
76 
77 std::ostream& operator<<(std::ostream&, const DirectoryBuild&);
78 
79 using Build = std::variant<DeviceBuild, DirectoryBuild>;
80 
81 std::ostream& operator<<(std::ostream&, const Build&);
82 
83 class BuildApi {
84   CurlWrapper curl;
85   std::unique_ptr<CredentialSource> credential_source;
86 
87   std::vector<std::string> Headers();
88 public:
89   BuildApi(std::unique_ptr<CredentialSource> credential_source);
90   ~BuildApi() = default;
91 
92   std::string LatestBuildId(const std::string& branch,
93                             const std::string& target);
94 
95   std::string BuildStatus(const DeviceBuild&);
96 
97   std::string ProductName(const DeviceBuild&);
98 
99   std::vector<Artifact> Artifacts(const DeviceBuild&);
100 
101   bool ArtifactToFile(const DeviceBuild& build, const std::string& artifact,
102                       const std::string& path);
103 
104   std::vector<Artifact> Artifacts(const DirectoryBuild&);
105 
106   bool ArtifactToFile(const DirectoryBuild& build, const std::string& artifact,
107                       const std::string& path);
108 
Artifacts(const Build & build)109   std::vector<Artifact> Artifacts(const Build& build) {
110     return std::visit([this](auto&& arg) { return Artifacts(arg); }, build);
111   }
112 
ArtifactToFile(const Build & build,const std::string & artifact,const std::string & path)113   bool ArtifactToFile(const Build& build, const std::string& artifact,
114                       const std::string& path) {
115     return std::visit([this, &artifact, &path](auto&& arg) {
116       return ArtifactToFile(arg, artifact, path);
117     }, build);
118   }
119 };
120 
121 Build ArgumentToBuild(BuildApi* api, const std::string& arg,
122                       const std::string& default_build_target,
123                       const std::chrono::seconds& retry_period);
124 
125 } // namespace cuttlefish
126