diff options
author | Galen Guyer <galen@galenguyer.com> | 2022-08-31 21:13:28 -0400 |
---|---|---|
committer | Galen Guyer <galen@galenguyer.com> | 2022-08-31 21:13:41 -0400 |
commit | a9c3095339e10f53fd9fba9e04efa75dbf05ef8a (patch) | |
tree | 7c3cc92999193727cfea463b28d2be1a8d966a86 | |
parent | 31370d1a1b287c3202fc69b5f6db4c9e174123c6 (diff) |
Add nameservers to newly created zones, cascade deletes
-rw-r--r-- | Cargo.lock | 2 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | migrations/2022-04-09-create-schema.sql | 6 | ||||
-rw-r--r-- | src/routes/v1/zones.rs | 30 |
4 files changed, 27 insertions, 13 deletions
@@ -545,7 +545,7 @@ dependencies = [ [[package]] name = "hdt-api" -version = "0.2.0" +version = "0.2.1" dependencies = [ "addr", "anyhow", @@ -1,6 +1,6 @@ [package] name = "hdt-api" -version = "0.2.0" +version = "0.2.1" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/migrations/2022-04-09-create-schema.sql b/migrations/2022-04-09-create-schema.sql index c616e74..8e47e60 100644 --- a/migrations/2022-04-09-create-schema.sql +++ b/migrations/2022-04-09-create-schema.sql @@ -20,7 +20,7 @@ CREATE TABLE IF NOT EXISTS zones ( created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT (now() AT TIME ZONE 'UTC'), modified_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT (now() AT TIME ZONE 'UTC'), owner_uuid uuid NOT NULL, - constraint owner_uuid_fk foreign key (owner_uuid) references users (id) + constraint owner_uuid_fk foreign key (owner_uuid) references users (id) ON DELETE CASCADE ); CREATE TABLE IF NOT EXISTS records ( @@ -32,7 +32,7 @@ CREATE TABLE IF NOT EXISTS records ( ttl INTEGER NOT NULL, created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT (now() AT TIME ZONE 'UTC'), modified_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT (now() AT TIME ZONE 'UTC'), - constraint zone_id_fk foreign key (zone_id) references zones (id) + constraint zone_id_fk foreign key (zone_id) references zones (id) ON DELETE CASCADE ); CREATE TABLE IF NOT EXISTS api_keys ( @@ -42,7 +42,7 @@ CREATE TABLE IF NOT EXISTS api_keys ( created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT (now() AT TIME ZONE 'UTC'), expires_at TIMESTAMP WITH TIME ZONE NOT NULL, last_used TIMESTAMP WITH TIME ZONE, - constraint owner_uuid_fk foreign key (owner_uuid) references users (id) + constraint owner_uuid_fk foreign key (owner_uuid) references users (id) ON DELETE CASCADE ); CREATE OR REPLACE FUNCTION update_modified_column() diff --git a/src/routes/v1/zones.rs b/src/routes/v1/zones.rs index 0281948..7835911 100644 --- a/src/routes/v1/zones.rs +++ b/src/routes/v1/zones.rs @@ -126,21 +126,35 @@ pub async fn create_zone( } let zone = db::zones::create_zone(&pool, &zone_id, user.sub).await; - match zone { - Ok(zone) => (StatusCode::OK, Json(json!(zone))), - Err(err) => match err { + if let Err(err) = zone { + match err { Error::Database(e) if e.code().unwrap_or(std::borrow::Cow::Borrowed("")) == "23505" => { - ( + return ( StatusCode::BAD_REQUEST, Json(json!({"error": "That zone already exists"})), ) } - _ => ( + _ => { + return ( + StatusCode::INTERNAL_SERVER_ERROR, + Json(json!({ "error": format!("{:?}", err) })), + ) + } + } + } + let zone = zone.unwrap(); + + for ns in NAMESERVERS.iter() { + if let Err(e) = db::records::create_record(&pool, &zone.id, &zone_id, "NS", ns, 3600).await + { + return ( StatusCode::INTERNAL_SERVER_ERROR, - Json(json!({ "error": format!("{:?}", err) })), - ), - }, + Json(json!({ "error": format!("{:?}", e) })), + ); + } } + + (StatusCode::OK, Json(json!(zone))) } pub(crate) fn ensure_trailing_dot(domain: &str) -> String { |