コード例 #1
0
 def __save_volume(self, tenant, service, tenant_service_volumes,
                   service_config_file):
     contain_config_file = False if not service_config_file else True
     if not service_config_file:
         contain_config_file = True
     volume_list = []
     config_list = []
     volume_name_id = {}
     for volume in tenant_service_volumes:
         index = volume.pop("ID")
         volume_name_id[volume["volume_name"]] = index
         if volume["volume_type"] == "config-file" and contain_config_file:
             for config_file in service_config_file:
                 if config_file["volume_id"] == index:
                     config_file.pop("ID")
                     new_config_file = TenantServiceConfigurationFile(
                         **config_file)
                     new_config_file.service_id = service.service_id
                     config_list.append(new_config_file)
         settings = volume_service.get_best_suitable_volume_settings(
             tenant, service, volume["volume_type"],
             volume.get("access_mode"), volume.get("share_policy"),
             volume.get("backup_policy"), None,
             volume.get("volume_provider_name"))
         if settings["changed"]:
             logger.debug('volume type changed from {0} to {1}'.format(
                 volume["volume_type"], settings["volume_type"]))
             volume["volume_type"] = settings["volume_type"]
         new_volume = TenantServiceVolume(**volume)
         new_volume.service_id = service.service_id
         volume_list.append(new_volume)
     if volume_list:
         # bulk_create do not return volume's id(django database connection feature can_return_ids_from_bulk_insert)
         TenantServiceVolume.objects.bulk_create(volume_list)
         # query again volume for volume_id
         volumes = volume_repo.get_service_volumes_with_config_file(
             service.service_id)
         # prepare old volume_id and new volume_id relations
         volume_name_ids = [{
             "volume_name": volume.volume_name,
             "volume_id": volume.ID
         } for volume in volumes]
         volume_id_relations = {}
         for vni in volume_name_ids:
             if volume_name_id.get(vni["volume_name"]):
                 old_volume_id = volume_name_id.get(vni["volume_name"])
                 new_volume_id = vni["volume_id"]
                 volume_id_relations[old_volume_id] = new_volume_id
         for config in config_list:
             if volume_id_relations.get(config.volume_id):
                 config.volume_id = volume_id_relations.get(
                     config.volume_id)
         TenantServiceConfigurationFile.objects.bulk_create(config_list)
コード例 #2
0
    def __save_volume(self, service, tenant_service_volumes,
                      service_config_file):
        if not service_config_file:
            volume_list = []
            for volume in tenant_service_volumes:
                volume.pop("ID")
                new_volume = TenantServiceVolume(**volume)
                new_volume.service_id = service.service_id
                volume_list.append(new_volume)
            if volume_list:
                TenantServiceVolume.objects.bulk_create(volume_list)
        else:
            for volume in tenant_service_volumes:
                volume_list = []
                config_list = []
                for config_file in service_config_file:
                    if config_file["volume_id"] == volume["ID"]:
                        config_file.pop("ID")
                        new_config_file = TenantServiceConfigurationFile(
                            **config_file)
                        new_config_file.service_id = service.service_id
                        config_list.append(new_config_file)
                volume.pop("ID")
                new_volume = TenantServiceVolume(**volume)
                new_volume.service_id = service.service_id
                volume_list.append(new_volume)
                logger.debug('----------------->{0}'.format(
                    new_volume.to_dict()))
                if volume_list:
                    volume_js = TenantServiceVolume.objects.bulk_create(
                        volume_list)
                    volume_j = volume_js[0]
                    logger.debug('--------------555-------------->{0}'.format(
                        volume_j.to_dict()))
                    for config in config_list:
                        volume_obj = volume_repo.get_service_volume_by_name(
                            service.service_id, volume_j.volume_name)
                        logger.debug(
                            '--------------2222121212-------------->{0}'.
                            format(volume_obj.to_dict()))
                        config.volume_id = volume_obj.ID
                        logger.debug(
                            '------------666---------------->{0}'.format(
                                config.volume_id))

                    TenantServiceConfigurationFile.objects.bulk_create(
                        config_list)