#!/usr/bin/env python

import urlparse
import os
import sys
import xapi
import xapi.datapath
import xapi.volume
from xapi.storage.datapath import tapdisk, image
from xapi.storage.datapath import log


class Implementation(xapi.datapath.Datapath_skeleton):

    def activate(self, dbg, uri, domain):
        return

    def attach(self, dbg, uri, domain):
        u = urlparse.urlparse(uri)
        # XXX need some datapath-specific errors below
        if not(os.path.exists(u.path)):
            raise xapi.volume.Volume_does_not_exist(u.path)
        if u.scheme[:3] == "vhd":
            img = image.Vhd(u.path)
        elif u.scheme[:3] == "raw":
            img = image.Raw(u.path)
        else:
            raise
        tap = tapdisk.create(dbg)
        tap.open(dbg, img)
        return {
            'domain_uuid': '0',
            'implementation': ['Tapdisk3', tap.block_device()],
        }

    def close(self, dbg, uri):
        u = urlparse.urlparse(uri)
        # XXX need some datapath-specific errors below
        if not(os.path.exists(u.path)):
            raise xapi.volume.Volume_does_not_exist(u.path)
        return None

    def deactivate(self, dbg, uri, domain):
        return

    def detach(self, dbg, uri, domain):
        u = urlparse.urlparse(uri)
        # XXX need a datapath-specific error
        if not(os.path.exists(u.path)):
            raise xapi.volume.Volume_does_not_exist(u.path)
        tap = tapdisk.find_by_file(dbg, image.Path(u.path))
        tap.close(dbg)
        tap.destroy(dbg)

    def open(self, dbg, uri, persistent):
        u = urlparse.urlparse(uri)
        # XXX need some datapath-specific errors below
        if not(os.path.exists(u.path)):
            raise xapi.volume.Volume_does_not_exist(u.path)
        return None

if __name__ == "__main__":
    log.log_call_argv()
    cmd = xapi.datapath.Datapath_commandline(Implementation())
    base = os.path.basename(sys.argv[0])
    if base == "Datapath.activate":
        cmd.activate()
    elif base == "Datapath.attach":
        cmd.attach()
    elif base == "Datapath.close":
        cmd.close()
    elif base == "Datapath.deactivate":
        cmd.deactivate()
    elif base == "Datapath.detach":
        cmd.detach()
    elif base == "Datapath.open":
        cmd.open()
    else:
        raise xapi.datapath.Unimplemented(base)

@@ -73,24 +73,36 @@
 import xapi.
+storage.api.
 datapath%0Aimp
@@ -106,24 +106,36 @@
 import xapi.
+storage.api.
 volume%0Afrom 
@@ -191,33 +191,24 @@
 xapi.storage
-.datapath
  import log%0A
@@ -231,24 +231,36 @@
 tation(xapi.
+storage.api.
 datapath.Dat
@@ -520,32 +520,44 @@
      raise xapi.
+storage.api.
 volume.Volume_do
@@ -1112,32 +1112,44 @@
      raise xapi.
+storage.api.
 volume.Volume_do
@@ -1425,32 +1425,44 @@
      raise xapi.
+storage.api.
 volume.Volume_do
@@ -1782,24 +1782,36 @@
  raise xapi.
+storage.api.
 volume.Volum
@@ -1918,24 +1918,36 @@
  cmd = xapi.
+storage.api.
 datapath.Dat
@@ -2394,24 +2394,36 @@
  raise xapi.
+storage.api.
 datapath.Uni

Use the new xapi.storage package structure