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 = PLC(self.config)
15 plcs = getattr(self.config, 'plcs', [])
17 if p['name'] in [plc_name]:
19 plc.config.update_api(plc)
22 auth = plc.config.auth
25 # Search config for objects that belong to this plc
26 # Any object with 'plc' defined as this plc's name or with
27 # no 'plc' defined will be added
28 this_plc = lambda object: 'plc' not in object or \
29 'plc' in object and object['plc'] == plc['name'] or \
32 sitelist = filter(this_plc, self.config.sites)
33 nodelist = filter(this_plc, self.config.nodes)
34 slicelist = filter(this_plc, self.config.slices)
35 personlist = filter(this_plc, self.config.persons)
39 sites = api.GetSites(auth, [site['login_base']])
41 site_id = api.AddSite(auth, dict(site))
42 site['site_id'] = site_id
43 if self.config.verbose:
44 utils.header("Added site: %s" % site['name'])
47 if self.config.verbose:
48 utils.header("Site %s found" % site['name'])
52 nodes = api.GetNodes(auth, [node['hostname']])
54 node_id = api.AddNode(auth, node['site'], dict(node))
55 node['node_id'] = node_id
56 if self.config.verbose:
57 utils.header("Added node: %s" % node['hostname'])
60 if self.config.verbose:
61 utils.header("Node %s found" % node['hostname'])
64 if 'nodenetwork_ids' not in node or not node['nodenetwork_ids']:
65 for nodenetwork in node['nodenetworks']:
66 nodenetwork_id = api.AddNodeNetwork(auth, node['hostname'], dict(nodenetwork))
67 if self.config.verbose:
68 utils.header("Added nodenetwork to %s" % node['hostname'])
70 if self.config.verbose:
71 utils.header("Nodenetwork found on node %s" % node['hostname'])
74 for slice in slicelist:
75 slices = api.GetSlices(auth, [slice['name']])
77 slice_id = api.AddSlice(auth, dict(slice))
78 slice['slice_id'] = slice_id
79 if self.config.verbose:
80 utils.header("Added slice: %s" % slice['name'])
82 slice.update(slices[0])
83 if self.config.verbose:
84 utils.header("Slice %s found" % slice['name'])
87 for node in slice['nodes']:
88 api.AddSliceToNodes(auth, slice['name'], [node])
89 if self.config.verbose:
90 utils.header("Added slice to %s" % node)
93 for person in personlist:
94 roles = person['roles']
95 persons = api.GetPersons(auth, [person['email']])
97 person_id = api.AddPerson(auth, dict(person))
98 person['person_id'] = person_id
99 api.UpdatePerson(auth, person_id, {'enabled': True})
100 if self.config.verbose:
101 utils.header("Added person: %s" % person['email'])
103 person.update(persons[0])
104 if self.config.verbose:
105 utils.header("Person %s found" % person['email'])
107 # Add roles to person
109 api.AddRoleToPerson(auth, role, person['email'])
110 if self.config.verbose:
111 utils.header("Added %s to %s" % (role, person['email']))
113 for site in person['sites']:
114 api.AddPersonToSite(auth, person['email'], site)
115 if self.config.verbose:
116 utils.header("Added %s to %s" % (person['email'], site))
118 # Add person to slice
119 for slice in person['slices']:
120 api.AddPersonToSlice(auth, person['email'], slice)
121 if self.config.verbose:
122 utils.header("Added %s to %s" % (person['email'], slice))
125 if __name__ == '__main__':
126 args = tuple(sys.argv[1:])
127 add_test_data()(*args)