handle update config / description / tags seperately

This commit is contained in:
mbecker20
2024-05-06 03:10:57 -07:00
parent ef43cb7920
commit 943bb4c61a
2 changed files with 97 additions and 37 deletions

View File

@@ -22,10 +22,17 @@ pub mod repo;
pub mod server;
pub mod server_template;
type ToUpdate<T> = Vec<(String, ResourceToml<T>)>;
type ToUpdate<T> = Vec<ToUpdateItem<T>>;
type ToCreate<T> = Vec<ResourceToml<T>>;
type UpdatesResult<T> = (ToCreate<T>, ToUpdate<T>);
pub struct ToUpdateItem<T> {
pub id: String,
pub resource: ResourceToml<T>,
pub update_description: bool,
pub update_tags: bool,
}
pub trait ResourceSync {
type PartialConfig: std::fmt::Debug
+ Clone
@@ -92,13 +99,21 @@ pub trait ResourceSync {
})
.collect::<Vec<_>>();
let update = ToUpdateItem {
id,
update_description: resource.description
!= original.description,
update_tags: resource.tags != original_tags,
resource,
};
// Only try to update if there are any fields to update,
// or a change to tags / description
if !resource.config.is_none()
|| resource.description != original.description
|| resource.tags != original_tags
if !update.resource.config.is_none()
|| update.update_description
|| update.update_tags
{
to_update.push((id, resource));
to_update.push(update);
}
}
None => {
@@ -140,18 +155,21 @@ pub trait ResourceSync {
"\n{}s {}: {:#?}",
Self::display(),
"TO UPDATE".blue(),
to_update.iter().map(|(_, item)| item.name.as_str())
to_update
.iter()
.map(|update| update.resource.name.as_str())
.collect::<Vec<_>>()
);
} else {
println!(
"\n{}",
to_update
.iter()
.map(|(_, r)| format!(
"{}: {}: {}: {r:#?}",
.map(|ToUpdateItem { resource, .. }| format!(
"{}: {}: {}: {resource:#?}",
"UPDATE".blue(),
Self::display(),
r.name.bold().green(),
resource.name.bold().green(),
))
.collect::<Vec<_>>()
.join("\n\n")
@@ -183,20 +201,42 @@ pub trait ResourceSync {
}
};
Self::update_tags(id.clone(), &name, tags).await;
Self::update_description(id, description).await;
Self::update_description(id, &name, description).await;
info!("{} {name} created", Self::display());
}
for (id, resource) in to_update {
for ToUpdateItem {
id,
resource,
update_description,
update_tags,
} in to_update
{
// Update resource
let name = resource.name.clone();
let tags = resource.tags.clone();
let description = resource.description.clone();
if let Err(e) = Self::update(id.clone(), resource).await {
warn!("failed to update {} {name} | {e:#}", Self::display());
if update_description {
Self::update_description(id.clone(), &name, description)
.await;
}
Self::update_tags(id.clone(), &name, tags).await;
Self::update_description(id, description).await;
if update_tags {
Self::update_tags(id.clone(), &name, tags).await;
}
if !resource.config.is_none() {
if let Err(e) = Self::update(id, resource).await {
warn!(
"failed to update config on {} {name} | {e:#}",
Self::display()
);
} else {
info!("updated {} {name} config", Self::display());
}
}
info!("{} {name} updated", Self::display());
}
@@ -208,27 +248,29 @@ pub trait ResourceSync {
}
}
async fn update_tags(
resource_id: String,
resource_name: &str,
tags: Vec<String>,
) {
async fn update_tags(id: String, name: &str, tags: Vec<String>) {
// Update tags
if let Err(e) = monitor_client()
.write(UpdateTagsOnResource {
target: Self::resource_target(resource_id),
target: Self::resource_target(id),
tags,
})
.await
{
warn!(
"failed to update tags on {} {resource_name} | {e:#}",
"failed to update tags on {} {name} | {e:#}",
Self::display(),
);
} else {
info!("updated {} {name} tags", Self::display());
}
}
async fn update_description(id: String, description: String) {
async fn update_description(
id: String,
name: &str,
description: String,
) {
if let Err(e) = monitor_client()
.write(UpdateDescription {
target: Self::resource_target(id.clone()),
@@ -237,6 +279,8 @@ pub trait ResourceSync {
.await
{
warn!("failed to update resource {id} description | {e:#}");
} else {
info!("updated {} {name} description", Self::display());
}
}
}

View File

@@ -16,7 +16,7 @@ use monitor_client::{
update::ResourceTarget,
},
};
use partial_derive2::PartialDiff;
use partial_derive2::{MaybeNone, PartialDiff};
use crate::{
maps::{
@@ -24,6 +24,7 @@ use crate::{
id_to_server, name_to_procedure,
},
monitor_client,
sync::resources::ToUpdateItem,
};
use super::{ResourceSync, ToCreate, ToUpdate};
@@ -83,28 +84,43 @@ impl ResourceSync for Procedure {
for i in 0..10 {
let mut to_pull = Vec::new();
for (id, resource) in &to_update {
for ToUpdateItem {
id,
resource,
update_description,
update_tags,
} in &to_update
{
// Update resource
let name = resource.name.clone();
let tags = resource.tags.clone();
let description = resource.description.clone();
if let Err(e) =
Self::update(id.clone(), resource.clone()).await
{
if i == 9 {
warn!(
"failed to update {} {name} | {e:#}",
Self::display()
);
if *update_description {
Self::update_description(id.clone(), &name, description)
.await;
}
if *update_tags {
Self::update_tags(id.clone(), &name, tags).await;
}
if !resource.config.is_none() {
if let Err(e) =
Self::update(id.clone(), resource.clone()).await
{
if i == 9 {
warn!(
"failed to update {} {name} | {e:#}",
Self::display()
);
}
}
}
Self::update_tags(id.clone(), &name, tags).await;
Self::update_description(id.clone(), description).await;
info!("{} {name} updated", Self::display());
// have to clone out so to_update is mutable
to_pull.push(id.clone());
}
to_update.retain(|(id, _)| !to_pull.contains(id));
//
to_update.retain(|resource| !to_pull.contains(&resource.id));
let mut to_pull = Vec::new();
for resource in &to_create {
@@ -124,7 +140,7 @@ impl ResourceSync for Procedure {
}
};
Self::update_tags(id.clone(), &name, tags).await;
Self::update_description(id, description).await;
Self::update_description(id, &name, description).await;
info!("{} {name} created", Self::display());
to_pull.push(name);
}