diff options
| author | David M. Syzdek <syzdek@bindlebinaries.com> | 2019-05-08 16:02:23 -0800 |
|---|---|---|
| committer | David M. Syzdek <syzdek@bindlebinaries.com> | 2019-05-08 16:02:23 -0800 |
| commit | 676530cdc7ca1a54a00b8594b678d5c36694c28d (patch) | |
| tree | d549a7f769111e1ea131c765768583088890b9f8 /notes | |
| parent | 9b2c49733978efd50e9480a4ca7769c001c2bc2c (diff) | |
| download | scratchpad-676530cdc7ca1a54a00b8594b678d5c36694c28d.tar.gz scratchpad-676530cdc7ca1a54a00b8594b678d5c36694c28d.tar.bz2 scratchpad-676530cdc7ca1a54a00b8594b678d5c36694c28d.tar.xz scratchpad-676530cdc7ca1a54a00b8594b678d5c36694c28d.zip | |
Expanding functions and triggers
Diffstat (limited to 'notes')
| -rw-r--r-- | notes/projects/radius/sql/ippool-database.sql | 2 | ||||
| -rw-r--r-- | notes/projects/radius/sql/ippool-demo.sql | 21 | ||||
| -rw-r--r-- | notes/projects/radius/sql/ippool-schema.sql | 175 |
3 files changed, 181 insertions, 17 deletions
diff --git a/notes/projects/radius/sql/ippool-database.sql b/notes/projects/radius/sql/ippool-database.sql index b7d0654..660e676 100644 --- a/notes/projects/radius/sql/ippool-database.sql +++ b/notes/projects/radius/sql/ippool-database.sql | |||
| @@ -9,6 +9,8 @@ | |||
| 9 | 9 | ||
| 10 | 10 | ||
| 11 | SET ROLE wheel; | 11 | SET ROLE wheel; |
| 12 | SET client_min_messages TO WARNING; | ||
| 13 | \set ON_ERROR_STOP on | ||
| 12 | 14 | ||
| 13 | 15 | ||
| 14 | CREATE ROLE radius_ippool | 16 | CREATE ROLE radius_ippool |
diff --git a/notes/projects/radius/sql/ippool-demo.sql b/notes/projects/radius/sql/ippool-demo.sql index 601ac48..4775d5f 100644 --- a/notes/projects/radius/sql/ippool-demo.sql +++ b/notes/projects/radius/sql/ippool-demo.sql | |||
| @@ -25,6 +25,27 @@ SELECT nas_add( nasName := 'bras.eng', regionName := 'Anchorage' ); | |||
| 25 | SELECT nas_add( nasName := 'br1.sol', regionName := 'Soldotna' ); | 25 | SELECT nas_add( nasName := 'br1.sol', regionName := 'Soldotna' ); |
| 26 | 26 | ||
| 27 | 27 | ||
| 28 | SELECT netblock_add | ||
| 29 | ( | ||
| 30 | netAddress := '209.112.192.0/19', | ||
| 31 | poolName := 'DHCP', | ||
| 32 | regionName := 'Anchorage' | ||
| 33 | ); | ||
| 34 | SELECT netblock_add | ||
| 35 | ( | ||
| 36 | netAddress := '209.112.128.0/18', | ||
| 37 | poolName := 'DHCP', | ||
| 38 | regionName := 'Anchorage' | ||
| 39 | ); | ||
| 40 | SELECT netblock_add | ||
| 41 | ( | ||
| 42 | netAddress := '209.112.156.0/24', | ||
| 43 | poolName := 'DHCP', | ||
| 44 | regionName := 'Anchorage', | ||
| 45 | netActive := FALSE, | ||
| 46 | netDesc := 'Admin Network' | ||
| 47 | ); | ||
| 48 | |||
| 28 | 49 | ||
| 29 | 50 | ||
| 30 | INSERT INTO block ( poolId, regionId, net, description ) VALUES | 51 | INSERT INTO block ( poolId, regionId, net, description ) VALUES |
diff --git a/notes/projects/radius/sql/ippool-schema.sql b/notes/projects/radius/sql/ippool-schema.sql index 5dfc5e1..df2bfeb 100644 --- a/notes/projects/radius/sql/ippool-schema.sql +++ b/notes/projects/radius/sql/ippool-schema.sql | |||
| @@ -8,6 +8,8 @@ | |||
| 8 | 8 | ||
| 9 | 9 | ||
| 10 | SET ROLE radius_ippool; | 10 | SET ROLE radius_ippool; |
| 11 | SET client_min_messages TO WARNING; | ||
| 12 | \set ON_ERROR_STOP on | ||
| 11 | 13 | ||
| 12 | 14 | ||
| 13 | -- ////////////// | 15 | -- ////////////// |
| @@ -103,15 +105,15 @@ CREATE INDEX | |||
| 103 | * id | 105 | * id |
| 104 | * : The primary key of a specific entry | 106 | * : The primary key of a specific entry |
| 105 | * | 107 | * |
| 108 | * network | ||
| 109 | * : network address and network size of block | ||
| 110 | * | ||
| 106 | * poolId | 111 | * poolId |
| 107 | * : reference to the pool type of the network | 112 | * : reference to the pool type of the network |
| 108 | * | 113 | * |
| 109 | * regionId | 114 | * regionId |
| 110 | * : reference to the physical region served by the block | 115 | * : reference to the physical region served by the block |
| 111 | * | 116 | * |
| 112 | * net | ||
| 113 | * : network address and network size of block | ||
| 114 | * | ||
| 115 | * active | 117 | * active |
| 116 | * : assignable to customer if true, disabled/inactive if false | 118 | * : assignable to customer if true, disabled/inactive if false |
| 117 | * | 119 | * |
| @@ -121,9 +123,9 @@ CREATE INDEX | |||
| 121 | CREATE TABLE IF NOT EXISTS netblock | 123 | CREATE TABLE IF NOT EXISTS netblock |
| 122 | ( | 124 | ( |
| 123 | id SERIAL, | 125 | id SERIAL, |
| 126 | network CIDR NOT NULL UNIQUE, | ||
| 124 | poolId INTEGER NOT NULL, | 127 | poolId INTEGER NOT NULL, |
| 125 | regionId INTEGER, | 128 | regionId INTEGER, |
| 126 | network CIDR NOT NULL UNIQUE, | ||
| 127 | active BOOLEAN NOT NULL DEFAULT TRUE, | 129 | active BOOLEAN NOT NULL DEFAULT TRUE, |
| 128 | description VARCHAR(32), | 130 | description VARCHAR(32), |
| 129 | PRIMARY KEY ( id ), | 131 | PRIMARY KEY ( id ), |
| @@ -150,39 +152,39 @@ CREATE INDEX | |||
| 150 | * address | 152 | * address |
| 151 | * : user assignable IP address | 153 | * : user assignable IP address |
| 152 | * | 154 | * |
| 153 | * poolId | ||
| 154 | * : reference to the pool type of the network | ||
| 155 | * | ||
| 156 | * assigned | ||
| 157 | * : TRUE if currently assigned, FALSE if available | ||
| 158 | * | ||
| 159 | * modified | 155 | * modified |
| 160 | * : timestamp generated when entry is modified | 156 | * : timestamp generated when entry is modified |
| 161 | * | 157 | * |
| 162 | * username | 158 | * username |
| 163 | * : username currently assigned or last assigned address | 159 | * : username currently assigned or last assigned address |
| 160 | * | ||
| 161 | * acctSession | ||
| 162 | * : RADIUS accounting session currently holding address | ||
| 163 | * | ||
| 164 | * nasId | ||
| 165 | * : reference to RADIUS NAS client currently holding address | ||
| 164 | */ | 166 | */ |
| 165 | CREATE TABLE IF NOT EXISTS ip | 167 | CREATE TABLE IF NOT EXISTS ip |
| 166 | ( | 168 | ( |
| 167 | address INET, | 169 | address INET, |
| 168 | poolId INTEGER NOT NULL, | ||
| 169 | assigned BOOLEAN NOT NULL DEFAULT FALSE, | ||
| 170 | modified timestamp WITH TIME ZONE NOT NULL | 170 | modified timestamp WITH TIME ZONE NOT NULL |
| 171 | DEFAULT TIMESTAMP 'epoch', | 171 | DEFAULT TIMESTAMP 'epoch', |
| 172 | username VARCHAR(64), | 172 | username VARCHAR(64), |
| 173 | acctSession VARCHAR(32), | ||
| 174 | nasId INTEGER, | ||
| 173 | PRIMARY KEY ( address ), | 175 | PRIMARY KEY ( address ), |
| 174 | FOREIGN KEY ( poolId ) | 176 | FOREIGN KEY ( nasId ) |
| 175 | REFERENCES pool | 177 | REFERENCES nas |
| 176 | ( id ) | 178 | ( id ) |
| 177 | ); | 179 | ); |
| 178 | CREATE INDEX | 180 | CREATE INDEX |
| 179 | IF NOT EXISTS ip_index | 181 | IF NOT EXISTS ip_index |
| 180 | ON ip | 182 | ON ip |
| 181 | ( address, | 183 | ( address, |
| 182 | poolId, | ||
| 183 | assigned, | ||
| 184 | modified, | 184 | modified, |
| 185 | username | 185 | username, |
| 186 | acctSession, | ||
| 187 | nasId | ||
| 186 | ); | 188 | ); |
| 187 | 189 | ||
| 188 | 190 | ||
| @@ -270,6 +272,137 @@ $$; | |||
| 270 | 272 | ||
| 271 | 273 | ||
| 272 | /* | 274 | /* |
| 275 | * Creates entry for network block | ||
| 276 | * | ||
| 277 | * % SELECT netblock_add( | ||
| 278 | * netAddress := '198.51.100.0/24', | ||
| 279 | * poolName := 'PPPoE' | ||
| 280 | * ); | ||
| 281 | * % SELECT netblock_add( | ||
| 282 | * netAddress := '203.0.113.0/24', | ||
| 283 | * poolName := 'DHCP', | ||
| 284 | * regionName := 'Soldotna', | ||
| 285 | * netActive := FALSE, | ||
| 286 | * netDesc := 'Lab IP Pool' | ||
| 287 | * ); | ||
| 288 | * | ||
| 289 | * netAddress | ||
| 290 | * : network address and network size being added to database | ||
| 291 | * | ||
| 292 | * poolName | ||
| 293 | * : IP pool network is being assigned | ||
| 294 | * | ||
| 295 | * regionName | ||
| 296 | * : the name of the region in which the network will physically serve | ||
| 297 | * | ||
| 298 | * netActive | ||
| 299 | * : TRUE if addresses within network are assignable, FALSE if addresses are | ||
| 300 | * : not assignable | ||
| 301 | * | ||
| 302 | * netDesc | ||
| 303 | * : human readable description of the network | ||
| 304 | * | ||
| 305 | * Returns the `id` of the created nas client | ||
| 306 | */ | ||
| 307 | CREATE OR REPLACE FUNCTION netblock_add | ||
| 308 | ( | ||
| 309 | netAddress CIDR, | ||
| 310 | poolName VARCHAR(32), | ||
| 311 | regionName VARCHAR(32) DEFAULT NULL, | ||
| 312 | netActive BOOLEAN DEFAULT TRUE, | ||
| 313 | netDesc VARCHAR(128) DEFAULT NULL | ||
| 314 | ) | ||
| 315 | RETURNS INTEGER | ||
| 316 | LANGUAGE plpgsql | ||
| 317 | AS $$ | ||
| 318 | DECLARE | ||
| 319 | netRegionId INTEGER DEFAULT NULL; | ||
| 320 | netPoolId INTEGER; | ||
| 321 | returnId INTEGER DEFAULT 1; | ||
| 322 | BEGIN | ||
| 323 | |||
| 324 | -- searches for region id | ||
| 325 | if ( regionName IS NOT NULL ) THEN | ||
| 326 | SELECT id INTO netRegionId | ||
| 327 | FROM region | ||
| 328 | WHERE name LIKE lower(regionName); | ||
| 329 | END IF; | ||
| 330 | |||
| 331 | -- searches for pool id | ||
| 332 | SELECT id INTO netPoolId | ||
| 333 | FROM pool | ||
| 334 | WHERE name LIKE lower(poolName); | ||
| 335 | IF ( netPoolId IS NULL ) THEN | ||
| 336 | RAISE EXCEPTION 'invalid pool'; | ||
| 337 | END IF; | ||
| 338 | |||
| 339 | -- adds new pool | ||
| 340 | INSERT INTO netblock | ||
| 341 | ( network, | ||
| 342 | poolId, | ||
| 343 | regionId, | ||
| 344 | active, | ||
| 345 | description | ||
| 346 | ) | ||
| 347 | VALUES ( netAddress, | ||
| 348 | netPoolId, | ||
| 349 | netRegionId, | ||
| 350 | netActive, | ||
| 351 | netDesc | ||
| 352 | ) | ||
| 353 | RETURNING id INTO returnId; | ||
| 354 | |||
| 355 | RETURN returnId; | ||
| 356 | END; | ||
| 357 | $$; | ||
| 358 | |||
| 359 | |||
| 360 | /* | ||
| 361 | * updates timestamp of addresses | ||
| 362 | * | ||
| 363 | * returns trigger | ||
| 364 | */ | ||
| 365 | CREATE OR REPLACE FUNCTION netblock_trigger | ||
| 366 | ( ) | ||
| 367 | RETURNS trigger | ||
| 368 | LANGUAGE plpgsql | ||
| 369 | AS $$ | ||
| 370 | DECLARE | ||
| 371 | resultCount INTEGER; | ||
| 372 | BEGIN | ||
| 373 | |||
| 374 | -- verifies that new region does not conflict with new network | ||
| 375 | SELECT count(id) INTO resultCount | ||
| 376 | FROM netblock | ||
| 377 | WHERE regionId != NEW.regionId | ||
| 378 | AND | ||
| 379 | ( NEW.network >>= network | ||
| 380 | OR | ||
| 381 | NEW.network <<= network | ||
| 382 | ); | ||
| 383 | IF ( resultCount != 0 ) THEN | ||
| 384 | RAISE EXCEPTION 'conflict with existing network regions'; | ||
| 385 | END IF; | ||
| 386 | |||
| 387 | -- verifies that new region does not conflict with new network | ||
| 388 | SELECT count(id) INTO resultCount | ||
| 389 | FROM netblock | ||
| 390 | WHERE poolId != NEW.poolId | ||
| 391 | AND | ||
| 392 | ( NEW.network >>= network | ||
| 393 | OR | ||
| 394 | NEW.network <<= network | ||
| 395 | ); | ||
| 396 | IF ( resultCount != 0 ) THEN | ||
| 397 | RAISE EXCEPTION 'conflict with existing network pools'; | ||
| 398 | END IF; | ||
| 399 | |||
| 400 | RETURN NEW; | ||
| 401 | END; | ||
| 402 | $$; | ||
| 403 | |||
| 404 | |||
| 405 | /* | ||
| 273 | * Creates entry for IP Pool | 406 | * Creates entry for IP Pool |
| 274 | * | 407 | * |
| 275 | * % SELECT pool_add( | 408 | * % SELECT pool_add( |
| @@ -373,4 +506,12 @@ CREATE TRIGGER ip_timestamp | |||
| 373 | EXECUTE PROCEDURE ip_timestamp(); | 506 | EXECUTE PROCEDURE ip_timestamp(); |
| 374 | 507 | ||
| 375 | 508 | ||
| 509 | -- netblock table triggers | ||
| 510 | CREATE TRIGGER netblock_trigger | ||
| 511 | BEFORE INSERT OR UPDATE | ||
| 512 | ON netblock | ||
| 513 | FOR EACH ROW | ||
| 514 | EXECUTE PROCEDURE netblock_trigger(); | ||
| 515 | |||
| 516 | |||
| 376 | /* end of sql */ | 517 | /* end of sql */ |
