first draft for node tags & new node groups:
[plcapi.git] / planetlab5.sql
1 --
2 -- PlanetLab Central database schema
3 -- Version 4, PostgreSQL
4 --
5 -- Aaron Klingaman <alk@cs.princeton.edu>
6 -- Reid Moran <rmoran@cs.princeton.edu>
7 -- Mark Huang <mlhuang@cs.princeton.edu>
8 -- Tony Mack <tmack@cs.princeton.edu>
9 --
10 -- Copyright (C) 2006 The Trustees of Princeton University
11 --
12 -- $Id$
13 --
14
15 SET client_encoding = 'UNICODE';
16
17 --------------------------------------------------------------------------------
18 -- Version
19 --------------------------------------------------------------------------------
20
21 -- Database version
22 CREATE TABLE plc_db_version (
23     version integer NOT NULL,
24     subversion integer NOT NULL DEFAULT 0
25 ) WITH OIDS;
26
27 INSERT INTO plc_db_version (version, subversion) VALUES (5, 0);
28
29 --------------------------------------------------------------------------------
30 -- Aggregates and store procedures
31 --------------------------------------------------------------------------------
32
33 -- Like MySQL GROUP_CONCAT(), this function aggregates values into a
34 -- PostgreSQL array.
35 CREATE AGGREGATE array_accum (
36     sfunc = array_append,
37     basetype = anyelement,
38     stype = anyarray,
39     initcond = '{}'
40 );
41
42 --------------------------------------------------------------------------------
43 -- Accounts
44 --------------------------------------------------------------------------------
45
46 -- Accounts
47 CREATE TABLE persons (
48     -- Mandatory
49     person_id serial PRIMARY KEY, -- Account identifier
50     email text NOT NULL, -- E-mail address
51     first_name text NOT NULL, -- First name
52     last_name text NOT NULL, -- Last name
53     deleted boolean NOT NULL DEFAULT false, -- Has been deleted
54     enabled boolean NOT NULL DEFAULT false, -- Has been disabled
55
56     -- Password
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 DEFAULT 'inst', -- Node boot state
265     deleted boolean NOT NULL DEFAULT false, -- Is deleted
266
267     -- Optional
268     model text, -- Hardware make and model
269     boot_nonce text, -- Random nonce updated by Boot Manager
270     version text, -- Boot CD version string updated by Boot Manager
271     ssh_rsa_key text, -- SSH host key updated by Boot Manager
272     key text, -- Node key generated by API when configuration file is downloaded
273
274     -- Timestamps
275     date_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
276     last_updated timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
277     last_contact timestamp without time zone    
278 ) WITH OIDS;
279 CREATE INDEX nodes_hostname_idx ON nodes (hostname);
280 CREATE INDEX nodes_site_id_idx ON nodes (site_id);
281
282 -- Nodes at each site
283 CREATE OR REPLACE VIEW site_nodes AS
284 SELECT site_id,
285 array_accum(node_id) AS node_ids
286 FROM nodes
287 WHERE deleted IS false
288 GROUP BY site_id;
289
290 --------------------------------------------------------------------------------
291 -- node tags
292 --------------------------------------------------------------------------------
293 CREATE TABLE node_tag_types (
294
295     node_tag_type_id serial PRIMARY KEY,        -- ID
296     name text UNIQUE NOT NULL,                  -- Tag Name
297     description text,                           -- Optional Description
298     category text NOT NULL,                     -- Free text for grouping tags together
299     min_role_id integer REFERENCES roles        -- set minimal role required
300 ) WITH OIDS;
301
302 CREATE TABLE node_tag (
303     node_tag_id serial PRIMARY KEY,             -- ID
304     node_id integer REFERENCES nodes NOT NULL,  -- node id
305     node_tag_type_id integer REFERENCES node_tag_types,
306                                                 -- tag type id
307     value 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 node_tag_types.node_tag_type_id,
321 node_tag_types.name,
322 node_tag_types.description,
323 node_tag_types.category,
324 node_tag_types.min_role_id,
325 node_tag.value
326 FROM node_tag 
327 INNER JOIN node_tag_types USING (node_tag_type_id);
328
329 --------------------------------------------------------------------------------
330 -- Node groups
331 --------------------------------------------------------------------------------
332
333 -- Node groups
334 CREATE TABLE nodegroups (
335     nodegroup_id serial PRIMARY KEY,                    -- Group identifier
336     name text UNIQUE NOT NULL,                          -- Group name
337     node_tag_type_id integer REFERENCES node_tag_types, -- node is in nodegroup if it has this tag defined
338     value text                                          -- with value 'value'
339 ) WITH OIDS;
340
341
342 CREATE OR REPLACE VIEW nodegroup_node AS
343 SELECT nodegroup_id, node_id 
344 FROM node_tag_types 
345 JOIN node_tag 
346 USING (node_tag_type_id) 
347 JOIN nodegroups 
348 USING (node_tag_type_id,value);
349
350 CREATE OR REPLACE VIEW nodegroup_nodes AS
351 SELECT nodegroup_id,
352 array_accum(node_id) AS node_ids
353 FROM nodegroup_node
354 GROUP BY nodegroup_id;
355
356 -- Node groups that each node is a member of
357 CREATE OR REPLACE VIEW node_nodegroups AS
358 SELECT node_id,
359 array_accum(nodegroup_id) AS nodegroup_ids
360 FROM nodegroup_node
361 GROUP BY node_id;
362
363 --------------------------------------------------------------------------------
364 -- Node configuration files
365 --------------------------------------------------------------------------------
366
367 CREATE TABLE conf_files (
368     conf_file_id serial PRIMARY KEY, -- Configuration file identifier
369     enabled bool NOT NULL DEFAULT true, -- Configuration file is active
370     source text NOT NULL, -- Relative path on the boot server where file can be downloaded
371     dest text NOT NULL, -- Absolute path where file should be installed
372     file_permissions text NOT NULL DEFAULT '0644', -- chmod(1) permissions
373     file_owner text NOT NULL DEFAULT 'root', -- chown(1) owner
374     file_group text NOT NULL DEFAULT 'root', -- chgrp(1) owner
375     preinstall_cmd text, -- Shell command to execute prior to installing
376     postinstall_cmd text, -- Shell command to execute after installing
377     error_cmd text, -- Shell command to execute if any error occurs
378     ignore_cmd_errors bool NOT NULL DEFAULT false, -- Install file anyway even if an error occurs
379     always_update bool NOT NULL DEFAULT false -- Always attempt to install file even if unchanged
380 ) WITH OIDS;
381
382 CREATE TABLE conf_file_node (
383     conf_file_id integer REFERENCES conf_files NOT NULL, -- Configuration file identifier
384     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
385     PRIMARY KEY (conf_file_id, node_id)
386 );
387 CREATE INDEX conf_file_node_conf_file_id_idx ON conf_file_node (conf_file_id);
388 CREATE INDEX conf_file_node_node_id_idx ON conf_file_node (node_id);
389
390 -- Nodes linked to each configuration file
391 CREATE OR REPLACE VIEW conf_file_nodes AS
392 SELECT conf_file_id,
393 array_accum(node_id) AS node_ids
394 FROM conf_file_node
395 GROUP BY conf_file_id;
396
397 -- Configuration files linked to each node
398 CREATE OR REPLACE VIEW node_conf_files AS
399 SELECT node_id,
400 array_accum(conf_file_id) AS conf_file_ids
401 FROM conf_file_node
402 GROUP BY node_id;
403
404 CREATE TABLE conf_file_nodegroup (
405     conf_file_id integer REFERENCES conf_files NOT NULL, -- Configuration file identifier
406     nodegroup_id integer REFERENCES nodegroups NOT NULL, -- Node group identifier
407     PRIMARY KEY (conf_file_id, nodegroup_id)
408 );
409 CREATE INDEX conf_file_nodegroup_conf_file_id_idx ON conf_file_nodegroup (conf_file_id);
410 CREATE INDEX conf_file_nodegroup_nodegroup_id_idx ON conf_file_nodegroup (nodegroup_id);
411
412 -- Node groups linked to each configuration file
413 CREATE OR REPLACE VIEW conf_file_nodegroups AS
414 SELECT conf_file_id,
415 array_accum(nodegroup_id) AS nodegroup_ids
416 FROM conf_file_nodegroup
417 GROUP BY conf_file_id;
418
419 -- Configuration files linked to each node group
420 CREATE OR REPLACE VIEW nodegroup_conf_files AS
421 SELECT nodegroup_id,
422 array_accum(conf_file_id) AS conf_file_ids
423 FROM conf_file_nodegroup
424 GROUP BY nodegroup_id;
425
426 --------------------------------------------------------------------------------
427 -- Node network interfaces
428 --------------------------------------------------------------------------------
429
430 -- Valid network addressing schemes
431 CREATE TABLE network_types (
432     type text PRIMARY KEY -- Addressing scheme
433 ) WITH OIDS;
434 INSERT INTO network_types (type) VALUES ('ipv4');
435
436 -- Valid network configuration methods
437 CREATE TABLE network_methods (
438     method text PRIMARY KEY -- Configuration method
439 ) WITH OIDS;
440 INSERT INTO network_methods (method) VALUES
441         ('static'), ('dhcp'), ('proxy'), ('tap'), ('ipmi'), ('unknown');
442
443 -- Node network interfaces
444 CREATE TABLE interfaces (
445     -- Mandatory
446     interface_id serial PRIMARY KEY, -- Network interface identifier
447     node_id integer REFERENCES nodes NOT NULL, -- Which node
448     is_primary boolean NOT NULL DEFAULT false, -- Is the primary interface for this node
449     type text REFERENCES network_types NOT NULL, -- Addressing scheme
450     method text REFERENCES network_methods NOT NULL, -- Configuration method
451
452     -- Optional, depending on type and method
453     ip text, -- IP address
454     mac text, -- MAC address
455     gateway text, -- Default gateway address
456     network text, -- Network address
457     broadcast text, -- Network broadcast address
458     netmask text, -- Network mask
459     dns1 text, -- Primary DNS server
460     dns2 text, -- Secondary DNS server
461     bwlimit integer, -- Bandwidth limit in bps
462     hostname text -- Hostname of this interface
463 ) WITH OIDS;
464 CREATE INDEX interfaces_node_id_idx ON interfaces (node_id);
465
466 -- Ordered by primary interface first
467 CREATE OR REPLACE VIEW interfaces_ordered AS
468 SELECT node_id, interface_id
469 FROM interfaces
470 ORDER BY is_primary DESC;
471
472 -- Network interfaces on each node
473 CREATE OR REPLACE VIEW node_interfaces AS
474 SELECT node_id,
475 array_accum(interface_id) AS interface_ids
476 FROM interfaces_ordered
477 GROUP BY node_id;
478
479 --------------------------------------------------------------------------------
480 -- Interface setting types and interfaces settings
481 --------------------------------------------------------------------------------
482
483 CREATE TABLE interface_setting_types (
484     interface_setting_type_id serial PRIMARY KEY,       
485                                                 -- Setting Type Identifier
486     name text UNIQUE NOT NULL,                  -- Setting Name    
487     description text,                           -- Optional Description
488     category text NOT NULL,                     -- Category, e.g. Wifi, or whatever
489     min_role_id integer REFERENCES roles        -- If set, minimal role required
490 ) WITH OIDS;
491
492 CREATE TABLE interface_setting (
493     interface_setting_id serial PRIMARY KEY,    -- Interface Setting Identifier
494     interface_id integer REFERENCES interfaces NOT NULL,
495                                                 -- the interface this applies to
496     interface_setting_type_id integer REFERENCES interface_setting_types NOT NULL,
497                                                 -- the setting type
498     value text                                  -- value attached
499 ) WITH OIDS;
500
501 CREATE OR REPLACE VIEW interface_settings AS 
502 SELECT interface_id,
503 array_accum(interface_setting_id) AS setting_ids
504 FROM interface_setting
505 GROUP BY interface_id;
506
507 CREATE OR REPLACE VIEW view_interface_settings AS
508 SELECT
509 interface_setting.interface_setting_id,
510 interface_setting.interface_id,
511 interface_setting_types.interface_setting_type_id,
512 interface_setting_types.name,
513 interface_setting_types.description,
514 interface_setting_types.category,
515 interface_setting_types.min_role_id,
516 interface_setting.value
517 FROM interface_setting
518 INNER JOIN interface_setting_types USING (interface_setting_type_id);
519
520 CREATE OR REPLACE VIEW view_interfaces AS
521 SELECT
522 interfaces.interface_id,
523 interfaces.node_id,
524 interfaces.is_primary,
525 interfaces.type,
526 interfaces.method,
527 interfaces.ip,
528 interfaces.mac,
529 interfaces.gateway,
530 interfaces.network,
531 interfaces.broadcast,
532 interfaces.netmask,
533 interfaces.dns1,
534 interfaces.dns2,
535 interfaces.bwlimit,
536 interfaces.hostname,
537 COALESCE((SELECT setting_ids FROM interface_settings WHERE interface_settings.interface_id = interfaces.interface_id), '{}') AS setting_ids
538 FROM interfaces;
539
540 --------------------------------------------------------------------------------
541 -- Power control units (PCUs)
542 --------------------------------------------------------------------------------
543
544 CREATE TABLE pcus (
545     -- Mandatory
546     pcu_id serial PRIMARY KEY, -- PCU identifier
547     site_id integer REFERENCES sites NOT NULL, -- Site identifier
548     hostname text, -- Hostname, not necessarily unique (multiple logical sites could use the same PCU)
549     ip text NOT NULL, -- IP, not necessarily unique
550
551     -- Optional
552     protocol text, -- Protocol, e.g. ssh or https or telnet
553     username text, -- Username, if applicable
554     "password" text, -- Password, if applicable
555     model text, -- Model, e.g. BayTech or iPal
556     notes text -- Random notes
557 ) WITH OIDS;
558 CREATE INDEX pcus_site_id_idx ON pcus (site_id);
559
560 CREATE OR REPLACE VIEW site_pcus AS
561 SELECT site_id,
562 array_accum(pcu_id) AS pcu_ids
563 FROM pcus
564 GROUP BY site_id;
565
566 CREATE TABLE pcu_node (
567     pcu_id integer REFERENCES pcus NOT NULL, -- PCU identifier
568     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
569     port integer NOT NULL, -- Port number
570     PRIMARY KEY (pcu_id, node_id), -- The same node cannot be controlled by different ports
571     UNIQUE (pcu_id, port) -- The same port cannot control multiple nodes
572 );
573 CREATE INDEX pcu_node_pcu_id_idx ON pcu_node (pcu_id);
574 CREATE INDEX pcu_node_node_id_idx ON pcu_node (node_id);
575
576 CREATE OR REPLACE VIEW node_pcus AS
577 SELECT node_id,
578 array_accum(pcu_id) AS pcu_ids,
579 array_accum(port) AS ports
580 FROM pcu_node
581 GROUP BY node_id;
582
583 CREATE OR REPLACE VIEW pcu_nodes AS
584 SELECT pcu_id,
585 array_accum(node_id) AS node_ids,
586 array_accum(port) AS ports
587 FROM pcu_node
588 GROUP BY pcu_id;
589
590 --------------------------------------------------------------------------------
591 -- Slices
592 --------------------------------------------------------------------------------
593
594 CREATE TABLE slice_instantiations (
595     instantiation text PRIMARY KEY
596 ) WITH OIDS;
597 INSERT INTO slice_instantiations (instantiation) VALUES 
598        ('not-instantiated'),     -- Placeholder slice
599        ('plc-instantiated'),     -- Instantiated by Node Manager
600        ('delegated'),            -- Manually instantiated
601        ('nm-controller');        -- NM Controller
602
603 -- Slices
604 CREATE TABLE slices (
605     slice_id serial PRIMARY KEY, -- Slice identifier
606     site_id integer REFERENCES sites NOT NULL, -- Site identifier
607
608     name text NOT NULL, -- Slice name
609     instantiation text REFERENCES slice_instantiations NOT NULL DEFAULT 'plc-instantiated', -- Slice state, e.g. plc-instantiated
610     url text, -- Project URL
611     description text, -- Project description
612
613     max_nodes integer NOT NULL DEFAULT 100, -- Maximum number of nodes that can be assigned to this slice
614
615     creator_person_id integer REFERENCES persons, -- Creator
616     created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, -- Creation date
617     expires timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP + '2 weeks', -- Expiration date
618
619     is_deleted boolean NOT NULL DEFAULT false
620 ) WITH OIDS;
621 CREATE INDEX slices_site_id_idx ON slices (site_id);
622 CREATE INDEX slices_name_idx ON slices (name);
623
624 -- Slivers
625 CREATE TABLE slice_node (
626     slice_id integer REFERENCES slices NOT NULL, -- Slice identifier
627     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
628     PRIMARY KEY (slice_id, node_id)
629 ) WITH OIDS;
630 CREATE INDEX slice_node_slice_id_idx ON slice_node (slice_id);
631 CREATE INDEX slice_node_node_id_idx ON slice_node (node_id);
632
633 -- Synonym for slice_node
634 CREATE OR REPLACE VIEW slivers AS
635 SELECT * FROM slice_node;
636
637 -- Nodes in each slice
638 CREATE OR REPLACE VIEW slice_nodes AS
639 SELECT slice_id,
640 array_accum(node_id) AS node_ids
641 FROM slice_node
642 GROUP BY slice_id;
643
644 -- Slices on each node
645 CREATE OR REPLACE VIEW node_slices AS
646 SELECT node_id,
647 array_accum(slice_id) AS slice_ids
648 FROM slice_node
649 GROUP BY node_id;
650
651 -- Slices at each site
652 CREATE OR REPLACE VIEW site_slices AS
653 SELECT site_id,
654 array_accum(slice_id) AS slice_ids
655 FROM slices
656 WHERE is_deleted is false
657 GROUP BY site_id;
658
659 -- Slice membership
660 CREATE TABLE slice_person (
661     slice_id integer REFERENCES slices NOT NULL, -- Slice identifier
662     person_id integer REFERENCES persons NOT NULL, -- Account identifier
663     PRIMARY KEY (slice_id, person_id)
664 ) WITH OIDS;
665 CREATE INDEX slice_person_slice_id_idx ON slice_person (slice_id);
666 CREATE INDEX slice_person_person_id_idx ON slice_person (person_id);
667
668 -- Members of the slice
669 CREATE OR REPLACE VIEW slice_persons AS
670 SELECT slice_id,
671 array_accum(person_id) AS person_ids
672 FROM slice_person
673 GROUP BY slice_id;
674
675 -- Slices of which each person is a member
676 CREATE OR REPLACE VIEW person_slices AS
677 SELECT person_id,
678 array_accum(slice_id) AS slice_ids
679 FROM slice_person
680 GROUP BY person_id;
681
682 --------------------------------------------------------------------------------
683 -- Slice whitelist
684 --------------------------------------------------------------------------------
685 -- slice whitelist on nodes
686 CREATE TABLE node_slice_whitelist (
687     node_id integer REFERENCES nodes NOT NULL, -- Node id of whitelist
688     slice_id integer REFERENCES slices NOT NULL, -- Slice id thats allowd on this node
689     PRIMARY KEY (node_id, slice_id)
690 ) WITH OIDS;
691 CREATE INDEX node_slice_whitelist_node_id_idx ON node_slice_whitelist (node_id);
692 CREATE INDEX node_slice_whitelist_slice_id_idx ON node_slice_whitelist (slice_id);
693
694 -- Slices on each node
695 CREATE OR REPLACE VIEW node_slices_whitelist AS
696 SELECT node_id,
697 array_accum(slice_id) AS slice_ids_whitelist
698 FROM node_slice_whitelist
699 GROUP BY node_id;
700
701 --------------------------------------------------------------------------------
702 -- Slice attributes
703 --------------------------------------------------------------------------------
704
705 -- Slice attribute types
706 CREATE TABLE slice_attribute_types (
707     attribute_type_id serial PRIMARY KEY, -- Attribute type identifier
708     name text UNIQUE NOT NULL, -- Attribute name
709     description text, -- Attribute description
710     min_role_id integer REFERENCES roles DEFAULT 10 -- If set, minimum (least powerful) role that can set or change this attribute
711 ) WITH OIDS;
712
713 -- Slice/sliver attributes
714 CREATE TABLE slice_attribute (
715     slice_attribute_id serial PRIMARY KEY, -- Slice attribute identifier
716     slice_id integer REFERENCES slices NOT NULL, -- Slice identifier
717     node_id integer REFERENCES nodes, -- Sliver attribute if set
718     nodegroup_id integer REFERENCES nodegroups, -- Node group attribute if set
719     attribute_type_id integer REFERENCES slice_attribute_types NOT NULL, -- Attribute type identifier
720     value text
721 ) WITH OIDS;
722 CREATE INDEX slice_attribute_slice_id_idx ON slice_attribute (slice_id);
723 CREATE INDEX slice_attribute_node_id_idx ON slice_attribute (node_id);
724 CREATE INDEX slice_attribute_nodegroup_id_idx ON slice_attribute (nodegroup_id);
725
726 CREATE OR REPLACE VIEW slice_attributes AS
727 SELECT slice_id,
728 array_accum(slice_attribute_id) AS slice_attribute_ids
729 FROM slice_attribute
730 GROUP BY slice_id;
731
732 --------------------------------------------------------------------------------
733 -- Initscripts
734 --------------------------------------------------------------------------------
735
736 -- Initscripts
737 CREATE TABLE initscripts (
738     initscript_id serial PRIMARY KEY, -- Initscript identifier
739     name text NOT NULL, -- Initscript name
740     enabled bool NOT NULL DEFAULT true, -- Initscript is active
741     script text NOT NULL, -- Initscript
742     UNIQUE (name)
743 ) WITH OIDS;
744 CREATE INDEX initscripts_name_idx ON initscripts (name);
745
746
747 --------------------------------------------------------------------------------
748 -- Peers
749 --------------------------------------------------------------------------------
750
751 -- Peers
752 CREATE TABLE peers (
753     peer_id serial PRIMARY KEY, -- Peer identifier
754     peername text NOT NULL, -- Peer name
755     peer_url text NOT NULL, -- (HTTPS) URL of the peer PLCAPI interface
756     cacert text, -- (SSL) Public certificate of peer API server
757     key text, -- (GPG) Public key used for authentication
758     deleted boolean NOT NULL DEFAULT false
759 ) WITH OIDS;
760 CREATE INDEX peers_peername_idx ON peers (peername) WHERE deleted IS false;
761
762 -- Objects at each peer
763 CREATE TABLE peer_site (
764     site_id integer REFERENCES sites PRIMARY KEY, -- Local site identifier
765     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
766     peer_site_id integer NOT NULL, -- Foreign site identifier at peer
767     UNIQUE (peer_id, peer_site_id) -- The same foreign site should not be cached twice
768 ) WITH OIDS;
769 CREATE INDEX peer_site_peer_id_idx ON peers (peer_id);
770
771 CREATE OR REPLACE VIEW peer_sites AS
772 SELECT peer_id,
773 array_accum(site_id) AS site_ids,
774 array_accum(peer_site_id) AS peer_site_ids
775 FROM peer_site
776 GROUP BY peer_id;
777
778 CREATE TABLE peer_person (
779     person_id integer REFERENCES persons PRIMARY KEY, -- Local user identifier
780     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
781     peer_person_id integer NOT NULL, -- Foreign user identifier at peer
782     UNIQUE (peer_id, peer_person_id) -- The same foreign user should not be cached twice
783 ) WITH OIDS;
784 CREATE INDEX peer_person_peer_id_idx ON peer_person (peer_id);
785
786 CREATE OR REPLACE VIEW peer_persons AS
787 SELECT peer_id,
788 array_accum(person_id) AS person_ids,
789 array_accum(peer_person_id) AS peer_person_ids
790 FROM peer_person
791 GROUP BY peer_id;
792
793 CREATE TABLE peer_key (
794     key_id integer REFERENCES keys PRIMARY KEY, -- Local key identifier
795     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
796     peer_key_id integer NOT NULL, -- Foreign key identifier at peer
797     UNIQUE (peer_id, peer_key_id) -- The same foreign key should not be cached twice
798 ) WITH OIDS;
799 CREATE INDEX peer_key_peer_id_idx ON peer_key (peer_id);
800
801 CREATE OR REPLACE VIEW peer_keys AS
802 SELECT peer_id,
803 array_accum(key_id) AS key_ids,
804 array_accum(peer_key_id) AS peer_key_ids
805 FROM peer_key
806 GROUP BY peer_id;
807
808 CREATE TABLE peer_node (
809     node_id integer REFERENCES nodes PRIMARY KEY, -- Local node identifier
810     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
811     peer_node_id integer NOT NULL, -- Foreign node identifier
812     UNIQUE (peer_id, peer_node_id) -- The same foreign node should not be cached twice
813 ) WITH OIDS;
814 CREATE INDEX peer_node_peer_id_idx ON peer_node (peer_id);
815
816 CREATE OR REPLACE VIEW peer_nodes AS
817 SELECT peer_id,
818 array_accum(node_id) AS node_ids,
819 array_accum(peer_node_id) AS peer_node_ids
820 FROM peer_node
821 GROUP BY peer_id;
822
823 CREATE TABLE peer_slice (
824     slice_id integer REFERENCES slices PRIMARY KEY, -- Local slice identifier
825     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
826     peer_slice_id integer NOT NULL, -- Slice identifier at peer
827     UNIQUE (peer_id, peer_slice_id) -- The same foreign slice should not be cached twice
828 ) WITH OIDS;
829 CREATE INDEX peer_slice_peer_id_idx ON peer_slice (peer_id);
830
831 CREATE OR REPLACE VIEW peer_slices AS
832 SELECT peer_id,
833 array_accum(slice_id) AS slice_ids,
834 array_accum(peer_slice_id) AS peer_slice_ids
835 FROM peer_slice
836 GROUP BY peer_id;
837
838 --------------------------------------------------------------------------------
839 -- Authenticated sessions
840 --------------------------------------------------------------------------------
841
842 -- Authenticated sessions
843 CREATE TABLE sessions (
844     session_id text PRIMARY KEY, -- Session identifier
845     expires timestamp without time zone
846 ) WITH OIDS;
847
848 -- People can have multiple sessions
849 CREATE TABLE person_session (
850     person_id integer REFERENCES persons NOT NULL, -- Account identifier
851     session_id text REFERENCES sessions NOT NULL, -- Session identifier
852     PRIMARY KEY (person_id, session_id),
853     UNIQUE (session_id) -- Sessions are unique
854 ) WITH OIDS;
855 CREATE INDEX person_session_person_id_idx ON person_session (person_id);
856
857 -- Nodes can have only one session
858 CREATE TABLE node_session (
859     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
860     session_id text REFERENCES sessions NOT NULL, -- Session identifier
861     UNIQUE (node_id), -- Nodes can have only one session
862     UNIQUE (session_id) -- Sessions are unique
863 ) WITH OIDS;
864
865
866
867 -------------------------------------------------------------------------------
868 -- PCU Types
869 ------------------------------------------------------------------------------
870 CREATE TABLE pcu_types (
871     pcu_type_id serial PRIMARY KEY,
872     model text NOT NULL ,               -- PCU model name
873     name text                           -- Full PCU model name
874 ) WITH OIDS;
875 CREATE INDEX pcu_types_model_idx ON pcu_types (model);
876
877 CREATE TABLE pcu_protocol_type (
878     pcu_protocol_type_id serial PRIMARY KEY,
879     pcu_type_id integer REFERENCES pcu_types NOT NULL,  -- PCU type identifier
880     port integer NOT NULL,                              -- PCU port
881     protocol text NOT NULL,                             -- Protocol
882     supported boolean NOT NULL DEFAULT True             -- Does PLC support
883 ) WITH OIDS;
884 CREATE INDEX pcu_protocol_type_pcu_type_id ON pcu_protocol_type (pcu_type_id);
885
886
887 CREATE OR REPLACE VIEW pcu_protocol_types AS
888 SELECT pcu_type_id,
889 array_accum(pcu_protocol_type_id) as pcu_protocol_type_ids
890 FROM pcu_protocol_type
891 GROUP BY pcu_type_id;
892
893 --------------------------------------------------------------------------------
894 -- Message templates
895 --------------------------------------------------------------------------------
896
897 CREATE TABLE messages (
898     message_id text PRIMARY KEY, -- Message name
899     subject text, -- Message summary
900     template text, -- Message template
901     enabled bool NOT NULL DEFAULT true -- Whether message is enabled
902 ) WITH OIDS;
903
904 --------------------------------------------------------------------------------
905 -- Events
906 --------------------------------------------------------------------------------
907
908 -- Events
909 CREATE TABLE events (
910     event_id serial PRIMARY KEY,  -- Event identifier
911     person_id integer REFERENCES persons, -- Person responsible for event, if any
912     node_id integer REFERENCES nodes, -- Node responsible for event, if any
913     auth_type text, -- Type of auth used. i.e. AuthMethod
914     fault_code integer NOT NULL DEFAULT 0, -- Did this event result in error
915     call_name text NOT NULL, -- Call responsible for this event
916     call text NOT NULL, -- Call responsible for this event, including parameters
917     message text, -- High level description of this event
918     runtime float DEFAULT 0, -- Event run time
919     time timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP -- Event timestamp
920 ) WITH OIDS;
921
922 -- Database object(s) that may have been affected by a particular event
923 CREATE TABLE event_object (
924     event_id integer REFERENCES events NOT NULL, -- Event identifier
925     object_id integer NOT NULL, -- Object identifier
926     object_type text NOT NULL Default 'Unknown' -- What type of object is this event affecting
927 ) WITH OIDS;
928 CREATE INDEX event_object_event_id_idx ON event_object (event_id);
929 CREATE INDEX event_object_object_id_idx ON event_object (object_id);
930 CREATE INDEX event_object_object_type_idx ON event_object (object_type);
931
932 CREATE OR REPLACE VIEW event_objects AS
933 SELECT event_id,
934 array_accum(object_id) AS object_ids,
935 array_accum(object_type) AS object_types
936 FROM event_object
937 GROUP BY event_id;
938
939 --------------------------------------------------------------------------------
940 -- Useful views
941 --------------------------------------------------------------------------------
942 CREATE OR REPLACE VIEW view_pcu_types AS
943 SELECT
944 pcu_types.pcu_type_id,
945 pcu_types.model,
946 pcu_types.name,
947 COALESCE((SELECT pcu_protocol_type_ids FROM pcu_protocol_types WHERE pcu_protocol_types.pcu_type_id = pcu_types.pcu_type_id), '{}') AS pcu_protocol_type_ids
948 FROM pcu_types;
949
950 CREATE OR REPLACE VIEW view_events AS
951 SELECT
952 events.event_id,
953 events.person_id,
954 events.node_id,
955 events.auth_type,
956 events.fault_code,
957 events.call_name,
958 events.call,
959 events.message,
960 events.runtime,
961 CAST(date_part('epoch', events.time) AS bigint) AS time,
962 COALESCE((SELECT object_ids FROM event_objects WHERE event_objects.event_id = events.event_id), '{}') AS object_ids,
963 COALESCE((SELECT object_types FROM event_objects WHERE event_objects.event_id = events.event_id), '{}') AS object_types
964 FROM events;
965
966 CREATE OR REPLACE VIEW view_event_objects AS 
967 SELECT
968 events.event_id,
969 events.person_id,
970 events.node_id,
971 events.fault_code,
972 events.call_name,
973 events.call,
974 events.message,
975 events.runtime,
976 CAST(date_part('epoch', events.time) AS bigint) AS time,
977 event_object.object_id,
978 event_object.object_type
979 FROM events LEFT JOIN event_object USING (event_id);
980
981 CREATE OR REPLACE VIEW view_persons AS
982 SELECT
983 persons.person_id,
984 persons.email,
985 persons.first_name,
986 persons.last_name,
987 persons.deleted,
988 persons.enabled,
989 persons.password,
990 persons.verification_key,
991 CAST(date_part('epoch', persons.verification_expires) AS bigint) AS verification_expires,
992 persons.title,
993 persons.phone,
994 persons.url,
995 persons.bio,
996 CAST(date_part('epoch', persons.date_created) AS bigint) AS date_created,
997 CAST(date_part('epoch', persons.last_updated) AS bigint) AS last_updated,
998 peer_person.peer_id,
999 peer_person.peer_person_id,
1000 COALESCE((SELECT role_ids FROM person_roles WHERE person_roles.person_id = persons.person_id), '{}') AS role_ids,
1001 COALESCE((SELECT roles FROM person_roles WHERE person_roles.person_id = persons.person_id), '{}') AS roles,
1002 COALESCE((SELECT site_ids FROM person_sites WHERE person_sites.person_id = persons.person_id), '{}') AS site_ids,
1003 COALESCE((SELECT key_ids FROM person_keys WHERE person_keys.person_id = persons.person_id), '{}') AS key_ids,
1004 COALESCE((SELECT slice_ids FROM person_slices WHERE person_slices.person_id = persons.person_id), '{}') AS slice_ids
1005 FROM persons
1006 LEFT JOIN peer_person USING (person_id);
1007
1008 CREATE OR REPLACE VIEW view_peers AS
1009 SELECT 
1010 peers.*, 
1011 COALESCE((SELECT site_ids FROM peer_sites WHERE peer_sites.peer_id = peers.peer_id), '{}') AS site_ids,
1012 COALESCE((SELECT peer_site_ids FROM peer_sites WHERE peer_sites.peer_id = peers.peer_id), '{}') AS peer_site_ids,
1013 COALESCE((SELECT person_ids FROM peer_persons WHERE peer_persons.peer_id = peers.peer_id), '{}') AS person_ids,
1014 COALESCE((SELECT peer_person_ids FROM peer_persons WHERE peer_persons.peer_id = peers.peer_id), '{}') AS peer_person_ids,
1015 COALESCE((SELECT key_ids FROM peer_keys WHERE peer_keys.peer_id = peers.peer_id), '{}') AS key_ids,
1016 COALESCE((SELECT peer_key_ids FROM peer_keys WHERE peer_keys.peer_id = peers.peer_id), '{}') AS peer_key_ids,
1017 COALESCE((SELECT node_ids FROM peer_nodes WHERE peer_nodes.peer_id = peers.peer_id), '{}') AS node_ids,
1018 COALESCE((SELECT peer_node_ids FROM peer_nodes WHERE peer_nodes.peer_id = peers.peer_id), '{}') AS peer_node_ids,
1019 COALESCE((SELECT slice_ids FROM peer_slices WHERE peer_slices.peer_id = peers.peer_id), '{}') AS slice_ids,
1020 COALESCE((SELECT peer_slice_ids FROM peer_slices WHERE peer_slices.peer_id = peers.peer_id), '{}') AS peer_slice_ids
1021 FROM peers;
1022
1023 CREATE OR REPLACE VIEW view_nodes AS
1024 SELECT
1025 nodes.node_id,
1026 nodes.hostname,
1027 nodes.site_id,
1028 nodes.boot_state,
1029 nodes.deleted,
1030 nodes.model,
1031 nodes.boot_nonce,
1032 nodes.version,
1033 nodes.ssh_rsa_key,
1034 nodes.key,
1035 CAST(date_part('epoch', nodes.date_created) AS bigint) AS date_created,
1036 CAST(date_part('epoch', nodes.last_updated) AS bigint) AS last_updated,
1037 CAST(date_part('epoch', nodes.last_contact) AS bigint) AS last_contact,  
1038 peer_node.peer_id,
1039 peer_node.peer_node_id,
1040 COALESCE((SELECT interface_ids FROM node_interfaces WHERE node_interfaces.node_id = nodes.node_id), '{}') AS interface_ids,
1041 COALESCE((SELECT nodegroup_ids FROM node_nodegroups WHERE node_nodegroups.node_id = nodes.node_id), '{}') AS nodegroup_ids,
1042 COALESCE((SELECT slice_ids FROM node_slices WHERE node_slices.node_id = nodes.node_id), '{}') AS slice_ids,
1043 COALESCE((SELECT slice_ids_whitelist FROM node_slices_whitelist WHERE node_slices_whitelist.node_id = nodes.node_id), '{}') AS slice_ids_whitelist,
1044 COALESCE((SELECT pcu_ids FROM node_pcus WHERE node_pcus.node_id = nodes.node_id), '{}') AS pcu_ids,
1045 COALESCE((SELECT ports FROM node_pcus WHERE node_pcus.node_id = nodes.node_id), '{}') AS ports,
1046 COALESCE((SELECT conf_file_ids FROM node_conf_files WHERE node_conf_files.node_id = nodes.node_id), '{}') AS conf_file_ids,
1047 COALESCE((SELECT tag_ids FROM node_tags WHERE node_tags.node_id = nodes.node_id), '{}') AS tag_ids,
1048 node_session.session_id AS session
1049 FROM nodes
1050 LEFT JOIN peer_node USING (node_id)
1051 LEFT JOIN node_session USING (node_id);
1052
1053 CREATE OR REPLACE VIEW view_nodegroups AS
1054 SELECT
1055 nodegroups.*,
1056 COALESCE((SELECT node_ids FROM nodegroup_nodes WHERE nodegroup_nodes.nodegroup_id = nodegroups.nodegroup_id), '{}') AS node_ids,
1057 COALESCE((SELECT conf_file_ids FROM nodegroup_conf_files WHERE nodegroup_conf_files.nodegroup_id = nodegroups.nodegroup_id), '{}') AS conf_file_ids
1058 FROM nodegroups;
1059
1060 CREATE OR REPLACE VIEW view_conf_files AS
1061 SELECT
1062 conf_files.*,
1063 COALESCE((SELECT node_ids FROM conf_file_nodes WHERE conf_file_nodes.conf_file_id = conf_files.conf_file_id), '{}') AS node_ids,
1064 COALESCE((SELECT nodegroup_ids FROM conf_file_nodegroups WHERE conf_file_nodegroups.conf_file_id = conf_files.conf_file_id), '{}') AS nodegroup_ids
1065 FROM conf_files;
1066
1067 CREATE OR REPLACE VIEW view_pcus AS
1068 SELECT
1069 pcus.*,
1070 COALESCE((SELECT node_ids FROM pcu_nodes WHERE pcu_nodes.pcu_id = pcus.pcu_id), '{}') AS node_ids,
1071 COALESCE((SELECT ports FROM pcu_nodes WHERE pcu_nodes.pcu_id = pcus.pcu_id), '{}') AS ports
1072 FROM pcus;
1073
1074 CREATE OR REPLACE VIEW view_sites AS
1075 SELECT
1076 sites.site_id,
1077 sites.login_base,
1078 sites.name,
1079 sites.abbreviated_name,
1080 sites.deleted,
1081 sites.enabled,
1082 sites.is_public,
1083 sites.max_slices,
1084 sites.max_slivers,
1085 sites.latitude,
1086 sites.longitude,
1087 sites.url,
1088 sites.ext_consortium_id,
1089 CAST(date_part('epoch', sites.date_created) AS bigint) AS date_created,
1090 CAST(date_part('epoch', sites.last_updated) AS bigint) AS last_updated,
1091 peer_site.peer_id,
1092 peer_site.peer_site_id,
1093 COALESCE((SELECT person_ids FROM site_persons WHERE site_persons.site_id = sites.site_id), '{}') AS person_ids,
1094 COALESCE((SELECT node_ids FROM site_nodes WHERE site_nodes.site_id = sites.site_id), '{}') AS node_ids,
1095 COALESCE((SELECT address_ids FROM site_addresses WHERE site_addresses.site_id = sites.site_id), '{}') AS address_ids,
1096 COALESCE((SELECT slice_ids FROM site_slices WHERE site_slices.site_id = sites.site_id), '{}') AS slice_ids,
1097 COALESCE((SELECT pcu_ids FROM site_pcus WHERE site_pcus.site_id = sites.site_id), '{}') AS pcu_ids
1098 FROM sites
1099 LEFT JOIN peer_site USING (site_id);
1100
1101 CREATE OR REPLACE VIEW view_addresses AS
1102 SELECT
1103 addresses.*,
1104 COALESCE((SELECT address_type_ids FROM address_address_types WHERE address_address_types.address_id = addresses.address_id), '{}') AS address_type_ids,
1105 COALESCE((SELECT address_types FROM address_address_types WHERE address_address_types.address_id = addresses.address_id), '{}') AS address_types
1106 FROM addresses;
1107
1108 CREATE OR REPLACE VIEW view_keys AS
1109 SELECT
1110 keys.*,
1111 person_key.person_id,
1112 peer_key.peer_id,
1113 peer_key.peer_key_id
1114 FROM keys
1115 LEFT JOIN person_key USING (key_id)
1116 LEFT JOIN peer_key USING (key_id);
1117
1118 CREATE OR REPLACE VIEW view_slices AS
1119 SELECT
1120 slices.slice_id,
1121 slices.site_id,
1122 slices.name,
1123 slices.instantiation,
1124 slices.url,
1125 slices.description,
1126 slices.max_nodes,
1127 slices.creator_person_id,
1128 slices.is_deleted,
1129 CAST(date_part('epoch', slices.created) AS bigint) AS created,
1130 CAST(date_part('epoch', slices.expires) AS bigint) AS expires,
1131 peer_slice.peer_id,
1132 peer_slice.peer_slice_id,
1133 COALESCE((SELECT node_ids FROM slice_nodes WHERE slice_nodes.slice_id = slices.slice_id), '{}') AS node_ids,
1134 COALESCE((SELECT person_ids FROM slice_persons WHERE slice_persons.slice_id = slices.slice_id), '{}') AS person_ids,
1135 COALESCE((SELECT slice_attribute_ids FROM slice_attributes WHERE slice_attributes.slice_id = slices.slice_id), '{}') AS slice_attribute_ids
1136 FROM slices
1137 LEFT JOIN peer_slice USING (slice_id);
1138
1139 CREATE OR REPLACE VIEW view_slice_attributes AS
1140 SELECT
1141 slice_attribute.slice_attribute_id,
1142 slice_attribute.slice_id,
1143 slice_attribute.node_id,
1144 slice_attribute.nodegroup_id,
1145 slice_attribute_types.attribute_type_id,
1146 slice_attribute_types.name,
1147 slice_attribute_types.description,
1148 slice_attribute_types.min_role_id,
1149 slice_attribute.value
1150 FROM slice_attribute
1151 INNER JOIN slice_attribute_types USING (attribute_type_id);
1152
1153 CREATE OR REPLACE VIEW view_sessions AS
1154 SELECT
1155 sessions.session_id,
1156 CAST(date_part('epoch', sessions.expires) AS bigint) AS expires,
1157 person_session.person_id,
1158 node_session.node_id
1159 FROM sessions
1160 LEFT JOIN person_session USING (session_id)
1161 LEFT JOIN node_session USING (session_id);
1162
1163 --------------------------------------------------------------------------------
1164 -- Built-in maintenance account and default site
1165 --------------------------------------------------------------------------------
1166
1167 INSERT INTO persons
1168 (first_name, last_name, email, password, enabled)
1169 VALUES
1170 ('Maintenance', 'Account', 'maint@localhost.localdomain', 'nopass', true);
1171
1172 INSERT INTO person_role (person_id, role_id) 
1173        VALUES (1, 10), (1, 20), (1, 30), (1, 40);
1174
1175 INSERT INTO sites
1176 (login_base, name, abbreviated_name, max_slices)
1177 VALUES
1178 ('pl', 'PlanetLab Central', 'PLC', 100);