129 lines
4.0 KiB
C++
129 lines
4.0 KiB
C++
#include "google/runtime/engine.h"
|
|||
|
|
|
||
|
|
#include "google/errors.h"
|
||
|
|
#include "google/runtime/caps.h"
|
||
|
|
|
||
|
|
namespace molo::google::runtime {
|
||
|
|
|
||
|
|
using clients::body::Json;
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
|
||
|
|
void check_text(const std::string& s, std::size_t max, const char* what) {
|
||
|
|
if (s.size() > max) throw GoogleError::invalid(std::string(what) + " too long");
|
||
|
|
}
|
||
|
|
|
||
|
|
void check_labels(const std::map<std::string, std::string>& labels) {
|
||
|
|
if (labels.size() > RuntimeCaps::kMaxLabels)
|
||
|
|
throw GoogleError::invalid("too many labels");
|
||
|
|
for (const auto& [k, v] : labels) {
|
||
|
|
if (k.empty() || k.size() > RuntimeCaps::kMaxLabelChars ||
|
||
|
|
v.size() > RuntimeCaps::kMaxLabelChars)
|
||
|
|
throw GoogleError::invalid("bad label");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Json labels_json(const std::map<std::string, std::string>& labels) {
|
||
|
|
Json o = Json::object();
|
||
|
|
for (const auto& [k, v] : labels) o.set(k, Json(v));
|
||
|
|
return o;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string str(const Json& doc, const char* key) {
|
||
|
|
const Json* v = doc.find(key);
|
||
|
|
return v && v->is_string() ? v->as_string() : "";
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace
|
||
|
|
|
||
|
|
std::string Engine::id() const {
|
||
|
|
auto slash = name.rfind('/');
|
||
|
|
return slash == std::string::npos ? name : name.substr(slash + 1);
|
||
|
|
}
|
||
|
|
|
||
|
|
Engine Engine::from_json(const Json& doc) {
|
||
|
|
Engine e;
|
||
|
|
e.raw = doc;
|
||
|
|
if (!doc.is_object()) return e;
|
||
|
|
e.name = str(doc, "name");
|
||
|
|
e.display_name = str(doc, "displayName");
|
||
|
|
e.description = str(doc, "description");
|
||
|
|
e.etag = str(doc, "etag");
|
||
|
|
e.create_time = str(doc, "createTime");
|
||
|
|
e.update_time = str(doc, "updateTime");
|
||
|
|
if (const Json* l = doc.find("labels"); l && l->is_object())
|
||
|
|
for (const auto& [k, v] : l->as_object())
|
||
|
|
if (v.is_string()) e.labels[k] = v.as_string();
|
||
|
|
if (const Json* s = doc.find("spec")) e.spec = EngineSpec::from_json(*s);
|
||
|
|
if (const Json* k = doc.find("encryptionSpec")) e.kms_key_name = str(*k, "kmsKeyName");
|
||
|
|
return e;
|
||
|
|
}
|
||
|
|
|
||
|
|
void EngineDraft::validate() const {
|
||
|
|
if (display_name.empty()) throw GoogleError::invalid("displayName is required");
|
||
|
|
check_text(display_name, RuntimeCaps::kMaxDisplayName, "displayName");
|
||
|
|
check_text(description, RuntimeCaps::kMaxDescription, "description");
|
||
|
|
check_labels(labels);
|
||
|
|
check_text(kms_key_name, 512, "kmsKeyName");
|
||
|
|
spec.validate();
|
||
|
|
}
|
||
|
|
|
||
|
|
Json EngineDraft::to_json() const {
|
||
|
|
validate();
|
||
|
|
Json o = Json::object();
|
||
|
|
o.set("displayName", Json(display_name));
|
||
|
|
if (!description.empty()) o.set("description", Json(description));
|
||
|
|
if (!labels.empty()) o.set("labels", labels_json(labels));
|
||
|
|
Json s = spec.to_json();
|
||
|
|
if (!s.as_object().empty()) o.set("spec", s);
|
||
|
|
if (!kms_key_name.empty()) {
|
||
|
|
Json k = Json::object();
|
||
|
|
k.set("kmsKeyName", Json(kms_key_name));
|
||
|
|
o.set("encryptionSpec", k);
|
||
|
|
}
|
||
|
|
return o;
|
||
|
|
}
|
||
|
|
|
||
|
|
void EngineUpdate::validate() const {
|
||
|
|
if (!display_name && !description && !labels && !spec)
|
||
|
|
throw GoogleError::invalid("nothing to update");
|
||
|
|
if (display_name) {
|
||
|
|
if (display_name->empty()) throw GoogleError::invalid("displayName must not be empty");
|
||
|
|
check_text(*display_name, RuntimeCaps::kMaxDisplayName, "displayName");
|
||
|
|
}
|
||
|
|
if (description) check_text(*description, RuntimeCaps::kMaxDescription, "description");
|
||
|
|
if (labels) check_labels(*labels);
|
||
|
|
if (spec) {
|
||
|
|
spec->validate();
|
||
|
|
if (spec->mask().empty()) throw GoogleError::invalid("spec has no fields to update");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Json EngineUpdate::to_json() const {
|
||
|
|
validate();
|
||
|
|
Json o = Json::object();
|
||
|
|
if (display_name) o.set("displayName", Json(*display_name));
|
||
|
|
if (description) o.set("description", Json(*description));
|
||
|
|
if (labels) o.set("labels", labels_json(*labels));
|
||
|
|
if (spec) o.set("spec", spec->to_json());
|
||
|
|
return o;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string EngineUpdate::update_mask() const {
|
||
|
|
validate();
|
||
|
|
std::string m;
|
||
|
|
auto add = [&](const std::string& part) {
|
||
|
|
if (!m.empty()) m += ",";
|
||
|
|
m += part;
|
||
|
|
};
|
||
|
|
if (display_name) add("displayName");
|
||
|
|
if (description) add("description");
|
||
|
|
if (labels) add("labels");
|
||
|
|
if (spec)
|
||
|
|
for (const auto& p : spec->mask()) add(p);
|
||
|
|
if (m.size() > RuntimeCaps::kMaxMaskChars) throw GoogleError::invalid("updateMask too long");
|
||
|
|
return m;
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace molo::google::runtime
|