module Aws.Ec2.InstanceMetadata where

import           Control.Applicative
import           Control.Exception
import           Control.Monad.Trans.Resource (throwM)
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.Char8 as B8
import           Data.ByteString.Lazy.UTF8 as BU
import           Data.Typeable
import qualified Network.HTTP.Conduit as HTTP
import           Prelude

data InstanceMetadataException
  = MetadataNotFound String
  deriving (Int -> InstanceMetadataException -> ShowS
[InstanceMetadataException] -> ShowS
InstanceMetadataException -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [InstanceMetadataException] -> ShowS
$cshowList :: [InstanceMetadataException] -> ShowS
show :: InstanceMetadataException -> [Char]
$cshow :: InstanceMetadataException -> [Char]
showsPrec :: Int -> InstanceMetadataException -> ShowS
$cshowsPrec :: Int -> InstanceMetadataException -> ShowS
Show, Typeable)

instance Exception InstanceMetadataException

getInstanceMetadata :: HTTP.Manager -> String -> String -> IO L.ByteString
getInstanceMetadata :: Manager -> [Char] -> [Char] -> IO ByteString
getInstanceMetadata Manager
mgr [Char]
p [Char]
x = do
    Request
req <- forall (m :: * -> *). MonadThrow m => [Char] -> m Request
HTTP.parseUrlThrow ([Char]
"http://169.254.169.254/" forall a. [a] -> [a] -> [a]
++ [Char]
p forall a. [a] -> [a] -> [a]
++ Char
'/' forall a. a -> [a] -> [a]
: [Char]
x)
    forall body. Response body -> body
HTTP.responseBody forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *).
MonadIO m =>
Request -> Manager -> m (Response ByteString)
HTTP.httpLbs Request
req Manager
mgr

getInstanceMetadataListing :: HTTP.Manager -> String -> IO [String]
getInstanceMetadataListing :: Manager -> [Char] -> IO [[Char]]
getInstanceMetadataListing Manager
mgr [Char]
p = forall a b. (a -> b) -> [a] -> [b]
map ByteString -> [Char]
BU.toString forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ByteString -> [ByteString]
B8.split Char
'\n' forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Manager -> [Char] -> [Char] -> IO ByteString
getInstanceMetadata Manager
mgr [Char]
p [Char]
""

getInstanceMetadataFirst :: HTTP.Manager -> String -> IO L.ByteString
getInstanceMetadataFirst :: Manager -> [Char] -> IO ByteString
getInstanceMetadataFirst Manager
mgr [Char]
p = do [[Char]]
listing <- Manager -> [Char] -> IO [[Char]]
getInstanceMetadataListing Manager
mgr [Char]
p
                                    case [[Char]]
listing of
                                      [] -> forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM ([Char] -> InstanceMetadataException
MetadataNotFound [Char]
p)
                                      ([Char]
x:[[Char]]
_) -> Manager -> [Char] -> [Char] -> IO ByteString
getInstanceMetadata Manager
mgr [Char]
p [Char]
x

getInstanceMetadataOrFirst :: HTTP.Manager -> String -> Maybe String -> IO L.ByteString
getInstanceMetadataOrFirst :: Manager -> [Char] -> Maybe [Char] -> IO ByteString
getInstanceMetadataOrFirst Manager
mgr [Char]
p (Just [Char]
x) = Manager -> [Char] -> [Char] -> IO ByteString
getInstanceMetadata Manager
mgr [Char]
p [Char]
x
getInstanceMetadataOrFirst Manager
mgr [Char]
p Maybe [Char]
Nothing = Manager -> [Char] -> IO ByteString
getInstanceMetadataFirst Manager
mgr [Char]
p