{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric      #-}
{-# LANGUAGE OverloadedStrings  #-}
{-# LANGUAGE RecordWildCards    #-}
{-# LANGUAGE TypeFamilies       #-}

{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-unused-binds   #-}
{-# OPTIONS_GHC -fno-warn-unused-matches #-}

-- Derived from AWS service descriptions, licensed under Apache 2.0.

-- |
-- Module      : Network.AWS.CloudWatchLogs.CreateExportTask
-- Copyright   : (c) 2013-2015 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay <brendan.g.hay@gmail.com>
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
--
-- Creates an 'ExportTask' which allows you to efficiently export data from
-- a Log Group to your Amazon S3 bucket.
--
-- This is an asynchronous call. If all the required information is
-- provided, this API will initiate an export task and respond with the
-- task Id. Once started, 'DescribeExportTasks' can be used to get the
-- status of an export task.
--
-- /See:/ <http://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_CreateExportTask.html AWS API Reference> for CreateExportTask.
module Network.AWS.CloudWatchLogs.CreateExportTask
    (
    -- * Creating a Request
      createExportTask
    , CreateExportTask
    -- * Request Lenses
    , cetDestinationPrefix
    , cetTaskName
    , cetLogStreamNamePrefix
    , cetLogGroupName
    , cetFrom
    , cetTo
    , cetDestination

    -- * Destructuring the Response
    , createExportTaskResponse
    , CreateExportTaskResponse
    -- * Response Lenses
    , cetrsTaskId
    , cetrsResponseStatus
    ) where

import           Network.AWS.CloudWatchLogs.Types
import           Network.AWS.CloudWatchLogs.Types.Product
import           Network.AWS.Prelude
import           Network.AWS.Request
import           Network.AWS.Response

-- | /See:/ 'createExportTask' smart constructor.
data CreateExportTask = CreateExportTask'
    { _cetDestinationPrefix   :: !(Maybe Text)
    , _cetTaskName            :: !(Maybe Text)
    , _cetLogStreamNamePrefix :: !(Maybe Text)
    , _cetLogGroupName        :: !Text
    , _cetFrom                :: !Nat
    , _cetTo                  :: !Nat
    , _cetDestination         :: !Text
    } deriving (Eq,Read,Show,Data,Typeable,Generic)

-- | Creates a value of 'CreateExportTask' with the minimum fields required to make a request.
--
-- Use one of the following lenses to modify other fields as desired:
--
-- * 'cetDestinationPrefix'
--
-- * 'cetTaskName'
--
-- * 'cetLogStreamNamePrefix'
--
-- * 'cetLogGroupName'
--
-- * 'cetFrom'
--
-- * 'cetTo'
--
-- * 'cetDestination'
createExportTask
    :: Text -- ^ 'cetLogGroupName'
    -> Natural -- ^ 'cetFrom'
    -> Natural -- ^ 'cetTo'
    -> Text -- ^ 'cetDestination'
    -> CreateExportTask
createExportTask pLogGroupName_ pFrom_ pTo_ pDestination_ =
    CreateExportTask'
    { _cetDestinationPrefix = Nothing
    , _cetTaskName = Nothing
    , _cetLogStreamNamePrefix = Nothing
    , _cetLogGroupName = pLogGroupName_
    , _cetFrom = _Nat # pFrom_
    , _cetTo = _Nat # pTo_
    , _cetDestination = pDestination_
    }

-- | Prefix that will be used as the start of Amazon S3 key for every object
-- exported. If not specified, this defaults to \'exportedlogs\'.
cetDestinationPrefix :: Lens' CreateExportTask (Maybe Text)
cetDestinationPrefix = lens _cetDestinationPrefix (\ s a -> s{_cetDestinationPrefix = a});

-- | The name of the export task.
cetTaskName :: Lens' CreateExportTask (Maybe Text)
cetTaskName = lens _cetTaskName (\ s a -> s{_cetTaskName = a});

-- | Will only export log streams that match the provided
-- logStreamNamePrefix. If you don\'t specify a value, no prefix filter is
-- applied.
cetLogStreamNamePrefix :: Lens' CreateExportTask (Maybe Text)
cetLogStreamNamePrefix = lens _cetLogStreamNamePrefix (\ s a -> s{_cetLogStreamNamePrefix = a});

-- | The name of the log group to export.
cetLogGroupName :: Lens' CreateExportTask Text
cetLogGroupName = lens _cetLogGroupName (\ s a -> s{_cetLogGroupName = a});

-- | A unix timestamp indicating the start time of the range for the request.
-- Events with a timestamp prior to this time will not be exported.
cetFrom :: Lens' CreateExportTask Natural
cetFrom = lens _cetFrom (\ s a -> s{_cetFrom = a}) . _Nat;

-- | A unix timestamp indicating the end time of the range for the request.
-- Events with a timestamp later than this time will not be exported.
cetTo :: Lens' CreateExportTask Natural
cetTo = lens _cetTo (\ s a -> s{_cetTo = a}) . _Nat;

-- | Name of Amazon S3 bucket to which the log data will be exported.
-- __NOTE: Only buckets in the same AWS region are supported__
cetDestination :: Lens' CreateExportTask Text
cetDestination = lens _cetDestination (\ s a -> s{_cetDestination = a});

instance AWSRequest CreateExportTask where
        type Rs CreateExportTask = CreateExportTaskResponse
        request = postJSON cloudWatchLogs
        response
          = receiveJSON
              (\ s h x ->
                 CreateExportTaskResponse' <$>
                   (x .?> "taskId") <*> (pure (fromEnum s)))

instance ToHeaders CreateExportTask where
        toHeaders
          = const
              (mconcat
                 ["X-Amz-Target" =#
                    ("Logs_20140328.CreateExportTask" :: ByteString),
                  "Content-Type" =#
                    ("application/x-amz-json-1.1" :: ByteString)])

instance ToJSON CreateExportTask where
        toJSON CreateExportTask'{..}
          = object
              (catMaybes
                 [("destinationPrefix" .=) <$> _cetDestinationPrefix,
                  ("taskName" .=) <$> _cetTaskName,
                  ("logStreamNamePrefix" .=) <$>
                    _cetLogStreamNamePrefix,
                  Just ("logGroupName" .= _cetLogGroupName),
                  Just ("from" .= _cetFrom), Just ("to" .= _cetTo),
                  Just ("destination" .= _cetDestination)])

instance ToPath CreateExportTask where
        toPath = const "/"

instance ToQuery CreateExportTask where
        toQuery = const mempty

-- | /See:/ 'createExportTaskResponse' smart constructor.
data CreateExportTaskResponse = CreateExportTaskResponse'
    { _cetrsTaskId         :: !(Maybe Text)
    , _cetrsResponseStatus :: !Int
    } deriving (Eq,Read,Show,Data,Typeable,Generic)

-- | Creates a value of 'CreateExportTaskResponse' with the minimum fields required to make a request.
--
-- Use one of the following lenses to modify other fields as desired:
--
-- * 'cetrsTaskId'
--
-- * 'cetrsResponseStatus'
createExportTaskResponse
    :: Int -- ^ 'cetrsResponseStatus'
    -> CreateExportTaskResponse
createExportTaskResponse pResponseStatus_ =
    CreateExportTaskResponse'
    { _cetrsTaskId = Nothing
    , _cetrsResponseStatus = pResponseStatus_
    }

-- | Id of the export task that got created.
cetrsTaskId :: Lens' CreateExportTaskResponse (Maybe Text)
cetrsTaskId = lens _cetrsTaskId (\ s a -> s{_cetrsTaskId = a});

-- | The response status code.
cetrsResponseStatus :: Lens' CreateExportTaskResponse Int
cetrsResponseStatus = lens _cetrsResponseStatus (\ s a -> s{_cetrsResponseStatus = a});
