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