| 123 | |
| 124 | @classmethod |
| 125 | def insert( |
| 126 | cls, |
| 127 | model_object, |
| 128 | publish=True, |
| 129 | dispatch_trigger=True, |
| 130 | log_not_unique_error_as_debug=False, |
| 131 | ): |
| 132 | # Late import to avoid very expensive in-direct import (~1 second) when this function |
| 133 | # is not called / used |
| 134 | from mongoengine import NotUniqueError |
| 135 | |
| 136 | if model_object.id: |
| 137 | raise ValueError("id for object %s was unexpected." % model_object) |
| 138 | try: |
| 139 | model_object = cls._get_impl().insert(model_object) |
| 140 | except NotUniqueError as e: |
| 141 | if log_not_unique_error_as_debug: |
| 142 | LOG.debug("Conflict while trying to save in DB: %s.", six.text_type(e)) |
| 143 | else: |
| 144 | LOG.exception("Conflict while trying to save in DB.") |
| 145 | # On a conflict determine the conflicting object and return its id in |
| 146 | # the raised exception. |
| 147 | conflict_object = cls._get_by_object(model_object) |
| 148 | conflict_id = str(conflict_object.id) if conflict_object else None |
| 149 | message = six.text_type(e) |
| 150 | raise StackStormDBObjectConflictError( |
| 151 | message=message, conflict_id=conflict_id, model_object=model_object |
| 152 | ) |
| 153 | |
| 154 | # Publish internal event on the message bus |
| 155 | if publish: |
| 156 | try: |
| 157 | cls.publish_create(model_object) |
| 158 | except: |
| 159 | LOG.exception("Publish failed.") |
| 160 | |
| 161 | # Dispatch trigger |
| 162 | if dispatch_trigger: |
| 163 | try: |
| 164 | cls.dispatch_create_trigger(model_object) |
| 165 | except: |
| 166 | LOG.exception("Trigger dispatch failed.") |
| 167 | |
| 168 | return model_object |
| 169 | |
| 170 | @classmethod |
| 171 | def add_or_update( |