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