Skip to content

Commit

Permalink
Fix bugs in commissioning
Browse files Browse the repository at this point in the history
  • Loading branch information
HoKim98 committed Sep 11, 2022
1 parent 6a653cd commit 12add23
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
7 changes: 7 additions & 0 deletions kiss/api/src/box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,11 @@ pub mod request {
#[serde(flatten)]
pub machine: BoxMachineSpec,
}

#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
pub struct BoxCommissionQuery {
pub access: BoxAccessSpec,
pub machine: BoxMachineSpec,
pub power: Option<BoxPowerSpec>,
}
}
23 changes: 17 additions & 6 deletions kiss/gateway/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ use kiss_api::{
core::ObjectMeta,
Api, Client, CustomResourceExt,
},
r#box::{request::BoxNewQuery, BoxCrd, BoxSpec, BoxState, BoxStatus},
r#box::{
request::{BoxCommissionQuery, BoxNewQuery},
BoxCrd, BoxSpec, BoxState, BoxStatus,
},
serde_json::json,
};

Expand Down Expand Up @@ -94,19 +97,27 @@ async fn get_new(client: Data<Arc<Client>>, Query(query): Query<BoxNewQuery>) ->
}

#[post("/commission")]
async fn get_commission(client: Data<Arc<Client>>, Json(spec): Json<BoxSpec>) -> impl Responder {
async fn try_handle(client: Data<Arc<Client>>, spec: BoxSpec) -> Result<()> {
async fn get_commission(
client: Data<Arc<Client>>,
Json(query): Json<BoxCommissionQuery>,
) -> impl Responder {
async fn try_handle(client: Data<Arc<Client>>, query: BoxCommissionQuery) -> Result<()> {
let api = Api::<BoxCrd>::all((***client).clone());

let name = spec.machine.uuid.to_string();
let name = query.machine.uuid.to_string();

match api.get(&name).await {
Ok(r#box) => {
let crd = BoxCrd::api_resource();
let patch = Patch::Apply(json!({
"apiVersion": crd.api_version,
"kind": crd.kind,
"spec": spec,
"spec": BoxSpec {
access: query.access,
group: r#box.spec.group,
machine: query.machine,
power: query.power,
},
"status": BoxStatus {
state: BoxState::Ready,
bind_group: r#box.status.as_ref().and_then(|status| status.bind_group.as_ref()).cloned(),
Expand All @@ -122,7 +133,7 @@ async fn get_commission(client: Data<Arc<Client>>, Json(spec): Json<BoxSpec>) ->
Ok(())
}

match try_handle(client, spec).await {
match try_handle(client, query).await {
Ok(()) => HttpResponse::Ok().json("Ok"),
Err(e) => {
warn!("failed to commission a client: {e}");
Expand Down

0 comments on commit 12add23

Please sign in to comment.