update only flattens one level deep

This commit is contained in:
mbecker20
2024-06-22 23:56:01 -07:00
parent 5c4e6a6dbb
commit 645382856a
3 changed files with 27 additions and 3 deletions

View File

@@ -1,13 +1,14 @@
use std::time::Duration;
use anyhow::{anyhow, Context};
use mongo_indexed::Document;
use monitor_client::entities::{
permission::{Permission, PermissionLevel, UserTarget},
server::Server,
update::ResourceTarget,
user::User,
};
use mungos::mongodb::bson::doc;
use mungos::mongodb::bson::{doc, Bson};
use periphery_client::PeripheryClient;
use rand::{thread_rng, Rng};
@@ -117,3 +118,23 @@ pub async fn create_permission<T>(
error!("failed to create permission for {target:?} | {e:#}");
};
}
/// Flattens a document only one level deep
///
/// eg `{ config: { label: "yes", thing: { field1: "ok", field2: "ok" } } }` ->
/// `{ "config.label": "yes", "config.thing": { field1: "ok", field2: "ok" } }`
pub fn flatten_document(doc: Document) -> Document {
let mut target = Document::new();
for (outer_field, bson) in doc {
if let Bson::Document(doc) = bson {
for (inner_field, bson) in doc {
target.insert(format!("{outer_field}.{inner_field}"), bson);
}
} else {
target.insert(outer_field, bson);
}
}
target
}

View File

@@ -31,7 +31,7 @@ use serror::serialize_error_pretty;
use crate::{
config::core_config,
helpers::{
create_permission,
create_permission, flatten_document,
query::{
get_resource_ids_for_non_admin, get_tag,
get_user_permission_on_resource, id_or_name_filter,
@@ -422,10 +422,12 @@ pub async fn update<T: MonitorResource>(
let config_doc = T::update_document(resource, config)
.context("failed to serialize config to bson document")?;
let update_doc = flatten_document(doc! { "config": config_doc });
update_one_by_id(
T::coll().await,
&id,
mungos::update::Update::FlattenSet(doc! { "config": config_doc }),
doc! { "$set": update_doc },
None,
)
.await

View File

@@ -588,6 +588,7 @@ const REGISTRY_TYPES: Types.ImageRegistry["type"][] = [
"None",
"DockerHub",
"Ghcr",
"AwsEcr"
];
const RegistryTypeSelector = ({