4 from opnfv_testapi.common import message
5 from opnfv_testapi.common import raises
6 from opnfv_testapi.resources import handlers
7 import opnfv_testapi.resources.scenario_models as models
8 from opnfv_testapi.tornado_swagger import swagger
11 class GenericScenarioHandler(handlers.GenericApiHandler):
12 def __init__(self, application, request, **kwargs):
13 super(GenericScenarioHandler, self).__init__(application,
16 self.table = self.db_scenarios
17 self.table_cls = models.Scenario
20 class ScenariosCLHandler(GenericScenarioHandler):
21 @swagger.operation(nickname="queryScenarios")
24 @description: Retrieve scenario(s).
25 @notes: Retrieve scenario(s)
26 Available filters for this request are :
27 - name : scenario name
29 GET /scenarios?name=scenario_1
30 @param name: scenario name
34 @param installer: installer type
35 @type installer: L{string}
37 @required installer: False
38 @param version: version
39 @type version: L{string}
41 @required version: False
42 @param project: project name
43 @type project: L{string}
45 @required project: False
46 @return 200: all scenarios satisfy queries,
47 empty list if no scenario is found
54 for k in self.request.query_arguments.keys():
55 v = self.get_query_argument(k)
57 elem_query["installer"] = v
59 elem_query["versions.version"] = v
61 elem_query["versions.projects.project"] = v
65 query['installers'] = {'$elemMatch': elem_query}
68 self._list(_set_query())
70 @swagger.operation(nickname="createScenario")
73 @description: create a new scenario by name
74 @param body: scenario to be created
75 @type body: L{ScenarioCreateRequest}
77 @rtype: L{CreateResponse}
78 @return 200: scenario is created.
79 @raise 403: scenario already exists
80 @raise 400: body or name not provided
83 return {'name': data.name}
86 return httplib.FORBIDDEN, message.exist('scenario', data.name)
88 miss_checks = ['name']
89 db_checks = [(self.table, False, query, error)]
90 self._create(miss_checks=miss_checks, db_checks=db_checks)
93 class ScenarioGURHandler(GenericScenarioHandler):
94 @swagger.operation(nickname='getScenarioByName')
97 @description: get a single scenario by name
99 @return 200: scenario exist
100 @raise 404: scenario not exist
102 self._get_one({'name': name})
105 @swagger.operation(nickname="updateScenarioByName")
108 @description: update a single scenario by name
109 @param body: fields to be updated
110 @type body: L{ScenarioUpdateRequest}
113 @return 200: update success
114 @raise 404: scenario not exist
115 @raise 403: nothing to update
117 query = {'name': name}
119 self._update(query, db_keys)
121 @swagger.operation(nickname="deleteScenarioByName")
122 def delete(self, name):
124 @description: delete a scenario by name
125 @return 200: delete success
126 @raise 404: scenario not exist:
129 query = {'name': name}
132 def _update_query(self, keys, data):
135 if self._is_rename():
136 new = self._term.get('name')
143 def _update_requests(self, data):
145 ('name', 'update'): self._update_requests_rename,
146 ('installer', 'add'): self._update_requests_add_installer,
147 ('installer', 'delete'): self._update_requests_delete_installer,
148 ('version', 'add'): self._update_requests_add_version,
149 ('version', 'delete'): self._update_requests_delete_version,
150 ('owner', 'update'): self._update_requests_change_owner,
151 ('project', 'add'): self._update_requests_add_project,
152 ('project', 'delete'): self._update_requests_delete_project,
153 ('customs', 'add'): self._update_requests_add_customs,
154 ('customs', 'delete'): self._update_requests_delete_customs,
155 ('score', 'add'): self._update_requests_add_score,
156 ('trust_indicator', 'add'): self._update_requests_add_ti,
159 updates[(self._field, self._op)](data)
163 def _iter_installers(xstep):
164 @functools.wraps(xstep)
165 def magic(self, data):
166 [xstep(self, installer)
167 for installer in self._filter_installers(data.installers)]
170 def _iter_versions(xstep):
171 @functools.wraps(xstep)
172 def magic(self, installer):
173 [xstep(self, version)
174 for version in (self._filter_versions(installer.versions))]
177 def _iter_projects(xstep):
178 @functools.wraps(xstep)
179 def magic(self, version):
180 [xstep(self, project)
181 for project in (self._filter_projects(version.projects))]
184 def _update_requests_rename(self, data):
185 data.name = self._term.get('name')
187 raises.BadRequest(message.missing('name'))
189 def _update_requests_add_installer(self, data):
190 data.installers.append(models.ScenarioInstaller.from_dict(self._term))
192 def _update_requests_delete_installer(self, data):
193 data.installers = self._remove_installers(data.installers)
196 def _update_requests_add_version(self, installer):
197 installer.versions.append(models.ScenarioVersion.from_dict(self._term))
200 def _update_requests_delete_version(self, installer):
201 installer.versions = self._remove_versions(installer.versions)
205 def _update_requests_change_owner(self, version):
206 version.owner = self._term.get('owner')
210 def _update_requests_add_project(self, version):
211 version.projects.append(models.ScenarioProject.from_dict(self._term))
215 def _update_requests_delete_project(self, version):
216 version.projects = self._remove_projects(version.projects)
221 def _update_requests_add_customs(self, project):
222 project.customs = list(set(project.customs + self._term))
227 def _update_requests_delete_customs(self, project):
228 project.customs = filter(
229 lambda f: f not in self._term,
235 def _update_requests_add_score(self, project):
236 project.scores.append(
237 models.ScenarioScore.from_dict(self._term))
242 def _update_requests_add_ti(self, project):
243 project.trust_indicators.append(
244 models.ScenarioTI.from_dict(self._term))
246 def _is_rename(self):
247 return self._field == 'name' and self._op == 'update'
249 def _remove_installers(self, installers):
250 return self._remove('installer', installers)
252 def _filter_installers(self, installers):
253 return self._filter('installer', installers)
255 def _remove_versions(self, versions):
256 return self._remove('version', versions)
258 def _filter_versions(self, versions):
259 return self._filter('version', versions)
261 def _remove_projects(self, projects):
262 return self._remove('project', projects)
264 def _filter_projects(self, projects):
265 return self._filter('project', projects)
267 def _remove(self, field, fields):
269 lambda f: getattr(f, field) != self._locate.get(field),
272 def _filter(self, field, fields):
274 lambda f: getattr(f, field) == self._locate.get(field),
279 return self.json_args.get('field')
283 return self.json_args.get('op')
287 return self.json_args.get('locate')
291 return self.json_args.get('term')