on master help sign in
Implement network alias configuration via NetworkingConfig
commit60cc72f325c76492acaf844370b965ed46b26dcc authorDaQuirm <leo.daquirm@gmail.com> dateSun 25 Mar 2018, 19:14 (9 years ago) parent53a0a44
2 files changed+51−4
src/Docker/Client/Types.hs+34−3 tests/tests.hs+17−1
src/Docker/Client/Types.hs+34−3
@@ -68,6 +68,8 @@ module Docker.Client.Types (
6868 , UTSMode(..)
6969 , HostConfig(..)
7070 , defaultHostConfig
71+ , NetworkingConfig(..)
72+ , EndpointConfig(..)
7173 , Ulimit(..)
7274 , ContainerResources(..)
7375 , defaultContainerResources
@@ -611,20 +613,45 @@ instance FromJSON Image where
611613 return $ DockerImage imageId imageCreated imageParentId imageRepoTags imageRepoDigests imageSize imageVirtualSize imageLabels
612614 parseJSON _ = fail "Failed to parse DockerImage."
613615
616+-- | Alias for Aliases.
617+type Alias = Text
618+
619+-- | EndpointsConfig is container configuration for a specific network
620+newtype EndpointConfig = EndpointConfig [Alias] deriving (Eq, Show)
621+
622+instance ToJSON EndpointConfig where
623+ toJSON (EndpointConfig aliases) = JSON.object
624+ [ "Aliases" .= aliases
625+ ]
626+
627+-- | Data type for the NetworkingConfig section of the container settings
628+newtype NetworkingConfig = NetworkingConfig
629+ { endpointsConfig :: HM.HashMap Text EndpointConfig
630+ } deriving (Eq, Show)
631+
632+instance ToJSON NetworkingConfig where
633+ toJSON (NetworkingConfig endpointsConfig) = JSON.object
634+ [ "EndpointsConfig" .= endpointsConfig
635+ ]
614636
615637-- | Options used for creating a Container.
616638data CreateOpts = CreateOpts {
617639 containerConfig :: ContainerConfig
618640 , hostConfig :: HostConfig
641+ , networkingConfig :: Maybe NetworkingConfig
619642 } deriving (Eq, Show)
620643
621644instance ToJSON CreateOpts where
622− toJSON (CreateOpts cc hc) = do
645+ toJSON (CreateOpts cc hc nc) = do
623646 let ccJSON = toJSON cc
624647 let hcJSON = toJSON hc
625648 case ccJSON of
626649 JSON.Object (o :: HM.HashMap T.Text JSON.Value) -> do
627− JSON.Object $ HM.insert "HostConfig" hcJSON o
650+ let o1 = HM.insert "HostConfig" hcJSON o
651+ let o2 = case nc of
652+ Nothing -> o1
653+ Just _ -> HM.insert "NetworkingConfig" (toJSON nc) o1
654+ JSON.Object o2
628655 _ -> error "ContainerConfig is not an object." -- This should never happen.
629656
630657-- | Container configuration used for creating a container with sensible
@@ -706,7 +733,11 @@ defaultContainerResources = ContainerResources {
706733-- | Default create options when creating a container. You only need to
707734-- specify an image name and the rest is all sensible defaults.
708735defaultCreateOpts :: T.Text -> CreateOpts
709−defaultCreateOpts imageName = CreateOpts { containerConfig = defaultContainerConfig imageName, hostConfig = defaultHostConfig }
736+defaultCreateOpts imageName = CreateOpts
737+ { containerConfig = defaultContainerConfig imageName
738+ , hostConfig = defaultHostConfig
739+ , networkingConfig = Nothing
740+ }
710741
711742-- | Override the key sequence for detaching a container.
712743-- Format is a single character [a-Z] or ctrl-<value> where <value> is one of: a-z, @, ^, [, , or _.
tests/tests.hs+17−1
@@ -14,6 +14,7 @@ import Control.Monad (forM_)
1414import Control.Monad.IO.Class
1515import Control.Monad.Trans.Class (lift)
1616import qualified Data.Aeson as JSON
17+import Data.Aeson ((.=))
1718import Data.Aeson.Lens (key, _Array, _Null, _Object,
1819 _String, _Value)
1920import qualified Data.ByteString as B
@@ -92,7 +93,7 @@ testRunAndReadLog :: IO ()
9293testRunAndReadLog =
9394 runDocker $
9495 do let containerConfig = (defaultContainerConfig (testImageName <> ":latest")) {env = [EnvVar "TEST" "123"]}
95− containerId <- createContainer (CreateOpts containerConfig defaultHostConfig) Nothing
96+ containerId <- createContainer (CreateOpts containerConfig defaultHostConfig Nothing) Nothing
9697 c <- fromRight containerId
9798 status1 <- startContainer defaultStartOpts c
9899 _ <- inspectContainer c >>= fromRight
@@ -184,6 +185,20 @@ testEnvVarJson = testGroup "Testing EnvVar JSON" [testSampleEncode, testSampleDe
184185 testCase "Test fromJSON" $ assert $ (JSON.decode "\"cellar=door\"" :: Maybe EnvVar) ==
185186 Just (EnvVar "cellar" "door")
186187
188+testNetworkingConfigJson :: TestTree
189+testNetworkingConfigJson = testGroup "Testing NetworkingConfig JSON" [testSampleEncode]
190+ where
191+ testSampleEncode =
192+ let networkingConfig = NetworkingConfig $ HM.fromList [("custom-network", EndpointConfig ["cellar", "door"])]
193+ in testCase "Test toJSON" $ assert $ JSON.toJSON networkingConfig ==
194+ JSON.object
195+ [ "EndpointsConfig" .= JSON.object
196+ [ "custom-network" .= JSON.object
197+ [ "Aliases" .= (["cellar", "door"] :: [Text])
198+ ]
199+ ]
200+ ]
201+
187202integrationTests :: TestTree
188203integrationTests =
189204 testGroup
@@ -206,6 +221,7 @@ jsonTests =
206221 , testLogDriverOptionsJson
207222 , testEntrypointJson
208223 , testEnvVarJson
224+ , testNetworkingConfigJson
209225 ]
210226
211227setup :: IO ()