You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@trafficserver.apache.org by GitBox <gi...@apache.org> on 2021/03/01 20:05:48 UTC

[GitHub] [trafficserver] ywkaras commented on a change in pull request #7478: JSONRPC based interface for administrative API.

ywkaras commented on a change in pull request #7478:
URL: https://github.com/apache/trafficserver/pull/7478#discussion_r585013281



##########
File path: src/traffic_ctl_jsonrpc/CtrlPrinters.cc
##########
@@ -0,0 +1,318 @@
+/**
+  @section license License
+
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+*/
+#include <iostream>
+#include <unordered_map>
+
+#include "CtrlPrinters.h"
+#include "jsonrpc/yaml_codecs.h"
+#include "tscpp/util/ts_meta.h"
+#include <tscore/BufferWriter.h>
+#include "PrintUtils.h"
+
+//------------------------------------------------------------------------------------------------------------------------------------
+
+namespace
+{
+void
+print_record_error_list(std::vector<RecordLookUpResponse::RecordError> const &errors)
+{
+  if (errors.size()) {
+    std::cout << "------------ Errors ----------\n";
+    auto iter = std::begin(errors);
+    if (iter != std::end(errors)) {
+      std::cout << *iter;
+    }
+    ++iter;
+    for (auto err = iter; err != std::end(errors); ++err) {
+      std::cout << "--\n";
+      std::cout << *err;
+    }
+  }
+}
+} // namespace
+void
+BasePrinter::write_output(specs::JSONRPCResponse const &response)
+{
+  if (response.is_error() && _format == Format::PRETTY) {
+    std::cout << response.error.as<specs::JSONRPCError>();
+    return;
+  }
+  if (!response.result.IsNull()) {
+    write_output(response.result);
+  }
+}
+
+//------------------------------------------------------------------------------------------------------------------------------------
+void
+RecordPrinter::write_output(YAML::Node const &result)
+{
+  auto response = result.as<RecordLookUpResponse>();
+  if (is_format_legacy()) {
+    write_output_legacy(response);
+  } else {
+    write_output_pretty(response);
+  }
+}
+void
+RecordPrinter::write_output_legacy(RecordLookUpResponse const &response)
+{
+  std::string text;
+  for (auto &&recordInfo : response.recordList) {
+    if (!_printAsRecords) {
+      std::cout << ts::bwprint(text, "{}: {}\n", recordInfo.name, recordInfo.currentValue);
+    } else {
+      std::cout << ts::bwprint(text, "{} {} {} {} # default: {}\n", rec_labelof(recordInfo.rclass), recordInfo.name,
+                               recordInfo.dataType, recordInfo.currentValue, recordInfo.defaultValue);
+    }
+  }
+}
+void
+RecordPrinter::write_output_pretty(RecordLookUpResponse const &response)
+{
+  write_output_legacy(response);
+  print_record_error_list(response.errorList);
+}
+//------------------------------------------------------------------------------------------------------------------------------------
+void
+MetricRecordPrinter::write_output(YAML::Node const &result)
+{
+  std::string text;
+  auto response = result.as<RecordLookUpResponse>();
+  for (auto &&recordInfo : response.recordList) {
+    std::cout << ts::bwprint(text, "{} {}\n", recordInfo.name, recordInfo.currentValue);
+  }
+}
+//------------------------------------------------------------------------------------------------------------------------------------
+
+void
+DiffConfigPrinter::write_output(YAML::Node const &result)
+{
+  std::string text;
+  auto response = result.as<RecordLookUpResponse>();
+  for (auto &&recordInfo : response.recordList) {
+    auto const &currentValue = recordInfo.currentValue;
+    auto const &defaultValue = recordInfo.defaultValue;
+    const bool hasChanged    = (currentValue != defaultValue);
+    if (hasChanged) {
+      if (!_printAsRecords) {
+        std::cout << ts::bwprint(text, "{} has changed\n", recordInfo.name);
+        std::cout << ts::bwprint(text, "\tCurrent Value: {}\n", currentValue);
+        std::cout << ts::bwprint(text, "\tDefault Value: {}\n", defaultValue);
+      } else {
+        std::cout << ts::bwprint(text, "{} {} {} {} # default: {}\n", rec_labelof(recordInfo.rclass), recordInfo.name,
+                                 recordInfo.dataType, recordInfo.currentValue, recordInfo.defaultValue);
+      }
+    }
+  }
+}
+//------------------------------------------------------------------------------------------------------------------------------------
+
+//------------------------------------------------------------------------------------------------------------------------------------
+void
+ConfigReloadPrinter::write_output(YAML::Node const &result)
+{
+}
+//------------------------------------------------------------------------------------------------------------------------------------
+void
+RecordDescribePrinter::write_output(YAML::Node const &result)
+{
+  auto const &response = result.as<RecordLookUpResponse>();
+  if (is_format_legacy()) {
+    write_output_legacy(response);
+  } else {
+    write_output_pretty(response);
+  }
+}
+
+void
+RecordDescribePrinter::write_output_legacy(RecordLookUpResponse const &response)
+{
+  std::string text;
+  for (auto &&recordInfo : response.recordList) {
+    std::cout << ts::bwprint(text, "{:16s}: {}\n", "Name", recordInfo.name);
+    std::cout << ts::bwprint(text, "{:16s}: {}\n", "Current Value ", recordInfo.currentValue);
+    std::cout << ts::bwprint(text, "{:16s}: {}\n", "Default Value ", recordInfo.defaultValue);
+    std::cout << ts::bwprint(text, "{:16s}: {}\n", "Record Type ", rec_labelof(recordInfo.rclass));
+    std::cout << ts::bwprint(text, "{:16s}: {}\n", "Data Type ", recordInfo.dataType);
+
+    std::visit(ts::meta::overloaded{
+                 [&](RecordLookUpResponse::RecordParamInfo::ConfigMeta const &meta) {
+                   std::cout << ts::bwprint(text, "{:16s}: {}\n", "Access Control ", rec_accessof(meta.accessType));
+                   std::cout << ts::bwprint(text, "{:16s}: {}\n", "Update Type ", rec_updateof(meta.updateType));
+                   std::cout << ts::bwprint(text, "{:16s}: {}\n", "Update Status ", meta.updateStatus);
+                   std::cout << ts::bwprint(text, "{:16s}: {}\n", "Source ", rec_sourceof(meta.source));
+
+                   std::cout << ts::bwprint(text, "{:16s}: {}\n", "Syntax Check ", meta.checkExpr);
+                 },
+                 [&](RecordLookUpResponse::RecordParamInfo::StatMeta const &meta) {
+                   // This may not be what we want, as for a metric we may not need to print all the same info. In that case
+                   // just create a new printer for this.
+                   std::cout << ts::bwprint(text, "{:16s}: {}\n", "Persist Type ", meta.persistType);
+                 },
+               },
+               recordInfo.meta);
+
+    std::cout << ts::bwprint(text, "{:16s}: {}\n", "Overridable", (recordInfo.overridable ? "yes" : "no"));
+    std::cout << ts::bwprint(text, "{:16s}: {}\n", "Version ", recordInfo.version);
+    std::cout << ts::bwprint(text, "{:16s}: {}\n", "Order ", recordInfo.order);
+    std::cout << ts::bwprint(text, "{:16s}: {}\n", "Raw Stat Block ", recordInfo.rsb);
+  }
+}
+
+void
+RecordDescribePrinter::write_output_pretty(RecordLookUpResponse const &response)
+{
+  std::string text;
+
+  write_output_legacy(response);
+  print_record_error_list(response.errorList);
+}
+//------------------------------------------------------------------------------------------------------------------------------------
+void
+GetHostStatusPrinter::write_output(YAML::Node const &result)
+{
+  auto response = result.as<RecordLookUpResponse>();
+  std::string text;
+  for (auto &&recordInfo : response.recordList) {
+    std::cout << ts::bwprint(text, "{} {}\n", recordInfo.name, recordInfo.currentValue);
+  }
+  for (auto &&e : response.errorList) {
+    std::cout << ts::bwprint(text, "Failed  to fetch {}\n", e.recordName);
+  }
+}
+
+//------------------------------------------------------------------------------------------------------------------------------------
+void
+SetHostStatusPrinter::write_output(YAML::Node const &result)
+{
+  // do nothing.
+}
+//------------------------------------------------------------------------------------------------------------------------------------
+
+void
+CacheDiskStoragePrinter::write_output(YAML::Node const &result)
+{
+  // do nothing.
+  if (!is_format_legacy()) {
+    write_output_pretty(result);
+  }
+}
+void
+CacheDiskStoragePrinter::write_output_pretty(YAML::Node const &result)
+{
+  std::string text;
+  auto my_print = [&text](auto const &disk) {
+    std::cout << ts::bwprint(text, "Device: {}\n", disk.path);

Review comment:
       Why not just:
   ```
   std::cout << "Device: " << disk.path << '\n';
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org