Skip to content

Commit

Permalink
feat: list DNS records managed on Hetzner
Browse files Browse the repository at this point in the history
  • Loading branch information
vst committed May 18, 2024
1 parent c2723db commit 9239890
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/Clompse/Programs/ListDomainRecords.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Clompse.Config (CloudConnection (..), CloudProfile (..), Config (..))
import qualified Clompse.Providers.Aws.ApiAws as Providers.Aws
import qualified Clompse.Providers.Aws.ApiLightsail as Providers.Aws
import qualified Clompse.Providers.Do as Providers.Do
import qualified Clompse.Providers.Hetzner as Providers.Hetzner
import Clompse.Types (DnsRecord (_dnsRecordProvider))
import qualified Clompse.Types as Types
import qualified Control.Concurrent.Async.Pool as Async
Expand Down Expand Up @@ -81,8 +82,11 @@ listDomainRecordsForCloudConnection (CloudConnectionDo conn) = do
case eRecords of
Left e -> _log (" ERROR (DO Domain Records): " <> Z.Text.tshow e) >> pure []
Right records -> pure records
listDomainRecordsForCloudConnection (CloudConnectionHetzner _conn) = do
pure []
listDomainRecordsForCloudConnection (CloudConnectionHetzner conn) = do
eRecords <- runExceptT (Providers.Hetzner.listDnsRecords conn)
case eRecords of
Left e -> _log (" ERROR (Hetzner Domain Records): " <> Z.Text.tshow e) >> pure []
Right records -> pure records


_log :: MonadIO m => T.Text -> m ()
Expand Down
3 changes: 2 additions & 1 deletion src/Clompse/Providers/Hetzner.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
module Clompse.Providers.Hetzner (
HetznerError (..),
HetznerConnection (..),
listDnsRecords,
listDomains,
listServers,
) where

import Clompse.Providers.Hetzner.Api (listDomains, listServers)
import Clompse.Providers.Hetzner.Api (listDnsRecords, listDomains, listServers)
import Clompse.Providers.Hetzner.Connection (HetznerConnection (..))
import Clompse.Providers.Hetzner.Error (HetznerError (..))

31 changes: 30 additions & 1 deletion src/Clompse/Providers/Hetzner/Api.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TupleSections #-}

-- | This module provides functions to query remote Hetzner API and
-- convert responses to Clompse types.
Expand All @@ -10,7 +11,7 @@ import Clompse.Providers.Hetzner.Connection (HetznerConnection (..), hetznerConn
import Clompse.Providers.Hetzner.Error (HetznerError)
import qualified Clompse.Types as Types
import Control.Monad.Except (MonadError)
import Control.Monad.IO.Class (MonadIO)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.Int
import qualified Data.List as List
import qualified Data.List.NonEmpty as NE
Expand Down Expand Up @@ -57,6 +58,34 @@ listDomains conn = do
}


listDnsRecords
:: MonadIO m
=> MonadError HetznerError m
=> HetznerConnection
-> m [Types.DnsRecord]
listDnsRecords conn = do
zones <- maybe (pure []) (Hetzner.streamToList . Hetzner.streamPages . Hetzner.getZones) (hetznerConnectionTokenDns conn)
records <- concat <$> traverse (\Hetzner.Zone {..} -> liftIO $ fmap (zoneName,) <$> maybe (pure []) (`Hetzner.getRecords` Just zoneID) (hetznerConnectionTokenDns conn)) zones
pure $ fmap toDnsRecord records
where
toDnsRecord (zoneName, Hetzner.Record {..}) =
let (Hetzner.RecordID _dnsRecordId) = recordID
_dnsRecordType = Z.Text.tshow recordType
_dnsRecordName = recordName
_dnsRecordValue = recordValue
_dnsRecordPriority = Nothing
_dnsRecordPort = Nothing
_dnsRecordWeight = Nothing
_dnsRecordFlags = Nothing
_dnsRecordTtl = fromIntegral recordTTL
in Types.DnsRecord
{ _dnsRecordProvider = Types.ProviderHetzner
, _dnsRecordDomain = zoneName
, _dnsRecordId = Just _dnsRecordId
, ..
}


-- * Helpers


Expand Down

0 comments on commit 9239890

Please sign in to comment.