on master help sign in
Refactors PortBindings
44dc48aDeni Bertovic
src/Docker/Client/Types.hs+10−23
@@ -57,7 +57,6 @@ module Docker.Client.Types (
57 , NetworkOptions(..)
57 , NetworkOptions(..)
58 , Mount(..)
58 , Mount(..)
59 , PortBinding(..)
59 , PortBinding(..)
60− , PortBindings(..)
61 , HostPort(..)
60 , HostPort(..)
62 , RetryCount
61 , RetryCount
63 , RestartPolicy(..)
62 , RestartPolicy(..)
@@ -443,7 +442,7 @@ data NetworkSettings = NetworkSettings {
443 , networkSettingsHairpinMode :: Bool
442 , networkSettingsHairpinMode :: Bool
444 , networkSettingsLinkLocalIPv6Address :: Text
443 , networkSettingsLinkLocalIPv6Address :: Text
445 , networkSettingsLinkLocalIPv6PrefixLen :: Int
444 , networkSettingsLinkLocalIPv6PrefixLen :: Int
446− , networkSettingsPorts :: PortBindings
445+ , networkSettingsPorts :: [PortBinding]
447 , networkSettingsSandboxKey :: Text
446 , networkSettingsSandboxKey :: Text
448 , networkSettingsSecondaryIPAddresses :: Maybe [Text] -- TODO: 1.24 spec is unclear
447 , networkSettingsSecondaryIPAddresses :: Maybe [Text] -- TODO: 1.24 spec is unclear
449 , networkSettingsSecondaryIPv6Addresses :: Maybe [Text] -- TODO: 1.24 spec is unclear
448 , networkSettingsSecondaryIPv6Addresses :: Maybe [Text] -- TODO: 1.24 spec is unclear
@@ -615,7 +614,7 @@ defaultHostConfig = HostConfig {
615 , containerIDFile=Nothing
614 , containerIDFile=Nothing
616 , logConfig=LogDriverConfig JsonFile Nothing
615 , logConfig=LogDriverConfig JsonFile Nothing
617 , networkMode=Bridge
616 , networkMode=Bridge
618− , portBindings=PortBindings []
617+ , portBindings=[]
619 , restartPolicy=RestartOff
618 , restartPolicy=RestartOff
620 , volumeDriver=Nothing
619 , volumeDriver=Nothing
621 , volumesFrom=[]
620 , volumesFrom=[]
@@ -914,13 +913,6 @@ instance FromJSON PortType where
914-- interface like `127.0.0.1`.
913-- interface like `127.0.0.1`.
915-- __NOTE__: We should disallow duplicate port bindings as the ToJSON
914-- __NOTE__: We should disallow duplicate port bindings as the ToJSON
916-- instance will only send the last one.
915-- instance will only send the last one.
917−newtype PortBindings = PortBindings [PortBinding]
918− deriving (Eq, Show)
919−
920−instance Monoid PortBindings where
921− mempty = PortBindings []
922− mappend (PortBindings p1) (PortBindings p2) = PortBindings (p1 ++ p2)
923−
924-- { <port>/<protocol>: [{ "HostPort": "<port>" }] }
916-- { <port>/<protocol>: [{ "HostPort": "<port>" }] }
925data PortBinding = PortBinding {
917data PortBinding = PortBinding {
926 containerPort :: Port
918 containerPort :: Port
@@ -971,19 +963,14 @@ addVolumeFrom vf c = c{hostConfig=hc{volumesFrom=oldvfs <> [vf]}}
971-- Example:
963-- Example:
972--
964--
973-- >>> let pb = PortBinding 80 TCP [HostPort "0.0.0.0" 8000]
965-- >>> let pb = PortBinding 80 TCP [HostPort "0.0.0.0" 8000]
974−-- >>> addPortBinding (defaultCreateOpts "nginx:latest") pb
966+-- >>> addPortBinding pb $ defaultCreateOpts "nginx:latest"
975addPortBinding :: PortBinding -> CreateOpts -> CreateOpts
967addPortBinding :: PortBinding -> CreateOpts -> CreateOpts
976−addPortBinding pb c = c{hostConfig=hc{portBindings=pbs <> PortBindings [pb]}}
968+addPortBinding pb c = c{hostConfig=hc{portBindings=pbs <> [pb]}}
977 where hc = hostConfig c
969 where hc = hostConfig c
978 pbs = portBindings $ hostConfig c
970 pbs = portBindings $ hostConfig c
979
971
980−instance ToJSON PortBinding where
981− toJSON (PortBinding {..}) = object [portAndType2Text containerPort portType .= hostPorts]
982−
983−instance FromJSON PortBindings where
972+instance FromJSON [PortBinding] where
984− parseJSON (JSON.Object o) = do
973+ parseJSON (JSON.Object o) = HM.foldlWithKey' f (return []) o
985− PortBindings <$> HM.foldlWithKey' f (return []) o
986−
987 where
974 where
988 f accM k v = case T.split (== '/') k of
975 f accM k v = case T.split (== '/') k of
989 [port', portType'] -> do
976 [port', portType'] -> do
@@ -996,9 +983,9 @@ instance FromJSON PortBindings where
996 fail "Could not parse PortBindings"
983 fail "Could not parse PortBindings"
997 parseJSON _ = fail "PortBindings is not an object"
984 parseJSON _ = fail "PortBindings is not an object"
998
985
999−instance ToJSON PortBindings where
986+instance ToJSON [PortBinding] where
1000− toJSON (PortBindings []) = JSON.Object HM.empty
987+ toJSON [] = JSON.Object HM.empty
1001− toJSON (PortBindings (p:ps)) = JSON.Object $ foldl f HM.empty (p:ps)
988+ toJSON (p:ps) = JSON.Object $ foldl f HM.empty (p:ps)
1002 where mKey p = portAndType2Text (containerPort p) (portType p)
989 where mKey p = portAndType2Text (containerPort p) (portType p)
1003 mVal p = toJSON $ hostPorts p
990 mVal p = toJSON $ hostPorts p
1004 f acc p = HM.insert (mKey p) (mVal p) acc
991 f acc p = HM.insert (mKey p) (mVal p) acc
@@ -1054,7 +1041,7 @@ data HostConfig = HostConfig
1054 , containerIDFile :: Maybe FilePath -- 1.24: Only in responses, not create
1041 , containerIDFile :: Maybe FilePath -- 1.24: Only in responses, not create
1055 , logConfig :: LogDriverConfig
1042 , logConfig :: LogDriverConfig
1056 , networkMode :: NetworkMode
1043 , networkMode :: NetworkMode
1057− , portBindings :: PortBindings
1044+ , portBindings :: [PortBinding]
1058 , restartPolicy :: RestartPolicy
1045 , restartPolicy :: RestartPolicy
1059 , volumeDriver :: Maybe Text
1046 , volumeDriver :: Maybe Text
1060 , volumesFrom :: [VolumeFrom]
1047 , volumesFrom :: [VolumeFrom]