Skip to content

Commit

Permalink
refactor/nanocld: cleaner secret services with file separation
Browse files Browse the repository at this point in the history
  • Loading branch information
leon3s committed Oct 24, 2024
1 parent ebe76ea commit a96518c
Show file tree
Hide file tree
Showing 8 changed files with 318 additions and 267 deletions.
267 changes: 0 additions & 267 deletions bin/nanocld/src/services/secret.rs

This file was deleted.

32 changes: 32 additions & 0 deletions bin/nanocld/src/services/secret/count.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use ntex::web;

use nanocl_error::http::HttpResult;
use nanocl_stubs::generic::{GenericCount, GenericListQuery};

use crate::{
models::{SecretDb, SystemState},
repositories::generic::*,
utils,
};

/// Count secrets
#[cfg_attr(feature = "dev", utoipa::path(
get,
tag = "Secrets",
path = "/secrets/count",
params(
("filter" = Option<String>, Query, description = "Generic filter", example = "{ \"filter\": { \"where\": { \"name\": { \"eq\": \"global\" } } } }"),
),
responses(
(status = 200, description = "Count result", body = GenericCount),
),
))]
#[web::get("/secrets/count")]
pub async fn count_secret(
state: web::types::State<SystemState>,
qs: web::types::Query<GenericListQuery>,
) -> HttpResult<web::HttpResponse> {
let filter = utils::query_string::parse_qs_filter(&qs)?;
let count = SecretDb::count_by(&filter, &state.inner.pool).await?;
Ok(web::HttpResponse::Ok().json(&GenericCount { count }))
}

Check warning on line 32 in bin/nanocld/src/services/secret/count.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocld/src/services/secret/count.rs#L25-L32

Added lines #L25 - L32 were not covered by tests
47 changes: 47 additions & 0 deletions bin/nanocld/src/services/secret/create.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use ntex::web;

use bollard_next::auth::DockerCredentials;
use nanocl_error::http::{HttpError, HttpResult};
use nanocl_stubs::{proxy::ProxySslConfig, secret::SecretPartial};

use crate::{
models::{SecretDb, SystemState},
objects::generic::*,
utils,
};

/// Create a new secret
#[cfg_attr(feature = "dev", utoipa::path(
post,
request_body = SecretPartial,
tag = "Secrets",
path = "/secrets",
responses(
(status = 200, description = "List of secret", body = Secret),
(status = 409, description = "Namespace already exist", body = ApiError),
),
))]
#[web::post("/secrets")]
pub async fn create_secret(
state: web::types::State<SystemState>,
payload: web::types::Json<SecretPartial>,
) -> HttpResult<web::HttpResponse> {
utils::key::ensure_kind(&payload.kind)?;
match payload.kind.as_str() {
"nanocl.io/tls" => {
serde_json::from_value::<ProxySslConfig>(payload.data.clone())
.map_err(|e| HttpError::bad_request(e.to_string()))?;

Check warning on line 33 in bin/nanocld/src/services/secret/create.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocld/src/services/secret/create.rs#L32-L33

Added lines #L32 - L33 were not covered by tests
}
"nanocl.io/env" => {
serde_json::from_value::<Vec<String>>(payload.data.clone())
.map_err(|e| HttpError::bad_request(e.to_string()))?;

Check warning on line 37 in bin/nanocld/src/services/secret/create.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocld/src/services/secret/create.rs#L36-L37

Added lines #L36 - L37 were not covered by tests
}
"nanocl.io/container-registry" => {
serde_json::from_value::<DockerCredentials>(payload.data.clone())
.map_err(|e| HttpError::bad_request(e.to_string()))?;

Check warning on line 41 in bin/nanocld/src/services/secret/create.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocld/src/services/secret/create.rs#L40-L41

Added lines #L40 - L41 were not covered by tests
}
_ => {}
}
let secret = SecretDb::create_obj(&payload, &state).await?;
Ok(web::HttpResponse::Created().json(&secret))
}
30 changes: 30 additions & 0 deletions bin/nanocld/src/services/secret/delete.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use ntex::web;

use nanocl_error::http::HttpResult;

use crate::{
models::{SecretDb, SystemState},
objects::generic::*,
};

/// Delete a secret
#[cfg_attr(feature = "dev", utoipa::path(
delete,
tag = "Secrets",
path = "/secrets/{key}",
params(
("key" = String, Path, description = "Key of the secret")
),
responses(
(status = 202, description = "Secret have been deleted"),
(status = 404, description = "Secret don't exists", body = ApiError),
),
))]
#[web::delete("/secrets/{key}")]
pub async fn delete_secret(
state: web::types::State<SystemState>,
path: web::types::Path<(String, String)>,
) -> HttpResult<web::HttpResponse> {
SecretDb::del_obj_by_pk(&path.1, &(), &state).await?;
Ok(web::HttpResponse::Accepted().into())
}
Loading

0 comments on commit a96518c

Please sign in to comment.