# Path: virtman/blockservice.py
# LOG = logging.getLogger(__name__)
# def findloop():
# def try_linkloop(loop_dev):
# def is_looped(loop_dev):
# def linkloop(loop_dev, path):
# def unlinkloop(loop_dev):
# 
# Path: virtman/drivers/iscsi.py
# class TgtExecutor(TgtAdm):
#     def __init__(self, root_helper='', volumes_dir='/etc/tgt/stack.d'):
# def create_iscsi_target(iqn, path):
# def remove_iscsi_target(vol_id, vol_name):
# def exists(iqn):
# def is_connected(target_id):


import os
from virtman import blockservice
from virtman.drivers import iscsi
from virtman.openstack.common import log as logging

def create_image_target(image_name, file_path, loop_dev, iqn_prefix):
    """
    :returns : string
        "0:info" specifies SUCCESS, info="target_id:loop_dev"
        "1:info" specifies WARNING, info indicates image_name exists
        "2:info" specifies WARNING, info indicates image file_path not exists
    """
    LOG.debug("Virtman: Image Service: create image target started! "
              "image_name = %s, file_path = %s" % (image_name, file_path))
    if not image_name.startswith("volume-"):
        image_name = "volume-" + image_name
    if image_name in targetlist:
        LOG.warn("Virtman: Image Service: Warning! image_name = %s exists! "
                  "Please use another name" % image_name)
        return "1:" + "Virtman: Image Service: Warning! " \
                      "image_name = %s exists! Please use another name" \
                      % image_name
    if not os.path.exists(file_path):
        LOG.warn("Virtman: Image Service: Warning! "
                 "image file_path = %s not exists! Please use another "
                 "image file" % file_path)
        return "2:" + "Virtman: Image Service: Warning!" \
                      "image file_path = %s not exists! Please use " \
                      "another image file" % file_path
    if loop_dev is None:
        loop_dev = blockservice.findloop()
    else:
        blockservice.unlinkloop(loop_dev)
    blockservice.linkloop(loop_dev, file_path)

# Path: virtman/blockservice.py
# LOG = logging.getLogger(__name__)
# def findloop():
# def try_linkloop(loop_dev):
# def is_looped(loop_dev):
# def linkloop(loop_dev, path):
# def unlinkloop(loop_dev):
# 
# Path: virtman/drivers/iscsi.py
# class TgtExecutor(TgtAdm):
#     def __init__(self, root_helper='', volumes_dir='/etc/tgt/stack.d'):
# def create_iscsi_target(iqn, path):
# def remove_iscsi_target(vol_id, vol_name):
# def exists(iqn):
# def is_connected(target_id):


# Path: virtman/imageservice.py
import os
from virtman import blockservice
from virtman.drivers import iscsi
from virtman.openstack.common import log as logging


def create_image_target(image_name, file_path, loop_dev, iqn_prefix):
    """
    :returns : string
        "0:info" specifies SUCCESS, info="target_id:loop_dev"
        "1:info" specifies WARNING, info indicates image_name exists
        "2:info" specifies WARNING, info indicates image file_path not exists
    """
    LOG.debug("Virtman: Image Service: create image target started! "
              "image_name = %s, file_path = %s" % (image_name, file_path))
    if not image_name.startswith("volume-"):
        image_name = "volume-" + image_name
    if image_name in targetlist:
        LOG.warn("Virtman: Image Service: Warning! image_name = %s exists! "
                  "Please use another name" % image_name)
        return "1:" + "Virtman: Image Service: Warning! " \
                      "image_name = %s exists! Please use another name" \
                      % image_name
    if not os.path.exists(file_path):
        LOG.warn("Virtman: Image Service: Warning! "
                 "image file_path = %s not exists! Please use another "
                 "image file" % file_path)
        return "2:" + "Virtman: Image Service: Warning!" \
                      "image file_path = %s not exists! Please use " \
                      "another image file" % file_path
    if loop_dev is None:
        loop_dev = blockservice.findloop()
    else:
        blockservice.unlinkloop(loop_dev)
    blockservice.linkloop(loop_dev, file_path)

    target_id = iscsi.create_iscsi_target(iqn_prefix + image_name, loop_dev)