Skip to content

Commit

Permalink
feat: report various IPv4/6 information for servers
Browse files Browse the repository at this point in the history
  • Loading branch information
vst committed Apr 30, 2024
1 parent d2cce89 commit 590cd72
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/Clompse/Cli.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import qualified Data.Text.IO as TIO
import qualified Options.Applicative as OA
import System.Exit (ExitCode (..))
import qualified Text.Layout.Table as Tab
import qualified Zamazingo.Net as Z.Net
import qualified Zamazingo.Text as Z.Text


Expand Down Expand Up @@ -196,6 +197,7 @@ doServerListConsole rs =
, Tab.numCol
, Tab.column Tab.expand Tab.left Tab.noAlign Tab.noCutMark
, Tab.column Tab.expand Tab.left Tab.noAlign Tab.noCutMark
, Tab.column Tab.expand Tab.left Tab.noAlign Tab.noCutMark
]
hs =
Tab.titlesH
Expand All @@ -211,6 +213,7 @@ doServerListConsole rs =
, "Disk"
, "Type"
, "Created"
, "IPv4"
]
mkRows i Programs.ServerListItem {..} =
Tab.rowG . fmap T.unpack $
Expand All @@ -226,6 +229,7 @@ doServerListConsole rs =
, maybe "<unknown>" formatIntegral _serverListItemDisk
, fromMaybe "<unknown>" _serverListItemType
, maybe "<unknown>" Z.Text.tshow _serverListItemCreatedAt
, T.intercalate "," (fmap Z.Net.ipv4ToText (_serverListItemIPv4Static <> _serverListItemIPv4Public))
]
rows = fmap (uncurry mkRows) (zip [1 :: Int ..] rs)
in putStrLn $ Tab.tableString cs Tab.unicodeS hs rows
Expand Down
67 changes: 53 additions & 14 deletions src/Clompse/Programs/ListServers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import qualified Clompse.Providers.Aws as Providers
import qualified Clompse.Providers.Aws as Providers.Aws
import qualified Clompse.Providers.Do as Providers.Do
import qualified Clompse.Providers.Hetzner as Providers.Hetzner
import Clompse.Types (Server)
import Clompse.Types (Server, ServerIpInfo (..))
import qualified Clompse.Types as Types
import qualified Control.Concurrent.Async.Pool as Async
import Control.Monad.Except (runExceptT)
Expand All @@ -25,6 +25,7 @@ import qualified Data.Time as Time
import qualified Data.Vector as V
import GHC.Generics (Generic)
import qualified System.IO
import qualified Zamazingo.Net as Z.Net
import qualified Zamazingo.Text as Z.Text


Expand Down Expand Up @@ -110,6 +111,12 @@ data ServerListItem = ServerListItem
, _serverListItemDisk :: !(Maybe Int32)
, _serverListItemType :: !(Maybe T.Text)
, _serverListItemCreatedAt :: !(Maybe Time.UTCTime)
, _serverListItemIPv4Static :: ![Z.Net.IPv4]
, _serverListItemIPv4Public :: ![Z.Net.IPv4]
, _serverListItemIPv4Private :: ![Z.Net.IPv4]
, _serverListItemIPv6Static :: ![Z.Net.IPv6]
, _serverListItemIPv6Public :: ![Z.Net.IPv6]
, _serverListItemIPv6Private :: ![Z.Net.IPv6]
}
deriving (Eq, Generic, Show)
deriving (Aeson.FromJSON, Aeson.ToJSON) via (ADC.Autodocodec ServerListItem)
Expand All @@ -133,23 +140,37 @@ instance ADC.HasCodec ServerListItem where
<*> ADC.optionalField "disk" "Disk of the server." ADC..= _serverListItemDisk
<*> ADC.optionalField "type" "Type of the server." ADC..= _serverListItemType
<*> ADC.optionalField "created_at" "Creation time of the server." ADC..= _serverListItemCreatedAt
<*> ADC.requiredField "ipv4_static" "Static IPv4 addresses." ADC..= _serverListItemIPv4Static
<*> ADC.requiredField "ipv4_public" "Public IPv4 addresses." ADC..= _serverListItemIPv4Public
<*> ADC.requiredField "ipv4_private" "Private IPv4 addresses." ADC..= _serverListItemIPv4Private
<*> ADC.requiredField "ipv6_static" "Ptatic IPv6 addresses." ADC..= _serverListItemIPv6Static
<*> ADC.requiredField "ipv6_public" "Public IPv6 addresses." ADC..= _serverListItemIPv6Public
<*> ADC.requiredField "ipv6_private" "Private IPv6 addresses." ADC..= _serverListItemIPv6Private


