from flask import Flask, abort
from flask.ext import restful
from flask.ext.restful import reqparse
from common.nagios import NagiosControl,NagiosControlError

class NagiosScheduleDowntimeAPI(restful.Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('cluster', type=str, required=True, help="Type of incident e.g. triggered, acknowledge, etc.")
        self.parser.add_argument('servicename', type=str, required=True, help="Type of incident e.g. triggered, acknowledge, etc.")
        self.parser.add_argument('action', type=str, required=True, help="Type of incident e.g. triggered, acknowledge, etc.")
        self.parser.add_argument('duration', type=str, default="300", help="Type of incident e.g. triggered, acknowledge, etc.")
        super(NagiosScheduleDowntimeAPI, self).__init__()

    def put(self):
        args = self.parser.parse_args() 
        hosts = []
        data = "duration:%s,service:%s" % (args['duration'], args['servicename'])
        action = NagiosControl(hosts, args['servicename'], args['action'], data)
        action.perform_action()
        if args['action'] == 'schedule_downtime':
            action.check_downtime()
        return "Nagios has applied %s to service %s on cluster %s" % (args['action'], args['servicename'], args['cluster'])