aboutsummaryrefslogtreecommitdiffstats
path: root/notes/projects/radius/sql/ippool-schema.sql
diff options
context:
space:
mode:
Diffstat (limited to 'notes/projects/radius/sql/ippool-schema.sql')
-rw-r--r--notes/projects/radius/sql/ippool-schema.sql47
1 files changed, 23 insertions, 24 deletions
diff --git a/notes/projects/radius/sql/ippool-schema.sql b/notes/projects/radius/sql/ippool-schema.sql
index 00288b0..d64dfd5 100644
--- a/notes/projects/radius/sql/ippool-schema.sql
+++ b/notes/projects/radius/sql/ippool-schema.sql
@@ -145,8 +145,8 @@ CREATE INDEX
145 * regionId 145 * regionId
146 * : reference to the physical region served by the block 146 * : reference to the physical region served by the block
147 * 147 *
148 * active 148 * busyout
149 * : assignable to customer if true, disabled/inactive if false 149 * : auto assignable to customer if false, disabled/inactive if false
150 * 150 *
151 * description 151 * description
152 * : human readable description of network block 152 * : human readable description of network block
@@ -157,7 +157,7 @@ CREATE TABLE IF NOT EXISTS netblock
157 network CIDR NOT NULL UNIQUE, 157 network CIDR NOT NULL UNIQUE,
158 poolId INTEGER NOT NULL, 158 poolId INTEGER NOT NULL,
159 regionId INTEGER, 159 regionId INTEGER,
160 assignable BOOLEAN NOT NULL DEFAULT TRUE, 160 busyout BOOLEAN NOT NULL DEFAULT TRUE,
161 description VARCHAR(256), 161 description VARCHAR(256),
162 PRIMARY KEY ( id ), 162 PRIMARY KEY ( id ),
163 FOREIGN KEY ( poolId ) 163 FOREIGN KEY ( poolId )
@@ -173,7 +173,7 @@ CREATE INDEX
173 ( id, 173 ( id,
174 poolId, 174 poolId,
175 network, 175 network,
176 assignable 176 busyout
177 ); 177 );
178 178
179 179
@@ -299,7 +299,7 @@ CREATE OR REPLACE FUNCTION ip_before_update
299 LANGUAGE plpgsql 299 LANGUAGE plpgsql
300 AS $$ 300 AS $$
301DECLARE 301DECLARE
302 ipAssignable BOOLEAN; 302 ipBusyout BOOLEAN;
303BEGIN 303BEGIN
304 304
305 -- prevents IP address from being modified 305 -- prevents IP address from being modified
@@ -309,20 +309,20 @@ BEGIN
309 309
310 -- verifies netblock contains IP address 310 -- verifies netblock contains IP address
311 IF ( NEW.netblockId != NEW.netblockId ) THEN 311 IF ( NEW.netblockId != NEW.netblockId ) THEN
312 SELECT assignable INTO ipAssignable 312 SELECT busyout INTO ipBusyout
313 FROM netblock 313 FROM netblock
314 WHERE id = NEW.netblockId 314 WHERE id = NEW.netblockId
315 AND network >>= NEW.address; 315 AND network >>= NEW.address;
316 IF ipAssignable IS NULL THEN 316 IF ipBusyout IS NULL THEN
317 RAISE EXCEPTION 'invalid netblock'; 317 RAISE EXCEPTION 'invalid netblock';
318 END IF; 318 END IF;
319 IF ipAssignable IS TRUE THEN 319 IF ipBusyout IS FALSE THEN
320 PERFORM id 320 PERFORM id
321 FROM netblock 321 FROM netblock
322 WHERE assignable IS TRUE 322 WHERE busyout IS TRUE
323 AND network >>= NEW.address; 323 AND network >>= NEW.address;
324 IF FOUND THEN 324 IF FOUND THEN
325 RAISE EXCEPTION 'cannot associate assignable netblock if an unassignable netblock contains IP address'; 325 RAISE EXCEPTION 'cannot associate not busy netblock to a busied out netblock contains IP address';
326 END IF; 326 END IF;
327 END IF; 327 END IF;
328 END IF; 328 END IF;
@@ -330,8 +330,6 @@ BEGIN
330 -- update timestamp 330 -- update timestamp
331 IF ( ( OLD.nasId != NEW.nasId ) OR ( OLD.username != NEW.username ) ) THEN 331 IF ( ( OLD.nasId != NEW.nasId ) OR ( OLD.username != NEW.username ) ) THEN
332 NEW.modified := current_timestamp; 332 NEW.modified := current_timestamp;
333 ELSE
334 NEW.modified := OLD.modified;
335 END IF; 333 END IF;
336 334
337 RETURN NEW; 335 RETURN NEW;
@@ -409,17 +407,6 @@ BEGIN
409 RAISE EXCEPTION 'changing network will orphan existing IP addresses'; 407 RAISE EXCEPTION 'changing network will orphan existing IP addresses';
410 END IF; 408 END IF;
411 409
412 -- updates IP Association of netblock if an update and is now assignable
413 IF ( ( OLD.assignable IS FALSE ) AND ( NEW.assignable IS TRUE ) ) THEN
414 UPDATE ip
415 SET netblockId = nb.id
416 FROM ip
417 INNER JOIN netblock AS nb
418 ON nb.network >>= ip.address
419 WHERE ip.address <<= NEW.network
420 AND nb.assignable IS TRUE;
421 END IF;
422
423 RETURN NEW; 410 RETURN NEW;
424END; 411END;
425$$; 412$$;
@@ -432,8 +419,20 @@ CREATE OR REPLACE FUNCTION netblock_after_insert_or_update
432 AS $$ 419 AS $$
433DECLARE 420DECLARE
434BEGIN 421BEGIN
422
423 -- updates IP Association of netblock if an update and is now assignable
424 IF ( ( OLD.busyout IS TRUE ) AND ( NEW.busyout IS FALSE ) ) THEN
425 UPDATE ip
426 SET netblockId = nb.id
427 FROM ip
428 INNER JOIN netblock AS nb
429 ON nb.network >>= ip.address
430 WHERE ip.address <<= NEW.network
431 AND nb.busyout IS TRUE;
432 END IF;
433
435 -- updates IP association of netblock is not assignable 434 -- updates IP association of netblock is not assignable
436 IF ( NEW.assignable IS FALSE ) THEN 435 IF ( NEW.busyout IS TRUE ) THEN
437 UPDATE ip 436 UPDATE ip
438 SET netblockId = NEW.id 437 SET netblockId = NEW.id
439 WHERE address <<= NEW.network; 438 WHERE address <<= NEW.network;