Skip to content

Commit

Permalink
Implement update
Browse files Browse the repository at this point in the history
  • Loading branch information
jiahao6635 committed Jan 16, 2024
1 parent feee5e3 commit 34732b1
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 15 deletions.
30 changes: 26 additions & 4 deletions src/handlers/playbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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.
Expand Down Expand Up @@ -68,6 +69,8 @@ pub async fn detail(
) -> Result<impl IntoResponse> {
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(
Expand Down Expand Up @@ -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"
),
Expand All @@ -109,9 +112,10 @@ pub async fn trees(
pub async fn update(
Path(id): Path<Uuid>,
State(ctx): State<Arc<Context>>,
Json(req): Json<UpdatePlaybookRequest>,
Json(req): Json<Synchronization>,
) -> Result<impl IntoResponse> {
Ok(Json(PlaybookService::update(ctx, id, &req).await?))
PlaybookService::update(ctx, id, req).await?;
Ok(StatusCode::NO_CONTENT)
}

/// Delete a playbook
Expand Down Expand Up @@ -147,3 +151,21 @@ pub async fn start(Path(id): Path<Uuid>, State(ctx): State<Arc<Context>>) -> 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<Uuid>, State(ctx): State<Arc<Context>>) -> Result<impl IntoResponse> {
PlaybookService::logs(ctx, id).await;
Ok(StatusCode::OK)
}
4 changes: 2 additions & 2 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,7 +25,7 @@ pub fn build() -> Router<Arc<Context>> {
// 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))
Expand Down
49 changes: 40 additions & 9 deletions src/services/playbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,15 +39,27 @@ impl PlaybookService {
}

pub async fn trees(ctx: Arc<Context>, id: Uuid, reference: String, recursive: Option<bool>) -> Result<Tree> {
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<Context>, id: Uuid) -> Result<u16> {
info!("delete playbooks in {}...", id);
ctx.client.playbooks().delete(&id.to_string()).map_err(ApiError::NotFoundPlaybook)
}

Expand All @@ -58,11 +72,28 @@ impl PlaybookService {
ctx.client.playbooks().create(playbook_payload).map_err(ApiError::NotFoundPlaybook)
}

pub async fn update(_ctx: Arc<Context>, _id: Uuid, _req: &UpdatePlaybookRequest) -> Result<PlaybookSpec> {
unimplemented!()
pub async fn update(ctx: Arc<Context>, id: Uuid, req: Synchronization) -> Result<u16> {
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<Context>, id: Uuid) -> Result<u16> {
info!("Start playbooks in {}...", id);
ctx.client.playbooks().start(&id.to_string()).map_err(ApiError::NotFoundPlaybook)
}

pub async fn logs(_ctx: Arc<Context>, _id: Uuid) {
unreachable!()
}
}
3 changes: 3 additions & 0 deletions src/swagger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::{handlers, requests, responses};
handlers::playbook::delete,
handlers::playbook::trees,
handlers::playbook::start,
handlers::playbook::logs,
),
components(
schemas(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 34732b1

Please sign in to comment.