From: Eugene Crosser Date: Sat, 29 Mar 2014 20:59:43 +0000 (+0400) Subject: make pdnsOut more composable X-Git-Tag: 0.9.0.0~110 X-Git-Url: http://average.org/gitweb/?a=commitdiff_plain;h=b0c8a2dae29482014bcb8794a18c0aef24abe683;p=pdns-pipe-nmc.git make pdnsOut more composable --- diff --git a/PowerDns.hs b/PowerDns.hs index cb685dd..165b80e 100644 --- a/PowerDns.hs +++ b/PowerDns.hs @@ -63,5 +63,8 @@ pdnsParse ver s = }) _ -> Left $ "Unparseable PDNS Request: " ++ s -pdnsOut :: RRType -> NmcDom -> String -pdnsOut _ d = show d +pdnsOut :: Int -> RRType -> Either String NmcDom -> String +pdnsOut ver _ d = + case d of + Left err -> "LOG Error: " ++ err ++ "\nFAIL\n" + Right dom -> "DATA\n" ++ (show dom) ++ "\nEND\n" --FIXME diff --git a/pdns-pipe-nmc.hs b/pdns-pipe-nmc.hs index a3fc8c7..b010bac 100644 --- a/pdns-pipe-nmc.hs +++ b/pdns-pipe-nmc.hs @@ -5,7 +5,7 @@ module Main where import Control.Monad import qualified Data.ByteString.Char8 as C (pack, unpack) import qualified Data.ByteString.Lazy.Char8 as L (pack, unpack) -import Data.ByteString.Lazy as BS hiding (reverse, putStrLn) +import Data.ByteString.Lazy as BS hiding (reverse, putStr, putStrLn) import Data.List.Split import Data.Aeson (encode, decode, Value(..)) import Network.HTTP.Types @@ -55,6 +55,8 @@ qRsp rsp = -- NMC interface +descend subdom dom = dom + queryNmc :: Manager -> Config -> String -> String -> IO (Either String NmcDom) queryNmc mgr cfg fqdn qid = do @@ -62,7 +64,9 @@ queryNmc mgr cfg fqdn qid = do "bit":dn:xs -> do rsp <- runResourceT $ httpLbs (qReq cfg (L.pack ("d/" ++ dn)) (L.pack qid)) mgr - return $ qRsp rsp + return $ case qRsp rsp of + Left err -> Left err + Right dom -> Right $ descend xs dom _ -> return $ Left "Only \".bit\" domain is supported" @@ -92,24 +96,18 @@ main = do forever $ do l <- getLine case pdnsParse ver l of - Left e -> putStrLn $ "ERROR\t" ++ e + Left e -> putStrLn $ "FAIL\t" ++ e Right preq -> do case preq of - PdnsRequestQ qname qtype id _ _ _ -> do - ncres <- queryNmc mgr cfg qname id - case ncres of - Left e -> putStrLn $ "ERROR\t" ++ e - Right dom -> putStrLn $ pdnsOut qtype dom + PdnsRequestQ qname qtype id _ _ _ -> + queryNmc mgr cfg qname id >>= putStr . (pdnsOut ver qtype) PdnsRequestAXFR xfrreq -> - putStrLn ("ERROR\tNo support for AXFR " ++ xfrreq) - PdnsRequestPing -> putStrLn "OK" + putStrLn ("FAIL\tNo support for AXFR " ++ xfrreq) + PdnsRequestPing -> putStrLn "END" -- for testing ask str = do cfg <- readConfig confFile mgr <- newManager def - ncres <- queryNmc mgr cfg str "test-req-id" - case ncres of - Left e -> putStrLn $ "ERROR\t" ++ e - Right dom -> putStrLn $ pdnsOut RRTypeANY dom + queryNmc mgr cfg str "test-req-id" >>= putStr . (pdnsOut 1 RRTypeANY)