renaming SliceAttribute into SliceTag and InterfaceSetting into InterfaceTag
[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
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 -- Nodes
268 CREATE TABLE nodes (
269     -- Mandatory
270     node_id serial PRIMARY KEY,                         -- Node identifier
271     hostname text NOT NULL,                             -- Node hostname
272     site_id integer REFERENCES sites NOT NULL,          -- At which site 
273
274     boot_state text REFERENCES boot_states NOT NULL     -- Node boot state
275                DEFAULT 'install', 
276     deleted boolean NOT NULL DEFAULT false,             -- Is deleted
277
278     -- Optional
279     model text,                                         -- Hardware make and model
280     boot_nonce text,                                    -- Random nonce updated by Boot Manager
281     version text,                                       -- Boot CD version string updated by Boot Manager
282     ssh_rsa_key text,                                   -- SSH host key updated by Boot Manager
283     key text,                                           -- Node key generated when boot file is downloaded
284
285     -- Timestamps
286     date_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
287     last_updated timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
288     last_contact timestamp without time zone    
289 ) WITH OIDS;
290 CREATE INDEX nodes_hostname_idx ON nodes (hostname);
291 CREATE INDEX nodes_site_id_idx ON nodes (site_id);
292
293 -- Nodes at each site
294 CREATE OR REPLACE VIEW site_nodes AS
295 SELECT site_id,
296 array_accum(node_id) AS node_ids
297 FROM nodes
298 WHERE deleted IS false
299 GROUP BY site_id;
300
301 --------------------------------------------------------------------------------
302 -- node tags
303 --------------------------------------------------------------------------------
304 CREATE TABLE tag_types (
305
306     tag_type_id serial PRIMARY KEY,                     -- ID
307     tagname text UNIQUE NOT NULL,                       -- Tag Name
308     description text,                                   -- Optional Description
309     min_role_id integer REFERENCES roles DEFAULT 10,    -- set minimal role required
310     category text NOT NULL DEFAULT 'general'            -- Free text for grouping tags together
311 ) WITH OIDS;
312
313 CREATE TABLE node_tag (
314     node_tag_id serial PRIMARY KEY,                     -- ID
315     node_id integer REFERENCES nodes NOT NULL,          -- node id
316     tag_type_id integer REFERENCES tag_types,           -- tag type id
317     tagvalue text                                       -- value attached
318 ) WITH OIDS;
319
320 CREATE OR REPLACE VIEW node_tags AS
321 SELECT node_id,
322 array_accum(node_tag_id) AS tag_ids
323 FROM node_tag
324 GROUP BY node_id;
325
326 CREATE OR REPLACE VIEW view_node_tags AS
327 SELECT
328 node_tag.node_tag_id,
329 node_tag.node_id,
330 nodes.hostname,
331 tag_types.tag_type_id,
332 tag_types.tagname,
333 tag_types.description,
334 tag_types.category,
335 tag_types.min_role_id,
336 node_tag.tagvalue
337 FROM node_tag 
338 INNER JOIN tag_types USING (tag_type_id)
339 INNER JOIN nodes USING (node_id);
340
341 --------------------------------------------------------------------------------
342 -- (network) interfaces
343 --------------------------------------------------------------------------------
344
345 -- Valid network addressing schemes
346 CREATE TABLE network_types (
347     type text PRIMARY KEY -- Addressing scheme
348 ) WITH OIDS;
349 INSERT INTO network_types (type) VALUES ('ipv4');
350
351 -- Valid network configuration methods
352 CREATE TABLE network_methods (
353     method text PRIMARY KEY -- Configuration method
354 ) WITH OIDS;
355
356 INSERT INTO network_methods (method) VALUES ('static');
357 INSERT INTO network_methods (method) VALUES ('dhcp');
358 INSERT INTO network_methods (method) VALUES ('proxy');
359 INSERT INTO network_methods (method) VALUES ('tap');
360 INSERT INTO network_methods (method) VALUES ('ipmi');
361 INSERT INTO network_methods (method) VALUES ('unknown');
362
363 -- Node network interfaces
364 CREATE TABLE interfaces (
365     -- Mandatory
366     interface_id serial PRIMARY KEY,                    -- Network interface identifier
367     node_id integer REFERENCES nodes NOT NULL,          -- Which node
368     is_primary boolean NOT NULL DEFAULT false,          -- Is the primary interface for this node
369     type text REFERENCES network_types NOT NULL,        -- Addressing scheme
370     method text REFERENCES network_methods NOT NULL,    -- Configuration method
371
372     -- Optional, depending on type and method
373     ip text,                                            -- IP address
374     mac text,                                           -- MAC address
375     gateway text,                                       -- Default gateway address
376     network text,                                       -- Network address
377     broadcast text,                                     -- Network broadcast address
378     netmask text,                                       -- Network mask
379     dns1 text,                                          -- Primary DNS server
380     dns2 text,                                          -- Secondary DNS server
381     bwlimit integer,                                    -- Bandwidth limit in bps
382     hostname text                                       -- Hostname of this interface
383 ) WITH OIDS;
384 CREATE INDEX interfaces_node_id_idx ON interfaces (node_id);
385
386 -- Ordered by primary interface first
387 CREATE OR REPLACE VIEW interfaces_ordered AS
388 SELECT node_id, interface_id
389 FROM interfaces
390 ORDER BY is_primary DESC;
391
392 -- Network interfaces on each node
393 CREATE OR REPLACE VIEW node_interfaces AS
394 SELECT node_id,
395 array_accum(interface_id) AS interface_ids
396 FROM interfaces_ordered
397 GROUP BY node_id;
398
399 --------------------------------------------------------------------------------
400 -- Interface settings
401 --------------------------------------------------------------------------------
402
403 CREATE TABLE interface_tag (
404     interface_tag_id serial PRIMARY KEY,                -- Interface Setting Identifier
405     interface_id integer REFERENCES interfaces NOT NULL,-- the interface this applies to
406     tag_type_id integer REFERENCES tag_types NOT NULL,  -- the setting type
407     value text                                          -- value attached
408 ) WITH OIDS;
409
410 CREATE OR REPLACE VIEW interface_tags AS 
411 SELECT interface_id,
412 array_accum(interface_tag_id) AS interface_tag_ids
413 FROM interface_tag
414 GROUP BY interface_id;
415
416 CREATE OR REPLACE VIEW view_interface_tags AS
417 SELECT
418 interface_tag.interface_tag_id,
419 interface_tag.interface_id,
420 tag_types.tag_type_id,
421 tag_types.tagname,
422 tag_types.description,
423 tag_types.category,
424 tag_types.min_role_id,
425 interface_tag.value
426 FROM interface_tag
427 INNER JOIN tag_types USING (tag_type_id);
428
429 CREATE OR REPLACE VIEW view_interfaces AS
430 SELECT
431 interfaces.interface_id,
432 interfaces.node_id,
433 interfaces.is_primary,
434 interfaces.type,
435 interfaces.method,
436 interfaces.ip,
437 interfaces.mac,
438 interfaces.gateway,
439 interfaces.network,
440 interfaces.broadcast,
441 interfaces.netmask,
442 interfaces.dns1,
443 interfaces.dns2,
444 interfaces.bwlimit,
445 interfaces.hostname,
446 COALESCE((SELECT interface_tag_ids FROM interface_tags WHERE interface_tags.interface_id = interfaces.interface_id), '{}') AS interface_tag_ids
447 FROM interfaces;
448
449 --------------------------------------------------------------------------------
450 -- ilinks : links between interfaces
451 --------------------------------------------------------------------------------
452 CREATE TABLE ilink (
453        ilink_id serial PRIMARY KEY,                             -- id
454        tag_type_id integer REFERENCES tag_types,                -- id of the tag 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 tag_types 
462 INNER JOIN ilink USING (tag_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.tag_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     tag_type_id integer REFERENCES tag_types,   -- node is in nodegroup if it has this tag defined
491     -- can be null, make management faster & easier
492     tagvalue text                               -- with this value attached
493 ) WITH OIDS;
494
495 -- xxx - first rough implem. similar to former semantics but might be slow
496 CREATE OR REPLACE VIEW nodegroup_node AS
497 SELECT nodegroup_id, node_id 
498 FROM tag_types 
499 JOIN node_tag 
500 USING (tag_type_id) 
501 JOIN nodegroups 
502 USING (tag_type_id,tagvalue);
503
504 CREATE OR REPLACE VIEW nodegroup_nodes AS
505 SELECT nodegroup_id,
506 array_accum(node_id) AS node_ids
507 FROM nodegroup_node
508 GROUP BY nodegroup_id;
509
510 -- Node groups that each node is a member of
511 CREATE OR REPLACE VIEW node_nodegroups AS
512 SELECT node_id,
513 array_accum(nodegroup_id) AS nodegroup_ids
514 FROM nodegroup_node
515 GROUP BY node_id;
516
517 --------------------------------------------------------------------------------
518 -- Node configuration files
519 --------------------------------------------------------------------------------
520
521 CREATE TABLE conf_files (
522     conf_file_id serial PRIMARY KEY,                    -- Configuration file identifier
523     enabled bool NOT NULL DEFAULT true,                 -- Configuration file is active
524     source text NOT NULL,                               -- Relative path on the boot server
525                                                         -- where file can be downloaded
526     dest text NOT NULL,                                 -- Absolute path where file should be installed
527     file_permissions text NOT NULL DEFAULT '0644',      -- chmod(1) permissions
528     file_owner text NOT NULL DEFAULT 'root',            -- chown(1) owner
529     file_group text NOT NULL DEFAULT 'root',            -- chgrp(1) owner
530     preinstall_cmd text,                                -- Shell command to execute prior to installing
531     postinstall_cmd text,                               -- Shell command to execute after installing
532     error_cmd text,                                     -- Shell command to execute if any error occurs
533     ignore_cmd_errors bool NOT NULL DEFAULT false,      -- Install file anyway even if an error occurs
534     always_update bool NOT NULL DEFAULT false           -- Always attempt to install file even if unchanged
535 ) WITH OIDS;
536
537 CREATE TABLE conf_file_node (
538     conf_file_id integer REFERENCES conf_files NOT NULL,        -- Configuration file identifier
539     node_id integer REFERENCES nodes NOT NULL,                  -- Node identifier
540     PRIMARY KEY (conf_file_id, node_id)
541 );
542 CREATE INDEX conf_file_node_conf_file_id_idx ON conf_file_node (conf_file_id);
543 CREATE INDEX conf_file_node_node_id_idx ON conf_file_node (node_id);
544
545 -- Nodes linked to each configuration file
546 CREATE OR REPLACE VIEW conf_file_nodes AS
547 SELECT conf_file_id,
548 array_accum(node_id) AS node_ids
549 FROM conf_file_node
550 GROUP BY conf_file_id;
551
552 -- Configuration files linked to each node
553 CREATE OR REPLACE VIEW node_conf_files AS
554 SELECT node_id,
555 array_accum(conf_file_id) AS conf_file_ids
556 FROM conf_file_node
557 GROUP BY node_id;
558
559 CREATE TABLE conf_file_nodegroup (
560     conf_file_id integer REFERENCES conf_files NOT NULL,        -- Configuration file identifier
561     nodegroup_id integer REFERENCES nodegroups NOT NULL,        -- Node group identifier
562     PRIMARY KEY (conf_file_id, nodegroup_id)
563 );
564 CREATE INDEX conf_file_nodegroup_conf_file_id_idx ON conf_file_nodegroup (conf_file_id);
565 CREATE INDEX conf_file_nodegroup_nodegroup_id_idx ON conf_file_nodegroup (nodegroup_id);
566
567 -- Node groups linked to each configuration file
568 CREATE OR REPLACE VIEW conf_file_nodegroups AS
569 SELECT conf_file_id,
570 array_accum(nodegroup_id) AS nodegroup_ids
571 FROM conf_file_nodegroup
572 GROUP BY conf_file_id;
573
574 -- Configuration files linked to each node group
575 CREATE OR REPLACE VIEW nodegroup_conf_files AS
576 SELECT nodegroup_id,
577 array_accum(conf_file_id) AS conf_file_ids
578 FROM conf_file_nodegroup
579 GROUP BY nodegroup_id;
580
581 --------------------------------------------------------------------------------
582 -- Power control units (PCUs)
583 --------------------------------------------------------------------------------
584
585 CREATE TABLE pcus (
586     -- Mandatory
587     pcu_id serial PRIMARY KEY,                          -- PCU identifier
588     site_id integer REFERENCES sites NOT NULL,          -- Site identifier
589     hostname text,                                      -- Hostname, not necessarily unique 
590                                                         -- (multiple logical sites could use the same PCU)
591     ip text NOT NULL,                                   -- IP, not necessarily unique
592
593     -- Optional
594     protocol text,                                      -- Protocol, e.g. ssh or https or telnet
595     username text,                                      -- Username, if applicable
596     "password" text,                                    -- Password, if applicable
597     model text,                                         -- Model, e.g. BayTech or iPal
598     notes text                                          -- Random notes
599 ) WITH OIDS;
600 CREATE INDEX pcus_site_id_idx ON pcus (site_id);
601
602 CREATE OR REPLACE VIEW site_pcus AS
603 SELECT site_id,
604 array_accum(pcu_id) AS pcu_ids
605 FROM pcus
606 GROUP BY site_id;
607
608 CREATE TABLE pcu_node (
609     pcu_id integer REFERENCES pcus NOT NULL,            -- PCU identifier
610     node_id integer REFERENCES nodes NOT NULL,          -- Node identifier
611     port integer NOT NULL,                              -- Port number
612     PRIMARY KEY (pcu_id, node_id),                      -- The same node cannot be controlled by different ports
613     UNIQUE (pcu_id, port)                               -- The same port cannot control multiple nodes
614 );
615 CREATE INDEX pcu_node_pcu_id_idx ON pcu_node (pcu_id);
616 CREATE INDEX pcu_node_node_id_idx ON pcu_node (node_id);
617
618 CREATE OR REPLACE VIEW node_pcus AS
619 SELECT node_id,
620 array_accum(pcu_id) AS pcu_ids,
621 array_accum(port) AS ports
622 FROM pcu_node
623 GROUP BY node_id;
624
625 CREATE OR REPLACE VIEW pcu_nodes AS
626 SELECT pcu_id,
627 array_accum(node_id) AS node_ids,
628 array_accum(port) AS ports
629 FROM pcu_node
630 GROUP BY pcu_id;
631
632 --------------------------------------------------------------------------------
633 -- Slices
634 --------------------------------------------------------------------------------
635
636 CREATE TABLE slice_instantiations (
637     instantiation text PRIMARY KEY
638 ) WITH OIDS;
639 INSERT INTO slice_instantiations (instantiation) VALUES ('not-instantiated');   -- Placeholder slice
640 INSERT INTO slice_instantiations (instantiation) VALUES ('plc-instantiated');   -- Instantiated by Node Manager
641 INSERT INTO slice_instantiations (instantiation) VALUES ('delegated');          -- Manually instantiated
642 INSERT INTO slice_instantiations (instantiation) VALUES ('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/sliver attributes
750 CREATE TABLE slice_tag (
751     slice_tag_id serial PRIMARY KEY,            -- Slice attribute identifier
752     slice_id integer REFERENCES slices NOT NULL,        -- Slice identifier
753     node_id integer REFERENCES nodes,                   -- Sliver attribute if set
754     nodegroup_id integer REFERENCES nodegroups,         -- Node group attribute if set
755     tag_type_id integer REFERENCES tag_types NOT NULL,  -- Attribute type identifier
756     value text
757 ) WITH OIDS;
758 CREATE INDEX slice_tag_slice_id_idx ON slice_tag (slice_id);
759 CREATE INDEX slice_tag_node_id_idx ON slice_tag (node_id);
760 CREATE INDEX slice_tag_nodegroup_id_idx ON slice_tag (nodegroup_id);
761
762 CREATE OR REPLACE VIEW slice_tags AS
763 SELECT slice_id,
764 array_accum(slice_tag_id) AS slice_tag_ids
765 FROM slice_tag
766 GROUP BY slice_id;
767
768 --------------------------------------------------------------------------------
769 -- Initscripts
770 --------------------------------------------------------------------------------
771
772 -- Initscripts
773 CREATE TABLE initscripts (
774     initscript_id serial PRIMARY KEY,                   -- Initscript identifier
775     name text NOT NULL,                                 -- Initscript name
776     enabled bool NOT NULL DEFAULT true,                 -- Initscript is active
777     script text NOT NULL,                               -- Initscript body
778     UNIQUE (name)
779 ) WITH OIDS;
780 CREATE INDEX initscripts_name_idx ON initscripts (name);
781
782
783 --------------------------------------------------------------------------------
784 -- Peers
785 --------------------------------------------------------------------------------
786
787 -- Peers
788 CREATE TABLE peers (
789     peer_id serial PRIMARY KEY,                         -- Peer identifier
790     peername text NOT NULL,                             -- Peer name
791     peer_url text NOT NULL,                             -- (HTTPS) URL of the peer PLCAPI interface
792     cacert text,                                        -- (SSL) Public certificate of peer API server
793     key text,                                           -- (GPG) Public key used for authentication
794     deleted boolean NOT NULL DEFAULT false
795 ) WITH OIDS;
796 CREATE INDEX peers_peername_idx ON peers (peername) WHERE deleted IS false;
797
798 -- Objects at each peer
799 CREATE TABLE peer_site (
800     site_id integer REFERENCES sites PRIMARY KEY,       -- Local site identifier
801     peer_id integer REFERENCES peers NOT NULL,          -- Peer identifier
802     peer_site_id integer NOT NULL,                      -- Foreign site identifier at peer
803     UNIQUE (peer_id, peer_site_id)                      -- The same foreign site should not be cached twice
804 ) WITH OIDS;
805 CREATE INDEX peer_site_peer_id_idx ON peers (peer_id);
806
807 CREATE OR REPLACE VIEW peer_sites AS
808 SELECT peer_id,
809 array_accum(site_id) AS site_ids,
810 array_accum(peer_site_id) AS peer_site_ids
811 FROM peer_site
812 GROUP BY peer_id;
813
814 CREATE TABLE peer_person (
815     person_id integer REFERENCES persons PRIMARY KEY,   -- Local user identifier
816     peer_id integer REFERENCES peers NOT NULL,          -- Peer identifier
817     peer_person_id integer NOT NULL,                    -- Foreign user identifier at peer
818     UNIQUE (peer_id, peer_person_id)                    -- The same foreign user should not be cached twice
819 ) WITH OIDS;
820 CREATE INDEX peer_person_peer_id_idx ON peer_person (peer_id);
821
822 CREATE OR REPLACE VIEW peer_persons AS
823 SELECT peer_id,
824 array_accum(person_id) AS person_ids,
825 array_accum(peer_person_id) AS peer_person_ids
826 FROM peer_person
827 GROUP BY peer_id;
828
829 CREATE TABLE peer_key (
830     key_id integer REFERENCES keys PRIMARY KEY,         -- Local key identifier
831     peer_id integer REFERENCES peers NOT NULL,          -- Peer identifier
832     peer_key_id integer NOT NULL,                       -- Foreign key identifier at peer
833     UNIQUE (peer_id, peer_key_id)                       -- The same foreign key should not be cached twice
834 ) WITH OIDS;
835 CREATE INDEX peer_key_peer_id_idx ON peer_key (peer_id);
836
837 CREATE OR REPLACE VIEW peer_keys AS
838 SELECT peer_id,
839 array_accum(key_id) AS key_ids,
840 array_accum(peer_key_id) AS peer_key_ids
841 FROM peer_key
842 GROUP BY peer_id;
843
844 CREATE TABLE peer_node (
845     node_id integer REFERENCES nodes PRIMARY KEY,       -- Local node identifier
846     peer_id integer REFERENCES peers NOT NULL,          -- Peer identifier
847     peer_node_id integer NOT NULL,                      -- Foreign node identifier
848     UNIQUE (peer_id, peer_node_id)                      -- The same foreign node should not be cached twice
849 ) WITH OIDS;
850 CREATE INDEX peer_node_peer_id_idx ON peer_node (peer_id);
851
852 CREATE OR REPLACE VIEW peer_nodes AS
853 SELECT peer_id,
854 array_accum(node_id) AS node_ids,
855 array_accum(peer_node_id) AS peer_node_ids
856 FROM peer_node
857 GROUP BY peer_id;
858
859 CREATE TABLE peer_slice (
860     slice_id integer REFERENCES slices PRIMARY KEY,     -- Local slice identifier
861     peer_id integer REFERENCES peers NOT NULL,          -- Peer identifier
862     peer_slice_id integer NOT NULL,                     -- Slice identifier at peer
863     UNIQUE (peer_id, peer_slice_id)                     -- The same foreign slice should not be cached twice
864 ) WITH OIDS;
865 CREATE INDEX peer_slice_peer_id_idx ON peer_slice (peer_id);
866
867 CREATE OR REPLACE VIEW peer_slices AS
868 SELECT peer_id,
869 array_accum(slice_id) AS slice_ids,
870 array_accum(peer_slice_id) AS peer_slice_ids
871 FROM peer_slice
872 GROUP BY peer_id;
873
874 --------------------------------------------------------------------------------
875 -- Authenticated sessions
876 --------------------------------------------------------------------------------
877
878 -- Authenticated sessions
879 CREATE TABLE sessions (
880     session_id text PRIMARY KEY,                        -- Session identifier
881     expires timestamp without time zone
882 ) WITH OIDS;
883
884 -- People can have multiple sessions
885 CREATE TABLE person_session (
886     person_id integer REFERENCES persons NOT NULL,      -- Account identifier
887     session_id text REFERENCES sessions NOT NULL,       -- Session identifier
888     PRIMARY KEY (person_id, session_id),
889     UNIQUE (session_id)                                 -- Sessions are unique
890 ) WITH OIDS;
891 CREATE INDEX person_session_person_id_idx ON person_session (person_id);
892
893 -- Nodes can have only one session
894 CREATE TABLE node_session (
895     node_id integer REFERENCES nodes NOT NULL,          -- Node identifier
896     session_id text REFERENCES sessions NOT NULL,       -- Session identifier
897     UNIQUE (node_id),                                   -- Nodes can have only one session
898     UNIQUE (session_id)                                 -- Sessions are unique
899 ) WITH OIDS;
900
901 -------------------------------------------------------------------------------
902 -- PCU Types
903 ------------------------------------------------------------------------------
904 CREATE TABLE pcu_types (
905     pcu_type_id serial PRIMARY KEY,
906     model text NOT NULL ,                               -- PCU model name
907     name text                                           -- Full PCU model name
908 ) WITH OIDS;
909 CREATE INDEX pcu_types_model_idx ON pcu_types (model);
910
911 CREATE TABLE pcu_protocol_type (
912     pcu_protocol_type_id serial PRIMARY KEY,
913     pcu_type_id integer REFERENCES pcu_types NOT NULL,  -- PCU type identifier
914     port integer NOT NULL,                              -- PCU port
915     protocol text NOT NULL,                             -- Protocol
916     supported boolean NOT NULL DEFAULT True             -- Does PLC support
917 ) WITH OIDS;
918 CREATE INDEX pcu_protocol_type_pcu_type_id ON pcu_protocol_type (pcu_type_id);
919
920
921 CREATE OR REPLACE VIEW pcu_protocol_types AS
922 SELECT pcu_type_id,
923 array_accum(pcu_protocol_type_id) as pcu_protocol_type_ids
924 FROM pcu_protocol_type
925 GROUP BY pcu_type_id;
926
927 --------------------------------------------------------------------------------
928 -- Message templates
929 --------------------------------------------------------------------------------
930
931 CREATE TABLE messages (
932     message_id text PRIMARY KEY,                        -- Message name
933     subject text,                                       -- Message summary
934     template text,                                      -- Message template
935     enabled bool NOT NULL DEFAULT true                  -- Whether message is enabled
936 ) WITH OIDS;
937
938 --------------------------------------------------------------------------------
939 -- Events
940 --------------------------------------------------------------------------------
941
942 -- Events
943 CREATE TABLE events (
944     event_id serial PRIMARY KEY,                        -- Event identifier
945     person_id integer REFERENCES persons,               -- Person responsible for event, if any
946     node_id integer REFERENCES nodes,                   -- Node responsible for event, if any
947     auth_type text,                                     -- Type of auth used. i.e. AuthMethod
948     fault_code integer NOT NULL DEFAULT 0,              -- Did this event result in error
949     call_name text NOT NULL,                            -- Call responsible for this event
950     call text NOT NULL,                                 -- Call responsible for this event, including parameters
951     message text,                                       -- High level description of this event
952     runtime float DEFAULT 0,                            -- Event run time
953     time timestamp without time zone NOT NULL           -- Event timestamp
954         DEFAULT CURRENT_TIMESTAMP
955 ) WITH OIDS;
956
957 -- Database object(s) that may have been affected by a particular event
958 CREATE TABLE event_object (
959     event_id integer REFERENCES events NOT NULL,        -- Event identifier
960     object_id integer NOT NULL,                         -- Object identifier
961     object_type text NOT NULL Default 'Unknown'         -- What type of object is this event affecting
962 ) WITH OIDS;
963 CREATE INDEX event_object_event_id_idx ON event_object (event_id);
964 CREATE INDEX event_object_object_id_idx ON event_object (object_id);
965 CREATE INDEX event_object_object_type_idx ON event_object (object_type);
966
967 CREATE OR REPLACE VIEW event_objects AS
968 SELECT event_id,
969 array_accum(object_id) AS object_ids,
970 array_accum(object_type) AS object_types
971 FROM event_object
972 GROUP BY event_id;
973
974 --------------------------------------------------------------------------------
975 -- Useful views
976 --------------------------------------------------------------------------------
977 CREATE OR REPLACE VIEW view_pcu_types AS
978 SELECT
979 pcu_types.pcu_type_id,
980 pcu_types.model,
981 pcu_types.name,
982 COALESCE((SELECT pcu_protocol_type_ids FROM pcu_protocol_types
983                  WHERE pcu_protocol_types.pcu_type_id = pcu_types.pcu_type_id), '{}') 
984 AS pcu_protocol_type_ids
985 FROM pcu_types;
986
987 CREATE OR REPLACE VIEW view_events AS
988 SELECT
989 events.event_id,
990 events.person_id,
991 events.node_id,
992 events.auth_type,
993 events.fault_code,
994 events.call_name,
995 events.call,
996 events.message,
997 events.runtime,
998 CAST(date_part('epoch', events.time) AS bigint) AS time,
999 COALESCE((SELECT object_ids FROM event_objects WHERE event_objects.event_id = events.event_id), '{}') AS object_ids,
1000 COALESCE((SELECT object_types FROM event_objects WHERE event_objects.event_id = events.event_id), '{}') AS object_types
1001 FROM events;
1002
1003 CREATE OR REPLACE VIEW view_event_objects AS 
1004 SELECT
1005 events.event_id,
1006 events.person_id,
1007 events.node_id,
1008 events.fault_code,
1009 events.call_name,
1010 events.call,
1011 events.message,
1012 events.runtime,
1013 CAST(date_part('epoch', events.time) AS bigint) AS time,
1014 event_object.object_id,
1015 event_object.object_type
1016 FROM events LEFT JOIN event_object USING (event_id);
1017
1018 CREATE OR REPLACE VIEW view_persons AS
1019 SELECT
1020 persons.person_id,
1021 persons.email,
1022 persons.first_name,
1023 persons.last_name,
1024 persons.deleted,
1025 persons.enabled,
1026 persons.password,
1027 persons.verification_key,
1028 CAST(date_part('epoch', persons.verification_expires) AS bigint) AS verification_expires,
1029 persons.title,
1030 persons.phone,
1031 persons.url,
1032 persons.bio,
1033 CAST(date_part('epoch', persons.date_created) AS bigint) AS date_created,
1034 CAST(date_part('epoch', persons.last_updated) AS bigint) AS last_updated,
1035 peer_person.peer_id,
1036 peer_person.peer_person_id,
1037 COALESCE((SELECT role_ids FROM person_roles WHERE person_roles.person_id = persons.person_id), '{}') AS role_ids,
1038 COALESCE((SELECT roles FROM person_roles WHERE person_roles.person_id = persons.person_id), '{}') AS roles,
1039 COALESCE((SELECT site_ids FROM person_sites WHERE person_sites.person_id = persons.person_id), '{}') AS site_ids,
1040 COALESCE((SELECT key_ids FROM person_keys WHERE person_keys.person_id = persons.person_id), '{}') AS key_ids,
1041 COALESCE((SELECT slice_ids FROM person_slices WHERE person_slices.person_id = persons.person_id), '{}') AS slice_ids
1042 FROM persons
1043 LEFT JOIN peer_person USING (person_id);
1044
1045 CREATE OR REPLACE VIEW view_peers AS
1046 SELECT 
1047 peers.*, 
1048 COALESCE((SELECT site_ids FROM peer_sites WHERE peer_sites.peer_id = peers.peer_id), '{}') AS site_ids,
1049 COALESCE((SELECT peer_site_ids FROM peer_sites WHERE peer_sites.peer_id = peers.peer_id), '{}') AS peer_site_ids,
1050 COALESCE((SELECT person_ids FROM peer_persons WHERE peer_persons.peer_id = peers.peer_id), '{}') AS person_ids,
1051 COALESCE((SELECT peer_person_ids FROM peer_persons WHERE peer_persons.peer_id = peers.peer_id), '{}') AS peer_person_ids,
1052 COALESCE((SELECT key_ids FROM peer_keys WHERE peer_keys.peer_id = peers.peer_id), '{}') AS key_ids,
1053 COALESCE((SELECT peer_key_ids FROM peer_keys WHERE peer_keys.peer_id = peers.peer_id), '{}') AS peer_key_ids,
1054 COALESCE((SELECT node_ids FROM peer_nodes WHERE peer_nodes.peer_id = peers.peer_id), '{}') AS node_ids,
1055 COALESCE((SELECT peer_node_ids FROM peer_nodes WHERE peer_nodes.peer_id = peers.peer_id), '{}') AS peer_node_ids,
1056 COALESCE((SELECT slice_ids FROM peer_slices WHERE peer_slices.peer_id = peers.peer_id), '{}') AS slice_ids,
1057 COALESCE((SELECT peer_slice_ids FROM peer_slices WHERE peer_slices.peer_id = peers.peer_id), '{}') AS peer_slice_ids
1058 FROM peers;
1059
1060 CREATE OR REPLACE VIEW view_nodes AS
1061 SELECT
1062 nodes.node_id,
1063 nodes.hostname,
1064 nodes.site_id,
1065 nodes.boot_state,
1066 nodes.deleted,
1067 nodes.model,
1068 nodes.boot_nonce,
1069 nodes.version,
1070 nodes.ssh_rsa_key,
1071 nodes.key,
1072 CAST(date_part('epoch', nodes.date_created) AS bigint) AS date_created,
1073 CAST(date_part('epoch', nodes.last_updated) AS bigint) AS last_updated,
1074 CAST(date_part('epoch', nodes.last_contact) AS bigint) AS last_contact,  
1075 peer_node.peer_id,
1076 peer_node.peer_node_id,
1077 COALESCE((SELECT interface_ids FROM node_interfaces 
1078                  WHERE node_interfaces.node_id = nodes.node_id), '{}') 
1079 AS interface_ids,
1080 COALESCE((SELECT nodegroup_ids FROM node_nodegroups 
1081                  WHERE node_nodegroups.node_id = nodes.node_id), '{}') 
1082 AS nodegroup_ids,
1083 COALESCE((SELECT slice_ids FROM node_slices 
1084                  WHERE node_slices.node_id = nodes.node_id), '{}') 
1085 AS slice_ids,
1086 COALESCE((SELECT slice_ids_whitelist FROM node_slices_whitelist 
1087                  WHERE node_slices_whitelist.node_id = nodes.node_id), '{}') 
1088 AS slice_ids_whitelist,
1089 COALESCE((SELECT pcu_ids FROM node_pcus 
1090                  WHERE node_pcus.node_id = nodes.node_id), '{}') 
1091 AS pcu_ids,
1092 COALESCE((SELECT ports FROM node_pcus
1093                  WHERE node_pcus.node_id = nodes.node_id), '{}') 
1094 AS ports,
1095 COALESCE((SELECT conf_file_ids FROM node_conf_files
1096                  WHERE node_conf_files.node_id = nodes.node_id), '{}') 
1097 AS conf_file_ids,
1098 COALESCE((SELECT tag_ids FROM node_tags 
1099                  WHERE node_tags.node_id = nodes.node_id), '{}') 
1100 AS tag_ids,
1101 node_session.session_id AS session
1102 FROM nodes
1103 LEFT JOIN peer_node USING (node_id)
1104 LEFT JOIN node_session USING (node_id);
1105
1106 CREATE OR REPLACE VIEW view_nodegroups AS
1107 SELECT
1108 nodegroups.*,
1109 tag_types.tagname,
1110 COALESCE((SELECT conf_file_ids FROM nodegroup_conf_files 
1111                  WHERE nodegroup_conf_files.nodegroup_id = nodegroups.nodegroup_id), '{}') 
1112 AS conf_file_ids,
1113 COALESCE((SELECT node_ids FROM nodegroup_nodes 
1114                  WHERE nodegroup_nodes.nodegroup_id = nodegroups.nodegroup_id), '{}') 
1115 AS node_ids
1116 FROM nodegroups INNER JOIN tag_types USING (tag_type_id);
1117
1118 CREATE OR REPLACE VIEW view_conf_files AS
1119 SELECT
1120 conf_files.*,
1121 COALESCE((SELECT node_ids FROM conf_file_nodes 
1122                  WHERE conf_file_nodes.conf_file_id = conf_files.conf_file_id), '{}') 
1123 AS node_ids,
1124 COALESCE((SELECT nodegroup_ids FROM conf_file_nodegroups 
1125                  WHERE conf_file_nodegroups.conf_file_id = conf_files.conf_file_id), '{}') 
1126 AS nodegroup_ids
1127 FROM conf_files;
1128
1129 CREATE OR REPLACE VIEW view_pcus AS
1130 SELECT
1131 pcus.*,
1132 COALESCE((SELECT node_ids FROM pcu_nodes WHERE pcu_nodes.pcu_id = pcus.pcu_id), '{}') AS node_ids,
1133 COALESCE((SELECT ports FROM pcu_nodes WHERE pcu_nodes.pcu_id = pcus.pcu_id), '{}') AS ports
1134 FROM pcus;
1135
1136 CREATE OR REPLACE VIEW view_sites AS
1137 SELECT
1138 sites.site_id,
1139 sites.login_base,
1140 sites.name,
1141 sites.abbreviated_name,
1142 sites.deleted,
1143 sites.enabled,
1144 sites.is_public,
1145 sites.max_slices,
1146 sites.max_slivers,
1147 sites.latitude,
1148 sites.longitude,
1149 sites.url,
1150 sites.ext_consortium_id,
1151 CAST(date_part('epoch', sites.date_created) AS bigint) AS date_created,
1152 CAST(date_part('epoch', sites.last_updated) AS bigint) AS last_updated,
1153 peer_site.peer_id,
1154 peer_site.peer_site_id,
1155 COALESCE((SELECT person_ids FROM site_persons WHERE site_persons.site_id = sites.site_id), '{}') AS person_ids,
1156 COALESCE((SELECT node_ids FROM site_nodes WHERE site_nodes.site_id = sites.site_id), '{}') AS node_ids,
1157 COALESCE((SELECT address_ids FROM site_addresses WHERE site_addresses.site_id = sites.site_id), '{}') AS address_ids,
1158 COALESCE((SELECT slice_ids FROM site_slices WHERE site_slices.site_id = sites.site_id), '{}') AS slice_ids,
1159 COALESCE((SELECT pcu_ids FROM site_pcus WHERE site_pcus.site_id = sites.site_id), '{}') AS pcu_ids
1160 FROM sites
1161 LEFT JOIN peer_site USING (site_id);
1162
1163 CREATE OR REPLACE VIEW view_addresses AS
1164 SELECT
1165 addresses.*,
1166 COALESCE((SELECT address_type_ids FROM address_address_types WHERE address_address_types.address_id = addresses.address_id), '{}') AS address_type_ids,
1167 COALESCE((SELECT address_types FROM address_address_types WHERE address_address_types.address_id = addresses.address_id), '{}') AS address_types
1168 FROM addresses;
1169
1170 CREATE OR REPLACE VIEW view_keys AS
1171 SELECT
1172 keys.*,
1173 person_key.person_id,
1174 peer_key.peer_id,
1175 peer_key.peer_key_id
1176 FROM keys
1177 LEFT JOIN person_key USING (key_id)
1178 LEFT JOIN peer_key USING (key_id);
1179
1180 CREATE OR REPLACE VIEW view_slices AS
1181 SELECT
1182 slices.slice_id,
1183 slices.site_id,
1184 slices.name,
1185 slices.instantiation,
1186 slices.url,
1187 slices.description,
1188 slices.max_nodes,
1189 slices.creator_person_id,
1190 slices.is_deleted,
1191 CAST(date_part('epoch', slices.created) AS bigint) AS created,
1192 CAST(date_part('epoch', slices.expires) AS bigint) AS expires,
1193 peer_slice.peer_id,
1194 peer_slice.peer_slice_id,
1195 COALESCE((SELECT node_ids FROM slice_nodes WHERE slice_nodes.slice_id = slices.slice_id), '{}') AS node_ids,
1196 COALESCE((SELECT person_ids FROM slice_persons WHERE slice_persons.slice_id = slices.slice_id), '{}') AS person_ids,
1197 COALESCE((SELECT slice_tag_ids FROM slice_tags WHERE slice_tags.slice_id = slices.slice_id), '{}') AS slice_tag_ids
1198 FROM slices
1199 LEFT JOIN peer_slice USING (slice_id);
1200
1201 CREATE OR REPLACE VIEW view_slice_tags AS
1202 SELECT
1203 slice_tag.slice_tag_id,
1204 slice_tag.slice_id,
1205 slice_tag.node_id,
1206 slice_tag.nodegroup_id,
1207 tag_types.tag_type_id,
1208 tag_types.tagname,
1209 tag_types.description,
1210 tag_types.category,
1211 tag_types.min_role_id,
1212 slice_tag.value
1213 FROM slice_tag
1214 INNER JOIN tag_types USING (tag_type_id);
1215
1216 CREATE OR REPLACE VIEW view_sessions AS
1217 SELECT
1218 sessions.session_id,
1219 CAST(date_part('epoch', sessions.expires) AS bigint) AS expires,
1220 person_session.person_id,
1221 node_session.node_id
1222 FROM sessions
1223 LEFT JOIN person_session USING (session_id)
1224 LEFT JOIN node_session USING (session_id);
1225
1226 --------------------------------------------------------------------------------
1227 -- Built-in maintenance account and default site
1228 --------------------------------------------------------------------------------
1229
1230 INSERT INTO persons (first_name, last_name, email, password, enabled)
1231 VALUES              ('Maintenance', 'Account', 'maint@localhost.localdomain', 'nopass', true);
1232
1233 INSERT INTO person_role (person_id, role_id) VALUES (1, 10);
1234 INSERT INTO person_role (person_id, role_id) VALUES (1, 20);
1235 INSERT INTO person_role (person_id, role_id) VALUES (1, 30);
1236 INSERT INTO person_role (person_id, role_id) VALUES (1, 40);
1237
1238 INSERT INTO sites (login_base, name, abbreviated_name, max_slices)
1239 VALUES ('pl', 'PlanetLab Central', 'PLC', 100);