def add_service_volume(self,
                           tenant,
                           service,
                           volume_path,
                           volume_type,
                           volume_name,
                           file_content=None,
                           settings=None,
                           user_name='',
                           mode=None):

        volume = self.create_service_volume(
            tenant,
            service,
            volume_path,
            volume_type,
            volume_name,
            settings,
            mode=mode,
        )

        # region端添加数据
        if service.create_status == "complete":
            data = {
                "category": service.category,
                "volume_name": volume.volume_name,
                "volume_path": volume.volume_path,
                "volume_type": volume.volume_type,
                "enterprise_id": tenant.enterprise_id,
                "volume_capacity": volume.volume_capacity,
                "volume_provider_name": volume.volume_provider_name,
                "access_mode": volume.access_mode,
                "share_policy": volume.share_policy,
                "backup_policy": volume.backup_policy,
                "reclaim_policy": volume.reclaim_policy,
                "allow_expansion": volume.allow_expansion,
                "operator": user_name,
                "mode": mode,
            }
            if volume_type == "config-file":
                data["file_content"] = file_content
            res, body = region_api.add_service_volumes(service.service_region,
                                                       tenant.tenant_name,
                                                       service.service_alias,
                                                       data)
            logger.debug(body)

        volume.save()
        if volume_type == "config-file":
            file_data = {
                "service_id": service.service_id,
                "volume_id": volume.ID,
                "file_content": file_content
            }
            volume_repo.add_service_config_file(**file_data)
        return volume
    def add_service_volume(self, tenant, service, volume_path, volume_type, volume_name, file_content=None, settings=None):
        volume_name = volume_name.strip()
        volume_path = volume_path.strip()
        volume_name = self.check_volume_name(service, volume_name)
        self.check_volume_path(service, volume_path)
        host_path = "/grdata/tenant/{0}/service/{1}{2}".format(tenant.tenant_id, service.service_id, volume_path)
        volume_data = {
            "service_id": service.service_id,
            "category": service.category,
            "host_path": host_path,
            "volume_type": volume_type,
            "volume_path": volume_path,
            "volume_name": volume_name
        }

        self.check_volume_options(tenant, service, volume_type, settings)
        settings = self.setting_volume_properties(tenant, service, volume_type, settings)

        volume_data['volume_capacity'] = settings['volume_capacity']
        volume_data['volume_provider_name'] = settings['volume_provider_name']
        volume_data['access_mode'] = settings['access_mode']
        volume_data['share_policy'] = settings['share_policy']
        volume_data['backup_policy'] = settings['backup_policy']
        volume_data['reclaim_policy'] = settings['reclaim_policy']
        volume_data['allow_expansion'] = settings['allow_expansion']
        logger.debug("add console service volume is {0}".format(volume_data))
        # region端添加数据
        if service.create_status == "complete":
            data = {
                "category": service.category,
                "volume_name": volume_name,
                "volume_path": volume_path,
                "volume_type": volume_type,
                "enterprise_id": tenant.enterprise_id,
                "volume_capacity": settings['volume_capacity'],
                "volume_provider_name": settings['volume_provider_name'],
                "access_mode": settings['access_mode'],
                "share_policy": settings['share_policy'],
                "backup_policy": settings['backup_policy'],
                "reclaim_policy": settings['reclaim_policy'],
                "allow_expansion": settings['allow_expansion'],
            }
            if volume_type == "config-file":
                data["file_content"] = file_content
            res, body = region_api.add_service_volumes(service.service_region, tenant.tenant_name, service.service_alias, data)
            logger.debug(body)

        volume = volume_repo.add_service_volume(**volume_data)
        if volume_type == "config-file":
            file_data = {"service_id": service.service_id, "volume_id": volume.ID, "file_content": file_content}
            volume_repo.add_service_config_file(**file_data)
        return volume
 def _update_volumes(self, volumes):
     for volume in volumes.get("add"):
         volume["service_id"] = self.service.service_id
         host_path = "/grdata/tenant/{0}/service/{1}{2}".format(
             self.tenant.tenant_id, self.service.service_id,
             volume["volume_path"])
         volume["host_path"] = host_path
         file_content = volume.get("file_content", None)
         if file_content is not None:
             volume.pop("file_content")
         v = volume_repo.add_service_volume(**volume)
         if not file_content and volume["volume_type"] != "config-file":
             continue
         file_data = {
             "service_id": self.service.service_id,
             "volume_id": v.ID,
             "file_content": file_content
         }
         _ = volume_repo.add_service_config_file(**file_data)
     for volume in volumes.get("upd"):
         # only volume of type config-file can be updated,
         # and only the contents of the configuration file can be updated.
         file_content = volume.get("file_content", None)
         if not file_content and volume["volume_type"] != "config-file":
             continue
         v = volume_repo.get_service_volume_by_name(self.service.service_id,
                                                    volume["volume_name"])
         if not v:
             logger.warning(
                 "service id: {}; volume name: {}; failed to update volume: \
                 volume not found.".format(self.service.service_id,
                                           volume["volume_name"]))
         cfg = volume_repo.get_service_config_file(v.ID)
         cfg.file_content = file_content
         cfg.save()
 def volumes(self, service_volumes, service_config_file):
     volume_repo.delete_service_volumes(self.service.service_id)
     volume_repo.delete_config_files(self.service.service_id)
     id_cfg = {item["volume_id"]: item for item in service_config_file}
     for item in service_volumes:
         item.pop("ID")
         v = volume_repo.add_service_volume(**item)
         if v.volume_type != "config-file":
             continue
         cfg = id_cfg.get(item.ID, None)
         if cfg is None:
             continue
         cfg["volume_id"] = v.ID
         cfg.pop("ID")
         _ = volume_repo.add_service_config_file(**cfg)
    def add_service_volume(self,
                           tenant,
                           service,
                           volume_path,
                           volume_type,
                           volume_name,
                           file_content=None):
        volume_name = volume_name.strip()
        volume_path = volume_path.strip()
        code, msg, volume_name = self.check_volume_name(service, volume_name)
        dep_mnt_names = mnt_repo.get_service_mnts(
            tenant.tenant_id, service.service_id).values_list('mnt_dir',
                                                              flat=True)
        local_path = []
        if dep_mnt_names:
            local_path.append(
                dep_mnt_names.values("mnt_dir")[0].get("mnt_dir"))
        if code != 200:
            return code, msg, None
        code, msg = self.check_volume_path(service, volume_path, local_path)
        if code != 200:
            return code, msg, None
        host_path = "/grdata/tenant/{0}/service/{1}{2}".format(
            tenant.tenant_id, service.service_id, volume_path)
        volume_data = {
            "service_id": service.service_id,
            "category": service.category,
            "host_path": host_path,
            "volume_type": volume_type,
            "volume_path": volume_path,
            "volume_name": volume_name
        }
        # region端添加数据
        if service.create_status == "complete":
            if volume_type == "config-file":
                data = {
                    "category": service.category,
                    "volume_name": volume_name,
                    "volume_path": volume_path,
                    "volume_type": volume_type,
                    "file_content": file_content,
                    "enterprise_id": tenant.enterprise_id
                }
            else:
                data = {
                    "category": service.category,
                    "volume_name": volume_name,
                    "volume_path": volume_path,
                    "volume_type": volume_type,
                    "enterprise_id": tenant.enterprise_id
                }
            res, body = region_api.add_service_volumes(service.service_region,
                                                       tenant.tenant_name,
                                                       service.service_alias,
                                                       data)
            logger.debug(body)

        volume = volume_repo.add_service_volume(**volume_data)
        if volume_type == "config-file":
            file_data = {
                "service_id": service.service_id,
                "volume_id": volume.ID,
                "file_content": file_content
            }
            cf_file = volume_repo.add_service_config_file(**file_data)
        return 200, "success", volume