@@ -0,0 +1,234 @@
1+{-# LANGUAGE DeriveFunctor #-}
2+{-# LANGUAGE FlexibleContexts #-}
3+{-# LANGUAGE OverloadedStrings #-}
4+{-# LANGUAGE RecordWildCards #-}
5+{-# LANGUAGE TemplateHaskell #-}
7+module Network.Docker.Types where
9+import Control.Applicative
10+import Control.Lens.TH
11+import Control.Lens.TH
15+import qualified Data.ByteString.Lazy as BS
17+import qualified Data.Map as Map
18+import qualified Data.Text as T
20+import Network.Docker.Options
21+import Network.Wreq.Types (Postable)
25+type ApiVersion = String
26+type Endpoint = String
31+type PortType = String
33+data DockerClientOpts = DockerClientOpts {
34+ apiVersion :: ApiVersion
39+data ResourceId = ResourceId { _id :: String } deriving (Show, Eq)
42+data DockerImage = DockerImage
43+ { _imageId :: ResourceId
44+ , _imageCreatedAt :: Int
45+ , _parentId :: Maybe String
48+ , _virtualSize :: Int
49+ } deriving (Show, Eq)
52+data DockerVersion = DockerVersion
54+ , _GitCommit :: String
55+ , _GoVersion :: String
57+ , _KernelVersion :: String
58+ } deriving (Show, Eq)
61+-- The JSON looks likes this:
62+-- "Ports":[{"IP":"0.0.0.0","PrivatePort":55555,"PublicPort":55555,"Type":"tcp"}]
64+data PortMap = PortMap
66+ , _privatePort :: Port
67+ , _publicPort :: Port
69+ } deriving (Show, Eq)
72+data DockerContainer = DockerContainer
73+ { _containerId :: ResourceId
74+ , _containerImageId :: ResourceId
76+ , _containerCreatedAt :: Int
79+ , _ports :: Maybe [PortMap]
80+ } deriving (Show, Eq)
83+data CreateContainerOpts = CreateContainerOpts
84+ { _hostname :: String
88+ , _attachStdin :: Bool
89+ , _attachStdout :: Bool
90+ , _attachStderr :: Bool
91+ , _portSpecs :: Maybe Object
95+ , _env :: Maybe Object
98+ , _volumes :: Maybe Object
99+ , _volumesFrom :: Maybe Object
100+ , _workingDir :: String
101+ , _disableNetwork :: Bool
102+ , _exposedPorts :: Maybe Object
105+defaultCreateOpts = CreateContainerOpts {
110+ , _attachStdin = False
111+ , _attachStdout = False
112+ , _attachStderr = False
113+ , _portSpecs = Nothing
115+ , _openStdin = False
116+ , _stdinOnce = False
120+ , _volumes = Nothing
121+ , _volumesFrom = Nothing
123+ , _disableNetwork = False
124+ , _exposedPorts = Nothing
127+instance ToJSON CreateContainerOpts where
128+ toJSON (CreateContainerOpts {..}) = object
129+ [ "Hostname" .= _hostname
131+ , "Memory" .= _memory
132+ , "MemorySwap" .= _memorySwap
133+ , "AttachStdin" .= _attachStdin
134+ , "AttachStdout" .= _attachStdout
135+ , "AttachStderr" .= _attachStderr
136+ , "PortSpecs" .= _portSpecs
138+ , "OpenStdin" .= _openStdin
139+ , "StdinOnce" .= _stdinOnce
143+ , "Volumes" .= _volumes
144+ , "VolumesFrom" .= _volumesFrom
145+ , "WrokingDir" .= _workingDir
146+ , "DisableNetwork" .= _disableNetwork
147+ , "ExposedPorts" .= _exposedPorts
150+-- data CreateContainerResponse = CreateContainerResponse
151+-- { _createdContainerId :: String
152+-- , _warnings :: Maybe [T.Text]
155+data StartContainerOpts = StartContainerOpts
156+ { _Binds :: [T.Text]
157+ , _Links :: [T.Text]
158+ , _LxcConf :: [(T.Text, T.Text)]
159+ , _PortBindings :: [(T.Text, [(T.Text, T.Text)])]
160+ , _PublishAllPorts :: Bool
161+ , _Privileged :: Bool
163+ , _VolumesFrom :: [T.Text]
166+defaultStartOpts = StartContainerOpts
170+ , _PortBindings = []
171+ , _PublishAllPorts = False
172+ , _Privileged = False
177+instance ToJSON StartContainerOpts where
178+ toJSON (StartContainerOpts {..}) = object
181+ , "LxcConf" .= _LxcConf
182+ , "PortBindings" .= _PortBindings
183+ , "PublishAllPorts" .= _PublishAllPorts
184+ , "Privileged" .= _Privileged
186+ , "VolumesFrom" .= _VolumesFrom
189+makeClassy ''ResourceId
191+-- makeLenses ''CreateContainerResponse
192+makeLenses ''DockerImage
193+makeLenses ''DockerContainer
194+makeLenses ''CreateContainerOpts
196+instance HasResourceId DockerImage where
197+ resourceId = imageId
199+instance FromJSON DockerImage where
200+ parseJSON (Object v) =
201+ DockerImage <$> ResourceId <$> (v .: "Id")
202+ <*> (v .: "Created")
203+ <*> (v .:? "ParentId")
204+ <*> (v .: "RepoTags")
206+ <*> (v .: "VirtualSize")
208+instance FromJSON PortMap where
209+ parseJSON (Object v) =
210+ PortMap <$> (v .: "IP")
211+ <*> (v .: "PrivatePort")
212+ <*> (v .: "PublicPort")
215+instance HasResourceId DockerContainer where
216+ resourceId = containerId
218+instance FromJSON DockerContainer where
219+ parseJSON (Object v) =
220+ DockerContainer <$> (ResourceId <$> (v .: "Id"))
221+ <*> (ResourceId <$> (v .: "Id"))
222+ <*> (v .: "Command")
223+ <*> (v .: "Created")
228+-- instance FromJSON CreateContainerResponse where
229+-- parseJSON (Object v) =
230+-- CreateContainerResponse <$> (v .: "Id")
231+-- <*> (v .:? "warnings")
233+$(deriveJSON dopts ''DockerVersion)