From 8a6054501ee21c2ee0de821586ba6f64c91b7185 Mon Sep 17 00:00:00 2001 From: Vehbi Sinan Tunalioglu Date: Fri, 17 May 2024 08:44:09 +0800 Subject: [PATCH] feat: list DNS records managed on AWS Lightsail --- src/Clompse/Programs/ListDomainRecords.hs | 10 ++++- src/Clompse/Providers/Aws.hs | 3 +- src/Clompse/Providers/Aws/ApiLightsail.hs | 47 +++++++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/Clompse/Programs/ListDomainRecords.hs b/src/Clompse/Programs/ListDomainRecords.hs index 54819f5..7634c52 100644 --- a/src/Clompse/Programs/ListDomainRecords.hs +++ b/src/Clompse/Programs/ListDomainRecords.hs @@ -8,6 +8,7 @@ module Clompse.Programs.ListDomainRecords where import qualified Autodocodec as ADC 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 Clompse.Types (DnsRecord (_dnsRecordProvider)) import qualified Clompse.Types as Types @@ -66,10 +67,15 @@ listDomainRecordsForCloudConnection => CloudConnection -> m [Types.DnsRecord] listDomainRecordsForCloudConnection (CloudConnectionAws conn) = do - eRecords <- runExceptT (Providers.Aws.listDnsRecordsRoute53 conn) - case eRecords of + eRecordsRoute53 <- runExceptT (Providers.Aws.listDnsRecordsRoute53 conn) + recordsRoute53 <- case eRecordsRoute53 of Left e -> _log (" ERROR (Route53 Domain Records): " <> Z.Text.tshow e) >> pure [] Right records -> pure records + eRecordsLightsail <- runExceptT (Providers.Aws.listDnsRecordsLightsail conn) + recordsLightsail <- case eRecordsLightsail of + Left e -> _log (" ERROR (Lightsail Domain Records): " <> Z.Text.tshow e) >> pure [] + Right records -> pure records + pure (recordsRoute53 <> recordsLightsail) listDomainRecordsForCloudConnection (CloudConnectionDo conn) = do eRecords <- runExceptT (Providers.Do.listDomainRecords conn) case eRecords of diff --git a/src/Clompse/Providers/Aws.hs b/src/Clompse/Providers/Aws.hs index f844862..c7ae71c 100644 --- a/src/Clompse/Providers/Aws.hs +++ b/src/Clompse/Providers/Aws.hs @@ -5,6 +5,7 @@ module Clompse.Providers.Aws ( AwsConnection (..), listBucketsLightsail, listBucketsS3, + listDnsRecordsLightsail, listDnsRecordsRoute53, listDomainsLightsail, listDomainsRoute53, @@ -13,7 +14,7 @@ module Clompse.Providers.Aws ( ) where import Clompse.Providers.Aws.ApiAws (listBucketsS3, listDnsRecordsRoute53, listDomainsRoute53, listServersEc2) -import Clompse.Providers.Aws.ApiLightsail (listBucketsLightsail, listDomainsLightsail, listServersLightsail) +import Clompse.Providers.Aws.ApiLightsail (listBucketsLightsail, listDnsRecordsLightsail, listDomainsLightsail, listServersLightsail) import Clompse.Providers.Aws.Connection (AwsConnection (..)) import Clompse.Providers.Aws.Error (AwsError (..)) diff --git a/src/Clompse/Providers/Aws/ApiLightsail.hs b/src/Clompse/Providers/Aws/ApiLightsail.hs index aefe891..09ffb44 100644 --- a/src/Clompse/Providers/Aws/ApiLightsail.hs +++ b/src/Clompse/Providers/Aws/ApiLightsail.hs @@ -69,6 +69,53 @@ listDomainsLightsail cfg = do } +listDnsRecordsLightsail + :: MonadIO m + => MonadError AwsError m + => AwsConnection + -> m [Types.DnsRecord] +listDnsRecordsLightsail cfg = do + env <- _envFromConnection cfg + let prog = Aws.send env Aws.Lightsail.newGetDomains + resIs <- liftIO . fmap (fromMaybe [] . L.view Aws.Lightsail.Lens.getDomainsResponse_domains) . Aws.runResourceT $ prog + pure $ concatMap mkEntries resIs + where + mkEntries b = + let name = fromMaybe "" (b L.^. Aws.Lightsail.Lens.domain_name) + entries = fromMaybe [] (b L.^. Aws.Lightsail.Lens.domain_domainEntries) + in fmap (mkEntry name) entries + mkEntry name e = + let _dnsRecordId = e L.^. Aws.Lightsail.Lens.domainEntry_id + _dnsRecordName = fromMaybe " b L.^. Aws.Route53.Lens.resourceRecordSet_weight +-- _dnsRecordFlags = Nothing +-- in Types.DnsRecord +-- { _dnsRecordProvider = Types.ProviderAws +-- , _dnsRecordDomain = dmn +-- , .. +-- } + -- * Data Definitions