Skip to content

Instantly share code, notes, and snippets.

@rcook
Last active March 30, 2020 05:46
Show Gist options
  • Save rcook/b930582d2b389da857d84391f9d0bb19 to your computer and use it in GitHub Desktop.
Save rcook/b930582d2b389da857d84391f9d0bb19 to your computer and use it in GitHub Desktop.
AWS via Haskell Part 4 (SimpleDB)
name: aws-via-haskell
version: 0.1.0.0
homepage: https://github.com/rcook/aws-via-haskell#readme
license: MIT
license-file: LICENSE
author: Richard Cook
maintainer: [email protected]
copyright: 2017 Richard Cook
category: Command Line
build-type: Simple
cabal-version: >= 1.10
extra-source-files: README.md
source-repository head
type: git
location: https://github.com/rcook/aws-via-haskell.git
library
default-language: Haskell2010
if os(darwin)
cpp-options: -DOS_MACOS
if os(linux)
cpp-options: -DOS_LINUX
if os(windows)
cpp-options: -DOS_WINDOWS
hs-source-dirs: lib
ghc-options: -W -Wall -fwarn-incomplete-patterns -fwarn-unused-imports
build-depends: amazonka
, base >= 4.7 && < 5
, bytestring
, lens
, resourcet
, text
exposed-modules: AWSViaHaskell
, AWSViaHaskell.AWSInfo
, AWSViaHaskell.Util
executable dynamodb-app
default-language: Haskell2010
if os(darwin)
cpp-options: -DOS_MACOS
if os(linux)
cpp-options: -DOS_LINUX
if os(windows)
cpp-options: -DOS_WINDOWS
hs-source-dirs: dynamodb
main-is: Main.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N -W -Wall -fwarn-incomplete-patterns -fwarn-unused-imports
build-depends: amazonka
, amazonka-dynamodb
, aws-via-haskell
, base >= 4.7 && < 5
, lens
, text
, unordered-containers
executable s3-app
default-language: Haskell2010
if os(darwin)
cpp-options: -DOS_MACOS
if os(linux)
cpp-options: -DOS_LINUX
if os(windows)
cpp-options: -DOS_WINDOWS
hs-source-dirs: s3
main-is: Main.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N -W -Wall -fwarn-incomplete-patterns -fwarn-unused-imports
build-depends: amazonka
, amazonka-s3
, aws-via-haskell
, base >= 4.7 && < 5
, bytestring
, conduit-extra
, lens
, resourcet
, text
executable sdb-app
default-language: Haskell2010
if os(darwin)
cpp-options: -DOS_MACOS
if os(linux)
cpp-options: -DOS_LINUX
if os(windows)
cpp-options: -DOS_WINDOWS
hs-source-dirs: sdb
main-is: Main.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N -W -Wall -fwarn-incomplete-patterns -fwarn-unused-imports
build-depends: amazonka
, amazonka-sdb
, aws-via-haskell
, base >= 4.7 && < 5
, lens
, text
executable sqs-app
default-language: Haskell2010
if os(darwin)
cpp-options: -DOS_MACOS
if os(linux)
cpp-options: -DOS_LINUX
if os(windows)
cpp-options: -DOS_WINDOWS
hs-source-dirs: sqs
main-is: Main.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N -W -Wall -fwarn-incomplete-patterns -fwarn-unused-imports
build-depends: amazonka
, amazonka-sqs
, aws-via-haskell
, base >= 4.7 && < 5
, lens
, text
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RecordWildCards #-}
module AWSViaHaskell.AWSInfo
( AWSInfo(..)
, LoggingState(..)
, ServiceEndpoint(..)
, getAWSInfo
, withAWS
, withAWS'
) where
import Control.Lens ((<&>), set)
import Control.Monad.Trans.AWS
( AWST'
, runAWST
)
import Control.Monad.Trans.Resource
( MonadBaseControl
, ResourceT
)
import Data.ByteString (ByteString)
import Control.Monad.Trans.AWS
( Credentials(..)
, Env
, LogLevel(..)
, Region(..)
, Service
, envLogger
, newEnv
, newLogger
, reconfigure
, runResourceT
, setEndpoint
, within
)
import System.IO (stdout)
type HostName = ByteString
type Port = Int
data AWSInfo = AWSInfo
{ env :: Env
, region :: Region
, service :: Service
}
data LoggingState = LoggingEnabled | LoggingDisabled
data ServiceEndpoint = AWS Region | Local HostName Port
getAWSInfo :: LoggingState -> ServiceEndpoint -> Service -> IO AWSInfo
getAWSInfo loggingState serviceEndpoint service = do
e <- getEnv loggingState
let (r, s) = regionService serviceEndpoint service
return $ AWSInfo e r s
where
-- Standard discovery mechanism for credentials, log to standard output
getEnv LoggingEnabled = do
logger <- newLogger Debug stdout
newEnv Discover <&> set envLogger logger
-- Standard discovery mechanism for credentials, no logging
getEnv LoggingDisabled = newEnv Discover
-- Run against a DynamoDB instance running on AWS in specified region
regionService (AWS region) s = (region, s)
-- Run against a local DynamoDB instance on a given host and port
regionService (Local hostName port) s = (NorthVirginia, setEndpoint False hostName port s)
withAWS :: MonadBaseControl IO m =>
AWST' Env (ResourceT m) a
-> AWSInfo
-> m a
withAWS action AWSInfo{..} =
runResourceT . runAWST env . within region $ do
reconfigure service action
withAWS' :: MonadBaseControl IO m =>
AWSInfo
-> AWST' Env (ResourceT m) a
-> m a
withAWS' = flip withAWS
The MIT License (MIT)
Copyright (c) 2017 Richard Cook
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
{-# LANGUAGE OverloadedStrings #-}
module Main (main) where
import AWSViaHaskell
( AWSInfo
, LoggingState(..)
, ServiceEndpoint(..)
, getAWSInfo
, withAWS
)
import Control.Lens ((&), (^.), (.~))
import Control.Monad (forM_, void)
import Data.Monoid ((<>))
import Data.Text (Text)
import qualified Data.Text.IO as Text
import Network.AWS (send)
import Network.AWS.SDB
( aName
, aValue
, createDomain
, garsAttributes
, getAttributes
, ldrsDomainNames
, listDomains
, paAttributes
, putAttributes
, replaceableAttribute
, sdb
)
newtype DomainName = DomainName Text deriving Show
newtype ItemName = ItemName Text deriving Show
doCreateDomainIfNotExists :: DomainName -> AWSInfo -> IO ()
doCreateDomainIfNotExists (DomainName s) = withAWS $ do
void $ send $ createDomain s
doListDomains :: AWSInfo -> IO [DomainName]
doListDomains = withAWS $ do
result <- send $ listDomains
return [ DomainName s | s <- result ^. ldrsDomainNames ]
doPutAttributes :: DomainName -> ItemName -> [(Text, Text)] -> AWSInfo -> IO ()
doPutAttributes (DomainName sDN) (ItemName sIN) attrs = withAWS $ do
void $ send $ putAttributes sDN sIN
& paAttributes .~ map (uncurry replaceableAttribute) attrs
doGetAttributes :: DomainName -> ItemName -> AWSInfo -> IO [(Text, Text)]
doGetAttributes (DomainName sDN) (ItemName sIN) = withAWS $ do
result <- send $ getAttributes sDN sIN
return [ (attr ^. aName, attr ^. aValue) | attr <- result ^. garsAttributes ]
main :: IO ()
main = do
-- Default port for simpledb-dev2
awsInfo <- getAWSInfo LoggingDisabled (Local "localhost" 8080) sdb
let domainName = DomainName "my-domain"
itemName = ItemName "my-item"
putStrLn "CreateDomain"
doCreateDomainIfNotExists domainName awsInfo
putStrLn "ListDomains"
domainNames <- doListDomains awsInfo
forM_ domainNames $ \dn ->
putStrLn $ " " <> show dn
putStrLn "PutAttributes"
doPutAttributes domainName itemName [ ("1", "aaa"), ("2", "bbb") ] awsInfo
putStrLn "GetAttributes"
attrs <- doGetAttributes domainName itemName awsInfo
forM_ attrs $ \(k, v) ->
Text.putStrLn $ " " <> k <> " = " <> v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment