import logging

from pecan import expose, request
from pecan.ext.notario import validate
from uuid import uuid4

from ceph_installer.controllers import error
from ceph_installer.tasks import call_ansible
from ceph_installer import schemas
from ceph_installer import models
from ceph_installer import util


logger = logging.getLogger(__name__)


class MONController(object):

    @expose('json')
    def index(self):
        # TODO: allow some autodiscovery here so that clients can see what is
        # available
        return dict()

    @expose(generic=True, template='json')
    def install(self):
        error(405)

    @install.when(method='POST', template='json')
    @validate(schemas.install_schema, handler="/errors/schema")
    def install_post(self):
        hosts = request.json.get('hosts')
        extra_vars = util.get_install_extra_vars(request.json)
        identifier = str(uuid4())
        task = models.Task(
            identifier=identifier,
            endpoint=request.path,
        )
        # we need an explicit commit here because the command may finish before
        # we conclude this request
        models.commit()
        kwargs = dict(extra_vars=extra_vars, tags="package-install")
        call_ansible.apply_async(
            args=('mon', hosts, identifier),
            kwargs=kwargs,
        )

        return task

    @expose(generic=True, template='json')
    def configure(self):
        error(405)

    # we need validation here
    @configure.when(method='POST', template='json')
    def configure_post(self):
        return {}

@@ -1530,48 +1530,1129 @@
     
-def configure_post(self):%0A        return %7B%7D
+@validate(schemas.mon_configure_schema, handler=%22/errors/schema%22)%0A    def configure_post(self):%0A        hosts = %5Brequest.json.get('host')%5D%0A        # even with configuring we need to tell ceph-ansible%0A        # if we're working with upstream ceph or red hat ceph storage%0A        extra_vars = util.get_install_extra_vars(request.json)%0A        extra_vars%5B'monitor_interface'%5D = request.json.get('monitor_interface')%0A        if request.json.get('fsid'):%0A            extra_vars%5B'fsid'%5D = request.json.get('fsid')%0A        if request.json.get('monitor_secret'):%0A            extra_vars%5B'monitor_secret'%5D = request.json.get('monitor_secret')%0A        identifier = str(uuid4())%0A        task = models.Task(%0A            identifier=identifier,%0A            endpoint=request.path,%0A        )%0A        # we need an explicit commit here because the command may finish before%0A        # we conclude this request%0A        models.commit()%0A        kwargs = dict(extra_vars=extra_vars, skip_tags=%22package-install%22)%0A        call_ansible.apply_async(%0A            args=('mon', hosts, identifier),%0A            kwargs=kwargs,%0A        )%0A%0A        return task
 %0A

implement the api/mon/configure/ endpoint