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