diff --git a/src/handlers/playbook.rs b/src/handlers/playbook.rs index 116e003..8166209 100644 --- a/src/handlers/playbook.rs +++ b/src/handlers/playbook.rs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +use amp_common::sync::Synchronization; use std::sync::Arc; use axum::extract::{Path, Query, State}; @@ -22,7 +23,7 @@ use uuid::Uuid; use super::Result; use crate::context::Context; -use crate::requests::playbook::{CreatePlaybookRequest, GetPlaybookRequest, UpdatePlaybookRequest}; +use crate::requests::playbook::{CreatePlaybookRequest, GetPlaybookRequest}; use crate::services::playbook::PlaybookService; // The Playbooks Service Handlers. @@ -68,6 +69,8 @@ pub async fn detail( ) -> Result { Ok(Json(PlaybookService::get(ctx, params.id, params.reference, params.path).await?)) } + +/// Get file tree #[utoipa::path( get, path = "/v1/playbooks/:id/files/trees/:reference/:path?recursive=true | false", params( @@ -96,7 +99,7 @@ pub async fn trees( ("id" = Uuid, description = "The id of playbook"), ), request_body( - content = inline(UpdatePlaybookRequest), + content = inline(Synchronization), description = "Update playbook request", content_type = "application/json" ), @@ -109,9 +112,10 @@ pub async fn trees( pub async fn update( Path(id): Path, State(ctx): State>, - Json(req): Json, + Json(req): Json, ) -> Result { - Ok(Json(PlaybookService::update(ctx, id, &req).await?)) + PlaybookService::update(ctx, id, req).await?; + Ok(StatusCode::NO_CONTENT) } /// Delete a playbook @@ -147,3 +151,21 @@ pub async fn start(Path(id): Path, State(ctx): State>) -> Res PlaybookService::start(ctx, id).await?; Ok(StatusCode::NO_CONTENT) } + +/// get a playbook logs + +#[utoipa::path( + get, path = "/v1/playbooks/{id}/logs", + params( + ("id" = Uuid, description = "The id of playbook"), + ), + responses( + (status = 200, description = "Playbook logs found successfully"), + (status = 404, description = "Playbook not found") + ), + tag = "Playbooks" +)] +pub async fn logs(Path(id): Path, State(ctx): State>) -> Result { + PlaybookService::logs(ctx, id).await; + Ok(StatusCode::OK) +} diff --git a/src/routes.rs b/src/routes.rs index 778ccaf..12ef47c 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -14,7 +14,7 @@ use std::sync::Arc; -use axum::routing::{delete, get, patch, post}; +use axum::routing::{delete, get, post, put}; use axum::Router; use crate::context::Context; @@ -25,7 +25,7 @@ pub fn build() -> Router> { // playbooks .route("/v1/playbooks", post(handlers::playbook::create)) .route("/v1/playbooks/:id/files/:reference/:path", get(handlers::playbook::detail)) - .route("/v1/playbooks/:id", patch(handlers::playbook::update)) + .route("/v1/playbooks/:id", put(handlers::playbook::update)) .route("/v1/playbooks/:id", delete(handlers::playbook::delete)) .route("/v1/playbooks/:id/files/trees/:reference/:path", get(handlers::playbook::trees)) .route("/v1/playbooks/:id/actions/start", get(handlers::playbook::start)) diff --git a/src/services/playbook.rs b/src/services/playbook.rs index fb3492c..fa65a1f 100644 --- a/src/services/playbook.rs +++ b/src/services/playbook.rs @@ -16,13 +16,15 @@ use amp_client::playbooks::PlaybookPayload; use amp_common::resource::PlaybookSpec; use amp_common::scm::content::Content; use amp_common::scm::git::Tree; +use amp_common::sync::Synchronization; use std::sync::Arc; +use tracing::info; use uuid::Uuid; use crate::context::Context; use crate::errors::ApiError; -use crate::requests::playbook::{CreatePlaybookRequest, UpdatePlaybookRequest}; +use crate::requests::playbook::CreatePlaybookRequest; use crate::services::Result; pub struct PlaybookService; @@ -37,15 +39,27 @@ impl PlaybookService { } pub async fn trees(ctx: Arc, id: Uuid, reference: String, recursive: Option) -> Result { - let result = ctx.client.playbooks().get(&id.to_string()).map_err(ApiError::NotFoundPlaybook); - let spec = result.unwrap_or_default(); - let repo = spec.preface.repository.unwrap_or_default().repo; - let tree = - ctx.github_client.git().git_trees(repo.as_str(), reference.as_str(), recursive).ok().unwrap_or_default(); - Ok(tree.unwrap_or_default()) + let playbook_result = ctx.client.playbooks().get(&id.to_string()).map_err(ApiError::NotFoundPlaybook).ok(); + return match playbook_result { + Some(playbook) => { + let repo = playbook.preface.repository.unwrap_or_default().repo; + let tree = ctx + .github_client + .git() + .git_trees(repo.as_str(), reference.as_str(), recursive) + .ok() + .unwrap_or_default(); + Ok(tree.unwrap_or_default()) + } + None => { + info!("Not found playbooks in {}...", id); + Ok(Tree::default()) + } + }; } pub async fn delete(ctx: Arc, id: Uuid) -> Result { + info!("delete playbooks in {}...", id); ctx.client.playbooks().delete(&id.to_string()).map_err(ApiError::NotFoundPlaybook) } @@ -58,11 +72,28 @@ impl PlaybookService { ctx.client.playbooks().create(playbook_payload).map_err(ApiError::NotFoundPlaybook) } - pub async fn update(_ctx: Arc, _id: Uuid, _req: &UpdatePlaybookRequest) -> Result { - unimplemented!() + pub async fn update(ctx: Arc, id: Uuid, req: Synchronization) -> Result { + let playbook_result = ctx.client.playbooks().get(&id.to_string()).map_err(ApiError::NotFoundPlaybook).ok(); + return match playbook_result { + Some(playbook) => { + info!("update playbooks in {}...", id); + let uuid_str: &str = &id.to_string(); + let name = playbook.preface.name.as_str(); + ctx.client.actors().sync(uuid_str, name, req).map_err(ApiError::NotFoundPlaybook) + } + None => { + info!("Not found playbooks in {}...", id); + Ok(0) + } + }; } pub async fn start(ctx: Arc, id: Uuid) -> Result { + info!("Start playbooks in {}...", id); ctx.client.playbooks().start(&id.to_string()).map_err(ApiError::NotFoundPlaybook) } + + pub async fn logs(_ctx: Arc, _id: Uuid) { + unreachable!() + } } diff --git a/src/swagger.rs b/src/swagger.rs index 028fb40..00692d2 100644 --- a/src/swagger.rs +++ b/src/swagger.rs @@ -26,6 +26,7 @@ use crate::{handlers, requests, responses}; handlers::playbook::delete, handlers::playbook::trees, handlers::playbook::start, + handlers::playbook::logs, ), components( schemas( @@ -53,6 +54,8 @@ use crate::{handlers, requests, responses}; amp_common::scm::content::Content, amp_common::scm::git::Tree, amp_common::scm::git::TreeEntry, + + amp_common::sync::Synchronization, ) ), tags(