-- | Interrogate the magical URL http://169.254.169.254/latest/meta-data to
-- fill in an DataCenterAmazon.
discoverDataCenterAmazon :: Manager -> IO AmazonDataCenterInfo
discoverDataCenterAmazon manager =
    AmazonDataCenterInfo <$>
        getMeta "ami-id" <*>
        (read <$> getMeta "ami-launch-index") <*>
        getMeta "instance-id" <*>
        getMeta "instance-type" <*>
        getMeta "local-ipv4" <*>
        getMeta "placement/availability-zone" <*>
        getMeta "public-hostname" <*>
        getMeta "public-ipv4"
  where
    getMeta :: String -> IO String
    getMeta pathName = fromBS . responseBody <$> httpLbs metaRequest manager
      where
        metaRequest = fromJust . parseUrlThrow $ "http://169.254.169.254/latest/meta-data/" ++ pathName
        fromBS = T.unpack . decodeUtf8 . LBS.toStrict
discoverDataCenterAmazon :: Manager -> IO AmazonDataCenterInfo
discoverDataCenterAmazon manager =
    AmazonDataCenterInfo <$>
        getMeta "ami-id" <*>
        (read <$> getMeta "ami-launch-index") <*>
        getMeta "instance-id" <*>
        getMeta "instance-type" <*>
        getMeta "local-ipv4" <*>
        getMeta "placement/availability-zone" <*>
        getMeta "public-hostname" <*>
        getMeta "public-ipv4"
  where
    getMeta :: String -> IO String
    getMeta pathName = fromBS . responseBody <$> httpLbs metaRequest manager
      where
        metaRequest = fromJust . parseUrlThrow $ "http://169.254.169.254/latest/meta-data/" ++ pathName
        fromBS = T.unpack . decodeUtf8 . LBS.toStrict
discoverDataCenterAmazon manager =
    AmazonDataCenterInfo <$>
        getMeta "ami-id" <*>
        (read <$> getMeta "ami-launch-index") <*>
        getMeta "instance-id" <*>
        getMeta "instance-type" <*>
        getMeta "local-ipv4" <*>
        getMeta "placement/availability-zone" <*>
        getMeta "public-hostname" <*>
        getMeta "public-ipv4"
  where
    getMeta :: String -> IO String
    getMeta pathName = fromBS . responseBody <$> httpLbs metaRequest manager
      where
        metaRequest = fromJust . parseUrlThrow $ "http://169.254.169.254/latest/meta-data/" ++ pathName
        fromBS = T.unpack . decodeUtf8 . LBS.toStrict