Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 1 | import ast |
| 2 | |
| 3 | def xproto_check_synchronizer(m): |
| 4 | try: |
| 5 | sync_step_path = 'synchronizer/steps/sync_%s.py'%m['name'].lower() |
| 6 | sync_step = open(sync_step_path).read() |
| 7 | except IOError: |
| 8 | return '510 Model needs a sync step %s'%sync_step_path |
| 9 | |
| 10 | try: |
| 11 | sync_step_ast = ast.parse(sync_step) |
| 12 | except SyntaxError: |
| 13 | return '511 Could not parse sync step %s'%sync_step_path |
| 14 | |
| 15 | classes = filter(lambda x:isinstance(x, ast.ClassDef), sync_step_ast.body) |
| 16 | found_sync_step_class = False |
| 17 | |
| 18 | for c in classes: |
| 19 | base_names = [v.id for v in c.bases] |
| 20 | if 'SyncStep' in base_names or 'SyncInstanceUsingAnsible' in base_names: |
| 21 | attributes = filter(lambda x:isinstance(x, ast.Assign), c.body) |
| 22 | for a in attributes: |
| 23 | target_names = [t.id for t in a.targets] |
| 24 | values = a.value.elts if isinstance(a.value, ast.List) else [a.value] |
| 25 | value_names = [v.id for v in values] |
| 26 | |
| 27 | if 'observes' in target_names and m['name'] in value_names: |
| 28 | found_sync_step_class = True |
| 29 | break |
| 30 | |
| 31 | if not found_sync_step_class: |
| 32 | return '512 Synchronizer needs a sync step class with an observes field containing %s'%m['name'] |
| 33 | else: |
| 34 | return '200 OK' |
| 35 | |
| 36 | |
| 37 | def xproto_check_policy(m): |
| 38 | try: |
| 39 | model_policy_path = 'synchronizer/model_policies/model_policy_%s.py'%m['name'].lower() |
| 40 | model_policy = open(model_policy_path).read() |
| 41 | except IOError: |
| 42 | return '510 Model needs a model policy %s'%model_policy_path |
| 43 | |
| 44 | try: |
| 45 | model_policy_ast = ast.parse(model_policy) |
| 46 | except SyntaxError: |
| 47 | return '511 Could not parse sync step %s'%model_policy_path |
| 48 | |
| 49 | classes = filter(lambda x:isinstance(x, ast.ClassDef), model_policy_ast.body) |
| 50 | found_model_policy_class = False |
| 51 | for c in classes: |
| 52 | base_names = [v.id for v in c.bases] |
| 53 | if 'Policy' in base_names or 'TenantWithContainerPolicy' in base_names: |
| 54 | found_model_policy_class = True |
| 55 | break |
| 56 | |
| 57 | if not found_model_policy_class: |
| 58 | return '513 Synchronizer needs a model policy class' |
| 59 | else: |
| 60 | return '200 OK' |
| 61 | |