Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: list DNS records managed on Hetzner #57

Merged
merged 1 commit into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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