Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [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 | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 15 | from __future__ import absolute_import, print_function |
| 16 | |
| 17 | import imp |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 18 | import os |
| 19 | import sys |
| 20 | import threading |
| 21 | import time |
Andy Bavier | 04ee191 | 2019-01-30 14:17:16 -0700 | [diff] [blame] | 22 | import traceback |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 23 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 24 | from multistructlog import create_logger |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 25 | from xosconfig import Config |
Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 26 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 27 | """ |
| 28 | Django-like ORM layer for gRPC |
| 29 | |
| 30 | Usage: |
| 31 | api = ORMStub(stub) |
| 32 | |
| 33 | api.Slices.all() ... list all slices |
| 34 | |
| 35 | someSlice = api.Slices.get(id=1) ... get slice #1 |
| 36 | |
| 37 | someSlice.site ... automatically resolves site_id into a site object |
| 38 | someSlice.instances ... automatically resolves instances_ids into instance objects |
| 39 | someSlice.save() ... saves the slice object |
| 40 | """ |
| 41 | |
| 42 | """ |
| 43 | import grpc_client, orm |
| 44 | c=grpc_client.SecureClient("xos-core.cord.lab", username="padmin@vicci.org", password="letmein") |
Scott Baker | a1eae7a | 2017-06-06 09:20:15 -0700 | [diff] [blame] | 45 | u=c.xos_orm.User.objects.get(id=1) |
| 46 | """ |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 47 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 48 | log = create_logger(Config().get("logging")) |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 49 | |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 50 | convenience_wrappers = {} |
| 51 | |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 52 | |
Andy Bavier | 04ee191 | 2019-01-30 14:17:16 -0700 | [diff] [blame] | 53 | def get_synchronizer_function(): |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 54 | """ |
| 55 | Find the topmost synchronizer-specific function in the call stack |
| 56 | """ |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 57 | for file, line, func, stmt in traceback.extract_stack(): |
Andy Bavier | 04ee191 | 2019-01-30 14:17:16 -0700 | [diff] [blame] | 58 | if file.startswith("/opt/xos/synchronizers"): |
Andy Bavier | fe87b66 | 2019-06-10 13:51:19 -0700 | [diff] [blame] | 59 | result = "%s:%s()" % (file, func) |
| 60 | return result |
| 61 | return None |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 62 | |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 63 | |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 64 | class ORMGenericContentNotFoundException(Exception): |
| 65 | pass |
| 66 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 67 | |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 68 | class ORMGenericObjectNotFoundException(Exception): |
| 69 | pass |
| 70 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 71 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 72 | class ORMWrapper(object): |
| 73 | """ Wraps a protobuf object to provide ORM features """ |
| 74 | |
| 75 | def __init__(self, wrapped_class, stub, is_new=False): |
| 76 | super(ORMWrapper, self).__setattr__("_wrapped_class", wrapped_class) |
| 77 | super(ORMWrapper, self).__setattr__("stub", stub) |
| 78 | super(ORMWrapper, self).__setattr__("cache", {}) |
| 79 | super(ORMWrapper, self).__setattr__("reverse_cache", {}) |
Sapan Bhatia | 71f5768 | 2017-08-23 20:09:08 -0400 | [diff] [blame] | 80 | super(ORMWrapper, self).__setattr__("synchronizer_step", None) |
Sapan Bhatia | 2b307f7 | 2017-11-02 11:39:17 -0400 | [diff] [blame] | 81 | super(ORMWrapper, self).__setattr__("dependent", None) |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 82 | super(ORMWrapper, self).__setattr__("is_new", is_new) |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 83 | super(ORMWrapper, self).__setattr__("post_save_fixups", []) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 84 | fkmap = self.gen_fkmap() |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 85 | super(ORMWrapper, self).__setattr__("_fkmap", fkmap) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 86 | reverse_fkmap = self.gen_reverse_fkmap() |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 87 | super(ORMWrapper, self).__setattr__("_reverse_fkmap", reverse_fkmap) |
Scott Baker | 5b7fba0 | 2018-10-17 08:46:46 -0700 | [diff] [blame] | 88 | super(ORMWrapper, self).__setattr__("_initial", self._dict) |
| 89 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 90 | def fields_differ(self, f1, f2): |
| 91 | return f1 != f2 |
Scott Baker | 5b7fba0 | 2018-10-17 08:46:46 -0700 | [diff] [blame] | 92 | |
| 93 | @property |
| 94 | def _dict(self): |
| 95 | """ Return a dictionary of {fieldname: fieldvalue} for the object. |
| 96 | |
| 97 | This differs for the xos-core implementation of XOSBase. For new object, XOSBase will include field names |
| 98 | that are set to default values. ORM ignores fields that are set to default values. |
| 99 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 100 | d = {} |
Scott Baker | 5b7fba0 | 2018-10-17 08:46:46 -0700 | [diff] [blame] | 101 | for (fieldDesc, val) in self._wrapped_class.ListFields(): |
| 102 | name = fieldDesc.name |
| 103 | d[name] = val |
| 104 | return d |
| 105 | |
| 106 | @property |
| 107 | def diff(self): |
| 108 | d1 = self._initial |
| 109 | d2 = self._dict |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 110 | all_field_names = list(self._wrapped_class.DESCRIPTOR.fields_by_name.keys()) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 111 | diffs = [] |
Scott Baker | 5b7fba0 | 2018-10-17 08:46:46 -0700 | [diff] [blame] | 112 | for k in all_field_names: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 113 | if d1.get(k, None) != d2.get(k, None): |
| 114 | diffs.append((k, (d1.get(k, None), d2.get(k, None)))) |
Scott Baker | 5b7fba0 | 2018-10-17 08:46:46 -0700 | [diff] [blame] | 115 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 116 | # diffs = [(k, (v, d2[k])) for k, v in d1.items() if self.fields_differ(v,d2[k])] |
Scott Baker | 5b7fba0 | 2018-10-17 08:46:46 -0700 | [diff] [blame] | 117 | return dict(diffs) |
| 118 | |
| 119 | @property |
| 120 | def has_changed(self): |
| 121 | return bool(self.diff) |
| 122 | |
| 123 | @property |
| 124 | def changed_fields(self): |
| 125 | """ Return the list of changed fields. |
| 126 | |
| 127 | This differs for the xos-core implementation of XOSBase. For new object, XOSBase will include field names |
| 128 | that are set to default values. |
| 129 | """ |
| 130 | if self.is_new: |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 131 | return list(self._dict.keys()) |
Matteo Scandolo | 2fd7b87 | 2019-06-06 10:05:42 -0700 | [diff] [blame] | 132 | |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 133 | return list(self.diff.keys()) |
Scott Baker | 5b7fba0 | 2018-10-17 08:46:46 -0700 | [diff] [blame] | 134 | |
| 135 | def has_field_changed(self, field_name): |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 136 | return field_name in list(self.diff.keys()) |
Scott Baker | 5b7fba0 | 2018-10-17 08:46:46 -0700 | [diff] [blame] | 137 | |
| 138 | def get_field_diff(self, field_name): |
| 139 | return self.diff.get(field_name, None) |
| 140 | |
| 141 | def recompute_initial(self): |
| 142 | self._initial = self._dict |
| 143 | |
| 144 | def save_changed_fields(self, always_update_timestamp=False): |
Matteo Scandolo | 2fd7b87 | 2019-06-06 10:05:42 -0700 | [diff] [blame] | 145 | |
| 146 | # we need to ignore many-to-many fields as they are saved by do_post_save_fixups |
| 147 | # and can't be sent over the wire |
| 148 | m2m_fields = [v['src_fieldName'] for k, v in self._reverse_fkmap.items()] |
| 149 | |
Scott Baker | 5b7fba0 | 2018-10-17 08:46:46 -0700 | [diff] [blame] | 150 | if self.has_changed: |
Matteo Scandolo | 2fd7b87 | 2019-06-06 10:05:42 -0700 | [diff] [blame] | 151 | update_fields = [f for f in self.changed_fields if f not in m2m_fields] |
Scott Baker | 5b7fba0 | 2018-10-17 08:46:46 -0700 | [diff] [blame] | 152 | if always_update_timestamp and "updated" not in update_fields: |
| 153 | update_fields.append("updated") |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 154 | self.save( |
| 155 | update_fields=sorted(update_fields), |
| 156 | always_update_timestamp=always_update_timestamp, |
| 157 | ) |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 158 | |
Scott Baker | d78f647 | 2017-03-14 17:30:14 -0700 | [diff] [blame] | 159 | def create_attr(self, name, value=None): |
| 160 | """ setattr(self, ...) will fail for attributes that don't exist in the |
| 161 | wrapped grpc class. This is by design. However, if someone really |
| 162 | does want to attach a new attribute to this class, then they can |
| 163 | call create_attr() |
| 164 | """ |
| 165 | super(ORMWrapper, self).__setattr__(name, value) |
| 166 | |
Scott Baker | 03a163f | 2017-05-17 09:21:47 -0700 | [diff] [blame] | 167 | def get_generic_foreignkeys(self): |
| 168 | """ this is a placeholder until generic foreign key support is added |
| 169 | to xproto. |
| 170 | """ |
| 171 | return [] |
| 172 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 173 | def gen_fkmap(self): |
| 174 | fkmap = {} |
| 175 | |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 176 | all_field_names = list(self._wrapped_class.DESCRIPTOR.fields_by_name.keys()) |
Scott Baker | aa556b0 | 2017-03-07 16:07:34 -0800 | [diff] [blame] | 177 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 178 | for (name, field) in self._wrapped_class.DESCRIPTOR.fields_by_name.items(): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 179 | if name.endswith("_id"): |
| 180 | foreignKey = field.GetOptions().Extensions._FindExtensionByName( |
| 181 | "xos.foreignKey" |
| 182 | ) |
| 183 | fk = field.GetOptions().Extensions[foreignKey] |
| 184 | if fk and fk.modelName: |
| 185 | fkdict = { |
| 186 | "src_fieldName": name, |
| 187 | "modelName": fk.modelName, |
| 188 | "kind": "fk", |
| 189 | } |
| 190 | if fk.reverseFieldName: |
| 191 | fkdict["reverse_fieldName"] = fk.reverseFieldName |
| 192 | fkmap[name[:-3]] = fkdict |
| 193 | else: |
| 194 | # If there's a corresponding _type_id field, then see if this |
| 195 | # is a generic foreign key. |
| 196 | type_name = name[:-3] + "_type_id" |
| 197 | if type_name in all_field_names: |
| 198 | fkmap[name[:-3]] = { |
| 199 | "src_fieldName": name, |
| 200 | "ct_fieldName": type_name, |
| 201 | "kind": "generic_fk", |
| 202 | } |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 203 | |
Scott Baker | 03a163f | 2017-05-17 09:21:47 -0700 | [diff] [blame] | 204 | for gfk in self.get_generic_foreignkeys(): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 205 | fkmap[gfk["name"]] = { |
| 206 | "src_fieldName": gfk["id"], |
| 207 | "ct_fieldName": gfk["content_type"], |
| 208 | "kind": "generic_fk", |
| 209 | } |
Scott Baker | 03a163f | 2017-05-17 09:21:47 -0700 | [diff] [blame] | 210 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 211 | return fkmap |
| 212 | |
| 213 | def gen_reverse_fkmap(self): |
| 214 | reverse_fkmap = {} |
| 215 | |
| 216 | for (name, field) in self._wrapped_class.DESCRIPTOR.fields_by_name.items(): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 217 | if name.endswith("_ids"): |
| 218 | reverseForeignKey = field.GetOptions().Extensions._FindExtensionByName( |
| 219 | "xos.reverseForeignKey" |
| 220 | ) |
| 221 | fk = field.GetOptions().Extensions[reverseForeignKey] |
| 222 | if fk and fk.modelName: |
| 223 | reverse_fkmap[name[:-4]] = { |
| 224 | "src_fieldName": name, |
| 225 | "modelName": fk.modelName, |
| 226 | "writeable": False, |
| 227 | } |
| 228 | else: |
| 229 | manyToManyForeignKey = field.GetOptions().Extensions._FindExtensionByName( |
| 230 | "xos.manyToManyForeignKey" |
| 231 | ) |
| 232 | fk = field.GetOptions().Extensions[manyToManyForeignKey] |
| 233 | if fk and fk.modelName: |
| 234 | reverse_fkmap[name[:-4]] = { |
| 235 | "src_fieldName": name, |
| 236 | "modelName": fk.modelName, |
| 237 | "writeable": True, |
| 238 | } |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 239 | |
| 240 | return reverse_fkmap |
| 241 | |
| 242 | def fk_resolve(self, name): |
| 243 | if name in self.cache: |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 244 | return self.cache[name] |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 245 | |
| 246 | fk_entry = self._fkmap[name] |
Scott Baker | aa556b0 | 2017-03-07 16:07:34 -0800 | [diff] [blame] | 247 | fk_kind = fk_entry["kind"] |
| 248 | fk_id = getattr(self, fk_entry["src_fieldName"]) |
| 249 | |
| 250 | if not fk_id: |
| 251 | return None |
| 252 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 253 | if fk_kind == "fk": |
| 254 | id = self.stub.make_ID(id=fk_id) |
Scott Baker | aa556b0 | 2017-03-07 16:07:34 -0800 | [diff] [blame] | 255 | dest_model = self.stub.invoke("Get%s" % fk_entry["modelName"], id) |
| 256 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 257 | elif fk_kind == "generic_fk": |
| 258 | dest_model = self.stub.genericForeignKeyResolve( |
| 259 | getattr(self, fk_entry["ct_fieldName"]), fk_id |
| 260 | )._wrapped_class |
Scott Baker | aa556b0 | 2017-03-07 16:07:34 -0800 | [diff] [blame] | 261 | |
| 262 | else: |
| 263 | raise Exception("unknown fk_kind") |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 264 | |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 265 | dest_model = make_ORMWrapper(dest_model, self.stub) |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 266 | self.cache[name] = dest_model |
| 267 | |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 268 | return dest_model |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 269 | |
| 270 | def reverse_fk_resolve(self, name): |
| 271 | if name not in self.reverse_cache: |
| 272 | fk_entry = self._reverse_fkmap[name] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 273 | self.reverse_cache[name] = ORMLocalObjectManager( |
| 274 | self.stub, |
| 275 | fk_entry["modelName"], |
| 276 | getattr(self, fk_entry["src_fieldName"]), |
| 277 | fk_entry["writeable"], |
| 278 | ) |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 279 | |
Scott Baker | 7ab456b | 2019-01-08 14:58:13 -0800 | [diff] [blame] | 280 | return self.reverse_cache[name] |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 281 | |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 282 | def fk_set(self, name, model): |
| 283 | fk_entry = self._fkmap[name] |
Scott Baker | aa556b0 | 2017-03-07 16:07:34 -0800 | [diff] [blame] | 284 | fk_kind = fk_entry["kind"] |
Scott Baker | a1eae7a | 2017-06-06 09:20:15 -0700 | [diff] [blame] | 285 | if model: |
| 286 | id = model.id |
| 287 | else: |
| 288 | id = 0 |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 289 | setattr(self._wrapped_class, fk_entry["src_fieldName"], id) |
| 290 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 291 | if fk_kind == "generic_fk": |
| 292 | setattr( |
| 293 | self._wrapped_class, |
| 294 | fk_entry["ct_fieldName"], |
| 295 | model.self_content_type_id, |
| 296 | ) |
Scott Baker | aa556b0 | 2017-03-07 16:07:34 -0800 | [diff] [blame] | 297 | |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 298 | if name in self.cache: |
| 299 | old_model = self.cache[name] |
| 300 | if fk_entry.get("reverse_fieldName"): |
| 301 | # Note this fk change so that we can update the destination model after we save. |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 302 | self.post_save_fixups.append( |
| 303 | { |
| 304 | "src_fieldName": fk_entry["src_fieldName"], |
| 305 | "dest_id": id, |
| 306 | "dest_model": old_model, |
| 307 | "remove": True, |
| 308 | "reverse_fieldName": fk_entry.get("reverse_fieldName"), |
| 309 | } |
| 310 | ) |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 311 | del self.cache[name] |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 312 | |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 313 | if model: |
| 314 | self.cache[name] = model |
| 315 | if fk_entry.get("reverse_fieldName"): |
| 316 | # Note this fk change so that we can update the destination model after we save. |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 317 | self.post_save_fixups.append( |
| 318 | { |
| 319 | "src_fieldName": fk_entry["src_fieldName"], |
| 320 | "dest_id": id, |
| 321 | "dest_model": model, |
| 322 | "remove": False, |
| 323 | "reverse_fieldName": fk_entry.get("reverse_fieldName"), |
| 324 | } |
| 325 | ) |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 326 | elif name in self.cache: |
| 327 | del self.cache[name] |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 328 | |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 329 | def do_post_save_fixups(self): |
| 330 | # Perform post-save foreign key fixups. |
| 331 | # Fixup the models that we've set a foreign key to so that their in-memory representation has the correct |
| 332 | # reverse foreign key back to us. We can only do this after a save, because self.id isn't known until |
| 333 | # after save. |
| 334 | # See unit test test_foreign_key_set_without_invalidate |
| 335 | for fixup in self.post_save_fixups: |
| 336 | model = fixup["dest_model"] |
| 337 | reverse_fieldName_ids = fixup["reverse_fieldName"] + "_ids" |
| 338 | if not hasattr(model, reverse_fieldName_ids): |
| 339 | continue |
| 340 | if fixup["remove"]: |
| 341 | reverse_ids = getattr(model, reverse_fieldName_ids) |
| 342 | if self.id in reverse_ids: |
| 343 | reverse_ids.remove(self.id) |
| 344 | else: |
| 345 | reverse_ids = getattr(model, reverse_fieldName_ids) |
| 346 | if self.id not in reverse_ids: |
| 347 | reverse_ids.append(self.id) |
| 348 | model.invalidate_cache(fixup["reverse_fieldName"]) |
| 349 | self.post_save_fixups = [] |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 350 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 351 | def __getattr__(self, name, *args, **kwargs): |
| 352 | # note: getattr is only called for attributes that do not exist in |
| 353 | # self.__dict__ |
| 354 | |
Scott Baker | 186372f | 2017-02-23 13:49:36 -0800 | [diff] [blame] | 355 | # pk is a synonym for id |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 356 | if name == "pk": |
Scott Baker | 186372f | 2017-02-23 13:49:36 -0800 | [diff] [blame] | 357 | name = "id" |
| 358 | |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 359 | if name in list(self._fkmap.keys()): |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 360 | return self.fk_resolve(name) |
| 361 | |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 362 | if name in list(self._reverse_fkmap.keys()): |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 363 | return self.reverse_fk_resolve(name) |
| 364 | |
Scott Baker | 37cf9e2 | 2018-08-20 14:39:33 -0700 | [diff] [blame] | 365 | try: |
| 366 | # When sending a reply, XOS will leave the field unset if it is None in the data model. If |
| 367 | # HasField(<fieldname>)==False for an existing object, then the caller can infer that field was set to |
| 368 | # None. |
| 369 | if (not self.is_new) and (not self._wrapped_class.HasField(name)): |
| 370 | return None |
| 371 | except ValueError: |
| 372 | # ValueError is thrown if the field does not exist. We will handle that case in the getattr() below. |
| 373 | pass |
| 374 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 375 | return getattr(self._wrapped_class, name, *args, **kwargs) |
| 376 | |
| 377 | def __setattr__(self, name, value): |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 378 | if name in list(self._fkmap.keys()): |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 379 | self.fk_set(name, value) |
| 380 | elif name in self.__dict__: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 381 | super(ORMWrapper, self).__setattr__(name, value) |
Scott Baker | 37cf9e2 | 2018-08-20 14:39:33 -0700 | [diff] [blame] | 382 | elif value is None: |
| 383 | # When handling requests, XOS interprets gRPC HasField(<fieldname>)==False to indicate that the caller |
| 384 | # has not set the field and wants it to continue to use its existing (or default) value. That leaves us |
| 385 | # with no easy way to support setting a field to None. |
| 386 | raise ValueError("Setting a non-foreignkey field to None is not supported") |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 387 | else: |
| 388 | setattr(self._wrapped_class, name, value) |
| 389 | |
| 390 | def __repr__(self): |
Scott Baker | d194097 | 2017-05-01 15:45:32 -0700 | [diff] [blame] | 391 | class_name = self._wrapped_class.__class__.__name__ |
| 392 | id = getattr(self._wrapped_class, "id", "noid") |
| 393 | name = getattr(self._wrapped_class, "name", None) |
| 394 | if name: |
| 395 | return "<%s: %s>" % (class_name, name) |
| 396 | else: |
| 397 | return "<%s: id-%s>" % (class_name, id) |
| 398 | |
| 399 | def __str__(self): |
| 400 | class_name = self._wrapped_class.__class__.__name__ |
| 401 | id = getattr(self._wrapped_class, "id", "noid") |
| 402 | name = getattr(self._wrapped_class, "name", None) |
| 403 | if name: |
| 404 | return name |
| 405 | else: |
| 406 | return "%s-%s" % (class_name, id) |
| 407 | |
| 408 | def dumpstr(self): |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 409 | return self._wrapped_class.__repr__() |
| 410 | |
Scott Baker | d194097 | 2017-05-01 15:45:32 -0700 | [diff] [blame] | 411 | def dump(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 412 | print(self.dumpstr()) |
Scott Baker | d194097 | 2017-05-01 15:45:32 -0700 | [diff] [blame] | 413 | |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 414 | def invalidate_cache(self, name=None): |
| 415 | if name: |
| 416 | if name in self.cache: |
| 417 | del self.cache[name] |
| 418 | if name in self.reverse_cache: |
| 419 | del self.reverse_cache[name] |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 420 | else: |
| 421 | self.cache.clear() |
| 422 | self.reverse_cache.clear() |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 423 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 424 | def save( |
| 425 | self, |
| 426 | update_fields=None, |
| 427 | always_update_timestamp=False, |
| 428 | is_sync_save=False, |
| 429 | is_policy_save=False, |
| 430 | ): |
Matteo Scandolo | 2fd7b87 | 2019-06-06 10:05:42 -0700 | [diff] [blame] | 431 | |
Andy Bavier | 04ee191 | 2019-01-30 14:17:16 -0700 | [diff] [blame] | 432 | classname = self._wrapped_class.__class__.__name__ |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 433 | if self.is_new: |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 434 | log.debug( |
| 435 | "save(): is new", |
| 436 | classname=classname, |
| 437 | syncstep=get_synchronizer_function(), |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 438 | ) |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 439 | new_class = self.stub.invoke("Create%s" % classname, self._wrapped_class) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 440 | self._wrapped_class = new_class |
| 441 | self.is_new = False |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 442 | else: |
Andy Bavier | 04ee191 | 2019-01-30 14:17:16 -0700 | [diff] [blame] | 443 | if self.has_changed: |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 444 | log.debug( |
| 445 | "save(): updated", |
| 446 | classname=classname, |
| 447 | changed_fields=self.changed_fields, |
| 448 | syncstep=get_synchronizer_function(), |
| 449 | ) |
Andy Bavier | 04ee191 | 2019-01-30 14:17:16 -0700 | [diff] [blame] | 450 | else: |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 451 | log.debug( |
| 452 | "save(): no changes", |
| 453 | classname=classname, |
| 454 | syncstep=get_synchronizer_function(), |
| 455 | ) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 456 | metadata = [] |
| 457 | if update_fields: |
| 458 | metadata.append(("update_fields", ",".join(update_fields))) |
| 459 | if always_update_timestamp: |
| 460 | metadata.append(("always_update_timestamp", "1")) |
| 461 | if is_policy_save: |
| 462 | metadata.append(("is_policy_save", "1")) |
| 463 | if is_sync_save: |
| 464 | metadata.append(("is_sync_save", "1")) |
| 465 | self.stub.invoke( |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 466 | "Update%s" % classname, self._wrapped_class, metadata=metadata |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 467 | ) |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 468 | self.do_post_save_fixups() |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 469 | |
Scott Baker | 5b7fba0 | 2018-10-17 08:46:46 -0700 | [diff] [blame] | 470 | # Now that object has saved, reset our initial state for diff calculation |
| 471 | self.recompute_initial() |
| 472 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 473 | def delete(self): |
| 474 | id = self.stub.make_ID(id=self._wrapped_class.id) |
| 475 | self.stub.invoke("Delete%s" % self._wrapped_class.__class__.__name__, id) |
| 476 | |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 477 | def tologdict(self): |
| 478 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 479 | d = {"model_name": self._wrapped_class.__class__.__name__, "pk": self.pk} |
| 480 | except BaseException: |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 481 | d = {} |
| 482 | |
| 483 | return d |
| 484 | |
Scott Baker | bb81e15 | 2017-03-02 15:28:36 -0800 | [diff] [blame] | 485 | @property |
Scott Baker | ff104cc | 2017-08-14 15:24:41 -0700 | [diff] [blame] | 486 | def leaf_model(self): |
| 487 | # Easy case - this model is already the leaf |
| 488 | if self.leaf_model_name == self._wrapped_class.__class__.__name__: |
| 489 | return self |
| 490 | |
| 491 | # This model is not the leaf, so use the stub to fetch the leaf model |
| 492 | return getattr(self.stub, self.leaf_model_name).objects.get(id=self.id) |
| 493 | |
| 494 | @property |
Scott Baker | d2543ed | 2017-03-07 21:46:48 -0800 | [diff] [blame] | 495 | def model_name(self): |
| 496 | return self._wrapped_class.__class__.__name__ |
| 497 | |
| 498 | @property |
Scott Baker | bb81e15 | 2017-03-02 15:28:36 -0800 | [diff] [blame] | 499 | def ansible_tag(self): |
| 500 | return "%s_%s" % (self._wrapped_class.__class__.__name__, self.id) |
| 501 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 502 | |
Scott Baker | b05393b | 2017-03-01 14:59:55 -0800 | [diff] [blame] | 503 | class ORMQuerySet(list): |
| 504 | """ Makes lists look like django querysets """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 505 | |
Scott Baker | b05393b | 2017-03-01 14:59:55 -0800 | [diff] [blame] | 506 | def first(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 507 | if len(self) > 0: |
Scott Baker | b05393b | 2017-03-01 14:59:55 -0800 | [diff] [blame] | 508 | return self[0] |
| 509 | else: |
| 510 | return None |
| 511 | |
Scott Baker | 8c7267d | 2017-03-14 19:34:13 -0700 | [diff] [blame] | 512 | def exists(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 513 | return len(self) > 0 |
| 514 | |
Scott Baker | 8c7267d | 2017-03-14 19:34:13 -0700 | [diff] [blame] | 515 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 516 | class ORMLocalObjectManager(object): |
| 517 | """ Manages a local list of objects """ |
| 518 | |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 519 | def __init__(self, stub, modelName, idList, writeable): |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 520 | self._stub = stub |
| 521 | self._modelName = modelName |
| 522 | self._idList = idList |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 523 | self._writeable = writeable |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 524 | self._cache = None |
| 525 | |
| 526 | def resolve_queryset(self): |
| 527 | if self._cache is not None: |
| 528 | return self._cache |
| 529 | |
| 530 | models = [] |
| 531 | for id in self._idList: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 532 | models.append( |
| 533 | self._stub.invoke("Get%s" % self._modelName, self._stub.make_ID(id=id)) |
| 534 | ) |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 535 | |
| 536 | self._cache = models |
| 537 | |
| 538 | return models |
| 539 | |
| 540 | def all(self): |
| 541 | models = self.resolve_queryset() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 542 | return [make_ORMWrapper(x, self._stub) for x in models] |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 543 | |
Scott Baker | 8c7267d | 2017-03-14 19:34:13 -0700 | [diff] [blame] | 544 | def exists(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 545 | return len(self._idList) > 0 |
Scott Baker | 8c7267d | 2017-03-14 19:34:13 -0700 | [diff] [blame] | 546 | |
Scott Baker | a1eae7a | 2017-06-06 09:20:15 -0700 | [diff] [blame] | 547 | def count(self): |
| 548 | return len(self._idList) |
| 549 | |
Scott Baker | 8c7267d | 2017-03-14 19:34:13 -0700 | [diff] [blame] | 550 | def first(self): |
| 551 | if self._idList: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 552 | model = make_ORMWrapper( |
| 553 | self._stub.invoke( |
| 554 | "Get%s" % self._modelName, self._stub.make_ID(id=self._idList[0]) |
| 555 | ), |
| 556 | self._stub, |
| 557 | ) |
Scott Baker | 8c7267d | 2017-03-14 19:34:13 -0700 | [diff] [blame] | 558 | return model |
| 559 | else: |
| 560 | return None |
| 561 | |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 562 | def add(self, model): |
| 563 | if not self._writeable: |
| 564 | raise Exception("Only ManyToMany lists are writeable") |
| 565 | |
| 566 | if isinstance(model, int): |
| 567 | id = model |
| 568 | else: |
| 569 | if not model.id: |
| 570 | raise Exception("Model %s has no id" % model) |
| 571 | id = model.id |
| 572 | |
| 573 | self._idList.append(id) |
| 574 | |
| 575 | def remove(self, model): |
| 576 | if not self._writeable: |
| 577 | raise Exception("Only ManyToMany lists are writeable") |
| 578 | |
| 579 | if isinstance(model, int): |
| 580 | id = model |
| 581 | else: |
| 582 | if not model.id: |
| 583 | raise Exception("Model %s has no id" % model) |
| 584 | id = model.id |
| 585 | |
| 586 | self._idList.remove(id) |
| 587 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 588 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 589 | class ORMObjectManager(object): |
| 590 | """ Manages a remote list of objects """ |
| 591 | |
Scott Baker | ac2f2b5 | 2017-02-21 14:53:23 -0800 | [diff] [blame] | 592 | # constants better agree with common.proto |
Scott Baker | ea1f4d0 | 2018-12-17 10:21:50 -0800 | [diff] [blame] | 593 | DEFAULT = 0 |
| 594 | ALL = 1 |
Scott Baker | bae9d84 | 2017-03-21 10:44:10 -0700 | [diff] [blame] | 595 | SYNCHRONIZER_DIRTY_OBJECTS = 2 |
| 596 | SYNCHRONIZER_DELETED_OBJECTS = 3 |
| 597 | SYNCHRONIZER_DIRTY_POLICIES = 4 |
| 598 | SYNCHRONIZER_DELETED_POLICIES = 5 |
Scott Baker | ac2f2b5 | 2017-02-21 14:53:23 -0800 | [diff] [blame] | 599 | |
Scott Baker | ea1f4d0 | 2018-12-17 10:21:50 -0800 | [diff] [blame] | 600 | def __init__(self, stub, modelName, packageName, kind=0): |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 601 | self._stub = stub |
| 602 | self._modelName = modelName |
| 603 | self._packageName = packageName |
Scott Baker | ea1f4d0 | 2018-12-17 10:21:50 -0800 | [diff] [blame] | 604 | self._kind = kind |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 605 | |
| 606 | def wrap_single(self, obj): |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 607 | return make_ORMWrapper(obj, self._stub) |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 608 | |
| 609 | def wrap_list(self, obj): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 610 | result = [] |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 611 | for item in obj.items: |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 612 | result.append(make_ORMWrapper(item, self._stub)) |
Scott Baker | b05393b | 2017-03-01 14:59:55 -0800 | [diff] [blame] | 613 | return ORMQuerySet(result) |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 614 | |
| 615 | def all(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 616 | if self._kind == self.DEFAULT: |
| 617 | return self.wrap_list( |
| 618 | self._stub.invoke("List%s" % self._modelName, self._stub.make_empty()) |
| 619 | ) |
Scott Baker | ea1f4d0 | 2018-12-17 10:21:50 -0800 | [diff] [blame] | 620 | else: |
| 621 | return self.filter() |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 622 | |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 623 | def first(self): |
Scott Baker | ea1f4d0 | 2018-12-17 10:21:50 -0800 | [diff] [blame] | 624 | objs = self.all() |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 625 | if not objs: |
| 626 | return None |
| 627 | return objs[0] |
| 628 | |
Scott Baker | ac2f2b5 | 2017-02-21 14:53:23 -0800 | [diff] [blame] | 629 | def filter(self, **kwargs): |
| 630 | q = self._stub.make_Query() |
Scott Baker | ea1f4d0 | 2018-12-17 10:21:50 -0800 | [diff] [blame] | 631 | q.kind = self._kind |
Scott Baker | ac2f2b5 | 2017-02-21 14:53:23 -0800 | [diff] [blame] | 632 | |
| 633 | for (name, val) in kwargs.items(): |
| 634 | el = q.elements.add() |
| 635 | |
| 636 | if name.endswith("__gt"): |
| 637 | name = name[:-4] |
| 638 | el.operator = el.GREATER_THAN |
| 639 | elif name.endswith("__gte"): |
| 640 | name = name[:-5] |
| 641 | el.operator = el.GREATER_THAN_OR_EQUAL |
| 642 | elif name.endswith("__lt"): |
| 643 | name = name[:-4] |
| 644 | el.operator = el.LESS_THAN |
| 645 | elif name.endswith("__lte"): |
| 646 | name = name[:-5] |
| 647 | el.operator = el.LESS_THAN_OR_EQUAL |
Scott Baker | e1607b8 | 2018-09-20 14:10:59 -0700 | [diff] [blame] | 648 | elif name.endswith("__iexact"): |
| 649 | name = name[:-8] |
| 650 | el.operator = el.IEXACT |
Scott Baker | ac2f2b5 | 2017-02-21 14:53:23 -0800 | [diff] [blame] | 651 | else: |
| 652 | el.operator = el.EQUAL |
| 653 | |
| 654 | el.name = name |
| 655 | if isinstance(val, int): |
| 656 | el.iValue = val |
| 657 | else: |
| 658 | el.sValue = val |
| 659 | |
| 660 | return self.wrap_list(self._stub.invoke("Filter%s" % self._modelName, q)) |
| 661 | |
| 662 | def filter_special(self, kind): |
| 663 | q = self._stub.make_Query() |
| 664 | q.kind = kind |
| 665 | return self.wrap_list(self._stub.invoke("Filter%s" % self._modelName, q)) |
| 666 | |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 667 | def get(self, **kwargs): |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 668 | if list(kwargs.keys()) == ["id"]: |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 669 | # the fast and easy case, look it up by id |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 670 | return self.wrap_single( |
| 671 | self._stub.invoke( |
| 672 | "Get%s" % self._modelName, self._stub.make_ID(id=kwargs["id"]) |
| 673 | ) |
| 674 | ) |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 675 | else: |
| 676 | # the slightly more difficult case, filter and return the first item |
| 677 | objs = self.filter(**kwargs) |
| 678 | return objs[0] |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 679 | |
| 680 | def new(self, **kwargs): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 681 | if self._kind != ORMObjectManager.DEFAULT: |
| 682 | raise Exception( |
| 683 | "Creating objects is only supported by the DEFAULT object manager" |
| 684 | ) |
Scott Baker | ea1f4d0 | 2018-12-17 10:21:50 -0800 | [diff] [blame] | 685 | |
Scott Baker | aa556b0 | 2017-03-07 16:07:34 -0800 | [diff] [blame] | 686 | cls = self._stub.all_grpc_classes[self._modelName] |
Scott Baker | fe42a6f | 2017-03-18 09:11:31 -0700 | [diff] [blame] | 687 | o = make_ORMWrapper(cls(), self._stub, is_new=True) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 688 | for (k, v) in kwargs.items(): |
Scott Baker | fe42a6f | 2017-03-18 09:11:31 -0700 | [diff] [blame] | 689 | setattr(o, k, v) |
Scott Baker | 5b7fba0 | 2018-10-17 08:46:46 -0700 | [diff] [blame] | 690 | o.recompute_initial() |
Scott Baker | fe42a6f | 2017-03-18 09:11:31 -0700 | [diff] [blame] | 691 | return o |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 692 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 693 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 694 | class ORMModelClass(object): |
| 695 | def __init__(self, stub, model_name, package_name): |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 696 | self.model_name = model_name |
Scott Baker | aa556b0 | 2017-03-07 16:07:34 -0800 | [diff] [blame] | 697 | self._stub = stub |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 698 | self.objects = ORMObjectManager(stub, model_name, package_name) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 699 | self.deleted_objects = ORMObjectManager( |
| 700 | stub, |
| 701 | model_name, |
| 702 | package_name, |
| 703 | ORMObjectManager.SYNCHRONIZER_DELETED_OBJECTS, |
| 704 | ) |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 705 | |
Scott Baker | bb81e15 | 2017-03-02 15:28:36 -0800 | [diff] [blame] | 706 | @property |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 707 | def __name__(self): |
| 708 | return self.model_name |
| 709 | |
Scott Baker | aa556b0 | 2017-03-07 16:07:34 -0800 | [diff] [blame] | 710 | @property |
| 711 | def content_type_id(self): |
| 712 | return self._stub.reverse_content_type_map[self.model_name] |
| 713 | |
Scott Baker | 8a6d91f | 2017-03-22 11:23:11 -0700 | [diff] [blame] | 714 | def __call__(self, *args, **kwargs): |
| 715 | return self.objects.new(*args, **kwargs) |
| 716 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 717 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 718 | class ORMStub(object): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 719 | def __init__( |
| 720 | self, |
| 721 | stub, |
| 722 | protos, |
| 723 | package_name, |
| 724 | invoker=None, |
| 725 | caller_kind="grpcapi", |
| 726 | empty=None, |
| 727 | enable_backoff=True, |
| 728 | restart_on_disconnect=False, |
| 729 | ): |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 730 | self.grpc_stub = stub |
Scott Baker | b96ba43 | 2018-02-26 09:53:48 -0800 | [diff] [blame] | 731 | self.protos = protos |
| 732 | self.common_protos = protos.common__pb2 |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 733 | self.all_model_names = [] |
Scott Baker | aa556b0 | 2017-03-07 16:07:34 -0800 | [diff] [blame] | 734 | self.all_grpc_classes = {} |
| 735 | self.content_type_map = {} |
| 736 | self.reverse_content_type_map = {} |
Scott Baker | ef8d85d | 2017-02-21 16:44:28 -0800 | [diff] [blame] | 737 | self.invoker = invoker |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 738 | self.caller_kind = caller_kind |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 739 | self.enable_backoff = enable_backoff |
Scott Baker | b06e3e0 | 2017-12-12 11:05:53 -0800 | [diff] [blame] | 740 | self.restart_on_disconnect = restart_on_disconnect |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 741 | |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 742 | if not empty: |
Scott Baker | b96ba43 | 2018-02-26 09:53:48 -0800 | [diff] [blame] | 743 | empty = self.protos.google_dot_protobuf_dot_empty__pb2.Empty |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 744 | self._empty = empty |
| 745 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 746 | for name in dir(stub): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 747 | if name.startswith("Get"): |
| 748 | model_name = name[3:] |
| 749 | setattr(self, model_name, ORMModelClass(self, model_name, package_name)) |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 750 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 751 | self.all_model_names.append(model_name) |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 752 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 753 | grpc_class = getattr(self.protos, model_name) |
| 754 | self.all_grpc_classes[model_name] = grpc_class |
Scott Baker | aa556b0 | 2017-03-07 16:07:34 -0800 | [diff] [blame] | 755 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 756 | ct = grpc_class.DESCRIPTOR.GetOptions().Extensions._FindExtensionByName( |
| 757 | "xos.contentTypeId" |
| 758 | ) |
| 759 | if ct: |
| 760 | ct = grpc_class.DESCRIPTOR.GetOptions().Extensions[ct] |
| 761 | if ct: |
| 762 | self.content_type_map[ct] = model_name |
| 763 | self.reverse_content_type_map[model_name] = ct |
Scott Baker | aa556b0 | 2017-03-07 16:07:34 -0800 | [diff] [blame] | 764 | |
| 765 | def genericForeignKeyResolve(self, content_type_id, id): |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 766 | if content_type_id.endswith("_decl"): |
| 767 | content_type_id = content_type_id[:-5] |
| 768 | |
| 769 | if content_type_id not in self.content_type_map: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 770 | raise ORMGenericContentNotFoundException( |
| 771 | "Content_type %s not found in self.content_type_map" % content_type_id |
| 772 | ) |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 773 | |
Scott Baker | aa556b0 | 2017-03-07 16:07:34 -0800 | [diff] [blame] | 774 | model_name = self.content_type_map[content_type_id] |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 775 | |
Scott Baker | aa556b0 | 2017-03-07 16:07:34 -0800 | [diff] [blame] | 776 | model = getattr(self, model_name) |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 777 | objs = model.objects.filter(id=id) |
| 778 | if not objs: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 779 | raise ORMGenericObjectNotFoundException( |
| 780 | "Object %s of model %s was not found" % (id, model_name) |
| 781 | ) |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 782 | |
Scott Baker | aa556b0 | 2017-03-07 16:07:34 -0800 | [diff] [blame] | 783 | return model.objects.get(id=id) |
| 784 | |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 785 | def add_default_metadata(self, metadata): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 786 | default_metadata = [("caller_kind", self.caller_kind)] |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 787 | |
Scott Baker | d824671 | 2018-07-12 18:08:31 -0700 | [diff] [blame] | 788 | # introspect to see if we're running from a synchronizer thread |
| 789 | if getattr(threading.current_thread(), "is_sync_thread", False): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 790 | default_metadata.append(("is_sync_save", "1")) |
Scott Baker | d824671 | 2018-07-12 18:08:31 -0700 | [diff] [blame] | 791 | |
| 792 | # introspect to see if we're running from a model_policy thread |
| 793 | if getattr(threading.current_thread(), "is_policy_thread", False): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 794 | default_metadata.append(("is_policy_save", "1")) |
Scott Baker | d824671 | 2018-07-12 18:08:31 -0700 | [diff] [blame] | 795 | |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 796 | # build up a list of metadata keys we already have |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 797 | md_keys = [x[0] for x in metadata] |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 798 | |
| 799 | # add any defaults that we don't already have |
| 800 | for md in default_metadata: |
| 801 | if md[0] not in md_keys: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 802 | metadata.append((md[0], md[1])) |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 803 | |
Scott Baker | 57c7482 | 2017-02-23 11:13:04 -0800 | [diff] [blame] | 804 | def invoke(self, name, request, metadata=[]): |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 805 | self.add_default_metadata(metadata) |
| 806 | |
Scott Baker | ef8d85d | 2017-02-21 16:44:28 -0800 | [diff] [blame] | 807 | if self.invoker: |
| 808 | # Hook in place to call Chameleon's invoke method, as soon as we |
| 809 | # have rewritten the synchronizer to use reactor. |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 810 | return self.invoker.invoke( |
| 811 | self.grpc_stub.__class__, name, request, metadata={} |
| 812 | ).result[0] |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 813 | elif self.enable_backoff: |
Scott Baker | ef8d85d | 2017-02-21 16:44:28 -0800 | [diff] [blame] | 814 | # Our own retry mechanism. This works fine if there is a temporary |
| 815 | # failure in connectivity, but does not re-download gRPC schema. |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 816 | import grpc |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 817 | |
Scott Baker | b06e3e0 | 2017-12-12 11:05:53 -0800 | [diff] [blame] | 818 | backoff = [0.5, 1, 2, 4, 8] |
Scott Baker | ef8d85d | 2017-02-21 16:44:28 -0800 | [diff] [blame] | 819 | while True: |
Scott Baker | ef8d85d | 2017-02-21 16:44:28 -0800 | [diff] [blame] | 820 | try: |
| 821 | method = getattr(self.grpc_stub, name) |
Scott Baker | 57c7482 | 2017-02-23 11:13:04 -0800 | [diff] [blame] | 822 | return method(request, metadata=metadata) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 823 | except grpc._channel._Rendezvous as e: |
Scott Baker | ef8d85d | 2017-02-21 16:44:28 -0800 | [diff] [blame] | 824 | code = e.code() |
| 825 | if code == grpc.StatusCode.UNAVAILABLE: |
Scott Baker | b06e3e0 | 2017-12-12 11:05:53 -0800 | [diff] [blame] | 826 | if self.restart_on_disconnect: |
| 827 | # This is a blunt technique... We lost connectivity to the core, and we don't know that |
| 828 | # the core is still serving up the same models it was when we established connectivity, |
| 829 | # so restart the synchronizer. |
| 830 | # TODO: Hash check on the core models to tell if something changed would be better. |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 831 | os.execv(sys.executable, ["python"] + sys.argv) |
Scott Baker | ef8d85d | 2017-02-21 16:44:28 -0800 | [diff] [blame] | 832 | if not backoff: |
| 833 | raise Exception("No more retries on %s" % name) |
| 834 | time.sleep(backoff.pop(0)) |
| 835 | else: |
| 836 | raise |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 837 | except BaseException: |
Scott Baker | ef8d85d | 2017-02-21 16:44:28 -0800 | [diff] [blame] | 838 | raise |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 839 | else: |
| 840 | method = getattr(self.grpc_stub, name) |
| 841 | return method(request, metadata=metadata) |
| 842 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 843 | def make_ID(self, id): |
Scott Baker | b96ba43 | 2018-02-26 09:53:48 -0800 | [diff] [blame] | 844 | return getattr(self.common_protos, "ID")(id=id) |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 845 | |
| 846 | def make_empty(self): |
| 847 | return self._empty() |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 848 | |
Scott Baker | ac2f2b5 | 2017-02-21 14:53:23 -0800 | [diff] [blame] | 849 | def make_Query(self): |
Scott Baker | b96ba43 | 2018-02-26 09:53:48 -0800 | [diff] [blame] | 850 | return getattr(self.common_protos, "Query")() |
Scott Baker | ac2f2b5 | 2017-02-21 14:53:23 -0800 | [diff] [blame] | 851 | |
Scott Baker | f6145a2 | 2017-03-29 14:50:25 -0700 | [diff] [blame] | 852 | def listObjects(self): |
| 853 | return self.all_model_names |
| 854 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 855 | |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 856 | def register_convenience_wrapper(class_name, wrapper): |
| 857 | global convenience_wrappers |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 858 | |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 859 | convenience_wrappers[class_name] = wrapper |
| 860 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 861 | |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 862 | def make_ORMWrapper(wrapped_class, *args, **kwargs): |
Scott Baker | 2f314d5 | 2018-08-24 08:31:19 -0700 | [diff] [blame] | 863 | cls = None |
| 864 | |
| 865 | if (not cls) and wrapped_class.__class__.__name__ in convenience_wrappers: |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 866 | cls = convenience_wrappers[wrapped_class.__class__.__name__] |
Scott Baker | 2f314d5 | 2018-08-24 08:31:19 -0700 | [diff] [blame] | 867 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 868 | if not cls: |
Scott Baker | 2f314d5 | 2018-08-24 08:31:19 -0700 | [diff] [blame] | 869 | # Search the list of class names for this model to see if we have any applicable wrappers. The list is always |
| 870 | # sorted from most specific to least specific, so the first one we find will automatically be the most relevant |
| 871 | # one. If we don't find any, then default to ORMWrapper |
| 872 | |
| 873 | # Note: Only works on objects that have been fetched from the server, not objects that are created on the |
| 874 | # client. This is because wrapped_class.class_names is filled in by the server. |
| 875 | |
| 876 | # TODO(smbaker): Ought to be able to make this work with newly created objects after they are saved. |
| 877 | |
| 878 | for name in wrapped_class.class_names.split(","): |
| 879 | if name in convenience_wrappers: |
| 880 | cls = convenience_wrappers[name] |
| 881 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 882 | if not cls: |
Scott Baker | 22796cc | 2017-02-23 16:53:34 -0800 | [diff] [blame] | 883 | cls = ORMWrapper |
| 884 | |
| 885 | return cls(wrapped_class, *args, **kwargs) |
| 886 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 887 | |
Matteo Scandolo | 10a2f3c | 2018-04-20 16:59:38 +0200 | [diff] [blame] | 888 | def import_convenience_methods(): |
Scott Baker | 8203dc6 | 2019-03-14 16:16:19 -0700 | [diff] [blame] | 889 | # The ORM has several built-in convenience methods that are contained here |
| 890 | lib_dir = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 891 | base_convenience_dir = os.path.join(lib_dir, "convenience") |
Matteo Scandolo | 10a2f3c | 2018-04-20 16:59:38 +0200 | [diff] [blame] | 892 | |
Scott Baker | 8203dc6 | 2019-03-14 16:16:19 -0700 | [diff] [blame] | 893 | # Service convenience methods are placed here during dynamicload |
| 894 | service_convenience_dir = "/var/run/xosapi/convenience" |
Matteo Scandolo | 10a2f3c | 2018-04-20 16:59:38 +0200 | [diff] [blame] | 895 | |
Scott Baker | 8203dc6 | 2019-03-14 16:16:19 -0700 | [diff] [blame] | 896 | for api_convenience_dir in [base_convenience_dir, service_convenience_dir]: |
| 897 | log.info("Loading convenience methods", api_convenience_dir=api_convenience_dir) |
| 898 | |
| 899 | if not os.path.exists(api_convenience_dir): |
| 900 | log.info("No convenience methods found", api_convenience_dir=api_convenience_dir) |
| 901 | continue |
| 902 | |
| 903 | for file in os.listdir(api_convenience_dir): |
| 904 | if file.endswith(".py") and "test" not in file: |
| 905 | pathname = os.path.join(api_convenience_dir, file) |
| 906 | try: |
| 907 | log.debug("Loading: %s" % file) |
| 908 | imp.load_source(file[:-3], pathname) |
| 909 | except Exception: |
| 910 | log.exception( |
| 911 | "Cannot import api convenience method for: %s, %s" |
| 912 | % (file[:-3], pathname) |
| 913 | ) |