X-Git-Url: http://www.average.org/gitweb/?p=pdns-pipe-nmc.git;a=blobdiff_plain;f=NmcDom.hs;h=3b8f06018f039e69e324c40a48055be4386bf6cb;hp=db600304df2a38a55bef100676ee969712bb26fe;hb=444411783cdc992b99a6d9fe0e0a5922686d1931;hpb=414c9f928e12e0c240d55d79ff8360adc6b4d138 diff --git a/NmcDom.hs b/NmcDom.hs index db60030..3b8f060 100644 --- a/NmcDom.hs +++ b/NmcDom.hs @@ -16,8 +16,8 @@ import Data.List (union) import Data.List.Split import Data.Vector ((!), length) import qualified Data.Vector as V (singleton) -import Data.Map (Map, unionWith) -import qualified Data.Map as M (singleton, empty) +import Data.Map (Map, unionWith, foldrWithKey) +import qualified Data.Map as M (singleton, empty, insert, insertWith) import qualified Data.HashMap.Strict as H (lookup) import Data.Aeson import Data.Aeson.Types @@ -74,7 +74,25 @@ makeSubmap :: Object -> Parser (Maybe (Map String NmcDom)) makeSubmap o = ((.).(.)) merge merge <$> takeTls o <*> takeSrv o <*> takeMap o takeMap :: Object -> Parser (Maybe (Map String NmcDom)) -takeMap o = o .:? "map" +takeMap o = + case H.lookup "map" o of + Nothing -> pure Nothing + Just (Object mo) -> do + unsplit <- (parseJSON (Object mo) :: Parser (Maybe (Map String NmcDom))) + let result = fmap splitup unsplit + return result + where + splitup :: Map String NmcDom -> Map String NmcDom + splitup x = foldrWithKey stow M.empty x + stow fqdn sdom acc = M.insertWith merge fqdn' sdom' acc + where + (fqdn', sdom') = nest (filter (/= "") (splitOnDots fqdn), sdom) + splitOnDots s = splitOn "." s + nest ([], v) = (fqdn, v) -- can split result be empty? + nest ([k], v) = (k, v) + nest (k:ks, v) = + nest (ks, def { domSubmap = Just (M.singleton k v) }) + _ -> empty takeSrv :: Object -> Parser (Maybe (Map String NmcDom)) takeSrv o = @@ -97,7 +115,25 @@ takeSrv o = -- takeTls is almost, but not quite, entirely unlike takeSrv takeTls :: Object -> Parser (Maybe (Map String NmcDom)) -takeTls o = o .:? "map" -- FIXME +takeTls o = + case H.lookup "tls" o of + Nothing -> pure Nothing + Just (Object t) -> + (parseJSON (Object t) :: Parser (Map String (Map String [NmcRRTlsa]))) + >>= tmap2dmap + where + tmap2dmap :: Map String (Map String [NmcRRTlsa]) + -> Parser (Maybe (Map String NmcDom)) + -- FIXME return parse error on invalid proto or port + tmap2dmap m1 = return $ foldrWithKey addprotoelem (Just M.empty) m1 + addprotoelem k1 m2 acc = protoelem k1 m2 `merge` acc + protoelem k1 m2 = Just (M.singleton ("_" ++ k1) (pmap2dmap m2)) + pmap2dmap m2 = foldrWithKey addportelem def m2 + addportelem k2 v acc = portelem k2 v `merge` acc + portelem k2 v = + def { domSubmap = Just (M.singleton ("_" ++ k2) + def { domTlsa = Just v }) } + Just _ -> empty class Mergeable a where merge :: a -> a -> a -- bias towads second arg @@ -153,6 +189,15 @@ data NmcRRTlsa = NmcRRTlsa , tlsIncSubdoms :: Int -- 1:enforce on subdoms 0:no } deriving (Show, Eq) +instance FromJSON NmcRRTlsa where + parseJSON (Array a) = + if length a == 3 then NmcRRTlsa + <$> parseJSON (a ! 0) + <*> parseJSON (a ! 1) + <*> parseJSON (a ! 2) + else empty + parseJSON _ = empty + instance Mergeable NmcRRTlsa where merge _ b = b