def index(self, req, tenant_id): """ Return all backups information for a tenant ID. """ LOG.debug("Listing Backups for tenant '%s'" % tenant_id) context = req.environ[wsgi.CONTEXT_KEY] ## add customise conditions when list instance search_ops = {} search_ops.update(req.GET) search_ops = self._validate_search_ops(search_ops) op_chain = search_ops.get('chain', None) if op_chain: backups = KSC_Backup.get_chain_whole(context, op_chain) else: backups = Backup.list(context, conditions=search_ops) LOG.debug("index.backups: '%s'", backups) bks = [] for backup in backups: try: #service = inst_models.ServiceImage.find_by(id=backup.service_image_id) #backup.db_type = service['service_name'] ds,ds_version = ds_patch_models.find_datastore_by_image_id(backup['service_image_id']) backup.db_type = ds.name except Exception as ex: backup['db_type'] = "" LOG.warn("Failed get db type information of backup %s, %s"%(backup['id'], ex)) bks.append(backup) backups = bks return wsgi.Result(views.BackupViews(backups).data(), 200)
def show(self, req, tenant_id, id): """Return a single backup.""" LOG.info(_("Showing a backup for tenant '%s'") % tenant_id) LOG.info(_("id : '%s'\n\n") % id) context = req.environ[wsgi.CONTEXT_KEY] backup = Backup.get_by_id(context, id) try: #service = inst_models.ServiceImage.find_by(id=backup.service_image_id) #backup.db_type = service['service_name'] ds,ds_version = ds_patch_models.find_datastore_by_image_id(backup.service_image_id) backup.db_type = ds.name except Exception as ex: backup.db_type = "" LOG.warn("Failed get db type information of backup %s, %s"%(backup.id, ex)) chain = self._get_chain_ids(context, id) LOG.info(_("chain : '%s'") % chain) return wsgi.Result(views.BackupView(backup, chain=chain).data(), 200)
def create(self, req, body, tenant_id): LOG.debug("Creating a Backup for tenant '%s'" % tenant_id) context = req.environ[wsgi.CONTEXT_KEY] data = body['backup'] instance = data.get('instance',None) group = data.get('group',None) name = data['name'] type = data.get("type", "snapshot") #expire_at = data.get("expire_after", 7) desc = data.get('description') parent_id = data.get('parent_id') LOG.info("parent_id:%s", parent_id) if group is None and instance is None: raise exception.BadRequest("you must specify group or instance") instance_id = None if group is not None: try: instance_id = InstanceGroupItem.get_by_gid_type(context, group, DBInstanceType.STANDBY).instance_id except: instance_id = InstanceGroupItem.get_by_gid_type(context, group, DBInstanceType.SINGLE).instance_id if instance_id is None and instance is not None: instance_id = inst_utils.virtual_instid_2_origin_instid(instance) _instance = DBInstance.find_by(context,id=instance_id) _type = _instance.service_type #_image = ServiceImage.find_by(context,service_name=_type) #service_image_id = _image.id ds,ds_version = ds_models.get_datastore_version(_type) service_image_id = ds_version.image_id grp_item = InstanceGroupItem.get_by_instance_id(context, _instance.id) group_id = grp_item.group_id # get this group's autobackup config and set the expire_after default _autobackup = AutoBackup.get_by_gid(context, group_id) expire_after = data.get("expire_after", _autobackup.expire_after) duration = _autobackup.duration expire_at = AutoBackup.calculate_expire_at(expire_after, duration) LOG.info("group_id %s, expire_at :%s", group_id, time.ctime(expire_at)) if grp_item.type == DBInstanceType.MASTER: try: instance_id = InstanceGroupItem.get_by_gid_type(context, group_id, DBInstanceType.STANDBY).instance_id except Exception as e: LOG.error(e) backup = Backup.create(context, instance_id, name, description=desc,group_id=group_id,backup_type=type,expire_at=expire_at,service_image_id=service_image_id,parent_id=parent_id) try: #service = inst_models.ServiceImage.find_by(id=backup.service_image_id) #backup.db_type = service['service_name'] ds,ds_version = ds_patch_models.find_datastore_by_image_id(backup.service_image_id) backup.db_type = ds.name except Exception as ex: backup.db_type = "" LOG.warn("Failed get db type information of backup %s, %s", backup.id, ex) chain = self._get_chain_ids(context, id) LOG.info(_("chain : '%s'") % chain) return wsgi.Result(views.BackupView(backup).data(), 202)
def backups(self, req, tenant_id, id): """Return all backups for the specified instance.""" LOG.info(_("req : '%s'\n\n") % req) LOG.info(_("Indexing backups for instance or group '%s'")%id) if False==uuid.is_uuid_like(id): msg = (_("Wrong id of instance or group.")) LOG.error(msg) raise exception.BadRequest(msg) group_id = None context = req.environ[wsgi.CONTEXT_KEY] try: db_info = models.get_db_info(context, id) group_id = db_info.group_id except exception.NotFound: LOG.debug("Not instance id %s"%id) pass if group_id==None: try: InstanceGroup.get_by_groupid(context, id) group_id = id except exception.NotFound: msg = (_("Without find instance or group of the id")) LOG.error(msg) raise exception.BadRequest(msg) else: try: item = InstanceGroupItem.get_by_instance_id(context, id) if item.type==DBInstanceType.READ_REPLI or item.type==DBInstanceType.STANDBY: msg = (_("Without backups for the instance.")) LOG.error(msg) raise exception.BadRequest(msg) except exception.NotFound: msg = (_("Without find instance group item information.")) LOG.error(msg) raise exception.BadRequest(msg) backups = backup_model.list_autobackup(context, group_id) backups += backup_model.list_snapshot(context, group_id) bks = [] #for backup in backups: # instance = models.FreshInstance.load(context, backup.instance_id) # backup.db_type = instance.service_type # bks.append(backup) #TODO(ksc-need-discuss) backup.db_type didn't used in backup view, really need it? tmp = dict() for backup in backups: ## autobackup's service_image_id is NULL if backup.service_image_id: if backup.service_image_id not in tmp: #service_image = models.ServiceImage.find_by(id=backup.service_image_id) #backup.db_type = service_image.service_name #tmp[backup.service_image_id] = service_image.service_name try: #image_id may be is not exists in datastaore datastore, datastore_version = ds_path_models.find_datastore_by_image_id(backup.service_image_id) tmp[backup.service_image_id] = datastore.name backup.db_type = datastore.name except Exception: backup.db_type = "" else: backup.db_type = tmp[backup.service_image_id] else: backup.db_type = "" bks.append(backup) backups = bks LOG.debug('Getted backups %s'%backups) return wsgi.Result(backup_views.BackupViews(backups).data(), 200)