- remove UNIQUE constraint on named keys from tables that support
[plcapi.git] / planetlab4.sql
1 --
2 -- PlanetLab Central database schema
3 -- Version 4, PostgreSQL
4 --
5 -- Aaron Klingaman <alk@cs.princeton.edu>
6 -- Reid Moran <rmoran@cs.princeton.edu>
7 -- Mark Huang <mlhuang@cs.princeton.edu>
8 -- Tony Mack <tmack@cs.princeton.edu>
9 --
10 -- Copyright (C) 2006 The Trustees of Princeton University
11 --
12 -- $Id: planetlab4.sql,v 1.2 2006/09/25 18:34:48 mlhuang Exp $
13 --
14
15 --------------------------------------------------------------------------------
16 -- Aggregates and store procedures
17 --------------------------------------------------------------------------------
18
19 -- Like MySQL GROUP_CONCAT(), this function aggregates values into a
20 -- PostgreSQL array.
21 CREATE AGGREGATE array_accum (
22     sfunc = array_append,
23     basetype = anyelement,
24     stype = anyarray,
25     initcond = '{}'
26 );
27
28 --------------------------------------------------------------------------------
29 -- Accounts
30 --------------------------------------------------------------------------------
31
32 -- Accounts
33 CREATE TABLE persons (
34     -- Mandatory
35     person_id serial PRIMARY KEY, -- Account identifier
36     email text NOT NULL, -- E-mail address
37     first_name text NOT NULL, -- First name
38     last_name text NOT NULL, -- Last name
39     deleted boolean NOT NULL DEFAULT false, -- Has been deleted
40     enabled boolean NOT NULL DEFAULT false, -- Has been disabled
41
42     -- Password
43     password text NOT NULL, -- Password (md5crypted)
44     verification_key text, -- Reset password key
45     verification_expires timestamp without time zone,
46
47     -- Optional
48     title text, -- Honorific
49     phone text, -- Telephone number
50     url text, -- Home page
51     bio text, -- Biography
52
53     -- Timestamps
54     date_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
55     last_updated timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP
56 );
57 CREATE INDEX persons_email_key ON persons (email);
58
59 --------------------------------------------------------------------------------
60 -- Sites
61 --------------------------------------------------------------------------------
62
63 -- Sites
64 CREATE TABLE sites (
65     -- Mandatory
66     site_id serial PRIMARY KEY, -- Site identifier
67     login_base text NOT NULL, -- Site slice prefix
68     name text NOT NULL, -- Site name
69     abbreviated_name text NOT NULL, -- Site abbreviated name
70     deleted boolean NOT NULL DEFAULT false, -- Has been deleted
71     is_public boolean NOT NULL DEFAULT true, -- Shows up in public lists
72     max_slices integer NOT NULL DEFAULT 0, -- Maximum number of slices
73     max_slivers integer NOT NULL DEFAULT 1000, -- Maximum number of instantiated slivers
74
75     -- Optional
76     latitude real,
77     longitude real,
78     url text,
79
80     -- Timestamps
81     date_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
82     last_updated timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP
83 );
84 CREATE INDEX sites_login_base_key ON sites (login_base);
85
86 -- Account site membership
87 CREATE TABLE person_site (
88     person_id integer REFERENCES persons, -- Account identifier
89     site_id integer REFERENCES sites, -- Site identifier
90     is_primary boolean NOT NULL DEFAULT false, -- Is the primary site for this account
91     PRIMARY KEY (person_id, site_id)
92 );
93 CREATE INDEX person_site_person_id_key ON person_site (person_id);
94 CREATE INDEX person_site_site_id_key ON person_site (site_id);
95
96 -- Ordered by primary site first
97 CREATE VIEW person_site_ordered AS
98 SELECT person_id, site_id
99 FROM person_site
100 ORDER BY is_primary DESC;
101
102 -- Sites that each person is a member of
103 CREATE VIEW person_sites AS
104 SELECT person_id,
105 array_to_string(array_accum(site_id), ',') AS site_ids
106 FROM person_site_ordered
107 GROUP BY person_id;
108
109 -- Accounts at each site
110 CREATE VIEW site_persons AS
111 SELECT site_id,
112 array_to_string(array_accum(person_id), ',') AS person_ids
113 FROM person_site
114 GROUP BY site_id;
115
116 --------------------------------------------------------------------------------
117 -- Mailing Addresses
118 --------------------------------------------------------------------------------
119
120 -- Mailing addresses
121 CREATE TABLE addresses (
122     address_id serial PRIMARY KEY, -- Address identifier
123     address_type text, -- Address type, e.g. shipping or billing
124     line1 text NOT NULL, -- Address line 1
125     line2 text, -- Address line 2
126     line3 text, -- Address line 3
127     city text NOT NULL, -- City
128     state text NOT NULL, -- State or province
129     postalcode text NOT NULL, -- Postal code
130     country text NOT NULL -- Country
131 );
132
133 -- Site mailing addresses
134 CREATE TABLE site_address (
135     site_id integer REFERENCES sites, -- Account identifier
136     address_id integer REFERENCES addresses, -- Address identifier
137     PRIMARY KEY (site_id, address_id)
138 );
139 CREATE INDEX site_address_site_id_key ON site_address (site_id);
140 CREATE INDEX site_address_address_id_key ON site_address (address_id);
141
142 CREATE VIEW site_addresses AS
143 SELECT site_id,
144 array_to_string(array_accum(address_id), ',') AS address_ids
145 FROM site_address
146 GROUP BY site_id;
147
148 --------------------------------------------------------------------------------
149 -- Authentication Keys
150 --------------------------------------------------------------------------------
151
152 -- Valid key types
153 CREATE TABLE key_types (
154     key_type text PRIMARY KEY -- Key type
155 );
156 INSERT INTO key_types (key_type) VALUES ('ssh');
157
158 -- Authentication keys
159 CREATE TABLE keys (
160     key_id serial PRIMARY KEY, -- Key identifier
161     key_type text REFERENCES key_types, -- Key type
162     key text NOT NULL, -- Key material
163     is_blacklisted boolean NOT NULL DEFAULT false -- Has been blacklisted
164 );
165
166 -- Account authentication key(s)
167 CREATE TABLE person_key (
168     person_id integer REFERENCES persons, -- Account identifier
169     key_id integer REFERENCES keys, -- Key identifier
170     is_primary boolean NOT NULL DEFAULT false, -- Is the primary key for this account
171     PRIMARY KEY (person_id, key_id)
172 );
173 CREATE INDEX person_key_person_id_key ON person_key (person_id);
174 CREATE INDEX person_key_key_id_key ON person_key (key_id);
175
176 CREATE VIEW person_keys AS
177 SELECT person_id,
178 array_to_string(array_accum(key_id), ',') AS key_ids
179 FROM person_key
180 GROUP BY person_id;
181
182 --------------------------------------------------------------------------------
183 -- Account roles
184 --------------------------------------------------------------------------------
185
186 -- Valid account roles
187 CREATE TABLE roles (
188     role_id integer PRIMARY KEY, -- Role identifier
189     name text UNIQUE NOT NULL -- Role symbolic name
190 );
191 INSERT INTO roles (role_id, name) VALUES (10, 'admin');
192 INSERT INTO roles (role_id, name) VALUES (20, 'pi');
193 INSERT INTO roles (role_id, name) VALUES (30, 'user');
194 INSERT INTO roles (role_id, name) VALUES (40, 'tech');
195 INSERT INTO roles (role_id, name) VALUES (1000, 'node');
196 INSERT INTO roles (role_id, name) VALUES (2000, 'anonymous');
197
198 CREATE TABLE person_role (
199     person_id integer REFERENCES persons, -- Account identifier
200     role_id integer REFERENCES roles, -- Role identifier
201     PRIMARY KEY (person_id, role_id)
202 );
203 CREATE INDEX person_role_person_id_key ON person_role (person_id);
204
205 -- Account roles
206 CREATE VIEW person_roles AS
207 SELECT person_id,
208 array_to_string(array_accum(role_id), ',') AS role_ids,
209 array_to_string(array_accum(roles.name), ',') AS roles
210 FROM person_role
211 LEFT JOIN roles USING (role_id)
212 GROUP BY person_id;
213
214 --------------------------------------------------------------------------------
215 -- Nodes
216 --------------------------------------------------------------------------------
217
218 -- Valid node boot states
219 CREATE TABLE boot_states (
220     boot_state text PRIMARY KEY
221 );
222 INSERT INTO boot_states (boot_state) VALUES ('boot');
223 INSERT INTO boot_states (boot_state) VALUES ('dbg');
224 INSERT INTO boot_states (boot_state) VALUES ('inst');
225 INSERT INTO boot_states (boot_state) VALUES ('rins');
226 INSERT INTO boot_states (boot_state) VALUES ('rcnf');
227 INSERT INTO boot_states (boot_state) VALUES ('new');
228
229 -- Nodes
230 CREATE TABLE nodes (
231     -- Mandatory
232     node_id serial PRIMARY KEY, -- Node identifier
233     hostname text NOT NULL, -- Node hostname
234     site_id integer REFERENCES sites, -- At which site
235     boot_state text REFERENCES boot_states, -- Node boot state
236     deleted boolean NOT NULL DEFAULT false, -- Is deleted
237
238     -- Optional
239     model text, -- Hardware make and model
240     boot_nonce text, -- Random nonce updated by Boot Manager
241     version text, -- Boot CD version string updated by Boot Manager
242     -- XXX Should be key_id integer REFERENCES keys
243     ssh_rsa_key text, -- SSH host key updated by Boot Manager
244     key text, -- Node key generated by API when configuration file is downloaded
245     session text, -- Session key generated by PLC when Boot Manager authenticates
246
247     -- Timestamps
248     date_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
249     last_updated timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP
250 );
251 CREATE INDEX nodes_hostname_key ON nodes (hostname);
252 CREATE INDEX nodes_site_id_key ON nodes (site_id);
253
254 -- Nodes at each site
255 CREATE VIEW site_nodes AS
256 SELECT site_id,
257 array_to_string(array_accum(node_id), ',') AS node_ids
258 FROM nodes
259 GROUP BY site_id;
260
261 --------------------------------------------------------------------------------
262 -- Node groups
263 --------------------------------------------------------------------------------
264
265 -- Node groups
266 CREATE TABLE nodegroups (
267     nodegroup_id serial PRIMARY KEY, -- Group identifier
268     name text UNIQUE NOT NULL, -- Group name
269     description text -- Group description
270 );
271
272 -- Node group membership
273 CREATE TABLE nodegroup_node (
274     nodegroup_id integer REFERENCES nodegroups, -- Group identifier
275     node_id integer REFERENCES nodes, -- Node identifier
276     PRIMARY KEY (nodegroup_id, node_id)
277 );
278 CREATE INDEX nodegroup_node_nodegroup_id_key ON nodegroup_node (nodegroup_id);
279 CREATE INDEX nodegroup_node_node_id_key ON nodegroup_node (node_id);
280
281 -- Nodes in each node gruop
282 CREATE VIEW nodegroup_nodes AS
283 SELECT nodegroup_id,
284 array_to_string(array_accum(node_id), ',') AS node_ids
285 FROM nodegroup_node
286 GROUP BY nodegroup_id;
287
288 -- Node groups that each node is a member of
289 CREATE VIEW node_nodegroups AS
290 SELECT node_id,
291 array_to_string(array_accum(nodegroup_id), ',') AS nodegroup_ids
292 FROM nodegroup_node
293 GROUP BY node_id;
294
295 --------------------------------------------------------------------------------
296 -- Node network interfaces
297 --------------------------------------------------------------------------------
298
299 -- Valid network addressing schemes
300 CREATE TABLE nodenetwork_types (
301     type text PRIMARY KEY -- Addressing scheme
302 );
303 INSERT INTO nodenetwork_types (type) VALUES ('ipv4');
304 INSERT INTO nodenetwork_types (type) VALUES ('ipv6');
305
306 -- Valid network configuration methods
307 CREATE TABLE nodenetwork_methods (
308     method text PRIMARY KEY -- Configuration method
309 );
310 INSERT INTO nodenetwork_methods (method) VALUES ('static');
311 INSERT INTO nodenetwork_methods (method) VALUES ('dhcp');
312 INSERT INTO nodenetwork_methods (method) VALUES ('proxy');
313 INSERT INTO nodenetwork_methods (method) VALUES ('tap');
314 INSERT INTO nodenetwork_methods (method) VALUES ('ipmi');
315 INSERT INTO nodenetwork_methods (method) VALUES ('unknown');
316
317 -- Node network interfaces
318 CREATE TABLE nodenetworks (
319     -- Mandatory
320     nodenetwork_id serial PRIMARY KEY, -- Network interface identifier
321     node_id integer REFERENCES nodes, -- Which node
322     is_primary boolean NOT NULL DEFAULT false, -- Is the primary interface for this node
323     type text REFERENCES nodenetwork_types, -- Addressing scheme
324     method text REFERENCES nodenetwork_methods, -- Configuration method
325
326     -- Optional, depending on type and method
327     ip text, -- IP address
328     mac text, -- MAC address
329     gateway text, -- Default gateway address
330     network text, -- Network address
331     broadcast text, -- Network broadcast address
332     netmask text, -- Network mask
333     dns1 text, -- Primary DNS server
334     dns2 text, -- Secondary DNS server
335     bwlimit integer, -- Bandwidth limit in bps
336     hostname text -- Hostname of this interface
337 );
338 CREATE INDEX nodenetworks_node_id_key ON nodenetworks (node_id);
339
340 -- Ordered by primary interface first
341 CREATE VIEW nodenetworks_ordered AS
342 SELECT node_id, nodenetwork_id
343 FROM nodenetworks
344 ORDER BY is_primary DESC;
345
346 -- Network interfaces on each node
347 CREATE VIEW node_nodenetworks AS
348 SELECT node_id,
349 array_to_string(array_accum(nodenetwork_id), ',') AS nodenetwork_ids
350 FROM nodenetworks_ordered
351 GROUP BY node_id;
352
353 --------------------------------------------------------------------------------
354 -- Slices
355 --------------------------------------------------------------------------------
356
357 CREATE TABLE slice_instantiations (
358     instantiation text PRIMARY KEY
359 );
360 INSERT INTO slice_instantiations (instantiation) VALUES ('not-instantiated'); -- Placeholder slice
361 INSERT INTO slice_instantiations (instantiation) VALUES ('plc-instantiated'); -- Instantiated by Node Manager
362 INSERT INTO slice_instantiations (instantiation) VALUES ('delegated'); -- Manually instantiated
363
364 -- Slices
365 CREATE TABLE slices (
366     slice_id serial PRIMARY KEY, -- Slice identifier
367     site_id integer REFERENCES sites, -- Site identifier
368     name text NOT NULL, -- Slice name
369     instantiation text REFERENCES slice_instantiations DEFAULT 'plc-instantiated', -- Slice state, e.g. plc-instantiated
370     url text, -- Project URL
371     description text, -- Project description
372
373     max_nodes integer NOT NULL DEFAULT 100, -- Maximum number of nodes that can be assigned to this slice
374
375     creator_person_id integer REFERENCES persons, -- Creator
376     created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, -- Creation date
377     expires timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP + '2 weeks', -- Expiration date
378
379     is_deleted boolean NOT NULL DEFAULT false
380 );
381 CREATE INDEX slices_site_id_key ON slices (site_id);
382 CREATE INDEX slices_name_key ON slices (name);
383
384 -- Slivers
385 CREATE TABLE slice_node (
386     slice_id integer REFERENCES slices, -- Slice identifier
387     node_id integer REFERENCES nodes -- Node identifier
388 );
389 CREATE INDEX slice_node_slice_id_key ON slice_node (slice_id);
390 CREATE INDEX slice_node_node_id_key ON slice_node (node_id);
391
392 -- Synonym for slice_node
393 CREATE VIEW slivers AS
394 SELECT * FROM slice_node;
395
396 -- Nodes in each slice
397 CREATE VIEW slice_nodes AS
398 SELECT slice_id,
399 array_to_string(array_accum(node_id), ',') AS node_ids
400 FROM slice_node
401 GROUP BY slice_id;
402
403 -- Slices on each node
404 CREATE VIEW node_slices AS
405 SELECT node_id,
406 array_to_string(array_accum(slice_id), ',') AS slice_ids
407 FROM slice_node
408 GROUP BY node_id;
409
410 -- Slices at each site
411 CREATE VIEW site_slices AS
412 SELECT site_id,
413 array_to_string(array_accum(slice_id), ',') AS slice_ids
414 FROM slices
415 GROUP BY site_id;
416
417 -- Slice membership
418 CREATE TABLE slice_person (
419     slice_id integer REFERENCES slices, -- Slice identifier
420     person_id integer REFERENCES persons, -- Account identifier
421     PRIMARY KEY (slice_id, person_id)
422 );
423 CREATE INDEX slice_person_slice_id_key ON slice_person (slice_id);
424 CREATE INDEX slice_person_person_id_key ON slice_person (person_id);
425
426 -- Members of the slice
427 CREATE VIEW slice_persons AS
428 SELECT slice_id,
429 array_to_string(array_accum(person_id), ',') AS person_ids
430 FROM slice_person
431 GROUP BY slice_id;
432
433 -- Slices of which each person is a member
434 CREATE VIEW person_slices AS
435 SELECT person_id,
436 array_to_string(array_accum(slice_id), ',') AS slice_ids
437 FROM slice_person
438 GROUP BY person_id;
439
440 --------------------------------------------------------------------------------
441 -- Slice attributes
442 --------------------------------------------------------------------------------
443
444 CREATE TABLE attributes (
445     attribute_id serial PRIMARY KEY, -- Attribute identifier
446     name text UNIQUE NOT NULL, -- Attribute name
447     description text, -- Attribute description
448     min_role_id integer REFERENCES roles -- Minimum (least powerful) role that can set or change this attribute
449 );
450
451 -- Slice/sliver attributes
452 CREATE TABLE slice_attribute (
453     slice_id integer REFERENCES slices, -- Slice identifier
454     attribute_id integer REFERENCES attributes, -- Attribute identifier
455     node_id integer, -- Sliver attribute if set
456     value text,
457     PRIMARY KEY (slice_id, attribute_id, node_id)
458 );
459 CREATE INDEX slice_attribute_slice_id_key ON slice_attribute (slice_id);
460 CREATE INDEX slice_attribute_node_id_key ON slice_attribute (node_id);
461
462 CREATE VIEW slice_attributes AS
463 SELECT slice_id,
464 array_to_string(array_accum(attribute_id), ',') AS attribute_ids
465 FROM slice_attribute
466 WHERE node_id IS NULL
467 GROUP BY slice_id;
468
469 -- No sliver_attributes view since it by definition requires a conditional on node_id
470
471 --------------------------------------------------------------------------------
472 -- Useful views
473 --------------------------------------------------------------------------------
474
475 CREATE VIEW view_persons AS
476 SELECT
477 persons.person_id,
478 persons.email,
479 persons.first_name,
480 persons.last_name,
481 persons.deleted,
482 persons.enabled,
483 persons.password,
484 persons.verification_key,
485 persons.verification_expires,
486 persons.title,
487 persons.phone,
488 persons.url,
489 persons.bio,
490 CAST(date_part('epoch', persons.date_created) AS bigint) AS date_created,
491 CAST(date_part('epoch', persons.last_updated) AS bigint) AS last_updated,
492 person_roles.role_ids, person_roles.roles,
493 person_sites.site_ids,
494 person_keys.key_ids,
495 person_slices.slice_ids
496 FROM persons
497 LEFT JOIN person_roles USING (person_id)
498 LEFT JOIN person_sites USING (person_id)
499 LEFT JOIN person_keys USING (person_id)
500 LEFT JOIN person_slices USING (person_id);
501
502 CREATE VIEW view_nodes AS
503 SELECT
504 nodes.node_id,
505 nodes.hostname,
506 nodes.site_id,
507 nodes.boot_state,
508 nodes.deleted,
509 nodes.model,
510 nodes.boot_nonce,
511 nodes.version,
512 nodes.ssh_rsa_key,
513 nodes.key,
514 nodes.session,
515 CAST(date_part('epoch', nodes.date_created) AS bigint) AS date_created,
516 CAST(date_part('epoch', nodes.last_updated) AS bigint) AS last_updated,
517 node_nodenetworks.nodenetwork_ids,
518 node_nodegroups.nodegroup_ids,
519 node_slices.slice_ids
520 FROM nodes
521 LEFT JOIN node_nodenetworks USING (node_id)
522 LEFT JOIN node_nodegroups USING (node_id)
523 LEFT JOIN node_slices USING (node_id);
524
525 CREATE VIEW view_nodegroups AS
526 SELECT
527 nodegroups.nodegroup_id,
528 nodegroups.name,
529 nodegroups.description,
530 nodegroup_nodes.node_ids
531 FROM nodegroups
532 LEFT JOIN nodegroup_nodes USING (nodegroup_id);
533
534 CREATE VIEW view_sites AS
535 SELECT
536 sites.site_id,
537 sites.login_base,
538 sites.name,
539 sites.abbreviated_name,
540 sites.deleted,
541 sites.is_public,
542 sites.max_slices,
543 sites.max_slivers,
544 sites.latitude,
545 sites.longitude,
546 sites.url,
547 CAST(date_part('epoch', sites.date_created) AS bigint) AS date_created,
548 CAST(date_part('epoch', sites.last_updated) AS bigint) AS last_updated,
549 site_persons.person_ids,
550 site_nodes.node_ids,
551 site_addresses.address_ids,
552 site_slices.slice_ids
553 FROM sites
554 LEFT JOIN site_persons USING (site_id)
555 LEFT JOIN site_nodes USING (site_id)
556 LEFT JOIN site_addresses USING (site_id)
557 LEFT JOIN site_slices USING (site_id);
558
559 CREATE VIEW view_addresses AS
560 SELECT
561 addresses.address_id,
562 addresses.address_type,
563 addresses.line1,
564 addresses.line2,
565 addresses.line3,
566 addresses.city,
567 addresses.state,
568 addresses.postalcode,
569 addresses.country,
570 site_address.site_id
571 FROM addresses
572 LEFT JOIN site_address USING (address_id);
573
574 CREATE VIEW view_slices AS
575 SELECT
576 slices.slice_id,
577 slices.site_id,
578 slices.name,
579 slices.instantiation,
580 slices.url,
581 slices.description,
582 slices.max_nodes,
583 slices.creator_person_id,
584 slices.is_deleted,
585 CAST(date_part('epoch', slices.created) AS bigint) AS created,
586 CAST(date_part('epoch', slices.expires) AS bigint) AS expires,
587 slice_nodes.node_ids,
588 slice_persons.person_ids,
589 slice_attributes.attribute_ids
590 FROM slices
591 LEFT JOIN slice_nodes USING (slice_id)
592 LEFT JOIN slice_persons USING (slice_id)
593 LEFT JOIN slice_attributes USING (slice_id);
594
595 --------------------------------------------------------------------------------
596 -- Built-in maintenance account and default site
597 --------------------------------------------------------------------------------
598
599 INSERT INTO persons
600 (first_name, last_name, email, password, enabled)
601 VALUES
602 ('Maintenance', 'Account', 'maint@localhost.localdomain', 'nopass', true);
603
604 INSERT INTO person_role (person_id, role_id) VALUES (1, 10);
605 INSERT INTO person_role (person_id, role_id) VALUES (1, 20);
606 INSERT INTO person_role (person_id, role_id) VALUES (1, 30);
607 INSERT INTO person_role (person_id, role_id) VALUES (1, 40);
608
609 INSERT INTO sites
610 (login_base, name, abbreviated_name, max_slices)
611 VALUES
612 ('pl', 'PlanetLab Central', 'PLC', 100);