@@ -11,6 +11,9 @@ module Docker.Client.Types (
1111 , ImageID
1212 , fromImageID
1313 , toImageID
14+ , NetworkID
15+ , fromNetworkID
16+ , toNetworkID
1417 , Timeout(..)
1518 , StatusCode(..)
1619 , Signal(..)
@@ -23,7 +26,7 @@ module Docker.Client.Types (
2326 , ContainerPortInfo(..)
2427 , Container(..)
2528 , ContainerState(..)
26− , Status(..)
29+ , State(..)
2730 , Digest
2831 , Label(..)
2932 , Tag
@@ -37,12 +40,16 @@ module Docker.Client.Types (
3740 , DetachKeys(..)
3841 , StartOpts(..)
3942 , defaultStartOpts
40− , DeleteOpts(..)
41− , defaultDeleteOpts
43+ , ContainerDeleteOpts(..)
44+ , defaultContainerDeleteOpts
45+ , ImageDeleteOpts(..)
46+ , defaultImageDeleteOpts
4247 , Timestamp
4348 , TailLogOpt(..)
4449 , LogOpts(..)
4550 , defaultLogOpts
51+ , CreateNetworkOpts(..)
52+ , defaultCreateNetworkOpts
4653 , VolumePermission(..)
4754 , Bind(..)
4855 , Volume(..)
@@ -68,6 +75,8 @@ module Docker.Client.Types (
6875 , UTSMode(..)
6976 , HostConfig(..)
7077 , defaultHostConfig
78+ , NetworkingConfig(..)
79+ , EndpointConfig(..)
7180 , Ulimit(..)
7281 , ContainerResources(..)
7382 , defaultContainerResources
@@ -123,10 +132,13 @@ data Endpoint =
123132 | UnpauseContainerEndpoint ContainerID
124133 | ContainerLogsEndpoint LogOpts Bool ContainerID -- ^ Second argument (Bool) is whether to follow which is currently hardcoded to False.
125134 -- See note in 'Docker.Client.Api.getContainerLogs' for explanation why.
126− | DeleteContainerEndpoint DeleteOpts ContainerID
135+ | DeleteContainerEndpoint ContainerDeleteOpts ContainerID
127136 | InspectContainerEndpoint ContainerID
128137 | BuildImageEndpoint BuildOpts FilePath
129138 | CreateImageEndpoint T.Text Tag (Maybe T.Text) -- ^ Either pull an image from docker hub or imports an image from a tarball (or URL)
139+ | DeleteImageEndpoint ImageDeleteOpts ImageID
140+ | CreateNetworkEndpoint CreateNetworkOpts
141+ | RemoveNetworkEndpoint NetworkID
130142 deriving (Eq, Show)
131143
132144-- | We should newtype this
@@ -238,7 +250,7 @@ data ContainerDetails = ContainerDetails {
238250 , processLabel :: Text
239251 , resolveConfPath :: FilePath
240252 , restartCount :: Int
241− , state :: ContainerState
253+ , containerDetailsState :: ContainerState
242254 , mounts :: [Mount]
243255 }
244256 deriving (Eq, Show, Generic)
@@ -246,11 +258,11 @@ data ContainerDetails = ContainerDetails {
246258-- | Data type used for parsing the mount information from a container
247259-- list.
248260data Mount = Mount {
249− mountName :: Text
261+ mountName :: Maybe Text -- this is optional
250262 , mountSource :: FilePath
251263 , mountDestination :: FilePath
252− , mountDriver :: Text
253− , mountMode :: Maybe VolumePermission -- apparently this can be null
264+ , mountDriver :: Maybe Text
265+ -- , mountMode :: Maybe VolumePermission -- apparently this can be null
254266 , mountRW :: Bool
255267 , mountPropogation :: Text
256268 }
@@ -258,14 +270,14 @@ data Mount = Mount {
258270
259271instance FromJSON Mount where
260272 parseJSON (JSON.Object o) = do
261− name <- o .: "Name"
273+ name <- o .:? "Name"
262274 src <- o .: "Source"
263275 dest <- o .: "Destination"
264− driver <- o .: "Driver"
265− mode <- o .: "Mode"
276+ driver <- o .:? "Driver"
277+ -- mode <- o .: "Mode"
266278 rw <- o .: "RW"
267279 prop <- o .: "Propagation"
268− return $ Mount name src dest driver mode rw prop
280+ return $ Mount name src dest driver rw prop
269281 parseJSON _ = fail "Mount is not an object"
270282
271283-- | Data type used for parsing the container state from a list of
@@ -281,7 +293,7 @@ data ContainerState = ContainerState {
281293 , restarting :: Bool
282294 , running :: Bool
283295 , startedAt :: UTCTime
284− , status :: Status
296+ , state :: State
285297 }
286298 deriving (Eq, Show, Generic)
287299
@@ -511,7 +523,8 @@ data Container = Container
511523 , containerImageId :: ImageID
512524 , containerCommand :: Text
513525 , containerCreatedAt :: Int
514− , containerStatus :: Status
526+ , containerState :: State
527+ , containerStatus :: Maybe Text
515528 , containerPorts :: [ContainerPortInfo]
516529 , containerLabels :: [Label]
517530 , containerNetworks :: [Network]
@@ -519,13 +532,14 @@ data Container = Container
519532 } deriving (Show, Eq)
520533
521534instance FromJSON Container where
522− parseJSON (JSON.Object v) =
523− Container <$> (v .: "Id")
535+ parseJSON o@(JSON.Object v) =
536+ Container <$> parseJSON o
524537 <*> (v .: "Names")
525538 <*> (v .: "Image")
526− <*> (v .: "ImageID")
527− <*> (v .: "Command")
539+ <*> (v .: "ImageID") -- Doesn't exist anymore
540+ <*> (v .: "Command") -- Doesn't exist anymore
528541 <*> (v .: "Created")
542+ <*> (v .: "State")
529543 <*> (v .: "Status")
530544 <*> (v .: "Ports")
531545 <*> (v .: "Labels")
@@ -535,21 +549,20 @@ instance FromJSON Container where
535549 parseNetworks (JSON.Object v) =
536550 (v .: "Networks") >>= parseJSON
537551 parseNetworks _ = fail "Container NetworkSettings: Not a JSON object."
538−
539552 parseJSON _ = fail "Container: Not a JSON object."
540553
541−-- | Represents the status of the container life cycle.
542−data Status = Created | Restarting | Running | Paused | Exited | Dead
554+-- | Represents the state of the container life cycle.
555+data State = Created | Restarting | Running | Paused | Exited | Dead
543556 deriving (Eq, Show, Generic)
544557
545−instance FromJSON Status where
558+instance FromJSON State where
546559 parseJSON (JSON.String "running") = return Running
547560 parseJSON (JSON.String "created") = return Created -- Note: Guessing on the string values of these.
548561 parseJSON (JSON.String "restarting") = return Restarting
549562 parseJSON (JSON.String "paused") = return Paused
550563 parseJSON (JSON.String "exited") = return Exited
551564 parseJSON (JSON.String "dead") = return Dead
552− parseJSON _ = fail "Unknown Status"
565+ parseJSON s = fail $ "Unknown Status: " ++ show s
553566
554567-- | Alias for representing a RepoDigest. We could newtype this and add
555568-- some validation.
@@ -610,20 +623,46 @@ instance FromJSON Image where
610623 return $ DockerImage imageId imageCreated imageParentId imageRepoTags imageRepoDigests imageSize imageVirtualSize imageLabels
611624 parseJSON _ = fail "Failed to parse DockerImage."
612625
626+-- | Alias for Aliases.
627+type Alias = Text
628+
629+-- | EndpointsConfig is container configuration for a specific network
630+newtype EndpointConfig = EndpointConfig [Alias] deriving (Eq, Show)
631+
632+instance ToJSON EndpointConfig where
633+ toJSON (EndpointConfig aliases) = JSON.object
634+ [ "Aliases" .= aliases
635+ ]
636+
637+-- | Alias for endpoint name
638+type EndpointName = Text
639+
640+-- | Data type for the NetworkingConfig section of the container settings
641+newtype NetworkingConfig = NetworkingConfig
642+ { endpointsConfig :: HM.HashMap EndpointName EndpointConfig
643+ } deriving (Eq, Show)
644+
645+instance ToJSON NetworkingConfig where
646+ toJSON (NetworkingConfig endpointsConfig) = JSON.object
647+ [ "EndpointsConfig" .= endpointsConfig
648+ ]
613649
614650-- | Options used for creating a Container.
615651data CreateOpts = CreateOpts {
616− containerConfig :: ContainerConfig
617− , hostConfig :: HostConfig
652+ containerConfig :: ContainerConfig
653+ , hostConfig :: HostConfig
654+ , networkingConfig :: NetworkingConfig
618655 } deriving (Eq, Show)
619656
620657instance ToJSON CreateOpts where
621− toJSON (CreateOpts cc hc) = do
658+ toJSON (CreateOpts cc hc nc) = do
622659 let ccJSON = toJSON cc
623660 let hcJSON = toJSON hc
624661 case ccJSON of
625662 JSON.Object (o :: HM.HashMap T.Text JSON.Value) -> do
626− JSON.Object $ HM.insert "HostConfig" hcJSON o
663+ let o1 = HM.insert "HostConfig" hcJSON o
664+ let o2 = HM.insert "NetworkingConfig" (toJSON nc) o1
665+ JSON.Object o2
627666 _ -> error "ContainerConfig is not an object." -- This should never happen.
628667
629668-- | Container configuration used for creating a container with sensible
@@ -658,7 +697,7 @@ defaultHostConfig = HostConfig {
658697 binds=[]
659698 , containerIDFile=Nothing
660699 , logConfig=LogDriverConfig JsonFile []
661− , networkMode=Bridge
700+ , networkMode=NetworkBridge
662701 , portBindings=[]
663702 , restartPolicy=RestartOff
664703 , volumeDriver=Nothing
@@ -705,7 +744,11 @@ defaultContainerResources = ContainerResources {
705744-- | Default create options when creating a container. You only need to
706745-- specify an image name and the rest is all sensible defaults.
707746defaultCreateOpts :: T.Text -> CreateOpts
708−defaultCreateOpts imageName = CreateOpts { containerConfig = defaultContainerConfig imageName, hostConfig = defaultHostConfig }
747+defaultCreateOpts imageName = CreateOpts
748+ { containerConfig = defaultContainerConfig imageName
749+ , hostConfig = defaultHostConfig
750+ , networkingConfig = NetworkingConfig HM.empty
751+ }
709752
710753-- | Override the key sequence for detaching a container.
711754-- Format is a single character [a-Z] or ctrl-<value> where <value> is one of: a-z, @, ^, [, , or _.
@@ -719,7 +762,7 @@ defaultStartOpts :: StartOpts
719762defaultStartOpts = StartOpts { detachKeys = DefaultDetachKey }
720763
721764-- | Options for deleting a container.
722−data DeleteOpts = DeleteOpts {
765+data ContainerDeleteOpts = ContainerDeleteOpts {
723766 deleteVolumes :: Bool -- ^ Automatically cleanup volumes that the container created as well.
724767 , force :: Bool -- ^ If the container is still running force deletion anyway.
725768 } deriving (Eq, Show)
@@ -749,8 +792,15 @@ defaultBuildOpts nameTag = BuildOpts { buildImageName = nameTag
749792-- | Default options for deleting a container. Most of the time we DON'T
750793-- want to delete the container's volumes or force delete it if it's
751794-- running.
752−defaultDeleteOpts :: DeleteOpts
753−defaultDeleteOpts = DeleteOpts { deleteVolumes = False, force = False }
795+defaultContainerDeleteOpts :: ContainerDeleteOpts
796+defaultContainerDeleteOpts = ContainerDeleteOpts { deleteVolumes = False, force = False }
797+
798+-- | Image delete opts
799+data ImageDeleteOpts = ImageDeleteOpts deriving (Eq, Show)
800+
801+-- | Sane image deletion defaults
802+defaultImageDeleteOpts :: ImageDeleteOpts
803+defaultImageDeleteOpts = ImageDeleteOpts
754804
755805-- | Timestamp alias.
756806type Timestamp = Integer
@@ -778,6 +828,36 @@ defaultLogOpts = LogOpts { stdout = True
778828 , tail = All
779829 }
780830
831+-- | Options for creating a network
832+data CreateNetworkOpts = CreateNetworkOpts
833+ { createNetworkName :: Text -- ^ The network's name
834+ , createNetworkCheckDuplicate :: Bool -- ^ Check for networks with duplicate names.
835+ , createNetworkDriver :: Text -- ^ Name of the network driver plugin to use.
836+ , createNetworkInternal :: Bool -- ^ Restrict external access to the network.
837+ , createNetworkEnableIPv6 :: Bool -- ^ Enable IPv6 on the network.
838+ } deriving (Eq, Show)
839+
840+-- | Sensible defalut for create network options
841+defaultCreateNetworkOpts :: Text -> CreateNetworkOpts
842+defaultCreateNetworkOpts name =
843+ CreateNetworkOpts
844+ { createNetworkName = name
845+ , createNetworkCheckDuplicate = False
846+ , createNetworkDriver = "bridge"
847+ , createNetworkInternal = True
848+ , createNetworkEnableIPv6 = False
849+ }
850+
851+instance ToJSON CreateNetworkOpts where
852+ toJSON opts =
853+ object
854+ [ "Name" .= createNetworkName opts
855+ , "CheckDuplicate" .= createNetworkCheckDuplicate opts
856+ , "Driver" .= createNetworkDriver opts
857+ , "Internal" .= createNetworkInternal opts
858+ , "EnableIPv6" .= createNetworkEnableIPv6 opts
859+ ]
860+
781861-- TOOD: Add support for SELinux Volume labels (eg. "ro,z" or "ro/Z")
782862-- | Set permissions on volumes that you mount in the container.
783863data VolumePermission = ReadWrite | ReadOnly deriving (Eq, Show, Generic)
@@ -789,6 +869,8 @@ instance ToJSON VolumePermission where
789869instance FromJSON VolumePermission where
790870 parseJSON "rw" = return ReadWrite
791871 parseJSON "ro" = return ReadOnly
872+ parseJSON "RW" = return ReadWrite
873+ parseJSON "RO" = return ReadOnly
792874 parseJSON _ = fail "Failed to parse VolumePermission"
793875
794876-- | Used for marking a directory in the container as "exposed" hence
@@ -935,19 +1017,41 @@ instance FromJSON LogDriverConfig where
9351017 parseJSON _ = fail "LogDriverConfig is not an object"
9361018
9371019-- TODO: Add container:<name|id> mode
938−data NetworkMode = Bridge | Host | NetworkDisabled
1020+data NetworkMode = NetworkBridge | NetworkHost | NetworkDisabled | NetworkNamed Text
9391021 deriving (Eq, Show, Ord)
9401022
9411023instance FromJSON NetworkMode where
942− parseJSON (JSON.String "bridge") = return Bridge
943− parseJSON (JSON.String "host") = return Host -- Note: Guessing on these.
1024+ parseJSON (JSON.String "bridge") = return NetworkBridge
1025+ parseJSON (JSON.String "host") = return NetworkHost -- Note: Guessing on these.
9441026 parseJSON (JSON.String "none") = return NetworkDisabled
1027+ parseJSON (JSON.String n) = return $ NetworkNamed n
9451028 parseJSON _ = fail "Unknown NetworkMode"
9461029
9471030instance ToJSON NetworkMode where
948− toJSON Bridge = JSON.String "bridge"
949− toJSON Host = JSON.String "host"
950− toJSON NetworkDisabled = JSON.String "none"
1031+ toJSON NetworkBridge = JSON.String "bridge"
1032+ toJSON NetworkHost = JSON.String "host"
1033+ toJSON NetworkDisabled = JSON.String "none"
1034+ toJSON (NetworkNamed n) = JSON.String n
1035+
1036+newtype NetworkID = NetworkID Text
1037+ deriving (Eq, Show)
1038+
1039+-- | Used for extracting the id of the container from the newtype
1040+fromNetworkID :: NetworkID -> Text
1041+fromNetworkID (NetworkID t) = t
1042+
1043+-- | Used for parsing a Text value into a NetworkID.
1044+toNetworkID :: Text -> Maybe NetworkID
1045+toNetworkID t = Just $ NetworkID t
1046+
1047+instance FromJSON NetworkID where
1048+ parseJSON (JSON.Object o) = do
1049+ nid <- o .: "Id"
1050+ return $ NetworkID nid
1051+ parseJSON _ = fail "NetworkID is not an object."
1052+
1053+instance ToJSON NetworkID where
1054+ toJSON (NetworkID nid) = object ["Id" .= nid]
9511055
9521056data PortType = TCP | UDP deriving (Eq, Generic, Read, Ord)
9531057
@@ -1460,7 +1564,11 @@ instance FromJSON ContainerConfig where
14601564 macAddress labels stopSignal
14611565 parseJSON _ = fail "NetworkSettings is not an object."
14621566
1567+#if MIN_VERSION_base(4,13,0)
1568+parseIntegerText :: (MonadFail m) => Text -> m Integer
1569+#else
14631570parseIntegerText :: (Monad m) => Text -> m Integer
1571+#endif
14641572parseIntegerText t = case readMaybe $ T.unpack t of
14651573 Nothing ->
14661574 fail "Could not parse Integer"