aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid M. Syzdek <syzdek@bindlebinaries.com>2019-05-07 15:07:34 -0800
committerDavid M. Syzdek <syzdek@bindlebinaries.com>2019-05-07 15:07:34 -0800
commitff321c3edfdc93988f7bb7496c8b79a8c4caef45 (patch)
tree745cf886a32e57f4f97a471b0539c9608427aefc
parent886e6b18f3298ba4eefbb50a55bf36309692655f (diff)
downloadscratchpad-ff321c3edfdc93988f7bb7496c8b79a8c4caef45.tar.gz
scratchpad-ff321c3edfdc93988f7bb7496c8b79a8c4caef45.tar.bz2
scratchpad-ff321c3edfdc93988f7bb7496c8b79a8c4caef45.tar.xz
scratchpad-ff321c3edfdc93988f7bb7496c8b79a8c4caef45.zip
Cleaning up ippool sql
-rw-r--r--notes/projects/radius/sql/ippool-database.sql35
-rw-r--r--notes/projects/radius/sql/ippool-schema.sql233
2 files changed, 222 insertions, 46 deletions
diff --git a/notes/projects/radius/sql/ippool-database.sql b/notes/projects/radius/sql/ippool-database.sql
index ed188cd..00c0889 100644
--- a/notes/projects/radius/sql/ippool-database.sql
+++ b/notes/projects/radius/sql/ippool-database.sql
@@ -6,22 +6,25 @@
6 */ 6 */
7 7
8 8
9CREATE ROLE radius_ippool WITH 9SET ROLE wheel;
10 NOSUPERUSER 10
11 NOCREATEDB 11
12 NOCREATEROLE 12CREATE ROLE radius_ippool
13 NOINHERIT 13 WITH NOSUPERUSER
14 NOLOGIN 14 NOCREATEDB
15 NOREPLICATION 15 NOCREATEROLE
16 NOBYPASSRLS 16 NOINHERIT
17 ADMIN admin; 17 NOLOGIN
18 18 NOREPLICATION
19 19 NOBYPASSRLS
20CREATE DATABASE radius_ippool WITH 20 ADMIN admin;
21 OWNER = radius_ippool 21
22 ENCODING = "UTF8" 22
23 LC_COLLATE = "en_US.UTF-8" 23CREATE DATABASE radius_ippool
24 LC_CTYPE = "en_US.UTF-8"; 24 WITH OWNER = radius_ippool
25 ENCODING = "UTF8"
26 LC_COLLATE = "en_US.UTF-8"
27 LC_CTYPE = "en_US.UTF-8";
25 28
26 29
27/* end of sql */ 30/* end of sql */
diff --git a/notes/projects/radius/sql/ippool-schema.sql b/notes/projects/radius/sql/ippool-schema.sql
index 485a5cc..fdc5bee 100644
--- a/notes/projects/radius/sql/ippool-schema.sql
+++ b/notes/projects/radius/sql/ippool-schema.sql
@@ -3,62 +3,235 @@
3 * 3 *
4 * ippool-schema.sql - builds database schema for RADIUS IP Pools 4 * ippool-schema.sql - builds database schema for RADIUS IP Pools
5 * 5 *
6 * Usage: psql -f ippool-schema.sql radius_ippool
6 */ 7 */
7 8
8 9
9SET ROLE radius_ippool; 10SET ROLE radius_ippool;
10 11
11 12
13-- //////////////
14-- // //
15-- // Tables //
16-- // //
17-- //////////////
18
19
20/*
21 * RADIUS Pool Type
22 *
23 * id
24 * : The primary key of a specific entry
25 *
26 * name
27 * : the pool name/type
28 *
29 * description
30 * : human readable description of pool
31 */
12CREATE TABLE IF NOT EXISTS pool 32CREATE TABLE IF NOT EXISTS pool
13( 33(
14 id SERIAL PRIMARY KEY, 34 id SERIAL,
15 name VARCHAR(32) NOT NULL UNIQUE 35 name VARCHAR(32) NOT NULL UNIQUE,
36 description VARCHAR(128),
37 PRIMARY KEY ( id )
16); 38);
39CREATE INDEX
40 IF NOT EXISTS pool_index
41 ON pool
42 ( id );
43
17 44
45/*
46 * RADIUS geographic region
47 *
48 * id
49 * : The primary key of a specific entry
50 *
51 * name
52 * : the region's name/locality
53 *
54 * description
55 * : human readable description of region
56 */
18CREATE TABLE IF NOT EXISTS region 57CREATE TABLE IF NOT EXISTS region
19( 58(
20 id SERIAL PRIMARY KEY, 59 id SERIAL,
21 name VARCHAR(32) NOT NULL, 60 name VARCHAR(32) NOT NULL,
22 description VARCHAR(128) 61 description VARCHAR(128),
62 PRIMARY KEY ( id )
23); 63);
64CREATE INDEX
65 IF NOT EXISTS region_index
66 ON region
67 ( id );
68
24 69
70/*
71 * RADIUS NAS client
72 *
73 * id
74 * : The primary key of a specific entry
75 *
76 * name
77 * : the NAS truncated DNS name (i.e. br1.cwc)
78 *
79 * regionId
80 * : reference to the region in which the NAS is physically located
81 */
25CREATE TABLE IF NOT EXISTS nas 82CREATE TABLE IF NOT EXISTS nas
26( 83(
27 id SERIAL PRIMARY KEY, 84 id SERIAL,
28 name VARCHAR(32) NOT NULL, 85 name VARCHAR(32) NOT NULL,
29 regionId INTEGER NOT NULL, 86 regionId INTEGER NOT NULL,
30 FOREIGN KEY (regionId) REFERENCES region (id) 87 PRIMARY KEY ( id ),
88 FOREIGN KEY ( regionId )
89 REFERENCES region
90 ( id )
31); 91);
92CREATE INDEX
93 IF NOT EXISTS nas_index
94 ON nas
95 ( id,
96 regionId
97 );
98
32 99
100/*
101 * RADIUS network block
102 *
103 * id
104 * : The primary key of a specific entry
105 *
106 * poolId
107 * : reference to the pool type of the network
108 *
109 * regionId
110 * : reference to the physical region served by the block
111 *
112 * net
113 * : network address and network size of block
114 *
115 * active
116 * : assignable to customer if true, disabled/inactive if false
117 *
118 * description
119 * : human readable description of network block
120 */
33CREATE TABLE IF NOT EXISTS block 121CREATE TABLE IF NOT EXISTS block
34( 122(
35 id SERIAL PRIMARY KEY, 123 id SERIAL,
36 poolId INTEGER NOT NULL, 124 poolId INTEGER NOT NULL,
37 regionId INTEGER NOT NULL, 125 regionId INTEGER,
38 net INET NOT NULL UNIQUE, 126 net CIDR NOT NULL UNIQUE,
39 active BOOLEAN NOT NULL DEFAULT TRUE, 127 active BOOLEAN NOT NULL DEFAULT TRUE,
40 description VARCHAR(32), 128 description VARCHAR(32),
41 FOREIGN KEY (poolId) REFERENCES pool (id), 129 PRIMARY KEY ( id ),
42 FOREIGN KEY (regionId) REFERENCES region (id) 130 FOREIGN KEY ( poolId )
131 REFERENCES pool
132 ( id ),
133 FOREIGN KEY ( regionId )
134 REFERENCES region
135 ( id )
43); 136);
137CREATE INDEX
138 IF NOT EXISTS block_index
139 ON block
140 ( id,
141 poolId,
142 net,
143 active
144 );
145
44 146
45CREATE TABLE IF NOT EXISTS address 147/*
148 * RADIUS user-assignable IP address
149 *
150 * address
151 * : user assignable IP address
152 *
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
160 * : timestamp generated when entry is modified
161 *
162 * username
163 * : username currently assigned or last assigned address
164 */
165CREATE TABLE IF NOT EXISTS ip
46( 166(
47 address INET PRIMARY KEY, 167 address INET,
48 poolId INTEGER NOT NULL, 168 poolId INTEGER NOT NULL,
49 assigned BOOLEAN NOT NULL DEFAULT FALSE, 169 assigned BOOLEAN NOT NULL DEFAULT FALSE,
50 lastChanged timestamp WITH TIME ZONE NOT NULL DEFAULT TIMESTAMP 'epoch', 170 modified timestamp WITH TIME ZONE NOT NULL
51 username VARCHAR(64), 171 DEFAULT TIMESTAMP 'epoch',
52 FOREIGN KEY (poolId) REFERENCES pool (id) 172 username VARCHAR(64),
173 PRIMARY KEY ( address ),
174 FOREIGN KEY ( poolId )
175 REFERENCES pool
176 ( id )
53); 177);
178CREATE INDEX
179 IF NOT EXISTS ip_index
180 ON ip
181 ( address,
182 poolId,
183 assigned,
184 modified,
185 username
186 );
187
188
189-- /////////////////
190-- // //
191-- // Functions //
192-- // //
193-- /////////////////
194
195
196/*
197 * updates timestamp of addresses
198 *
199 * returns trigger
200 */
201CREATE OR REPLACE FUNCTION ip_timestamp
202( )
203 RETURNS trigger
204 LANGUAGE plpgsql
205 AS $$
206DECLARE
207BEGIN
208
209 -- prevents IP address from being modified
210 IF ( NEW.address != OLD.address ) THEN
211 RAISE EXCEPTION 'address cannot be modified';
212 END IF;
213
214 -- update timestamp
215 NEW.lastModified := current_timestamp;
216
217 RETURN NEW;
218END;
219$$;
220
221
54 222
223-- ////////////////
224-- // //
225-- // Triggers //
226-- // //
227-- ////////////////
55 228
56CREATE INDEX IF NOT EXISTS pool_index ON pool ( id ); 229-- address table triggers
57CREATE INDEX IF NOT EXISTS region_index ON region ( id ); 230CREATE TRIGGER ip_timestamp
58CREATE INDEX IF NOT EXISTS nas_index ON nas ( id, regionId ); 231 BEFORE UPDATE
59CREATE INDEX IF NOT EXISTS block_index ON block ( id, poolId, net, active ); 232 ON ip
60CREATE INDEX IF NOT EXISTS address_index ON address ( address, poolId, 233 FOR EACH ROW
61 assigned, lastChanged, lastuser ); 234 EXECUTE PROCEDURE ip_timestamp();
62 235
63 236
64/* end of sql */ 237/* end of sql */