Browse Source

Read a hostname in a loop

changes/91/636791/1
James E. Blair 3 months ago
parent
commit
5105fe9e96
1 changed files with 47 additions and 14 deletions
  1. 47
    14
      zuul-preview/main.cc

+ 47
- 14
zuul-preview/main.cc View File

@@ -23,21 +23,54 @@
23 23
 
24 24
 using namespace std;
25 25
 
26
+vector<string> split(string in);
27
+
28
+vector<string> split(string in)
29
+{
30
+  istringstream stream(in);
31
+  vector<string> parts;
32
+  string part;
33
+  while (getline(stream, part, '.')) {
34
+    parts.push_back(part);
35
+  }
36
+  return parts;
37
+}
38
+
26 39
 int main(int, char**)
27 40
 {
28 41
   web::http::client::http_client client("https://zuul.opendev.org");
29
-  auto uri = web::uri_builder("/api/tenant/openstack/build");
30
-  uri.append_path("75031cad206c4014ad7a3387091d15ab");
31
-  auto response = client.request(
32
-    web::http::methods::GET, uri.to_string()).get();
33
-  // body is a web::json::value
34
-  auto body = response.extract_json().get();
35
-  cout << response.status_code() << endl;
36
-  cout << body.serialize() << endl;
37
-  // body["log_url"] returns a web::json::value
38
-  cout << "The log url is " << body["log_url"] << endl;
39
-  // body["log_url"].as_string() returns a const std::string&
40
-  cout << "The log url is " << body["log_url"].as_string() << endl;
41
-  // body.at("log_url").as_string() returns a const std::string&
42
-  cout << "The log url is " << body.at("log_url").as_string() << endl;
42
+
43
+  string hostname;
44
+  while (getline(cin, hostname)) {
45
+    // Expected hostname:
46
+    // site.75031cad206c4014ad7a3387091d15ab.openstack.preview.opendev.org
47
+    // Apache will drop "preview.opendev.org", so our expected input will be:
48
+    // site.75031cad206c4014ad7a3387091d15ab.openstack
49
+
50
+    auto parts = split(hostname);
51
+    if (parts.size() < 3) {
52
+      cout << "not enough args" << endl;
53
+      continue;
54
+    }
55
+    auto artifact = parts[0];
56
+    auto buildid = parts[1];
57
+    auto tenant = parts[2];
58
+    cout << artifact << endl
59
+         << buildid << endl
60
+         << tenant << endl;
61
+
62
+     // 75031cad206c4014ad7a3387091d15ab
63
+    auto uri = web::uri_builder("/api/tenant/" + tenant + "/build");
64
+    uri.append_path(buildid);
65
+    auto response = client.request(
66
+        web::http::methods::GET, uri.to_string()).get();
67
+    // body is a web::json::value
68
+    auto body = response.extract_json().get();
69
+    cout << response.status_code() << endl;
70
+    cout << body.serialize() << endl;
71
+
72
+    // TODO: use artifact
73
+    // body["log_url"].as_string() returns a const std::string&
74
+    cout << body["log_url"].as_string() << endl;
75
+  }
43 76
 }

Loading…
Cancel
Save