Skip to content

Commit

Permalink
feat: verify project exists before sending destroy task (#474)
Browse files Browse the repository at this point in the history
  • Loading branch information
oddgrd authored Nov 15, 2022
1 parent 3bcc683 commit e10f096
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
27 changes: 23 additions & 4 deletions gateway/src/api/latest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@ async fn delete_project(
) -> Result<AxumJson<project::Response>, Error> {
let project_name = project.clone();

let state = service.find_project(&project_name).await?;

let mut response = project::Response {
name: project_name.to_string(),
state: state.into(),
};

if response.state == shuttle_common::models::project::State::Destroyed {
return Ok(AxumJson(response));
}

// if project exists and isn't `Destroyed`, send destroy task
service
.new_task()
.project(project)
Expand All @@ -132,10 +144,8 @@ async fn delete_project(
.send(&sender)
.await?;

let response = project::Response {
name: project_name.to_string(),
state: shuttle_common::models::project::State::Destroying,
};
response.state = shuttle_common::models::project::State::Destroying;

Ok(AxumJson(response))
}

Expand Down Expand Up @@ -356,6 +366,15 @@ pub mod tests {
.await
.unwrap();

// delete returns 404 for project that doesn't exist
router
.call(delete_project("resurrections").with_header(&authorization))
.map_ok(|resp| {
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
})
.await
.unwrap();

Ok(())
}

Expand Down
17 changes: 17 additions & 0 deletions gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -893,5 +893,22 @@ pub mod tests {
break;
}
});

// Attempting to delete already Destroyed project will return Destroyed
api_client
.request(
Request::delete("/projects/matrix")
.with_header(&authorization)
.body(Body::empty())
.unwrap(),
)
.map_ok(|resp| {
assert_eq!(resp.status(), StatusCode::OK);
let resp =
serde_json::from_slice::<project::Response>(resp.body().as_slice()).unwrap();
assert_eq!(resp.state, project::State::Destroyed);
})
.await
.unwrap();
}
}

0 comments on commit e10f096

Please sign in to comment.