Matteo Scandolo | 920e8fd | 2017-08-08 13:05:24 -0700 | [diff] [blame^] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 17 | from resources import RESOURCES |
| 18 | |
| 19 | class GRPCModelsAccessor: |
| 20 | """ |
| 21 | This class provide the glue between the models managed by TOSCA and the ones living in xos-core |
| 22 | """ |
| 23 | |
| 24 | @staticmethod |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 25 | def get_model_from_classname(class_name, data, username, password): |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 26 | """ |
| 27 | Give a Model Class Name and some data, check if that exits or instantiate a new one |
| 28 | """ |
| 29 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 30 | if data.get('name'): |
| 31 | used_key = 'name' |
| 32 | else: |
| 33 | used_key = data.keys()[0] |
| 34 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 35 | key = "%s~%s" % (username, password) |
| 36 | if not key in RESOURCES: |
| 37 | raise Exception("[XOS-TOSCA] User '%s' does not have ready resources" % username) |
| 38 | if class_name not in RESOURCES[key]: |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 39 | raise Exception('[XOS-TOSCA] The model you are trying to create (%s: %s, class: %s) is not know by xos-core' % (used_key, data[used_key], class_name)) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 40 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 41 | cls = RESOURCES[key][class_name] |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 42 | models = cls.objects.filter(**{used_key: data[used_key]}) |
| 43 | |
| 44 | if len(models) == 1: |
| 45 | print "[XOS-Tosca] Model %s already exist, retrieving instance..." % data[used_key] |
| 46 | model = models[0] |
| 47 | elif len(models) == 0: |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 48 | |
| 49 | if 'must-exist' in data and data['must-exist']: |
| 50 | raise Exception("[XOS-TOSCA] Model %s:%s has property 'must-exist' but cannot be found" % (class_name, data[used_key])) |
| 51 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 52 | model = cls.objects.new() |
| 53 | print "[XOS-Tosca] Model %s is new, creating new instance..." % data[used_key] |
| 54 | else: |
| 55 | raise Exception("[XOS-Tosca] Model %s has multiple instances, I can't handle it" % data[used_key]) |
| 56 | return model |