Scott Baker | 083a3b6 | 2018-03-09 20:48:30 -0800 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 15 | from __future__ import absolute_import |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 16 | import ast |
| 17 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 18 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 19 | def xproto_check_synchronizer(m): |
| 20 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 21 | sync_step_path = "synchronizer/steps/sync_%s.py" % m["name"].lower() |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 22 | sync_step = open(sync_step_path).read() |
| 23 | except IOError: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 24 | return "510 Model needs a sync step %s" % sync_step_path |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 25 | |
| 26 | try: |
| 27 | sync_step_ast = ast.parse(sync_step) |
| 28 | except SyntaxError: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 29 | return "511 Could not parse sync step %s" % sync_step_path |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 30 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 31 | classes = [x for x in sync_step_ast.body if isinstance(x, ast.ClassDef)] |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 32 | found_sync_step_class = False |
| 33 | |
| 34 | for c in classes: |
| 35 | base_names = [v.id for v in c.bases] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 36 | if "SyncStep" in base_names or "SyncInstanceUsingAnsible" in base_names: |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 37 | attributes = [x for x in c.body if isinstance(x, ast.Assign)] |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 38 | for a in attributes: |
| 39 | target_names = [t.id for t in a.targets] |
| 40 | values = a.value.elts if isinstance(a.value, ast.List) else [a.value] |
| 41 | value_names = [v.id for v in values] |
| 42 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 43 | if "observes" in target_names and m["name"] in value_names: |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 44 | found_sync_step_class = True |
| 45 | break |
| 46 | |
| 47 | if not found_sync_step_class: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 48 | return ( |
| 49 | "512 Synchronizer needs a sync step class with an observes field containing %s" |
| 50 | % m["name"] |
| 51 | ) |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 52 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 53 | return "200 OK" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 54 | |
| 55 | |
| 56 | def xproto_check_policy(m): |
| 57 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 58 | model_policy_path = ( |
| 59 | "synchronizer/model_policies/model_policy_%s.py" % m["name"].lower() |
| 60 | ) |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 61 | model_policy = open(model_policy_path).read() |
| 62 | except IOError: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 63 | return "510 Model needs a model policy %s" % model_policy_path |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 64 | |
| 65 | try: |
| 66 | model_policy_ast = ast.parse(model_policy) |
| 67 | except SyntaxError: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 68 | return "511 Could not parse sync step %s" % model_policy_path |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 69 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 70 | classes = [x for x in model_policy_ast.body if isinstance(x, ast.ClassDef)] |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 71 | found_model_policy_class = False |
| 72 | for c in classes: |
| 73 | base_names = [v.id for v in c.bases] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 74 | if "Policy" in base_names or "TenantWithContainerPolicy" in base_names: |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 75 | found_model_policy_class = True |
| 76 | break |
| 77 | |
| 78 | if not found_model_policy_class: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 79 | return "513 Synchronizer needs a model policy class" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 80 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 81 | return "200 OK" |