Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[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 6903 2007-11-12 22:45:01Z tmack $
13 --
14
15 SET client_encoding = 'UNICODE';
16
17 --------------------------------------------------------------------------------
18 -- Aggregates and store procedures
19 --------------------------------------------------------------------------------
20
21 -- Like MySQL GROUP_CONCAT(), this function aggregates values into a
22 -- PostgreSQL array.
23 CREATE AGGREGATE array_accum (
24     sfunc = array_append,
25     basetype = anyelement,
26     stype = anyarray,
27     initcond = '{}'
28 );
29
30 --------------------------------------------------------------------------------
31 -- Version
32 --------------------------------------------------------------------------------
33
34 -- Database version
35 CREATE TABLE plc_db_version (
36     version integer NOT NULL,
37     subversion integer NOT NULL DEFAULT 0
38 ) WITH OIDS;
39
40 INSERT INTO plc_db_version (version, subversion) VALUES (4, 9);
41
42 --------------------------------------------------------------------------------
43 -- Accounts
44 --------------------------------------------------------------------------------
45
46 -- Accounts
47 CREATE TABLE persons (
48     -- Mandatory
49     person_id serial PRIMARY KEY, -- Account identifier
50     email text NOT NULL, -- E-mail address
51     first_name text NOT NULL, -- First name
52     last_name text NOT NULL, -- Last name
53     deleted boolean NOT NULL DEFAULT false, -- Has been deleted
54     enabled boolean NOT NULL DEFAULT false, -- Has been disabled
55
56     -- Password
57     password text NOT NULL DEFAULT 'nopass', -- Password (md5crypted)
58     verification_key text, -- Reset password key
59     verification_expires timestamp without time zone,
60
61     -- Optional
62     title text, -- Honorific
63     phone text, -- Telephone number
64     url text, -- Home page
65     bio text, -- Biography
66
67     -- Timestamps
68     date_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
69     last_updated timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP
70 ) WITH OIDS;
71 CREATE INDEX persons_email_idx ON persons (email) WHERE deleted IS false;
72
73 --------------------------------------------------------------------------------
74 -- Sites
75 --------------------------------------------------------------------------------
76
77 -- Sites
78 CREATE TABLE sites (
79     -- Mandatory
80     site_id serial PRIMARY KEY, -- Site identifier
81     login_base text NOT NULL, -- Site slice prefix
82     name text NOT NULL, -- Site name
83     abbreviated_name text NOT NULL, -- Site abbreviated name
84     enabled boolean NOT NULL Default true, -- Is this site enabled
85     deleted boolean NOT NULL DEFAULT false, -- Has been deleted
86     is_public boolean NOT NULL DEFAULT true, -- Shows up in public lists
87     max_slices integer NOT NULL DEFAULT 0, -- Maximum number of slices
88     max_slivers integer NOT NULL DEFAULT 1000, -- Maximum number of instantiated slivers
89
90     -- Optional
91     latitude real,
92     longitude real,
93     url text,
94     ext_consortium_id integer,  -- external consortium id
95
96     -- Timestamps
97     date_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
98     last_updated timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP
99 ) WITH OIDS;
100 CREATE INDEX sites_login_base_idx ON sites (login_base) WHERE deleted IS false;
101
102 -- Account site membership
103 CREATE TABLE person_site (
104     person_id integer REFERENCES persons NOT NULL, -- Account identifier
105     site_id integer REFERENCES sites NOT NULL, -- Site identifier
106     is_primary boolean NOT NULL DEFAULT false, -- Is the primary site for this account
107     PRIMARY KEY (person_id, site_id)
108 );
109 CREATE INDEX person_site_person_id_idx ON person_site (person_id);
110 CREATE INDEX person_site_site_id_idx ON person_site (site_id);
111
112 -- Ordered by primary site first
113 CREATE VIEW person_site_ordered AS
114 SELECT person_id, site_id
115 FROM person_site
116 ORDER BY is_primary DESC;
117
118 -- Sites that each person is a member of
119 CREATE VIEW person_sites AS
120 SELECT person_id,
121 array_accum(site_id) AS site_ids
122 FROM person_site_ordered
123 GROUP BY person_id;
124
125 -- Accounts at each site
126 CREATE VIEW site_persons AS
127 SELECT site_id,
128 array_accum(person_id) AS person_ids
129 FROM person_site
130 GROUP BY site_id;
131
132 --------------------------------------------------------------------------------
133 -- Mailing Addresses
134 --------------------------------------------------------------------------------
135
136 CREATE TABLE address_types (
137     address_type_id serial PRIMARY KEY, -- Address type identifier
138     name text UNIQUE NOT NULL, -- Address type
139     description text -- Address type description
140 ) WITH OIDS;
141
142 INSERT INTO address_types (name) VALUES ('Personal');
143 INSERT INTO address_types (name) VALUES ('Shipping');
144 -- XXX Used to be Site
145 INSERT INTO address_types (name) VALUES ('Billing');
146
147 -- Mailing addresses
148 CREATE TABLE addresses (
149     address_id serial PRIMARY KEY, -- Address identifier
150     line1 text NOT NULL, -- Address line 1
151     line2 text, -- Address line 2
152     line3 text, -- Address line 3
153     city text NOT NULL, -- City
154     state text NOT NULL, -- State or province
155     postalcode text NOT NULL, -- Postal code
156     country text NOT NULL -- Country
157 ) WITH OIDS;
158
159 -- Each mailing address can be one of several types
160 CREATE TABLE address_address_type (
161     address_id integer REFERENCES addresses NOT NULL, -- Address identifier
162     address_type_id integer REFERENCES address_types NOT NULL, -- Address type
163     PRIMARY KEY (address_id, address_type_id)
164 ) WITH OIDS;
165 CREATE INDEX address_address_type_address_id_idx ON address_address_type (address_id);
166 CREATE INDEX address_address_type_address_type_id_idx ON address_address_type (address_type_id);
167
168 CREATE VIEW address_address_types AS
169 SELECT address_id,
170 array_accum(address_type_id) AS address_type_ids,
171 array_accum(address_types.name) AS address_types
172 FROM address_address_type
173 LEFT JOIN address_types USING (address_type_id)
174 GROUP BY address_id;
175
176 CREATE TABLE site_address (
177     site_id integer REFERENCES sites NOT NULL, -- Site identifier
178     address_id integer REFERENCES addresses NOT NULL, -- Address identifier
179     PRIMARY KEY (site_id, address_id)
180 ) WITH OIDS;
181 CREATE INDEX site_address_site_id_idx ON site_address (site_id);
182 CREATE INDEX site_address_address_id_idx ON site_address (address_id);
183
184 CREATE VIEW site_addresses AS
185 SELECT site_id,
186 array_accum(address_id) AS address_ids
187 FROM site_address
188 GROUP BY site_id;
189
190 --------------------------------------------------------------------------------
191 -- Authentication Keys
192 --------------------------------------------------------------------------------
193
194 -- Valid key types
195 CREATE TABLE key_types (
196     key_type text PRIMARY KEY -- Key type
197 ) WITH OIDS;
198 INSERT INTO key_types (key_type) VALUES ('ssh');
199
200 -- Authentication keys
201 CREATE TABLE keys (
202     key_id serial PRIMARY KEY, -- Key identifier
203     key_type text REFERENCES key_types NOT NULL, -- Key type
204     key text NOT NULL, -- Key material
205     is_blacklisted boolean NOT NULL DEFAULT false -- Has been blacklisted
206 ) WITH OIDS;
207
208 -- Account authentication key(s)
209 CREATE TABLE person_key (
210     key_id integer REFERENCES keys PRIMARY KEY, -- Key identifier
211     person_id integer REFERENCES persons NOT NULL -- Account identifier
212 ) WITH OIDS;
213 CREATE INDEX person_key_person_id_idx ON person_key (person_id);
214
215 CREATE VIEW person_keys AS
216 SELECT person_id,
217 array_accum(key_id) AS key_ids
218 FROM person_key
219 GROUP BY person_id;
220
221 --------------------------------------------------------------------------------
222 -- Account roles
223 --------------------------------------------------------------------------------
224
225 -- Valid account roles
226 CREATE TABLE roles (
227     role_id integer PRIMARY KEY, -- Role identifier
228     name text UNIQUE NOT NULL -- Role symbolic name
229 ) WITH OIDS;
230 INSERT INTO roles (role_id, name) VALUES (10, 'admin');
231 INSERT INTO roles (role_id, name) VALUES (20, 'pi');
232 INSERT INTO roles (role_id, name) VALUES (30, 'user');
233 INSERT INTO roles (role_id, name) VALUES (40, 'tech');
234
235 CREATE TABLE person_role (
236     person_id integer REFERENCES persons NOT NULL, -- Account identifier
237     role_id integer REFERENCES roles NOT NULL, -- Role identifier
238     PRIMARY KEY (person_id, role_id)
239 ) WITH OIDS;
240 CREATE INDEX person_role_person_id_idx ON person_role (person_id);
241
242 -- Account roles
243 CREATE VIEW person_roles AS
244 SELECT person_id,
245 array_accum(role_id) AS role_ids,
246 array_accum(roles.name) AS roles
247 FROM person_role
248 LEFT JOIN roles USING (role_id)
249 GROUP BY person_id;
250
251 --------------------------------------------------------------------------------
252 -- Nodes
253 --------------------------------------------------------------------------------
254
255 -- Valid node boot states
256 CREATE TABLE boot_states (
257     boot_state text PRIMARY KEY
258 ) WITH OIDS;
259 INSERT INTO boot_states (boot_state) VALUES ('boot');
260 INSERT INTO boot_states (boot_state) VALUES ('dbg');
261 INSERT INTO boot_states (boot_state) VALUES ('inst');
262 INSERT INTO boot_states (boot_state) VALUES ('rins');
263 INSERT INTO boot_states (boot_state) VALUES ('rcnf');
264 INSERT INTO boot_states (boot_state) VALUES ('new');
265
266 -- Nodes
267 CREATE TABLE nodes (
268     -- Mandatory
269     node_id serial PRIMARY KEY, -- Node identifier
270     hostname text NOT NULL, -- Node hostname
271     site_id integer REFERENCES sites NOT NULL, -- At which site 
272
273     boot_state text REFERENCES boot_states NOT NULL DEFAULT 'inst', -- Node boot state
274     deleted boolean NOT NULL DEFAULT false, -- Is deleted
275
276     -- Optional
277     model text, -- Hardware make and model
278     boot_nonce text, -- Random nonce updated by Boot Manager
279     version text, -- Boot CD version string updated by Boot Manager
280     ssh_rsa_key text, -- SSH host key updated by Boot Manager
281     key text, -- Node key generated by API when configuration file is downloaded
282
283     -- Timestamps
284     date_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
285     last_updated timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
286     last_contact timestamp without time zone    
287 ) WITH OIDS;
288 CREATE INDEX nodes_hostname_idx ON nodes (hostname) WHERE deleted IS false;
289 CREATE INDEX nodes_site_id_idx ON nodes (site_id) WHERE deleted IS false;
290
291 -- Nodes at each site
292 CREATE VIEW site_nodes AS
293 SELECT site_id,
294 array_accum(node_id) AS node_ids
295 FROM nodes
296 WHERE deleted IS false
297 GROUP BY site_id;
298
299 --------------------------------------------------------------------------------
300 -- Node groups
301 --------------------------------------------------------------------------------
302
303 -- Node groups
304 CREATE TABLE nodegroups (
305     nodegroup_id serial PRIMARY KEY, -- Group identifier
306     name text UNIQUE NOT NULL, -- Group name
307     description text -- Group description
308 ) WITH OIDS;
309
310 -- Node group membership
311 CREATE TABLE nodegroup_node (
312     nodegroup_id integer REFERENCES nodegroups NOT NULL, -- Group identifier
313     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
314     PRIMARY KEY (nodegroup_id, node_id)
315 ) WITH OIDS;
316 CREATE INDEX nodegroup_node_nodegroup_id_idx ON nodegroup_node (nodegroup_id);
317 CREATE INDEX nodegroup_node_node_id_idx ON nodegroup_node (node_id);
318
319 -- Nodes in each node group
320 CREATE VIEW nodegroup_nodes AS
321 SELECT nodegroup_id,
322 array_accum(node_id) AS node_ids
323 FROM nodegroup_node
324 GROUP BY nodegroup_id;
325
326 -- Node groups that each node is a member of
327 CREATE VIEW node_nodegroups AS
328 SELECT node_id,
329 array_accum(nodegroup_id) AS nodegroup_ids
330 FROM nodegroup_node
331 GROUP BY node_id;
332
333 --------------------------------------------------------------------------------
334 -- Node configuration files
335 --------------------------------------------------------------------------------
336
337 CREATE TABLE conf_files (
338     conf_file_id serial PRIMARY KEY, -- Configuration file identifier
339     enabled bool NOT NULL DEFAULT true, -- Configuration file is active
340     source text NOT NULL, -- Relative path on the boot server where file can be downloaded
341     dest text NOT NULL, -- Absolute path where file should be installed
342     file_permissions text NOT NULL DEFAULT '0644', -- chmod(1) permissions
343     file_owner text NOT NULL DEFAULT 'root', -- chown(1) owner
344     file_group text NOT NULL DEFAULT 'root', -- chgrp(1) owner
345     preinstall_cmd text, -- Shell command to execute prior to installing
346     postinstall_cmd text, -- Shell command to execute after installing
347     error_cmd text, -- Shell command to execute if any error occurs
348     ignore_cmd_errors bool NOT NULL DEFAULT false, -- Install file anyway even if an error occurs
349     always_update bool NOT NULL DEFAULT false -- Always attempt to install file even if unchanged
350 );
351
352 CREATE TABLE conf_file_node (
353     conf_file_id integer REFERENCES conf_files NOT NULL, -- Configuration file identifier
354     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
355     PRIMARY KEY (conf_file_id, node_id)
356 );
357 CREATE INDEX conf_file_node_conf_file_id_idx ON conf_file_node (conf_file_id);
358 CREATE INDEX conf_file_node_node_id_idx ON conf_file_node (node_id);
359
360 -- Nodes linked to each configuration file
361 CREATE VIEW conf_file_nodes AS
362 SELECT conf_file_id,
363 array_accum(node_id) AS node_ids
364 FROM conf_file_node
365 GROUP BY conf_file_id;
366
367 -- Configuration files linked to each node
368 CREATE VIEW node_conf_files AS
369 SELECT node_id,
370 array_accum(conf_file_id) AS conf_file_ids
371 FROM conf_file_node
372 GROUP BY node_id;
373
374 CREATE TABLE conf_file_nodegroup (
375     conf_file_id integer REFERENCES conf_files NOT NULL, -- Configuration file identifier
376     nodegroup_id integer REFERENCES nodegroups NOT NULL, -- Node group identifier
377     PRIMARY KEY (conf_file_id, nodegroup_id)
378 );
379 CREATE INDEX conf_file_nodegroup_conf_file_id_idx ON conf_file_nodegroup (conf_file_id);
380 CREATE INDEX conf_file_nodegroup_nodegroup_id_idx ON conf_file_nodegroup (nodegroup_id);
381
382 -- Node groups linked to each configuration file
383 CREATE VIEW conf_file_nodegroups AS
384 SELECT conf_file_id,
385 array_accum(nodegroup_id) AS nodegroup_ids
386 FROM conf_file_nodegroup
387 GROUP BY conf_file_id;
388
389 -- Configuration files linked to each node group
390 CREATE VIEW nodegroup_conf_files AS
391 SELECT nodegroup_id,
392 array_accum(conf_file_id) AS conf_file_ids
393 FROM conf_file_nodegroup
394 GROUP BY nodegroup_id;
395
396 --------------------------------------------------------------------------------
397 -- Node network interfaces
398 --------------------------------------------------------------------------------
399
400 -- Valid network addressing schemes
401 CREATE TABLE network_types (
402     type text PRIMARY KEY -- Addressing scheme
403 ) WITH OIDS;
404 INSERT INTO network_types (type) VALUES ('ipv4');
405
406 -- Valid network configuration methods
407 CREATE TABLE network_methods (
408     method text PRIMARY KEY -- Configuration method
409 ) WITH OIDS;
410 INSERT INTO network_methods (method) VALUES ('static');
411 INSERT INTO network_methods (method) VALUES ('dhcp');
412 INSERT INTO network_methods (method) VALUES ('proxy');
413 INSERT INTO network_methods (method) VALUES ('tap');
414 INSERT INTO network_methods (method) VALUES ('ipmi');
415 INSERT INTO network_methods (method) VALUES ('unknown');
416
417 -- Node network interfaces
418 CREATE TABLE nodenetworks (
419     -- Mandatory
420     nodenetwork_id serial PRIMARY KEY, -- Network interface identifier
421     node_id integer REFERENCES nodes NOT NULL, -- Which node
422     is_primary boolean NOT NULL DEFAULT false, -- Is the primary interface for this node
423     type text REFERENCES network_types NOT NULL, -- Addressing scheme
424     method text REFERENCES network_methods NOT NULL, -- Configuration method
425
426     -- Optional, depending on type and method
427     ip text, -- IP address
428     mac text, -- MAC address
429     gateway text, -- Default gateway address
430     network text, -- Network address
431     broadcast text, -- Network broadcast address
432     netmask text, -- Network mask
433     dns1 text, -- Primary DNS server
434     dns2 text, -- Secondary DNS server
435     bwlimit integer, -- Bandwidth limit in bps
436     hostname text -- Hostname of this interface
437 ) WITH OIDS;
438 CREATE INDEX nodenetworks_node_id_idx ON nodenetworks (node_id);
439
440 -- Ordered by primary interface first
441 CREATE VIEW nodenetworks_ordered AS
442 SELECT node_id, nodenetwork_id
443 FROM nodenetworks
444 ORDER BY is_primary DESC;
445
446 -- Network interfaces on each node
447 CREATE VIEW node_nodenetworks AS
448 SELECT node_id,
449 array_accum(nodenetwork_id) AS nodenetwork_ids
450 FROM nodenetworks_ordered
451 GROUP BY node_id;
452
453 --------------------------------------------------------------------------------
454 -- Nodenetwork setting types and nodenetworks settings
455 --------------------------------------------------------------------------------
456
457 CREATE TABLE nodenetwork_setting_types (
458     nodenetwork_setting_type_id serial PRIMARY KEY,     
459                                                 -- Setting Type Identifier
460     name text UNIQUE NOT NULL,                  -- Setting Name    
461     description text,                           -- Optional Description
462     category text NOT NULL,                     -- Category, e.g. Wifi, or whatever
463     min_role_id integer references roles        -- If set, minimal role required
464 ) WITH OIDS;
465
466 CREATE TABLE nodenetwork_setting (
467     nodenetwork_setting_id serial PRIMARY KEY,  -- Nodenetwork Setting Identifier
468     nodenetwork_id integer REFERENCES nodenetworks NOT NULL,
469                                                 -- the nodenetwork this applies to
470     nodenetwork_setting_type_id integer REFERENCES nodenetwork_setting_types NOT NULL,
471                                                 -- the setting type
472     value text
473 ) WITH OIDS;
474
475 CREATE OR REPLACE VIEW nodenetwork_settings AS 
476 SELECT nodenetwork_id,
477 array_accum(nodenetwork_setting_id) AS nodenetwork_setting_ids
478 FROM nodenetwork_setting
479 GROUP BY nodenetwork_id;
480
481 CREATE OR REPLACE VIEW view_nodenetwork_settings AS
482 SELECT
483 nodenetwork_setting.nodenetwork_setting_id,
484 nodenetwork_setting.nodenetwork_id,
485 nodenetwork_setting_types.nodenetwork_setting_type_id,
486 nodenetwork_setting_types.name,
487 nodenetwork_setting_types.description,
488 nodenetwork_setting_types.category,
489 nodenetwork_setting_types.min_role_id,
490 nodenetwork_setting.value
491 FROM nodenetwork_setting
492 INNER JOIN nodenetwork_setting_types USING (nodenetwork_setting_type_id);
493
494 CREATE OR REPLACE VIEW view_nodenetworks AS
495 SELECT
496 nodenetworks.nodenetwork_id,
497 nodenetworks.node_id,
498 nodenetworks.is_primary,
499 nodenetworks.type,
500 nodenetworks.method,
501 nodenetworks.ip,
502 nodenetworks.mac,
503 nodenetworks.gateway,
504 nodenetworks.network,
505 nodenetworks.broadcast,
506 nodenetworks.netmask,
507 nodenetworks.dns1,
508 nodenetworks.dns2,
509 nodenetworks.bwlimit,
510 nodenetworks.hostname,
511 COALESCE((SELECT nodenetwork_setting_ids FROM nodenetwork_settings WHERE nodenetwork_settings.nodenetwork_id = nodenetworks.nodenetwork_id), '{}') AS nodenetwork_setting_ids
512 FROM nodenetworks;
513
514 --------------------------------------------------------------------------------
515 -- Power control units (PCUs)
516 --------------------------------------------------------------------------------
517
518 CREATE TABLE pcus (
519     -- Mandatory
520     pcu_id serial PRIMARY KEY, -- PCU identifier
521     site_id integer REFERENCES sites NOT NULL, -- Site identifier
522     hostname text, -- Hostname, not necessarily unique (multiple logical sites could use the same PCU)
523     ip text NOT NULL, -- IP, not necessarily unique
524
525     -- Optional
526     protocol text, -- Protocol, e.g. ssh or https or telnet
527     username text, -- Username, if applicable
528     "password" text, -- Password, if applicable
529     model text, -- Model, e.g. BayTech or iPal
530     notes text -- Random notes
531 ) WITH OIDS;
532 CREATE INDEX pcus_site_id_idx ON pcus (site_id);
533
534 CREATE VIEW site_pcus AS
535 SELECT site_id,
536 array_accum(pcu_id) AS pcu_ids
537 FROM pcus
538 GROUP BY site_id;
539
540 CREATE TABLE pcu_node (
541     pcu_id integer REFERENCES pcus NOT NULL, -- PCU identifier
542     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
543     port integer NOT NULL, -- Port number
544     PRIMARY KEY (pcu_id, node_id), -- The same node cannot be controlled by different ports
545     UNIQUE (pcu_id, port) -- The same port cannot control multiple nodes
546 );
547 CREATE INDEX pcu_node_pcu_id_idx ON pcu_node (pcu_id);
548 CREATE INDEX pcu_node_node_id_idx ON pcu_node (node_id);
549
550 CREATE VIEW node_pcus AS
551 SELECT node_id,
552 array_accum(pcu_id) AS pcu_ids,
553 array_accum(port) AS ports
554 FROM pcu_node
555 GROUP BY node_id;
556
557 CREATE VIEW pcu_nodes AS
558 SELECT pcu_id,
559 array_accum(node_id) AS node_ids,
560 array_accum(port) AS ports
561 FROM pcu_node
562 GROUP BY pcu_id;
563
564 --------------------------------------------------------------------------------
565 -- Slices
566 --------------------------------------------------------------------------------
567
568 CREATE TABLE slice_instantiations (
569     instantiation text PRIMARY KEY
570 ) WITH OIDS;
571 INSERT INTO slice_instantiations (instantiation) VALUES ('not-instantiated'); -- Placeholder slice
572 INSERT INTO slice_instantiations (instantiation) VALUES ('plc-instantiated'); -- Instantiated by Node Manager
573 INSERT INTO slice_instantiations (instantiation) VALUES ('delegated'); -- Manually instantiated
574 INSERT INTO slice_instantiations (instantiation) VALUES ('nm-controller'); -- NM Controller
575
576 -- Slices
577 CREATE TABLE slices (
578     slice_id serial PRIMARY KEY, -- Slice identifier
579     site_id integer REFERENCES sites NOT NULL, -- Site identifier
580
581     name text NOT NULL, -- Slice name
582     instantiation text REFERENCES slice_instantiations NOT NULL DEFAULT 'plc-instantiated', -- Slice state, e.g. plc-instantiated
583     url text, -- Project URL
584     description text, -- Project description
585
586     max_nodes integer NOT NULL DEFAULT 100, -- Maximum number of nodes that can be assigned to this slice
587
588     creator_person_id integer REFERENCES persons, -- Creator
589     created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, -- Creation date
590     expires timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP + '2 weeks', -- Expiration date
591
592     is_deleted boolean NOT NULL DEFAULT false
593 ) WITH OIDS;
594 CREATE INDEX slices_site_id_idx ON slices (site_id) WHERE is_deleted IS false;
595 CREATE INDEX slices_name_idx ON slices (name) WHERE is_deleted IS false;
596
597 -- Slivers
598 CREATE TABLE slice_node (
599     slice_id integer REFERENCES slices NOT NULL, -- Slice identifier
600     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
601     PRIMARY KEY (slice_id, node_id)
602 ) WITH OIDS;
603 CREATE INDEX slice_node_slice_id_idx ON slice_node (slice_id);
604 CREATE INDEX slice_node_node_id_idx ON slice_node (node_id);
605
606 -- Synonym for slice_node
607 CREATE VIEW slivers AS
608 SELECT * FROM slice_node;
609
610 -- Nodes in each slice
611 CREATE VIEW slice_nodes AS
612 SELECT slice_id,
613 array_accum(node_id) AS node_ids
614 FROM slice_node
615 GROUP BY slice_id;
616
617 -- Slices on each node
618 CREATE VIEW node_slices AS
619 SELECT node_id,
620 array_accum(slice_id) AS slice_ids
621 FROM slice_node
622 GROUP BY node_id;
623
624 -- Slices at each site
625 CREATE VIEW site_slices AS
626 SELECT site_id,
627 array_accum(slice_id) AS slice_ids
628 FROM slices
629 WHERE is_deleted is false
630 GROUP BY site_id;
631
632 -- Slice membership
633 CREATE TABLE slice_person (
634     slice_id integer REFERENCES slices NOT NULL, -- Slice identifier
635     person_id integer REFERENCES persons NOT NULL, -- Account identifier
636     PRIMARY KEY (slice_id, person_id)
637 ) WITH OIDS;
638 CREATE INDEX slice_person_slice_id_idx ON slice_person (slice_id);
639 CREATE INDEX slice_person_person_id_idx ON slice_person (person_id);
640
641 -- Members of the slice
642 CREATE VIEW slice_persons AS
643 SELECT slice_id,
644 array_accum(person_id) AS person_ids
645 FROM slice_person
646 GROUP BY slice_id;
647
648 -- Slices of which each person is a member
649 CREATE VIEW person_slices AS
650 SELECT person_id,
651 array_accum(slice_id) AS slice_ids
652 FROM slice_person
653 GROUP BY person_id;
654
655 --------------------------------------------------------------------------------
656 -- Slice whitelist
657 --------------------------------------------------------------------------------
658 -- slice whitelist on nodes
659 CREATE TABLE node_slice_whitelist (
660     node_id integer REFERENCES nodes NOT NULL, -- Node id of whitelist
661     slice_id integer REFERENCES slices NOT NULL, -- Slice id thats allowd on this node
662     PRIMARY KEY (node_id, slice_id)
663 ) WITH OIDS;
664 CREATE INDEX node_slice_whitelist_node_id_idx ON node_slice_whitelist (node_id);
665 CREATE INDEX node_slice_whitelist_slice_id_idx ON node_slice_whitelist (slice_id);
666
667 -- Slices on each node
668 CREATE VIEW node_slices_whitelist AS
669 SELECT node_id,
670 array_accum(slice_id) AS slice_ids_whitelist
671 FROM node_slice_whitelist
672 GROUP BY node_id;
673
674 --------------------------------------------------------------------------------
675 -- Slice attributes
676 --------------------------------------------------------------------------------
677
678 -- Slice attribute types
679 CREATE TABLE slice_attribute_types (
680     attribute_type_id serial PRIMARY KEY, -- Attribute type identifier
681     name text UNIQUE NOT NULL, -- Attribute name
682     description text, -- Attribute description
683     min_role_id integer REFERENCES roles DEFAULT 10 -- If set, minimum (least powerful) role that can set or change this attribute
684 ) WITH OIDS;
685
686 -- Slice/sliver attributes
687 CREATE TABLE slice_attribute (
688     slice_attribute_id serial PRIMARY KEY, -- Slice attribute identifier
689     slice_id integer REFERENCES slices NOT NULL, -- Slice identifier
690     node_id integer REFERENCES nodes, -- Sliver attribute if set
691     nodegroup_id integer REFERENCES nodegroups, -- Node group attribute if set
692     attribute_type_id integer REFERENCES slice_attribute_types NOT NULL, -- Attribute type identifier
693     value text
694 ) WITH OIDS;
695 CREATE INDEX slice_attribute_slice_id_idx ON slice_attribute (slice_id);
696 CREATE INDEX slice_attribute_node_id_idx ON slice_attribute (node_id);
697 CREATE INDEX slice_attribute_nodegroup_id_idx ON slice_attribute (nodegroup_id);
698
699 CREATE VIEW slice_attributes AS
700 SELECT slice_id,
701 array_accum(slice_attribute_id) AS slice_attribute_ids
702 FROM slice_attribute
703 GROUP BY slice_id;
704
705 --------------------------------------------------------------------------------
706 -- Initscripts
707 --------------------------------------------------------------------------------
708
709 -- Initscripts
710 CREATE TABLE initscripts (
711     initscript_id serial PRIMARY KEY, -- Initscript identifier
712     name text NOT NULL, -- Initscript name
713     enabled bool NOT NULL DEFAULT true, -- Initscript is active
714     script text NOT NULL, -- Initscript
715     UNIQUE (name)
716 ) WITH OIDS;
717 CREATE INDEX initscripts_name_idx ON initscripts (name);
718
719
720 --------------------------------------------------------------------------------
721 -- Peers
722 --------------------------------------------------------------------------------
723
724 -- Peers
725 CREATE TABLE peers (
726     peer_id serial PRIMARY KEY, -- Peer identifier
727     peername text NOT NULL, -- Peer name
728     peer_url text NOT NULL, -- (HTTPS) URL of the peer PLCAPI interface
729     cacert text, -- (SSL) Public certificate of peer API server
730     key text, -- (GPG) Public key used for authentication
731     deleted boolean NOT NULL DEFAULT false
732 ) WITH OIDS;
733 CREATE INDEX peers_peername_idx ON peers (peername) WHERE deleted IS false;
734
735 -- Objects at each peer
736 CREATE TABLE peer_site (
737     site_id integer REFERENCES sites PRIMARY KEY, -- Local site identifier
738     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
739     peer_site_id integer NOT NULL, -- Foreign site identifier at peer
740     UNIQUE (peer_id, peer_site_id) -- The same foreign site should not be cached twice
741 ) WITH OIDS;
742 CREATE INDEX peer_site_peer_id_idx ON peers (peer_id);
743
744 CREATE VIEW peer_sites AS
745 SELECT peer_id,
746 array_accum(site_id) AS site_ids,
747 array_accum(peer_site_id) AS peer_site_ids
748 FROM peer_site
749 GROUP BY peer_id;
750
751 CREATE TABLE peer_person (
752     person_id integer REFERENCES persons PRIMARY KEY, -- Local user identifier
753     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
754     peer_person_id integer NOT NULL, -- Foreign user identifier at peer
755     UNIQUE (peer_id, peer_person_id) -- The same foreign user should not be cached twice
756 ) WITH OIDS;
757 CREATE INDEX peer_person_peer_id_idx ON peer_person (peer_id);
758
759 CREATE VIEW peer_persons AS
760 SELECT peer_id,
761 array_accum(person_id) AS person_ids,
762 array_accum(peer_person_id) AS peer_person_ids
763 FROM peer_person
764 GROUP BY peer_id;
765
766 CREATE TABLE peer_key (
767     key_id integer REFERENCES keys PRIMARY KEY, -- Local key identifier
768     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
769     peer_key_id integer NOT NULL, -- Foreign key identifier at peer
770     UNIQUE (peer_id, peer_key_id) -- The same foreign key should not be cached twice
771 ) WITH OIDS;
772 CREATE INDEX peer_key_peer_id_idx ON peer_key (peer_id);
773
774 CREATE VIEW peer_keys AS
775 SELECT peer_id,
776 array_accum(key_id) AS key_ids,
777 array_accum(peer_key_id) AS peer_key_ids
778 FROM peer_key
779 GROUP BY peer_id;
780
781 CREATE TABLE peer_node (
782     node_id integer REFERENCES nodes PRIMARY KEY, -- Local node identifier
783     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
784     peer_node_id integer NOT NULL, -- Foreign node identifier
785     UNIQUE (peer_id, peer_node_id) -- The same foreign node should not be cached twice
786 ) WITH OIDS;
787 CREATE INDEX peer_node_peer_id_idx ON peer_node (peer_id);
788
789 CREATE VIEW peer_nodes AS
790 SELECT peer_id,
791 array_accum(node_id) AS node_ids,
792 array_accum(peer_node_id) AS peer_node_ids
793 FROM peer_node
794 GROUP BY peer_id;
795
796 CREATE TABLE peer_slice (
797     slice_id integer REFERENCES slices PRIMARY KEY, -- Local slice identifier
798     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
799     peer_slice_id integer NOT NULL, -- Slice identifier at peer
800     UNIQUE (peer_id, peer_slice_id) -- The same foreign slice should not be cached twice
801 ) WITH OIDS;
802 CREATE INDEX peer_slice_peer_id_idx ON peer_slice (peer_id);
803
804 CREATE VIEW peer_slices AS
805 SELECT peer_id,
806 array_accum(slice_id) AS slice_ids,
807 array_accum(peer_slice_id) AS peer_slice_ids
808 FROM peer_slice
809 GROUP BY peer_id;
810
811 --------------------------------------------------------------------------------
812 -- Authenticated sessions
813 --------------------------------------------------------------------------------
814
815 -- Authenticated sessions
816 CREATE TABLE sessions (
817     session_id text PRIMARY KEY, -- Session identifier
818     expires timestamp without time zone
819 ) WITH OIDS;
820
821 -- People can have multiple sessions
822 CREATE TABLE person_session (
823     person_id integer REFERENCES persons NOT NULL, -- Account identifier
824     session_id text REFERENCES sessions NOT NULL, -- Session identifier
825     PRIMARY KEY (person_id, session_id),
826     UNIQUE (session_id) -- Sessions are unique
827 ) WITH OIDS;
828 CREATE INDEX person_session_person_id_idx ON person_session (person_id);
829
830 -- Nodes can have only one session
831 CREATE TABLE node_session (
832     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
833     session_id text REFERENCES sessions NOT NULL, -- Session identifier
834     UNIQUE (node_id), -- Nodes can have only one session
835     UNIQUE (session_id) -- Sessions are unique
836 ) WITH OIDS;
837
838
839
840 -------------------------------------------------------------------------------
841 -- PCU Types
842 ------------------------------------------------------------------------------
843 CREATE TABLE pcu_types (
844     pcu_type_id serial PRIMARY KEY,
845     model text NOT NULL ,               -- PCU model name
846     name text                           -- Full PCU model name
847 ) WITH OIDS;
848 CREATE INDEX pcu_types_model_idx ON pcu_types (model);
849
850 CREATE TABLE pcu_protocol_type (
851     pcu_protocol_type_id serial PRIMARY KEY,
852     pcu_type_id integer REFERENCES pcu_types NOT NULL,  -- PCU type identifier
853     port integer NOT NULL,                              -- PCU port
854     protocol text NOT NULL,                             -- Protocol
855     supported boolean NOT NULL DEFAULT True             -- Does PLC support
856 ) WITH OIDS;
857 CREATE INDEX pcu_protocol_type_pcu_type_id ON pcu_protocol_type (pcu_type_id);
858
859
860 CREATE OR REPLACE VIEW pcu_protocol_types AS
861 SELECT pcu_type_id,
862 array_accum(pcu_protocol_type_id) as pcu_protocol_type_ids
863 FROM pcu_protocol_type
864 GROUP BY pcu_type_id;
865
866 --------------------------------------------------------------------------------
867 -- Message templates
868 --------------------------------------------------------------------------------
869
870 CREATE TABLE messages (
871     message_id text PRIMARY KEY, -- Message name
872     subject text, -- Message summary
873     template text, -- Message template
874     enabled bool NOT NULL DEFAULT true -- Whether message is enabled
875 ) WITH OIDS;
876
877 --------------------------------------------------------------------------------
878 -- Events
879 --------------------------------------------------------------------------------
880
881 -- Events
882 CREATE TABLE events (
883     event_id serial PRIMARY KEY,  -- Event identifier
884     person_id integer REFERENCES persons, -- Person responsible for event, if any
885     node_id integer REFERENCES nodes, -- Node responsible for event, if any
886     auth_type text, -- Type of auth used. i.e. AuthMethod
887     fault_code integer NOT NULL DEFAULT 0, -- Did this event result in error
888     call_name text NOT NULL, -- Call responsible for this event
889     call text NOT NULL, -- Call responsible for this event, including parameters
890     message text, -- High level description of this event
891     runtime float DEFAULT 0, -- Event run time
892     time timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP -- Event timestamp
893 ) WITH OIDS;
894
895 -- Database object(s) that may have been affected by a particular event
896 CREATE TABLE event_object (
897     event_id integer REFERENCES events NOT NULL, -- Event identifier
898     object_id integer NOT NULL, -- Object identifier
899     object_type text NOT NULL Default 'Unknown' -- What type of object is this event affecting
900 ) WITH OIDS;
901 CREATE INDEX event_object_event_id_idx ON event_object (event_id);
902 CREATE INDEX event_object_object_id_idx ON event_object (object_id);
903 CREATE INDEX event_object_object_type_idx ON event_object (object_type);
904
905 CREATE OR REPLACE VIEW event_objects AS
906 SELECT event_id,
907 array_accum(object_id) AS object_ids,
908 array_accum(object_type) AS object_types
909 FROM event_object
910 GROUP BY event_id;
911
912 --------------------------------------------------------------------------------
913 -- Useful views
914 --------------------------------------------------------------------------------
915 CREATE OR REPLACE VIEW view_pcu_types AS
916 SELECT
917 pcu_types.pcu_type_id,
918 pcu_types.model,
919 pcu_types.name,
920 COALESCE((SELECT pcu_protocol_type_ids FROM pcu_protocol_types WHERE pcu_protocol_types.pcu_type_id = pcu_types.pcu_type_id), '{}') AS pcu_protocol_type_ids
921 FROM pcu_types;
922
923 CREATE OR REPLACE VIEW view_events AS
924 SELECT
925 events.event_id,
926 events.person_id,
927 events.node_id,
928 events.auth_type,
929 events.fault_code,
930 events.call_name,
931 events.call,
932 events.message,
933 events.runtime,
934 CAST(date_part('epoch', events.time) AS bigint) AS time,
935 COALESCE((SELECT object_ids FROM event_objects WHERE event_objects.event_id = events.event_id), '{}') AS object_ids,
936 COALESCE((SELECT object_types FROM event_objects WHERE event_objects.event_id = events.event_id), '{}') AS object_types
937 FROM events;
938
939 CREATE OR REPLACE VIEW view_event_objects AS 
940 SELECT
941 events.event_id,
942 events.person_id,
943 events.node_id,
944 events.fault_code,
945 events.call_name,
946 events.call,
947 events.message,
948 events.runtime,
949 CAST(date_part('epoch', events.time) AS bigint) AS time,
950 event_object.object_id,
951 event_object.object_type
952 FROM events LEFT JOIN event_object USING (event_id);
953
954 CREATE OR REPLACE VIEW view_persons AS
955 SELECT
956 persons.person_id,
957 persons.email,
958 persons.first_name,
959 persons.last_name,
960 persons.deleted,
961 persons.enabled,
962 persons.password,
963 persons.verification_key,
964 CAST(date_part('epoch', persons.verification_expires) AS bigint) AS verification_expires,
965 persons.title,
966 persons.phone,
967 persons.url,
968 persons.bio,
969 CAST(date_part('epoch', persons.date_created) AS bigint) AS date_created,
970 CAST(date_part('epoch', persons.last_updated) AS bigint) AS last_updated,
971 peer_person.peer_id,
972 peer_person.peer_person_id,
973 COALESCE((SELECT role_ids FROM person_roles WHERE person_roles.person_id = persons.person_id), '{}') AS role_ids,
974 COALESCE((SELECT roles FROM person_roles WHERE person_roles.person_id = persons.person_id), '{}') AS roles,
975 COALESCE((SELECT site_ids FROM person_sites WHERE person_sites.person_id = persons.person_id), '{}') AS site_ids,
976 COALESCE((SELECT key_ids FROM person_keys WHERE person_keys.person_id = persons.person_id), '{}') AS key_ids,
977 COALESCE((SELECT slice_ids FROM person_slices WHERE person_slices.person_id = persons.person_id), '{}') AS slice_ids
978 FROM persons
979 LEFT JOIN peer_person USING (person_id);
980
981 CREATE OR REPLACE VIEW view_peers AS
982 SELECT 
983 peers.*, 
984 COALESCE((SELECT site_ids FROM peer_sites WHERE peer_sites.peer_id = peers.peer_id), '{}') AS site_ids,
985 COALESCE((SELECT peer_site_ids FROM peer_sites WHERE peer_sites.peer_id = peers.peer_id), '{}') AS peer_site_ids,
986 COALESCE((SELECT person_ids FROM peer_persons WHERE peer_persons.peer_id = peers.peer_id), '{}') AS person_ids,
987 COALESCE((SELECT peer_person_ids FROM peer_persons WHERE peer_persons.peer_id = peers.peer_id), '{}') AS peer_person_ids,
988 COALESCE((SELECT key_ids FROM peer_keys WHERE peer_keys.peer_id = peers.peer_id), '{}') AS key_ids,
989 COALESCE((SELECT peer_key_ids FROM peer_keys WHERE peer_keys.peer_id = peers.peer_id), '{}') AS peer_key_ids,
990 COALESCE((SELECT node_ids FROM peer_nodes WHERE peer_nodes.peer_id = peers.peer_id), '{}') AS node_ids,
991 COALESCE((SELECT peer_node_ids FROM peer_nodes WHERE peer_nodes.peer_id = peers.peer_id), '{}') AS peer_node_ids,
992 COALESCE((SELECT slice_ids FROM peer_slices WHERE peer_slices.peer_id = peers.peer_id), '{}') AS slice_ids,
993 COALESCE((SELECT peer_slice_ids FROM peer_slices WHERE peer_slices.peer_id = peers.peer_id), '{}') AS peer_slice_ids
994 FROM peers;
995
996 CREATE OR REPLACE VIEW view_nodes AS
997 SELECT
998 nodes.node_id,
999 nodes.hostname,
1000 nodes.site_id,
1001 nodes.boot_state,
1002 nodes.deleted,
1003 nodes.model,
1004 nodes.boot_nonce,
1005 nodes.version,
1006 nodes.ssh_rsa_key,
1007 nodes.key,
1008 CAST(date_part('epoch', nodes.date_created) AS bigint) AS date_created,
1009 CAST(date_part('epoch', nodes.last_updated) AS bigint) AS last_updated,
1010 CAST(date_part('epoch', nodes.last_contact) AS bigint) AS last_contact,  
1011 peer_node.peer_id,
1012 peer_node.peer_node_id,
1013 COALESCE((SELECT nodenetwork_ids FROM node_nodenetworks WHERE node_nodenetworks.node_id = nodes.node_id), '{}') AS nodenetwork_ids,
1014 COALESCE((SELECT nodegroup_ids FROM node_nodegroups WHERE node_nodegroups.node_id = nodes.node_id), '{}') AS nodegroup_ids,
1015 COALESCE((SELECT slice_ids FROM node_slices WHERE node_slices.node_id = nodes.node_id), '{}') AS slice_ids,
1016 COALESCE((SELECT slice_ids_whitelist FROM node_slices_whitelist WHERE node_slices_whitelist.node_id = nodes.node_id), '{}') AS slice_ids_whitelist,
1017 COALESCE((SELECT pcu_ids FROM node_pcus WHERE node_pcus.node_id = nodes.node_id), '{}') AS pcu_ids,
1018 COALESCE((SELECT ports FROM node_pcus WHERE node_pcus.node_id = nodes.node_id), '{}') AS ports,
1019 COALESCE((SELECT conf_file_ids FROM node_conf_files WHERE node_conf_files.node_id = nodes.node_id), '{}') AS conf_file_ids,
1020 node_session.session_id AS session
1021 FROM nodes
1022 LEFT JOIN peer_node USING (node_id)
1023 LEFT JOIN node_session USING (node_id);
1024
1025 CREATE OR REPLACE VIEW view_nodegroups AS
1026 SELECT
1027 nodegroups.*,
1028 COALESCE((SELECT node_ids FROM nodegroup_nodes WHERE nodegroup_nodes.nodegroup_id = nodegroups.nodegroup_id), '{}') AS node_ids,
1029 COALESCE((SELECT conf_file_ids FROM nodegroup_conf_files WHERE nodegroup_conf_files.nodegroup_id = nodegroups.nodegroup_id), '{}') AS conf_file_ids
1030 FROM nodegroups;
1031
1032 CREATE OR REPLACE VIEW view_conf_files AS
1033 SELECT
1034 conf_files.*,
1035 COALESCE((SELECT node_ids FROM conf_file_nodes WHERE conf_file_nodes.conf_file_id = conf_files.conf_file_id), '{}') AS node_ids,
1036 COALESCE((SELECT nodegroup_ids FROM conf_file_nodegroups WHERE conf_file_nodegroups.conf_file_id = conf_files.conf_file_id), '{}') AS nodegroup_ids
1037 FROM conf_files;
1038
1039 CREATE OR REPLACE VIEW view_pcus AS
1040 SELECT
1041 pcus.*,
1042 COALESCE((SELECT node_ids FROM pcu_nodes WHERE pcu_nodes.pcu_id = pcus.pcu_id), '{}') AS node_ids,
1043 COALESCE((SELECT ports FROM pcu_nodes WHERE pcu_nodes.pcu_id = pcus.pcu_id), '{}') AS ports
1044 FROM pcus;
1045
1046 CREATE OR REPLACE VIEW view_sites AS
1047 SELECT
1048 sites.site_id,
1049 sites.login_base,
1050 sites.name,
1051 sites.abbreviated_name,
1052 sites.deleted,
1053 sites.enabled,
1054 sites.is_public,
1055 sites.max_slices,
1056 sites.max_slivers,
1057 sites.latitude,
1058 sites.longitude,
1059 sites.url,
1060 sites.ext_consortium_id,
1061 CAST(date_part('epoch', sites.date_created) AS bigint) AS date_created,
1062 CAST(date_part('epoch', sites.last_updated) AS bigint) AS last_updated,
1063 peer_site.peer_id,
1064 peer_site.peer_site_id,
1065 COALESCE((SELECT person_ids FROM site_persons WHERE site_persons.site_id = sites.site_id), '{}') AS person_ids,
1066 COALESCE((SELECT node_ids FROM site_nodes WHERE site_nodes.site_id = sites.site_id), '{}') AS node_ids,
1067 COALESCE((SELECT address_ids FROM site_addresses WHERE site_addresses.site_id = sites.site_id), '{}') AS address_ids,
1068 COALESCE((SELECT slice_ids FROM site_slices WHERE site_slices.site_id = sites.site_id), '{}') AS slice_ids,
1069 COALESCE((SELECT pcu_ids FROM site_pcus WHERE site_pcus.site_id = sites.site_id), '{}') AS pcu_ids
1070 FROM sites
1071 LEFT JOIN peer_site USING (site_id);
1072
1073 CREATE OR REPLACE VIEW view_addresses AS
1074 SELECT
1075 addresses.*,
1076 COALESCE((SELECT address_type_ids FROM address_address_types WHERE address_address_types.address_id = addresses.address_id), '{}') AS address_type_ids,
1077 COALESCE((SELECT address_types FROM address_address_types WHERE address_address_types.address_id = addresses.address_id), '{}') AS address_types
1078 FROM addresses;
1079
1080 CREATE OR REPLACE VIEW view_keys AS
1081 SELECT
1082 keys.*,
1083 person_key.person_id,
1084 peer_key.peer_id,
1085 peer_key.peer_key_id
1086 FROM keys
1087 LEFT JOIN person_key USING (key_id)
1088 LEFT JOIN peer_key USING (key_id);
1089
1090 CREATE OR REPLACE VIEW view_slices AS
1091 SELECT
1092 slices.slice_id,
1093 slices.site_id,
1094 slices.name,
1095 slices.instantiation,
1096 slices.url,
1097 slices.description,
1098 slices.max_nodes,
1099 slices.creator_person_id,
1100 slices.is_deleted,
1101 CAST(date_part('epoch', slices.created) AS bigint) AS created,
1102 CAST(date_part('epoch', slices.expires) AS bigint) AS expires,
1103 peer_slice.peer_id,
1104 peer_slice.peer_slice_id,
1105 COALESCE((SELECT node_ids FROM slice_nodes WHERE slice_nodes.slice_id = slices.slice_id), '{}') AS node_ids,
1106 COALESCE((SELECT person_ids FROM slice_persons WHERE slice_persons.slice_id = slices.slice_id), '{}') AS person_ids,
1107 COALESCE((SELECT slice_attribute_ids FROM slice_attributes WHERE slice_attributes.slice_id = slices.slice_id), '{}') AS slice_attribute_ids
1108 FROM slices
1109 LEFT JOIN peer_slice USING (slice_id);
1110
1111 CREATE OR REPLACE VIEW view_slice_attributes AS
1112 SELECT
1113 slice_attribute.slice_attribute_id,
1114 slice_attribute.slice_id,
1115 slice_attribute.node_id,
1116 slice_attribute.nodegroup_id,
1117 slice_attribute_types.attribute_type_id,
1118 slice_attribute_types.name,
1119 slice_attribute_types.description,
1120 slice_attribute_types.min_role_id,
1121 slice_attribute.value
1122 FROM slice_attribute
1123 INNER JOIN slice_attribute_types USING (attribute_type_id);
1124
1125 CREATE OR REPLACE VIEW view_sessions AS
1126 SELECT
1127 sessions.session_id,
1128 CAST(date_part('epoch', sessions.expires) AS bigint) AS expires,
1129 person_session.person_id,
1130 node_session.node_id
1131 FROM sessions
1132 LEFT JOIN person_session USING (session_id)
1133 LEFT JOIN node_session USING (session_id);
1134
1135 --------------------------------------------------------------------------------
1136 -- Built-in maintenance account and default site
1137 --------------------------------------------------------------------------------
1138
1139 INSERT INTO persons
1140 (first_name, last_name, email, password, enabled)
1141 VALUES
1142 ('Maintenance', 'Account', 'maint@localhost.localdomain', 'nopass', true);
1143
1144 INSERT INTO person_role (person_id, role_id) VALUES (1, 10);
1145 INSERT INTO person_role (person_id, role_id) VALUES (1, 20);
1146 INSERT INTO person_role (person_id, role_id) VALUES (1, 30);
1147 INSERT INTO person_role (person_id, role_id) VALUES (1, 40);
1148
1149 INSERT INTO sites
1150 (login_base, name, abbreviated_name, max_slices)
1151 VALUES
1152 ('pl', 'PlanetLab Central', 'PLC', 100);