#include "google/registry/agent_gateway.h" #include "google/errors.h" #include "google/registry/caps.h" #include "google/registry/ids.h" namespace molo::google::registry { using clients::body::Json; namespace { void check_ref(const std::string& v, const char* what) { if (v.empty()) throw GoogleError::invalid(std::string(what) + " must not be empty"); Ids::require_text(v, 512, what); if (v.find("..") != std::string::npos || v.find(' ') != std::string::npos) throw GoogleError::invalid(std::string(what) + " is malformed"); } } // namespace Json GatewaySpec::to_json() const { Ids::require_text(description, RegistryCaps::kMaxDescription, "description"); if (labels.size() > RegistryCaps::kMaxLabels) throw GoogleError::invalid("too many labels"); if (registries.size() > RegistryCaps::kMaxRegistries) throw GoogleError::invalid("too many registries"); if (resource_uris.size() > RegistryCaps::kMaxResourceUris) throw GoogleError::invalid("too many resource uris"); if (dns_domains.size() > RegistryCaps::kMaxDnsDomains) throw GoogleError::invalid("too many dns domains"); Json j = Json::object(); if (!description.empty()) j.set("description", Json(description)); if (!labels.empty()) { Json l = Json::object(); for (const auto& kv : labels) { if (kv.first.empty()) throw GoogleError::invalid("label key is empty"); Ids::require_text(kv.first, RegistryCaps::kMaxLabelChars, "label key"); Ids::require_text(kv.second, RegistryCaps::kMaxLabelChars, "label value"); l.set(kv.first, Json(kv.second)); } j.set("labels", l); } if (!registries.empty()) { Json a = Json::array(); for (const auto& r : registries) { check_ref(r, "registry"); a.push(Json(r)); } j.set("registries", a); } if (!agent_connectivity_template.empty()) { check_ref(agent_connectivity_template, "agentConnectivityTemplate"); j.set("agentConnectivityTemplate", Json(agent_connectivity_template)); } if (mode == GatewayMode::kGoogleManaged) { if (!resource_uris.empty()) throw GoogleError::invalid("resource uris belong to selfManaged"); Json g = Json::object(); g.set("governedAccessPath", Json(access_path == GovernedAccessPath::kAgentToAnywhere ? "AGENT_TO_ANYWHERE" : "CLIENT_TO_AGENT")); j.set("googleManaged", g); } else { Json s = Json::object(); Json a = Json::array(); for (const auto& u : resource_uris) { check_ref(u, "resource uri"); a.push(Json(u)); } s.set("resourceUris", a); j.set("selfManaged", s); } if (!network_attachment.empty() || !dns_domains.empty()) { Json n = Json::object(); if (!network_attachment.empty()) { check_ref(network_attachment, "network attachment"); Json e = Json::object(); e.set("networkAttachment", Json(network_attachment)); n.set("egress", e); } if (!dns_domains.empty()) { Json d = Json::object(); Json a = Json::array(); for (const auto& dom : dns_domains) { check_ref(dom, "dns domain"); a.push(Json(dom)); } d.set("domains", a); if (!dns_target_project.empty()) d.set("targetProject", Json(dns_target_project)); if (!dns_target_network.empty()) d.set("targetNetwork", Json(dns_target_network)); n.set("dnsPeeringConfig", d); } j.set("networkConfig", n); } return j; } ServiceEndpoint AgentGateway::default_endpoint() { return endpoints::custom("networkservices", "https://networkservices.googleapis.com/v1"); } AgentGateway::AgentGateway(std::shared_ptr client, CloudContext context) : RegistryService(std::move(client), default_endpoint(), std::move(context)) {} Json AgentGateway::list() const { return runner_.list("networkservices.v1.agentGateways.list", "agentGateways", with("parent", context_.parent())); } Json AgentGateway::get(const std::string& id) const { return runner_.invoke("networkservices.v1.agentGateways.get", with("name", context_.child("agentGateways", id))); } Json AgentGateway::create(const std::string& id, const GatewaySpec& spec) const { Ids::require_segment(id, "gateway id"); CallArgs a = with("parent", context_.parent()); a.body = spec.to_json(); a.query["agentGatewayId"] = id; if (exists([&] { return get(id); })) throw GoogleError(409, "ALREADY_EXISTS", "gateway " + id + " already exists"); runner_.mutate("networkservices.v1.agentGateways.create", a); return get(id); } Json AgentGateway::update(const std::string& id, const GatewaySpec& spec, const std::vector& mask) const { Ids::require_segment(id, "gateway id"); CallArgs a = with("name", context_.child("agentGateways", id)); a.body = spec.to_json(); a.query["updateMask"] = CallRunner::update_mask("networkservices.v1.agentGateways.patch", mask); get(id); runner_.mutate("networkservices.v1.agentGateways.patch", a); return get(id); } Json AgentGateway::remove_gateway(const std::string& id, const std::string& etag) const { Ids::require_segment(id, "gateway id"); Ids::require_text(etag, 256, "etag"); get(id); CallArgs a = with("name", context_.child("agentGateways", id)); put_query(a, "etag", etag); return runner_.mutate("networkservices.v1.agentGateways.delete", a); } Json AgentGateway::operation(const std::string& operation_id) const { Ids::require_segment(operation_id, "operation id"); return runner_.invoke("networkservices.v1.operations.get", with("name", context_.parent() + "/operations/" + operation_id)); } } // namespace molo::google::registry