-
Notifications
You must be signed in to change notification settings - Fork 1
/
AWS.hs
58 lines (54 loc) · 3.11 KB
/
AWS.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module AwsDdns.AWS where
import Data.Conduit.Binary (sinkLbs)
import Data.Text (Text)
import Data.Text.Lazy (strip, toStrict)
import qualified Data.Text.Lazy.Encoding as TE
import Lens.Micro ((&), (?~))
import Lens.Micro.Extras (view)
import Network.AWS (MonadAWS, send,
sinkBody)
import Network.AWS.Prelude (NonEmpty ((:|)))
import Network.AWS.Route53 (ChangeAction (Upsert),
RecordType (A),
ResourceId (..),
change,
changeBatch,
changeResourceRecordSets,
resourceRecord,
resourceRecordSet,
rrsResourceRecords,
rrsTTL)
import Network.AWS.Route53.ChangeResourceRecordSets (ChangeResourceRecordSetsResponse)
import Network.AWS.S3.GetObject (getObject,
gorsBody)
import Network.AWS.S3.Types (BucketName (..),
ObjectKey (..))
consNEmpty :: a -> NonEmpty a
consNEmpty = flip (:|) []
-- | Read the IP address out of bucketKey in bucketName.
-- TODO eventually it'll make sense to validate the IP
getIp
:: (MonadAWS m)
=> Text -- ^ The target bucket.
-> Text -- ^ The target key to read from bucket.
-> m Text
getIp bucketName bucketKey = do
val <- send $ getObject (BucketName bucketName) (ObjectKey bucketKey)
toStrict . strip . TE.decodeUtf8 <$> sinkBody (view gorsBody val) sinkLbs
-- | Update the given A record in the given hosted zone with the given IP.
-- TODO make TTL configurable
-- TODO return something more semantically relevant than the raw AWS response
updateResourceRecordSet
:: (MonadAWS m)
=> Text -- ^ The hosted zone id to change record sets in.
-> Text -- ^ The A record in the hosted zone to update.
-> Text -- ^ The IP address to point our A record to.
-> m ChangeResourceRecordSetsResponse
updateResourceRecordSet hostedZoneId recordSetDomain ipAddress =
send $ changeResourceRecordSets (ResourceId hostedZoneId) $ changeBatch
(consNEmpty $ change Upsert aRecord)
where
aRecord =
resourceRecordSet recordSetDomain A
& (rrsResourceRecords ?~ (consNEmpty . resourceRecord $ ipAddress))
. (rrsTTL ?~ 3600)