]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | from pecan import expose, request, abort, response |
2 | from webob import exc | |
3 | from pecan.rest import RestController | |
4 | from ceph_brag.model import db | |
5 | import sys, traceback | |
6 | ||
7 | class RootController(RestController): | |
8 | def fail(self, status_code=200, msg="OK"): | |
9 | response.status = status_code | |
10 | return msg | |
11 | ||
12 | @expose('json') | |
13 | def get(self, *args, **kwargs): | |
14 | if len(args) == 0: | |
15 | #return the list of uuids | |
16 | try: | |
17 | result = db.get_uuids() | |
18 | except Exception as e: | |
19 | return self.fail(500, msg="Internal Server Error") | |
20 | elif len(args) == 1 or len(args) == 2 and args[1] == '': | |
21 | #/uuid | |
22 | try: | |
23 | result = db.get_versions(args[0]) | |
24 | except Exception as e: | |
25 | return self.fail(status_code=500, msg="Internal Server Error") | |
26 | ||
27 | if result is None: | |
28 | return self.fail(400, msg="Invalid UUID") | |
29 | elif len(args) == 2 or len(args) == 3 and args[2] == '': | |
30 | #/uuid/version_number | |
31 | try: | |
32 | result = db.get_brag(args[0], args[1]) | |
33 | except Exception as e: | |
34 | return self.fail(status_code=500, msg="Internal Server Error") | |
35 | ||
36 | if result is None: | |
37 | return self.fail(status_code=400, msg="Invalid UUID,version combination") | |
38 | else: | |
39 | return self.fail(status_code=400, msg="Invalid args") | |
40 | ||
41 | return result | |
42 | ||
43 | @expose(content_type='application/json') | |
44 | def put(self, *args, **kwargs): | |
45 | try: | |
46 | db.put_new_version(request.body.decode('utf-8')) | |
47 | except ValueError as ve: | |
48 | return self.fail(status_code=422, msg="Improper payload : " + str(ve)) | |
49 | except KeyError as ke: | |
50 | msg = "Payload not as expected, some keys are missing : " + str(ke) | |
51 | return self.fail(status_code=422, msg=msg) | |
52 | except Exception as e: | |
53 | return self.fail(status_code=500, msg="Internal Server Error : " + str(e)) | |
54 | ||
55 | response.status = 201 | |
56 | return "CREATED" | |
57 | ||
58 | @expose() | |
59 | def delete(self, *args, **kwargs): | |
60 | if 'uuid' not in kwargs: | |
61 | return self.fail(status_code=400, msg="Required uuid parameter") | |
62 | ||
63 | uuid = kwargs['uuid'] | |
64 | try: | |
65 | status = db.delete_uuid(uuid) | |
66 | except Exception as e: | |
67 | return self.fail(status_code=500, msg="Internal Server Error") | |
68 | ||
69 | if status is not None: | |
70 | return self.fail(status_code=status['status'], msg=status['msg']) | |
71 | ||
72 | response.status=200 | |
73 | return "DELETED" |