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]
(Int -> InstanceMetadataException -> ShowS)
-> (InstanceMetadataException -> [Char])
-> ([InstanceMetadataException] -> ShowS)
-> Show InstanceMetadataException
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> InstanceMetadataException -> ShowS
showsPrec :: Int -> InstanceMetadataException -> ShowS
$cshow :: InstanceMetadataException -> [Char]
show :: InstanceMetadataException -> [Char]
$cshowList :: [InstanceMetadataException] -> ShowS
showList :: [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 <- [Char] -> IO Request
forall (m :: * -> *). MonadThrow m => [Char] -> m Request
HTTP.parseUrlThrow ([Char]
"http://169.254.169.254/" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
p [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Char
'/' Char -> ShowS
forall a. a -> [a] -> [a]
: [Char]
x)
    Response ByteString -> ByteString
forall body. Response body -> body
HTTP.responseBody (Response ByteString -> ByteString)
-> IO (Response ByteString) -> IO ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Request -> Manager -> IO (Response ByteString)
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 = (ByteString -> [Char]) -> [ByteString] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map ByteString -> [Char]
BU.toString ([ByteString] -> [[Char]])
-> (ByteString -> [ByteString]) -> ByteString -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ByteString -> [ByteString]
B8.split Char
'\n' (ByteString -> [[Char]]) -> IO ByteString -> IO [[Char]]
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
                                      [] -> InstanceMetadataException -> IO ByteString
forall e a. (HasCallStack, Exception e) => e -> IO a
forall (m :: * -> *) e a.
(MonadThrow m, HasCallStack, 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