update description api

This commit is contained in:
mbecker20
2023-08-04 04:24:31 -04:00
parent 804a215190
commit 5eea95d118
11 changed files with 947 additions and 868 deletions

View File

@@ -128,7 +128,7 @@ impl Resolve<GetUpdate, RequestUser> for State {
return Ok(update);
}
match &update.target {
ResourceTarget::System => {
ResourceTarget::System(_) => {
return Err(anyhow!("user must be admin to view system updates"))
}
ResourceTarget::Server(id) => {

View File

@@ -0,0 +1,57 @@
use anyhow::anyhow;
use async_trait::async_trait;
use monitor_types::{
entities::{
alerter::Alerter, build::Build, builder::Builder, deployment::Deployment, repo::Repo,
server::Server, update::ResourceTarget,
},
requests::write::{UpdateDescription, UpdateDescriptionResponse},
};
use resolver_api::Resolve;
use crate::{auth::RequestUser, resource::Resource, state::State};
#[async_trait]
impl Resolve<UpdateDescription, RequestUser> for State {
async fn resolve(
&self,
UpdateDescription {
target,
description,
}: UpdateDescription,
user: RequestUser,
) -> anyhow::Result<UpdateDescriptionResponse> {
match target {
ResourceTarget::System(_) => {
return Err(anyhow!(
"cannot update description of System resource target"
))
}
ResourceTarget::Server(id) => {
<State as Resource<Server>>::update_description(self, &id, &description, &user)
.await?;
}
ResourceTarget::Deployment(id) => {
<State as Resource<Deployment>>::update_description(self, &id, &description, &user)
.await?;
}
ResourceTarget::Build(id) => {
<State as Resource<Build>>::update_description(self, &id, &description, &user)
.await?;
}
ResourceTarget::Repo(id) => {
<State as Resource<Repo>>::update_description(self, &id, &description, &user)
.await?;
}
ResourceTarget::Builder(id) => {
<State as Resource<Builder>>::update_description(self, &id, &description, &user)
.await?;
}
ResourceTarget::Alerter(id) => {
<State as Resource<Alerter>>::update_description(self, &id, &description, &user)
.await?;
}
}
Ok(UpdateDescriptionResponse {})
}
}

View File

@@ -19,6 +19,7 @@ mod alerter;
mod build;
mod builder;
mod deployment;
mod description;
mod permissions;
mod repo;
mod secret;
@@ -44,6 +45,9 @@ enum WriteRequest {
UpdateUserPerimissions(UpdateUserPermissions),
UpdateUserPermissionsOnTarget(UpdateUserPermissionsOnTarget),
// ==== DESCRIPTION ====
UpdateDescription(UpdateDescription),
// ==== SERVER ====
CreateServer(CreateServer),
DeleteServer(DeleteServer),

View File

@@ -57,7 +57,7 @@ impl Resolve<UpdateUserPermissions, RequestUser> for State {
.await?;
let end_ts = monitor_timestamp();
let mut update = Update {
target: ResourceTarget::System,
target: ResourceTarget::System("System".to_string()),
operation: Operation::UpdateUserPermissions,
logs: vec![Log::simple(
"modify user enabled",
@@ -111,7 +111,7 @@ impl Resolve<UpdateUserPermissionsOnTarget, RequestUser> for State {
return Err(anyhow!("user not enabled"));
}
let log_text = match &target {
ResourceTarget::System => return Err(anyhow!("target can not be system")),
ResourceTarget::System(_) => return Err(anyhow!("target can not be system")),
ResourceTarget::Build(id) => {
let build = self
.db

View File

@@ -118,6 +118,20 @@ pub trait Resource<T: Indexed + Send + Unpin + Permissioned> {
Ok(list)
}
async fn update_description(
&self,
id: &str,
description: &str,
user: &RequestUser,
) -> anyhow::Result<()> {
self.get_resource_check_permissions(id, user, PermissionLevel::Update)
.await?;
self.coll()
.update_one(id, mungos::Update::Set(doc! { "description": description }))
.await?;
Ok(())
}
}
#[async_trait]

View File

@@ -194,7 +194,7 @@ impl State {
ResourceTargetVariant::Alerter,
)
}
ResourceTarget::System => {
ResourceTarget::System(_) => {
return Err(anyhow!("user not admin, can't recieve system updates"))
}
};