From 53f4095b539147db9faf2a3f6f48e5dac441834d Mon Sep 17 00:00:00 2001 From: Michael Xavier Date: Mon, 2 Nov 2015 19:34:58 -0800 Subject: [PATCH] Add some finishing touches --- src/Database/Bloodhound/Client.hs | 6 ++++++ src/Database/Bloodhound/Types.hs | 1 + tests/tests.hs | 21 +++++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/src/Database/Bloodhound/Client.hs b/src/Database/Bloodhound/Client.hs index 24df13a..870a703 100644 --- a/src/Database/Bloodhound/Client.hs +++ b/src/Database/Bloodhound/Client.hs @@ -283,6 +283,11 @@ existentialQuery url = do reply <- head url return (reply, respIsTwoHunna reply) + +-- | Tries to parse a response body as the expected type @a@ and +-- failing that tries to parse it as an EsError. All well-formed, JSON +-- responses from elasticsearch should fall into these two +-- categories. If they don't, a 'StatusCodeException' will be thrown. parseEsResponse :: (MonadBH m, MonadThrow m, FromJSON a) => Reply -> m (Either EsError a) parseEsResponse reply @@ -362,6 +367,7 @@ updateIndexAliases actions = bindM2 post url (return body) body = Just (encode bodyJSON) bodyJSON = object [ "actions" .= NE.toList actions] +-- | Get all aliases configured on the server. getIndexAliases :: (MonadBH m, MonadThrow m) => m (Either EsError IndexAliasesSummary) getIndexAliases = parseEsResponse =<< get =<< url diff --git a/src/Database/Bloodhound/Types.hs b/src/Database/Bloodhound/Types.hs index c11232f..c07f9c1 100644 --- a/src/Database/Bloodhound/Types.hs +++ b/src/Database/Bloodhound/Types.hs @@ -469,6 +469,7 @@ newtype RoutingValue = RoutingValue { routingValue :: Text } deriving (Show, Eq, newtype IndexAliasesSummary = IndexAliasesSummary { indexAliasesSummary :: [IndexAliasSummary] } deriving (Show, Eq) +{-| 'IndexAliasSummary' is a summary of an index alias configured for a server. -} data IndexAliasSummary = IndexAliasSummary { indexAliasSummaryAlias :: IndexAlias , indexAliasSummaryCreate :: IndexAliasCreate} deriving (Show, Eq) diff --git a/tests/tests.hs b/tests/tests.hs index 5d8c50d..c58b2cf 100644 --- a/tests/tests.hs +++ b/tests/tests.hs @@ -1082,6 +1082,27 @@ main = hspec $ do L.find ((== alias) . indexAliasSummaryAlias) summs `shouldBe` Just expected Left e -> expectationFailure ("Expected an IndexAliasesSummary but got " <> show e)) `finally` cleanup + it "handles an alias with routing and a filter" $ do + let alias = IndexAlias (testIndex) (IndexAliasName (IndexName "bloodhound-tests-twitter-1-alias")) + let sar = SearchAliasRouting (RoutingValue "search val" :| []) + let iar = IndexAliasRouting (RoutingValue "index val") + let routing = GranularAliasRouting (Just sar) (Just iar) + let filter = LimitFilter 42 + let create = IndexAliasCreate (Just routing) (Just filter) + let action = AddAlias alias create + + withTestEnv $ do + resetIndex + resp <- updateIndexAliases (action :| []) + liftIO $ NHTS.statusCode (responseStatus resp) `shouldBe` 200 + let cleanup = withTestEnv (updateIndexAliases (RemoveAlias alias :| [])) + (do aliases <- withTestEnv getIndexAliases + let expected = IndexAliasSummary alias create + case aliases of + Right (IndexAliasesSummary summs) -> + L.find ((== alias) . indexAliasSummaryAlias) summs `shouldBe` Just expected + Left e -> expectationFailure ("Expected an IndexAliasesSummary but got " <> show e)) `finally` cleanup + describe "JSON instances" $ do propJSON (Proxy :: Proxy Version) propJSON (Proxy :: Proxy IndexName)