Susanta Gautam | 745a1f9 | 2021-04-22 18:31:59 +0545 | [diff] [blame] | 1 | from staffeln.db import api as db_api |
| 2 | from staffeln.objects import base |
| 3 | from staffeln.objects import fields as sfeild |
| 4 | |
| 5 | |
| 6 | @base.StaffelnObjectRegistry.register |
Susanta Gautam | 73a52bb | 2021-04-27 16:01:11 +0545 | [diff] [blame] | 7 | class Volume( |
| 8 | base.StaffelnPersistentObject, base.StaffelnObject, base.StaffelnObjectDictCompat |
| 9 | ): |
| 10 | VERSION = "1.0" |
Susanta Gautam | 745a1f9 | 2021-04-22 18:31:59 +0545 | [diff] [blame] | 11 | |
| 12 | dbapi = db_api.get_instance() |
| 13 | |
| 14 | fields = { |
Susanta Gautam | 73a52bb | 2021-04-27 16:01:11 +0545 | [diff] [blame] | 15 | "id": sfeild.IntegerField(), |
| 16 | "backup_id": sfeild.StringField(), |
| 17 | "instance_id": sfeild.StringField(), |
okozachenko | 179cd57 | 2021-05-06 22:23:53 +0300 | [diff] [blame] | 18 | "project_id": sfeild.UUIDField(), |
Susanta Gautam | 73a52bb | 2021-04-27 16:01:11 +0545 | [diff] [blame] | 19 | "volume_id": sfeild.UUIDField(), |
| 20 | "backup_completed": sfeild.IntegerField(), |
Susanta Gautam | 745a1f9 | 2021-04-22 18:31:59 +0545 | [diff] [blame] | 21 | } |
| 22 | |
Susanta Gautam | 6d9d3db | 2021-04-25 21:21:19 +0545 | [diff] [blame] | 23 | @base.remotable_classmethod |
Susanta Gautam | 9309870 | 2021-04-30 18:54:24 +0545 | [diff] [blame] | 24 | def list(cls, context, filters=None): |
Susanta Gautam | 745a1f9 | 2021-04-22 18:31:59 +0545 | [diff] [blame] | 25 | """Return a list of :class:`Backup` objects. |
| 26 | |
| 27 | :param filters: dict mapping the filter to a value. |
| 28 | """ |
Susanta Gautam | 9309870 | 2021-04-30 18:54:24 +0545 | [diff] [blame] | 29 | db_backups = cls.dbapi.get_backup_list(context, filters=filters) |
Susanta Gautam | 745a1f9 | 2021-04-22 18:31:59 +0545 | [diff] [blame] | 30 | |
Susanta Gautam | b1bf807 | 2021-05-06 16:54:14 +0545 | [diff] [blame] | 31 | return [cls._from_db_object(cls(context), obj) for obj in db_backups] |
Susanta Gautam | 745a1f9 | 2021-04-22 18:31:59 +0545 | [diff] [blame] | 32 | |
| 33 | @base.remotable |
| 34 | def create(self): |
| 35 | """Create a :class:`Backup_data` record in the DB""" |
| 36 | values = self.obj_get_changes() |
Susanta Gautam | 745a1f9 | 2021-04-22 18:31:59 +0545 | [diff] [blame] | 37 | db_backup = self.dbapi.create_backup(values) |
| 38 | self._from_db_object(self, db_backup) |
| 39 | |
| 40 | @base.remotable |
| 41 | def save(self): |
| 42 | """Save updates to the :class:`Backup_data`. |
| 43 | |
| 44 | Updates will be made column by column based on the results |
| 45 | of self.what_changed(). |
| 46 | """ |
| 47 | updates = self.obj_get_changes() |
| 48 | db_obj = self.dbapi.update_backup(self.uuid, updates) |
| 49 | obj = self._from_db_object(self, db_obj, eager=False) |
| 50 | self.obj_refresh(obj) |
| 51 | self.obj_reset_changes() |
| 52 | |
| 53 | @base.remotable |
| 54 | def refresh(self): |
| 55 | """Loads updates for this :class:`Backup_data`. |
| 56 | Loads a backup with the same backup_id from the database and |
| 57 | checks for updated attributes. Updates are applied from |
| 58 | the loaded backup column by column, if there are any updates. |
| 59 | """ |
Susanta Gautam | 6d9d3db | 2021-04-25 21:21:19 +0545 | [diff] [blame] | 60 | current = self.get_by_uuid(backup_id=self.backup_id) |
Susanta Gautam | 745a1f9 | 2021-04-22 18:31:59 +0545 | [diff] [blame] | 61 | self.obj_refresh(current) |
Susanta Gautam | 5442ea7 | 2021-05-05 13:25:18 +0545 | [diff] [blame] | 62 | |
Susanta Gautam | b1bf807 | 2021-05-06 16:54:14 +0545 | [diff] [blame] | 63 | @base.remotable |
| 64 | def delete_backup(self): |
| 65 | """Soft Delete the :class:`Queue_data` from the DB""" |
| 66 | db_obj = self.dbapi.soft_delete_backup(self.id) |
| 67 | |
Susanta Gautam | 5442ea7 | 2021-05-05 13:25:18 +0545 | [diff] [blame] | 68 | @base.remotable_classmethod |
| 69 | def get_backup_by_backup_id(cls, context, backup_id): |
| 70 | """Find a backup based on backup_id |
| 71 | :param context: Security context. NOTE: This should only |
| 72 | be used internally by the indirection_api. |
| 73 | Unfortunately, RPC requires context as the first |
| 74 | argument, even though we don't use it. |
| 75 | A context should be set when instantiating the |
| 76 | object, e.g.: Queue(context) |
| 77 | :param backup_id: the backup id of volume in volume data. |
| 78 | :returns: a :class:`Backup` object. |
| 79 | """ |
| 80 | db_backup = cls.dbapi.get_backup_by_backup_id(context, backup_id) |
| 81 | if db_backup is None: |
| 82 | return db_backup |
| 83 | else: |
| 84 | backup = cls._from_db_object(cls(context), db_backup) |
| 85 | return backup |
Susanta Gautam | f3b4e72 | 2021-05-06 19:06:24 +0545 | [diff] [blame] | 86 | |