Skip to content

Commit

Permalink
Merge pull request #35 from jiahao6635/master
Browse files Browse the repository at this point in the history
Implement file query,create,delete,start
  • Loading branch information
wangeguo authored Jan 16, 2024
2 parents b911493 + feee5e3 commit ceb66a7
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 85 deletions.
84 changes: 23 additions & 61 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ name = "playground"
path = "src/lib.rs"

[dependencies]
amp-client = { git = "https://github.com/amphitheatre-app/amp-client-rust", tag = "v0.6.0" }
amp-common = { git = "https://github.com/amphitheatre-app/common", tag = "v0.6.0" }
amp-client = { git = "https://github.com/amphitheatre-app/amp-client-rust", tag = "v0.7.2" }
amp-common = { git = "https://github.com/amphitheatre-app/common", tag = "v0.7.2" }
anyhow = "1.0"
axum = { version = "0.7.3" }
axum = { version = "0.7.4" }
clap = { version = "4.4.12", features = ["derive", "env"] }
dotenv = "0.15.0"
futures = "0.3"
Expand All @@ -29,11 +29,11 @@ thiserror = "1.0.56"
tokio = { version = "1.35.1", features = ["full"] }
tokio-stream = "0.1"
tower = { version = "0.4.13", features = ["full"] }
tower-http = { version = "0.5.0", features = ["full"] }
tower-http = { version = "0.5.1", features = ["full"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
utoipa = { version = "4.1.0", features = ["axum_extras", "uuid", "chrono"] }
utoipa-swagger-ui = { version = "5.0.0", features = ["axum"] }
utoipa-swagger-ui = { version = "6.0.0", features = ["axum"] }
uuid = { version = "1.6.1", features = ["serde", "v4", "fast-rng", "macro-diagnostics"] }
url = "2.4.1"
chrono = "0.4.31"
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ pub struct Config {
/// The Amphitheatre Server.
#[clap(long, env = "AMP_SERVER")]
pub amp_server: String,

#[clap(long, env = "AUTH_TOKEN")]
pub auth_token: Option<String>,
}
10 changes: 9 additions & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
// limitations under the License.

use crate::config::Config;
use amp_client::client::Client;
use amp_common::scm::client::Client as ScmClient;
use amp_common::scm::driver::github;
use std::sync::Arc;

/// The core type through which handler functions can access common API state.
///
Expand All @@ -25,10 +29,14 @@ use crate::config::Config;
#[derive(Clone)]
pub struct Context {
pub config: Config,
pub client: Arc<Client>,
pub github_client: Arc<ScmClient>,
}

impl Context {
pub async fn new(config: Config) -> anyhow::Result<Context> {
Ok(Context { config })
let client = Arc::new(Client::new(&config.amp_server, config.auth_token.clone()));
let github_client = Arc::new(ScmClient::new(github::default()));
Ok(Context { config, client, github_client })
}
}
4 changes: 4 additions & 0 deletions src/errors.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::http::HTTPError;
use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::Json;
Expand All @@ -26,13 +27,16 @@ pub enum ApiError {
InternalServerError,
#[error("Not Found")]
NotFound,
#[error("Not Found Playbook: {0}")]
NotFoundPlaybook(HTTPError),
}

impl IntoResponse for ApiError {
fn into_response(self) -> axum::response::Response {
let (status, message) = match self {
Self::InternalServerError => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()),
Self::NotFound => (StatusCode::NOT_FOUND, self.to_string()),
Self::NotFoundPlaybook(e) => (StatusCode::NOT_FOUND, e.to_string()),
};
error!("{} - {}", status, message);
(status, Json(json!({ "message": message }))).into_response()
Expand Down
59 changes: 50 additions & 9 deletions src/handlers/playbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@

use std::sync::Arc;

use axum::extract::{Path, State};
use axum::extract::{Path, Query, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::Json;
use uuid::Uuid;

use super::Result;
use crate::context::Context;
use crate::requests::playbook::{CreatePlaybookRequest, UpdatePlaybookRequest};
use crate::requests::playbook::{CreatePlaybookRequest, GetPlaybookRequest, UpdatePlaybookRequest};
use crate::services::playbook::PlaybookService;

// The Playbooks Service Handlers.
Expand All @@ -49,24 +49,49 @@ pub async fn create(

/// Returns a playbook detail.
#[utoipa::path(
get, path = "/v1/playbooks/{id}",
get, path = "/v1/playbooks/{id}/files/{reference}/{path}",
params(
("id" = Uuid, description = "The id of playbook"),
("reference" = String, description = "The name of the commit/branch/tag. Default: the repository’s default branch."),
("path" = String, description = "path parameter"),
),
responses(
(status = 200, description = "Playbook found successfully", body = PlaybookResponse),
(status = 200, description = "Playbook found successfully", body = Content),
(status = 404, description = "Playbook not found"),
(status = 500, description = "Internal Server Error"),
),
tag = "Playbooks"
)]
pub async fn detail(Path(id): Path<Uuid>, State(ctx): State<Arc<Context>>) -> Result<impl IntoResponse> {
Ok(Json(PlaybookService::get(ctx, id).await?))
pub async fn detail(
Path(params): Path<GetPlaybookRequest>,
State(ctx): State<Arc<Context>>,
) -> Result<impl IntoResponse> {
Ok(Json(PlaybookService::get(ctx, params.id, params.reference, params.path).await?))
}
#[utoipa::path(
get, path = "/v1/playbooks/:id/files/trees/:reference/:path?recursive=true | false",
params(
("id" = Uuid, description = "The id of playbook"),
("reference" = String, description = "The name of the commit/branch/tag. Default: the repository’s default branch."),
("path" = String, description = "path parameter"),
),
responses(
(status = 200, description = "Playbook found successfully", body = Tree),
(status = 404, description = "Playbook not found"),
(status = 500, description = "Internal Server Error"),
),
tag = "Playbooks"
)]
pub async fn trees(
Path(params): Path<GetPlaybookRequest>,
Query(recursive): Query<Option<bool>>,
State(ctx): State<Arc<Context>>,
) -> Result<impl IntoResponse> {
Ok(Json(PlaybookService::trees(ctx, params.id, params.reference, recursive).await?))
}

/// Update a playbook.
#[utoipa::path(
patch, path = "/v1/playbooks/{id}",
put, path = "/v1/playbooks/{id}",
params(
("id" = Uuid, description = "The id of playbook"),
),
Expand All @@ -76,7 +101,7 @@ pub async fn detail(Path(id): Path<Uuid>, State(ctx): State<Arc<Context>>) -> Re
content_type = "application/json"
),
responses(
(status = 200, description = "Playbook updated successfully", body = PlaybookResponse),
(status = 204, description = "Playbook updated successfully", body = PlaybookResponse),
(status = 404, description = "Playbook not found")
),
tag = "Playbooks"
Expand Down Expand Up @@ -106,3 +131,19 @@ pub async fn delete(Path(id): Path<Uuid>, State(ctx): State<Arc<Context>>) -> Re

Ok(StatusCode::NO_CONTENT)
}
/// start a playbook
#[utoipa::path(
post, path = "/v1/playbooks/{id}/actions/start",
params(
("id" = Uuid, description = "The id of playbook"),
),
responses(
(status = 204, description = "Playbook deleted successfully"),
(status = 404, description = "Playbook not found")
),
tag = "Playbooks"
)]
pub async fn start(Path(id): Path<Uuid>, State(ctx): State<Arc<Context>>) -> Result<impl IntoResponse> {
PlaybookService::start(ctx, id).await?;
Ok(StatusCode::NO_CONTENT)
}
Loading

0 comments on commit ceb66a7

Please sign in to comment.