| 1 |  |  | # Licensed to the StackStorm, Inc ('StackStorm') under one or more | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | # contributor license agreements.  See the NOTICE file distributed with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | # this work for additional information regarding copyright ownership. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | # The ASF licenses this file to You under the Apache License, Version 2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | # (the "License"); you may not use this file except in compliance with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | # the License.  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 |  |  | import abc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | import six | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | from oslo_config import cfg | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | from st2common.util import mongoescape as util_mongodb | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | from st2common import log as logging | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | __all__ = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     'BaseAPI', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     'APIUIDMixin' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | LOG = logging.getLogger(__name__) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | @six.add_metaclass(abc.ABCMeta) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | class BaseAPI(object): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     schema = abc.abstractproperty | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     def __init__(self, **kw): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         for key, value in kw.items(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             setattr(self, key, value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     def __repr__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         name = type(self).__name__ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         attrs = ', '.join("'%s': %r" % item for item in six.iteritems(vars(self))) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         # The format here is so that eval can be applied. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         return "%s(**{%s})" % (name, attrs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     def __str__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         name = type(self).__name__ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         attrs = ', '.join("%s=%r" % item for item in six.iteritems(vars(self))) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         return "%s[%s]" % (name, attrs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     def __json__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         return vars(self) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     def validate(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         Perform validation and return cleaned object on success. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         Note: This method doesn't mutate this object in place, but it returns a new one. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         :return: Cleaned / validated object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         from st2common.util import schema as util_schema | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         schema = getattr(self, 'schema', {}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         attributes = vars(self) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         cleaned = util_schema.validate(instance=attributes, schema=schema, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                                        cls=util_schema.CustomValidator, use_default=True, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                                        allow_default_none=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         # Note: We use type() instead of self.__class__ since self.__class__ confuses pylint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         return type(self)(**cleaned) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     def _from_model(cls, model, mask_secrets=False): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         doc = util_mongodb.unescape_chars(model.to_mongo()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         if '_id' in doc: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             doc['id'] = str(doc.pop('_id')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         if mask_secrets and cfg.CONF.log.mask_secrets: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             doc = model.mask_secrets(value=doc) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return doc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     def from_model(cls, model, mask_secrets=False): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         Create API model class instance for the provided DB model instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         :param model: DB model class instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         :type model: :class:`StormFoundationDB` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         :param mask_secrets: True to mask secrets in the resulting instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         :type mask_secrets: ``boolean`` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         doc = cls._from_model(model=model, mask_secrets=mask_secrets) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         attrs = {attr: value for attr, value in six.iteritems(doc) if value is not None} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         return cls(**attrs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     def to_model(cls, doc): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         Create a model class instance for the provided MongoDB document. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         :param doc: MongoDB document. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         raise NotImplementedError() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | class APIUIDMixin(object): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     """" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     Mixin class for retrieving UID for API objects. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     """ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |     def get_uid(self): | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |         # TODO: This is not the most efficient approach - refactor this functionality into util | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |         # module and re-use it here and in the DB model | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |         resource_db = self.to_model(self) | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |         resource_uid = resource_db.get_uid() | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |         return resource_uid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     def get_pack_uid(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         # TODO: This is not the most efficient approach - refactor this functionality into util | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         # module and re-use it here and in the DB model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         resource_db = self.to_model(self) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         pack_uid = resource_db.get_pack_uid() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         return pack_uid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     def has_valid_uid(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         resource_db = self.to_model(self) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         return resource_db.has_valid_uid() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | def cast_argument_value(value_type, value): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     if value_type == bool: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         def cast_func(value): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             value = str(value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             return value.lower() in ['1', 'true'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         cast_func = value_type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     result = cast_func(value) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 148 |  |  |     return result | 
            
                                                        
            
                                    
            
            
                | 149 |  |  |  |