instance Cassava.ToNamedRecord ServerListItem where
toNamedRecord ServerListItem {..} =
Cassava.namedRecord
[ "profile" Cassava..= _serverListItemProfile
, "provider" Cassava..= Types.providerCode _serverListItemProvider
, "region" Cassava..= _serverListItemRegion
, "id" Cassava..= _serverListItemId
, "name" Cassava..= _serverListItemName
, "state" Cassava..= Types.stateCode _serverListItemState
, "cpu" Cassava..= _serverListItemCpu
, "ram" Cassava..= _serverListItemRam
, "disk" Cassava..= _serverListItemDisk
, "type" Cassava..= _serverListItemType
, "created_at" Cassava..= fmap Z.Text.tshow _serverListItemCreatedAt
]
let reportIp4s = filterMaybe (not . T.null) . T.intercalate "," . fmap Z.Net.ipv4ToText
reportIp6s = filterMaybe (not . T.null) . T.intercalate "," . fmap Z.Net.ipv6ToText
in Cassava.namedRecord
[ "profile" Cassava..= _serverListItemProfile
, "provider" Cassava..= Types.providerCode _serverListItemProvider
, "region" Cassava..= _serverListItemRegion
, "id" Cassava..= _serverListItemId
, "name" Cassava..= _serverListItemName
, "state" Cassava..= Types.stateCode _serverListItemState
, "cpu" Cassava..= _serverListItemCpu
, "ram" Cassava..= _serverListItemRam
, "disk" Cassava..= _serverListItemDisk
, "type" Cassava..= _serverListItemType
, "created_at" Cassava..= fmap Z.Text.tshow _serverListItemCreatedAt
, "ipv4_static" Cassava..= reportIp4s _serverListItemIPv4Static
, "ipv4_public" Cassava..= reportIp4s _serverListItemIPv4Public
, "ipv4_private" Cassava..= reportIp4s _serverListItemIPv4Private
, "ipv6_static" Cassava..= reportIp6s _serverListItemIPv6Static
, "ipv6_public" Cassava..= reportIp6s _serverListItemIPv6Public
, "ipv6_private" Cassava..= reportIp6s _serverListItemIPv6Private
]


instance Cassava.DefaultOrdered ServerListItem where
Expand All @@ -166,6 +187,12 @@ instance Cassava.DefaultOrdered ServerListItem where
, "disk"
, "type"
, "created_at"
, "ipv4_static"
, "ipv4_public"
, "ipv4_private"
, "ipv6_static"
, "ipv6_public"
, "ipv6_private"
]


Expand All @@ -186,4 +213,16 @@ toServerList ListServersResult {..} =
, _serverListItemDisk = _serverDisk
, _serverListItemType = _serverType
, _serverListItemCreatedAt = _serverCreatedAt
, _serverListItemIPv4Static = _serverIpInfoStaticIpv4 _serverIpInfo
, _serverListItemIPv4Public = _serverIpInfoPublicIpv4 _serverIpInfo
, _serverListItemIPv4Private = _serverIpInfoPrivateIpv4 _serverIpInfo
, _serverListItemIPv6Static = _serverIpInfoStaticIpv6 _serverIpInfo
, _serverListItemIPv6Public = _serverIpInfoPublicIpv6 _serverIpInfo
, _serverListItemIPv6Private = _serverIpInfoPrivateIpv6 _serverIpInfo
}


filterMaybe :: (a -> Bool) -> a -> Maybe a
filterMaybe p a
| p a = Just a
| otherwise = Nothing

0 comments on commit 590cd72

Please sign in to comment.