5 from qa.PLCs import PLC, PLCs
7 class add_test_data(Test):
9 Adds the test data found in config to the plc db
11 def call(self, plc_name = None):
13 # Determine which plc to talk to
14 plc = self.config.get_plc(plc_name)
16 auth = plc.config.auth
18 # Search config for objects that belong to this plc
19 # Any object with 'plc' defined as this plc's name or with
20 # no 'plc' defined will be added
21 this_plc = lambda object: 'plc' not in object or \
22 'plc' in object and object['plc'] == plc['name'] or \
25 sitelist = filter(this_plc, self.config.sites)
26 nodelist = filter(this_plc, self.config.nodes)
27 slicelist = filter(this_plc, self.config.slices)
28 personlist = filter(this_plc, self.config.persons)
32 sites = api.GetSites(auth, [site['login_base']])
34 site_id = api.AddSite(auth, dict(site))
35 site['site_id'] = site_id
36 if self.config.verbose:
37 utils.header("Added site: %s" % site['name'])
40 if self.config.verbose:
41 utils.header("Site %s found" % site['name'])
45 nodes = api.GetNodes(auth, [node['hostname']])
47 node_id = api.AddNode(auth, node['site'], dict(node))
48 node['node_id'] = node_id
49 if self.config.verbose:
50 utils.header("Added node: %s" % node['hostname'])
53 if self.config.verbose:
54 utils.header("Node %s found" % node['hostname'])
57 if 'nodenetwork_ids' not in node or not node['nodenetwork_ids']:
58 for nodenetwork in node['nodenetworks']:
59 nodenetwork_id = api.AddNodeNetwork(auth, node['hostname'], dict(nodenetwork))
60 if self.config.verbose:
61 utils.header("Added nodenetwork to %s" % node['hostname'])
63 if self.config.verbose:
64 utils.header("Nodenetwork found on node %s" % node['hostname'])
67 for slice in slicelist:
68 slices = api.GetSlices(auth, [slice['name']])
70 slice_id = api.AddSlice(auth, dict(slice))
71 slice['slice_id'] = slice_id
72 if self.config.verbose:
73 utils.header("Added slice: %s" % slice['name'])
75 slice.update(slices[0])
76 if self.config.verbose:
77 utils.header("Slice %s found" % slice['name'])
80 for node in slice['nodes']:
81 api.AddSliceToNodes(auth, slice['name'], [node])
82 if self.config.verbose:
83 utils.header("Added slice to %s" % node)
86 for person in personlist:
87 roles = person['roles']
88 persons = api.GetPersons(auth, [person['email']])
90 person_id = api.AddPerson(auth, dict(person))
91 person['person_id'] = person_id
92 api.UpdatePerson(auth, person_id, {'enabled': True})
93 if self.config.verbose:
94 utils.header("Added person: %s" % person['email'])
96 person.update(persons[0])
97 if self.config.verbose:
98 utils.header("Person %s found" % person['email'])
100 # Add roles to person
102 api.AddRoleToPerson(auth, role, person['email'])
103 if self.config.verbose:
104 utils.header("Added %s to %s" % (role, person['email']))
106 for site in person['sites']:
107 api.AddPersonToSite(auth, person['email'], site)
108 if self.config.verbose:
109 utils.header("Added %s to %s" % (person['email'], site))
111 # Add person to slice
112 for slice in person['slices']:
113 api.AddPersonToSlice(auth, person['email'], slice)
114 if self.config.verbose:
115 utils.header("Added %s to %s" % (person['email'], slice))
118 if __name__ == '__main__':
119 args = tuple(sys.argv[1:])
120 add_test_data()(*args)