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