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