feat: rename restore targets and show the application next to them
- PUT /api/v1/restore-targets/:id changes only the display name (admin, validated, audited); ids, identity, relations and jobs are untouched - Relations console: Application column, inline rename, application in the target pickers - Refresh screen: the application on each card - registering a target suggests '<application> · <database>' Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -349,7 +349,7 @@ fn recipe(w: &World, row: &Row) -> (String, Option<Value>) {
|
||||
}
|
||||
"/api/v1/refresh-runs/:id" => Uuid::parse_str(&w.run).unwrap(),
|
||||
"/api/v1/orbits/:id/environment" => a.orbit,
|
||||
"/api/v1/restore-targets/:id/environment" => a.target,
|
||||
"/api/v1/restore-targets/:id" | "/api/v1/restore-targets/:id/environment" => a.target,
|
||||
"/api/v1/relations/:id"
|
||||
| "/api/v1/relations/:id/access"
|
||||
| "/api/v1/relations/:id/access/:grant" => a.relation,
|
||||
@@ -374,6 +374,7 @@ fn recipe(w: &World, row: &Row) -> (String, Option<Value>) {
|
||||
("POST", "/api/v1/orbits") => Some(
|
||||
json!({"name": "created", "provider": "filesystem", "source": "filesystem", "provider_config": {"path": "/nonexistent"}}),
|
||||
),
|
||||
("PUT", "/api/v1/restore-targets/:id") => Some(json!({"name": "renamed"})),
|
||||
("PUT", "/api/v1/orbits/:id/retention") => Some(json!({"decay_preset": "week-of-dailies"})),
|
||||
("PUT", "/api/v1/orbits/:id/outpost") => Some(json!({"outpost_id": a.outpost})),
|
||||
("POST", "/api/v1/reconstitute") => {
|
||||
|
||||
@@ -1739,3 +1739,110 @@ async fn reconciling_a_job_that_needs_no_recovery_says_so() {
|
||||
.await;
|
||||
assert_eq!(status, StatusCode::BAD_REQUEST);
|
||||
}
|
||||
|
||||
// ---- naming restore targets ----
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_administrator_can_rename_a_target_and_everything_that_points_at_it_keeps_working() {
|
||||
let lab = fresh().await;
|
||||
let admin = lab.admin();
|
||||
let (qa, _) = lab.refresher("qa");
|
||||
let uri = format!("/api/v1/restore-targets/{}", lab.target);
|
||||
let (status, _) = lab
|
||||
.send(
|
||||
&admin,
|
||||
"PUT",
|
||||
&uri,
|
||||
Some(json!({"name": "presensi · dev · app_dev"})),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(status, StatusCode::NO_CONTENT);
|
||||
|
||||
let (_, overview) = lab.send(&admin, "GET", "/api/v1/relations", None).await;
|
||||
let target = &overview["targets"][0];
|
||||
assert_eq!(target["name"], "presensi · dev · app_dev");
|
||||
assert_eq!(target["id"], lab.target.to_string());
|
||||
assert_eq!(
|
||||
target["identity"]["database"], "staging_app",
|
||||
"identity is untouched"
|
||||
);
|
||||
let relation = &overview["relations"][0];
|
||||
assert_eq!(relation["target_name"], "presensi · dev · app_dev");
|
||||
assert_eq!(
|
||||
relation["id"], lab.relation,
|
||||
"the relation is the same relation"
|
||||
);
|
||||
assert!(relation["usable"].as_bool().unwrap());
|
||||
// the person who refreshes through it sees the new name, and a run through it still starts
|
||||
let (_, mine) = lab
|
||||
.send(&qa, "GET", "/api/v1/refresh-relations", None)
|
||||
.await;
|
||||
assert_eq!(mine[0]["target_name"], "presensi · dev · app_dev");
|
||||
let confirmation = format!(
|
||||
"REFRESH prod-db TO presensi · dev · app_dev v{}",
|
||||
lab.relation_version
|
||||
);
|
||||
let (status, body) = lab
|
||||
.send(&qa, "POST", "/api/v1/refresh-runs", Some(json!({"relation_id": lab.relation, "confirmation": confirmation, "idempotency_key": "after-rename"})))
|
||||
.await;
|
||||
assert_eq!(status, StatusCode::ACCEPTED, "{body}");
|
||||
assert!(lab
|
||||
.audit_log()
|
||||
.iter()
|
||||
.any(|(event, detail)| event == "restore_target.renamed"
|
||||
&& detail.contains(&lab.target.to_string())));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_target_name_is_validated_like_a_registration_and_only_administrators_set_it() {
|
||||
let lab = fresh().await;
|
||||
let admin = lab.admin();
|
||||
let uri = format!("/api/v1/restore-targets/{}", lab.target);
|
||||
for name in ["", " ", &"x".repeat(129), "bad\nname", "tab\tname"] {
|
||||
let (status, _) = lab
|
||||
.send(&admin, "PUT", &uri, Some(json!({"name": name})))
|
||||
.await;
|
||||
assert_eq!(status, StatusCode::BAD_REQUEST, "{name:?}");
|
||||
}
|
||||
let (status, _) = lab
|
||||
.send(
|
||||
&admin,
|
||||
"PUT",
|
||||
&uri,
|
||||
Some(json!({"name": "ok", "identity": {}})),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(
|
||||
status,
|
||||
StatusCode::UNPROCESSABLE_ENTITY,
|
||||
"only the name is editable"
|
||||
);
|
||||
let unknown = format!("/api/v1/restore-targets/{}", Uuid::new_v4());
|
||||
assert_eq!(
|
||||
lab.send(&admin, "PUT", &unknown, Some(json!({"name": "ok"})))
|
||||
.await
|
||||
.0,
|
||||
StatusCode::NOT_FOUND
|
||||
);
|
||||
let (qa, _) = lab.refresher("qa");
|
||||
let status = lab
|
||||
.send(&qa, "PUT", &uri, Some(json!({"name": "mine"})))
|
||||
.await
|
||||
.0;
|
||||
assert!(
|
||||
status == StatusCode::FORBIDDEN || status == StatusCode::NOT_FOUND,
|
||||
"{status}"
|
||||
);
|
||||
let (_, overview) = lab.send(&admin, "GET", "/api/v1/relations", None).await;
|
||||
assert_eq!(
|
||||
overview["targets"][0]["name"], "staging-db",
|
||||
"nothing above changed it"
|
||||
);
|
||||
// the longest allowed name is fine
|
||||
assert_eq!(
|
||||
lab.send(&admin, "PUT", &uri, Some(json!({"name": "y".repeat(128)})))
|
||||
.await
|
||||
.0,
|
||||
StatusCode::NO_CONTENT
|
||||
);
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ use uuid::Uuid;
|
||||
pub(super) fn routes() -> Router<HubState> {
|
||||
Router::new()
|
||||
.route("/api/v1/restore-targets", post(register))
|
||||
.route("/api/v1/restore-targets/:id", axum::routing::put(rename))
|
||||
.route("/api/v1/capture-profiles/:id/restore-targets", get(targets))
|
||||
.route(
|
||||
"/api/v1/snapshots/:id/restore-targets",
|
||||
@@ -82,6 +83,43 @@ async fn register(
|
||||
Ok((StatusCode::CREATED, Json(target)))
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Rename {
|
||||
name: String,
|
||||
}
|
||||
|
||||
/// Same rules as at registration: not blank, at most 128 characters, no control characters.
|
||||
fn valid_target_name(name: &str) -> bool {
|
||||
!name.trim().is_empty() && name.len() <= 128 && !name.chars().any(char::is_control)
|
||||
}
|
||||
|
||||
async fn rename(
|
||||
ConnectInfo(peer): ConnectInfo<SocketAddr>,
|
||||
State(state): State<HubState>,
|
||||
headers: HeaderMap,
|
||||
Path(id): Path<Uuid>,
|
||||
Json(body): Json<Rename>,
|
||||
) -> Result<StatusCode, StatusCode> {
|
||||
let actor = admin_write(&state, peer, &headers)?;
|
||||
let name = body.name.trim();
|
||||
if !valid_target_name(name) {
|
||||
return Err(StatusCode::BAD_REQUEST);
|
||||
}
|
||||
if !state.db.rename_restore_target(id, name).map_err(internal)? {
|
||||
return Err(StatusCode::NOT_FOUND);
|
||||
}
|
||||
state
|
||||
.db
|
||||
.audit(
|
||||
"restore_target.renamed",
|
||||
&format!("actor={actor} target={id}"),
|
||||
)
|
||||
.await
|
||||
.map_err(internal)?;
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
async fn targets(
|
||||
ConnectInfo(peer): ConnectInfo<SocketAddr>,
|
||||
State(state): State<HubState>,
|
||||
|
||||
Reference in New Issue
Block a user