mirror of
https://github.com/moghtech/komodo.git
synced 2026-03-11 17:44:19 -05:00
implement resource sync cli
This commit is contained in:
@@ -51,7 +51,7 @@ fn read_resources_recursive(
|
||||
resources.builders.extend(more.builders);
|
||||
resources.alerters.extend(more.alerters);
|
||||
resources.server_templates.extend(more.server_templates);
|
||||
resources.syncs.extend(more.syncs);
|
||||
resources.resource_syncs.extend(more.resource_syncs);
|
||||
resources.user_groups.extend(more.user_groups);
|
||||
resources.variables.extend(more.variables);
|
||||
Ok(())
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
use colored::Colorize;
|
||||
use monitor_client::entities::{
|
||||
alerter::Alerter, build::Build, builder::Builder,
|
||||
self, alerter::Alerter, build::Build, builder::Builder,
|
||||
deployment::Deployment, procedure::Procedure, repo::Repo,
|
||||
server::Server, server_template::ServerTemplate,
|
||||
};
|
||||
use resource::ResourceSync;
|
||||
|
||||
use crate::{helpers::wait_for_enter, state::cli_args};
|
||||
|
||||
@@ -14,6 +13,8 @@ mod resources;
|
||||
mod user_group;
|
||||
mod variables;
|
||||
|
||||
use resource::ResourceSync;
|
||||
|
||||
pub async fn run(path: &str, delete: bool) -> anyhow::Result<()> {
|
||||
info!("resources path: {}", path.blue().bold());
|
||||
if delete {
|
||||
@@ -49,6 +50,14 @@ pub async fn run(path: &str, delete: bool) -> anyhow::Result<()> {
|
||||
resources.server_templates,
|
||||
delete,
|
||||
)?;
|
||||
let (
|
||||
resource_sync_creates,
|
||||
resource_sync_updates,
|
||||
resource_sync_deletes,
|
||||
) = resource::get_updates::<entities::sync::ResourceSync>(
|
||||
resources.resource_syncs,
|
||||
delete,
|
||||
)?;
|
||||
|
||||
let (variable_creates, variable_updates, variable_deletes) =
|
||||
variables::get_updates(resources.variables, delete)?;
|
||||
@@ -56,22 +65,33 @@ pub async fn run(path: &str, delete: bool) -> anyhow::Result<()> {
|
||||
let (user_group_creates, user_group_updates, user_group_deletes) =
|
||||
user_group::get_updates(resources.user_groups, delete).await?;
|
||||
|
||||
if server_template_creates.is_empty()
|
||||
if resource_sync_creates.is_empty()
|
||||
&& resource_sync_updates.is_empty()
|
||||
&& resource_sync_deletes.is_empty()
|
||||
&& server_template_creates.is_empty()
|
||||
&& server_template_updates.is_empty()
|
||||
&& server_template_deletes.is_empty()
|
||||
&& server_creates.is_empty()
|
||||
&& server_updates.is_empty()
|
||||
&& server_deletes.is_empty()
|
||||
&& deployment_creates.is_empty()
|
||||
&& deployment_updates.is_empty()
|
||||
&& deployment_deletes.is_empty()
|
||||
&& build_creates.is_empty()
|
||||
&& build_updates.is_empty()
|
||||
&& build_deletes.is_empty()
|
||||
&& builder_creates.is_empty()
|
||||
&& builder_updates.is_empty()
|
||||
&& builder_deletes.is_empty()
|
||||
&& alerter_creates.is_empty()
|
||||
&& alerter_updates.is_empty()
|
||||
&& alerter_deletes.is_empty()
|
||||
&& repo_creates.is_empty()
|
||||
&& repo_updates.is_empty()
|
||||
&& repo_deletes.is_empty()
|
||||
&& procedure_creates.is_empty()
|
||||
&& procedure_updates.is_empty()
|
||||
&& procedure_deletes.is_empty()
|
||||
&& user_group_creates.is_empty()
|
||||
&& user_group_updates.is_empty()
|
||||
&& user_group_deletes.is_empty()
|
||||
@@ -88,6 +108,12 @@ pub async fn run(path: &str, delete: bool) -> anyhow::Result<()> {
|
||||
}
|
||||
|
||||
// No deps
|
||||
entities::sync::ResourceSync::run_updates(
|
||||
resource_sync_creates,
|
||||
resource_sync_updates,
|
||||
resource_sync_deletes,
|
||||
)
|
||||
.await;
|
||||
ServerTemplate::run_updates(
|
||||
server_template_creates,
|
||||
server_template_updates,
|
||||
|
||||
@@ -6,3 +6,4 @@ mod procedure;
|
||||
mod repo;
|
||||
mod server;
|
||||
mod server_template;
|
||||
mod sync;
|
||||
|
||||
81
bin/cli/src/sync/resources/sync.rs
Normal file
81
bin/cli/src/sync/resources/sync.rs
Normal file
@@ -0,0 +1,81 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use monitor_client::{
|
||||
api::write::{
|
||||
CreateResourceSync, DeleteResourceSync, UpdateResourceSync,
|
||||
},
|
||||
entities::{
|
||||
self,
|
||||
resource::Resource,
|
||||
sync::{
|
||||
PartialResourceSyncConfig, ResourceSyncConfig,
|
||||
ResourceSyncConfigDiff, ResourceSyncInfo,
|
||||
},
|
||||
toml::ResourceToml,
|
||||
update::ResourceTarget,
|
||||
},
|
||||
};
|
||||
use partial_derive2::PartialDiff;
|
||||
|
||||
use crate::{
|
||||
maps::name_to_resource_sync, state::monitor_client,
|
||||
sync::resource::ResourceSync,
|
||||
};
|
||||
|
||||
impl ResourceSync for entities::sync::ResourceSync {
|
||||
type Config = ResourceSyncConfig;
|
||||
type Info = ResourceSyncInfo;
|
||||
type PartialConfig = PartialResourceSyncConfig;
|
||||
type ConfigDiff = ResourceSyncConfigDiff;
|
||||
|
||||
fn display() -> &'static str {
|
||||
"resource sync"
|
||||
}
|
||||
|
||||
fn resource_target(id: String) -> ResourceTarget {
|
||||
ResourceTarget::ResourceSync(id)
|
||||
}
|
||||
|
||||
fn name_to_resource(
|
||||
) -> &'static HashMap<String, Resource<Self::Config, Self::Info>>
|
||||
{
|
||||
name_to_resource_sync()
|
||||
}
|
||||
|
||||
async fn create(
|
||||
resource: ResourceToml<Self::PartialConfig>,
|
||||
) -> anyhow::Result<String> {
|
||||
monitor_client()
|
||||
.write(CreateResourceSync {
|
||||
name: resource.name,
|
||||
config: resource.config,
|
||||
})
|
||||
.await
|
||||
.map(|res| res.id)
|
||||
}
|
||||
|
||||
async fn update(
|
||||
id: String,
|
||||
resource: ResourceToml<Self::PartialConfig>,
|
||||
) -> anyhow::Result<()> {
|
||||
monitor_client()
|
||||
.write(UpdateResourceSync {
|
||||
id,
|
||||
config: resource.config,
|
||||
})
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_diff(
|
||||
original: Self::Config,
|
||||
update: Self::PartialConfig,
|
||||
) -> anyhow::Result<Self::ConfigDiff> {
|
||||
Ok(original.partial_diff(update))
|
||||
}
|
||||
|
||||
async fn delete(id: String) -> anyhow::Result<()> {
|
||||
monitor_client().write(DeleteResourceSync { id }).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -136,7 +136,7 @@ impl Resolve<RunSync, (User, Update)> for State {
|
||||
resource_syncs_to_update,
|
||||
resource_syncs_to_delete,
|
||||
) = get_updates_for_execution::<entities::sync::ResourceSync>(
|
||||
resources.syncs,
|
||||
resources.resource_syncs,
|
||||
sync.config.delete,
|
||||
&all_resources,
|
||||
&id_to_tags,
|
||||
|
||||
@@ -175,7 +175,7 @@ impl Resolve<ExportResourcesToToml, User> for State {
|
||||
PermissionLevel::Read,
|
||||
)
|
||||
.await?;
|
||||
res.syncs.push(convert_resource(sync, &names.tags))
|
||||
res.resource_syncs.push(convert_resource(sync, &names.tags))
|
||||
}
|
||||
ResourceTarget::ServerTemplate(id) => {
|
||||
let template = resource::get_check_permissions::<
|
||||
|
||||
@@ -176,7 +176,7 @@ impl Resolve<RefreshResourceSyncPending, User> for State {
|
||||
resource_sync_updates: get_updates_for_view::<
|
||||
entities::sync::ResourceSync,
|
||||
>(
|
||||
resources.syncs,
|
||||
resources.resource_syncs,
|
||||
sync.config.delete,
|
||||
&all_resources,
|
||||
&id_to_tags,
|
||||
|
||||
@@ -54,7 +54,7 @@ fn read_resources_recursive(
|
||||
resources.builders.extend(more.builders);
|
||||
resources.alerters.extend(more.alerters);
|
||||
resources.server_templates.extend(more.server_templates);
|
||||
resources.syncs.extend(more.syncs);
|
||||
resources.resource_syncs.extend(more.resource_syncs);
|
||||
resources.user_groups.extend(more.user_groups);
|
||||
resources.variables.extend(more.variables);
|
||||
Ok(())
|
||||
|
||||
@@ -72,10 +72,10 @@ pub struct ResourcesToml {
|
||||
|
||||
#[serde(
|
||||
default,
|
||||
rename = "sync",
|
||||
rename = "resource_sync",
|
||||
skip_serializing_if = "Vec::is_empty"
|
||||
)]
|
||||
pub syncs: Vec<ResourceToml<PartialResourceSyncConfig>>,
|
||||
pub resource_syncs: Vec<ResourceToml<PartialResourceSyncConfig>>,
|
||||
|
||||
#[serde(
|
||||
default,
|
||||
|
||||
Reference in New Issue
Block a user