{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -fno-warn-unused-binds #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-unused-matches #-}

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

-- |
-- Module      : Amazonka.Glacier.UploadArchive
-- Copyright   : (c) 2013-2021 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay <brendan.g.hay+amazonka@gmail.com>
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
--
-- This operation adds an archive to a vault. This is a synchronous
-- operation, and for a successful upload, your data is durably persisted.
-- Amazon S3 Glacier returns the archive ID in the @x-amz-archive-id@
-- header of the response.
--
-- You must use the archive ID to access your data in Amazon S3 Glacier.
-- After you upload an archive, you should save the archive ID returned so
-- that you can retrieve or delete the archive later. Besides saving the
-- archive ID, you can also index it and give it a friendly name to allow
-- for better searching. You can also use the optional archive description
-- field to specify how the archive is referred to in an external index of
-- archives, such as you might create in Amazon DynamoDB. You can also get
-- the vault inventory to obtain a list of archive IDs in a vault. For more
-- information, see InitiateJob.
--
-- You must provide a SHA256 tree hash of the data you are uploading. For
-- information about computing a SHA256 tree hash, see
-- <https://docs.aws.amazon.com/amazonglacier/latest/dev/checksum-calculations.html Computing Checksums>.
--
-- You can optionally specify an archive description of up to 1,024
-- printable ASCII characters. You can get the archive description when you
-- either retrieve the archive or get the vault inventory. For more
-- information, see InitiateJob. Amazon Glacier does not interpret the
-- description in any way. An archive description does not need to be
-- unique. You cannot use the description to retrieve or sort the archive
-- list.
--
-- Archives are immutable. After you upload an archive, you cannot edit the
-- archive or its description.
--
-- An AWS account has full permission to perform all operations (actions).
-- However, AWS Identity and Access Management (IAM) users don\'t have any
-- permissions by default. You must grant them explicit permission to
-- perform specific actions. For more information, see
-- <https://docs.aws.amazon.com/amazonglacier/latest/dev/using-iam-with-amazon-glacier.html Access Control Using AWS Identity and Access Management (IAM)>.
--
-- For conceptual information and underlying REST API, see
-- <https://docs.aws.amazon.com/amazonglacier/latest/dev/uploading-an-archive.html Uploading an Archive in Amazon Glacier>
-- and
-- <https://docs.aws.amazon.com/amazonglacier/latest/dev/api-archive-post.html Upload Archive>
-- in the /Amazon Glacier Developer Guide/.
module Amazonka.Glacier.UploadArchive
  ( -- * Creating a Request
    UploadArchive (..),
    newUploadArchive,

    -- * Request Lenses
    uploadArchive_checksum,
    uploadArchive_archiveDescription,
    uploadArchive_vaultName,
    uploadArchive_accountId,
    uploadArchive_body,

    -- * Destructuring the Response
    ArchiveCreationOutput (..),
    newArchiveCreationOutput,

    -- * Response Lenses
    archiveCreationOutput_archiveId,
    archiveCreationOutput_checksum,
    archiveCreationOutput_location,
  )
where

import qualified Amazonka.Core as Core
import Amazonka.Glacier.Types
import qualified Amazonka.Lens as Lens
import qualified Amazonka.Prelude as Prelude
import qualified Amazonka.Request as Request
import qualified Amazonka.Response as Response

-- | Provides options to add an archive to a vault.
--
-- /See:/ 'newUploadArchive' smart constructor.
data UploadArchive = UploadArchive'
  { -- | The SHA256 tree hash of the data being uploaded.
    UploadArchive -> Maybe Text
checksum :: Prelude.Maybe Prelude.Text,
    -- | The optional description of the archive you are uploading.
    UploadArchive -> Maybe Text
archiveDescription :: Prelude.Maybe Prelude.Text,
    -- | The name of the vault.
    UploadArchive -> Text
vaultName :: Prelude.Text,
    -- | The @AccountId@ value is the AWS account ID of the account that owns the
    -- vault. You can either specify an AWS account ID or optionally a single
    -- \'@-@\' (hyphen), in which case Amazon S3 Glacier uses the AWS account
    -- ID associated with the credentials used to sign the request. If you use
    -- an account ID, do not include any hyphens (\'-\') in the ID.
    UploadArchive -> Text
accountId :: Prelude.Text,
    -- | The data to upload.
    UploadArchive -> HashedBody
body :: Core.HashedBody
  }
  deriving (Int -> UploadArchive -> ShowS
[UploadArchive] -> ShowS
UploadArchive -> String
(Int -> UploadArchive -> ShowS)
-> (UploadArchive -> String)
-> ([UploadArchive] -> ShowS)
-> Show UploadArchive
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [UploadArchive] -> ShowS
$cshowList :: [UploadArchive] -> ShowS
show :: UploadArchive -> String
$cshow :: UploadArchive -> String
showsPrec :: Int -> UploadArchive -> ShowS
$cshowsPrec :: Int -> UploadArchive -> ShowS
Prelude.Show, (forall x. UploadArchive -> Rep UploadArchive x)
-> (forall x. Rep UploadArchive x -> UploadArchive)
-> Generic UploadArchive
forall x. Rep UploadArchive x -> UploadArchive
forall x. UploadArchive -> Rep UploadArchive x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep UploadArchive x -> UploadArchive
$cfrom :: forall x. UploadArchive -> Rep UploadArchive x
Prelude.Generic)

-- |
-- Create a value of 'UploadArchive' with all optional fields omitted.
--
-- Use <https://hackage.haskell.org/package/generic-lens generic-lens> or <https://hackage.haskell.org/package/optics optics> to modify other optional fields.
--
-- The following record fields are available, with the corresponding lenses provided
-- for backwards compatibility:
--
-- 'checksum', 'uploadArchive_checksum' - The SHA256 tree hash of the data being uploaded.
--
-- 'archiveDescription', 'uploadArchive_archiveDescription' - The optional description of the archive you are uploading.
--
-- 'vaultName', 'uploadArchive_vaultName' - The name of the vault.
--
-- 'accountId', 'uploadArchive_accountId' - The @AccountId@ value is the AWS account ID of the account that owns the
-- vault. You can either specify an AWS account ID or optionally a single
-- \'@-@\' (hyphen), in which case Amazon S3 Glacier uses the AWS account
-- ID associated with the credentials used to sign the request. If you use
-- an account ID, do not include any hyphens (\'-\') in the ID.
--
-- 'body', 'uploadArchive_body' - The data to upload.
newUploadArchive ::
  -- | 'vaultName'
  Prelude.Text ->
  -- | 'accountId'
  Prelude.Text ->
  -- | 'body'
  Core.HashedBody ->
  UploadArchive
newUploadArchive :: Text -> Text -> HashedBody -> UploadArchive
newUploadArchive Text
pVaultName_ Text
pAccountId_ HashedBody
pBody_ =
  UploadArchive' :: Maybe Text
-> Maybe Text -> Text -> Text -> HashedBody -> UploadArchive
UploadArchive'
    { $sel:checksum:UploadArchive' :: Maybe Text
checksum = Maybe Text
forall a. Maybe a
Prelude.Nothing,
      $sel:archiveDescription:UploadArchive' :: Maybe Text
archiveDescription = Maybe Text
forall a. Maybe a
Prelude.Nothing,
      $sel:vaultName:UploadArchive' :: Text
vaultName = Text
pVaultName_,
      $sel:accountId:UploadArchive' :: Text
accountId = Text
pAccountId_,
      $sel:body:UploadArchive' :: HashedBody
body = HashedBody
pBody_
    }

-- | The SHA256 tree hash of the data being uploaded.
uploadArchive_checksum :: Lens.Lens' UploadArchive (Prelude.Maybe Prelude.Text)
uploadArchive_checksum :: (Maybe Text -> f (Maybe Text)) -> UploadArchive -> f UploadArchive
uploadArchive_checksum = (UploadArchive -> Maybe Text)
-> (UploadArchive -> Maybe Text -> UploadArchive)
-> Lens UploadArchive UploadArchive (Maybe Text) (Maybe Text)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\UploadArchive' {Maybe Text
checksum :: Maybe Text
$sel:checksum:UploadArchive' :: UploadArchive -> Maybe Text
checksum} -> Maybe Text
checksum) (\s :: UploadArchive
s@UploadArchive' {} Maybe Text
a -> UploadArchive
s {$sel:checksum:UploadArchive' :: Maybe Text
checksum = Maybe Text
a} :: UploadArchive)

-- | The optional description of the archive you are uploading.
uploadArchive_archiveDescription :: Lens.Lens' UploadArchive (Prelude.Maybe Prelude.Text)
uploadArchive_archiveDescription :: (Maybe Text -> f (Maybe Text)) -> UploadArchive -> f UploadArchive
uploadArchive_archiveDescription = (UploadArchive -> Maybe Text)
-> (UploadArchive -> Maybe Text -> UploadArchive)
-> Lens UploadArchive UploadArchive (Maybe Text) (Maybe Text)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\UploadArchive' {Maybe Text
archiveDescription :: Maybe Text
$sel:archiveDescription:UploadArchive' :: UploadArchive -> Maybe Text
archiveDescription} -> Maybe Text
archiveDescription) (\s :: UploadArchive
s@UploadArchive' {} Maybe Text
a -> UploadArchive
s {$sel:archiveDescription:UploadArchive' :: Maybe Text
archiveDescription = Maybe Text
a} :: UploadArchive)

-- | The name of the vault.
uploadArchive_vaultName :: Lens.Lens' UploadArchive Prelude.Text
uploadArchive_vaultName :: (Text -> f Text) -> UploadArchive -> f UploadArchive
uploadArchive_vaultName = (UploadArchive -> Text)
-> (UploadArchive -> Text -> UploadArchive)
-> Lens UploadArchive UploadArchive Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\UploadArchive' {Text
vaultName :: Text
$sel:vaultName:UploadArchive' :: UploadArchive -> Text
vaultName} -> Text
vaultName) (\s :: UploadArchive
s@UploadArchive' {} Text
a -> UploadArchive
s {$sel:vaultName:UploadArchive' :: Text
vaultName = Text
a} :: UploadArchive)

-- | The @AccountId@ value is the AWS account ID of the account that owns the
-- vault. You can either specify an AWS account ID or optionally a single
-- \'@-@\' (hyphen), in which case Amazon S3 Glacier uses the AWS account
-- ID associated with the credentials used to sign the request. If you use
-- an account ID, do not include any hyphens (\'-\') in the ID.
uploadArchive_accountId :: Lens.Lens' UploadArchive Prelude.Text
uploadArchive_accountId :: (Text -> f Text) -> UploadArchive -> f UploadArchive
uploadArchive_accountId = (UploadArchive -> Text)
-> (UploadArchive -> Text -> UploadArchive)
-> Lens UploadArchive UploadArchive Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\UploadArchive' {Text
accountId :: Text
$sel:accountId:UploadArchive' :: UploadArchive -> Text
accountId} -> Text
accountId) (\s :: UploadArchive
s@UploadArchive' {} Text
a -> UploadArchive
s {$sel:accountId:UploadArchive' :: Text
accountId = Text
a} :: UploadArchive)

-- | The data to upload.
uploadArchive_body :: Lens.Lens' UploadArchive Core.HashedBody
uploadArchive_body :: (HashedBody -> f HashedBody) -> UploadArchive -> f UploadArchive
uploadArchive_body = (UploadArchive -> HashedBody)
-> (UploadArchive -> HashedBody -> UploadArchive)
-> Lens UploadArchive UploadArchive HashedBody HashedBody
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\UploadArchive' {HashedBody
body :: HashedBody
$sel:body:UploadArchive' :: UploadArchive -> HashedBody
body} -> HashedBody
body) (\s :: UploadArchive
s@UploadArchive' {} HashedBody
a -> UploadArchive
s {$sel:body:UploadArchive' :: HashedBody
body = HashedBody
a} :: UploadArchive)

instance Core.AWSRequest UploadArchive where
  type
    AWSResponse UploadArchive =
      ArchiveCreationOutput
  request :: UploadArchive -> Request UploadArchive
request =
    ByteString -> Request UploadArchive -> Request UploadArchive
forall a. ByteString -> Request a -> Request a
Request.glacierVersionHeader (Service -> ByteString
Core._serviceVersion Service
defaultService)
      (Request UploadArchive -> Request UploadArchive)
-> (UploadArchive -> Request UploadArchive)
-> UploadArchive
-> Request UploadArchive
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. Service -> UploadArchive -> Request UploadArchive
forall a. (ToRequest a, ToBody a) => Service -> a -> Request a
Request.postBody Service
defaultService
  response :: Logger
-> Service
-> Proxy UploadArchive
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse UploadArchive)))
response =
    (Int
 -> ResponseHeaders
 -> ()
 -> Either String (AWSResponse UploadArchive))
-> Logger
-> Service
-> Proxy UploadArchive
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse UploadArchive)))
forall (m :: * -> *) a.
MonadResource m =>
(Int -> ResponseHeaders -> () -> Either String (AWSResponse a))
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
Response.receiveEmpty
      ( \Int
s ResponseHeaders
h ()
x ->
          Maybe Text -> Maybe Text -> Maybe Text -> ArchiveCreationOutput
ArchiveCreationOutput'
            (Maybe Text -> Maybe Text -> Maybe Text -> ArchiveCreationOutput)
-> Either String (Maybe Text)
-> Either
     String (Maybe Text -> Maybe Text -> ArchiveCreationOutput)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (ResponseHeaders
h ResponseHeaders -> HeaderName -> Either String (Maybe Text)
forall a.
FromText a =>
ResponseHeaders -> HeaderName -> Either String (Maybe a)
Core..#? HeaderName
"x-amz-archive-id")
            Either String (Maybe Text -> Maybe Text -> ArchiveCreationOutput)
-> Either String (Maybe Text)
-> Either String (Maybe Text -> ArchiveCreationOutput)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (ResponseHeaders
h ResponseHeaders -> HeaderName -> Either String (Maybe Text)
forall a.
FromText a =>
ResponseHeaders -> HeaderName -> Either String (Maybe a)
Core..#? HeaderName
"x-amz-sha256-tree-hash")
            Either String (Maybe Text -> ArchiveCreationOutput)
-> Either String (Maybe Text)
-> Either String ArchiveCreationOutput
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (ResponseHeaders
h ResponseHeaders -> HeaderName -> Either String (Maybe Text)
forall a.
FromText a =>
ResponseHeaders -> HeaderName -> Either String (Maybe a)
Core..#? HeaderName
"Location")
      )

instance Core.ToBody UploadArchive where
  toBody :: UploadArchive -> RequestBody
toBody UploadArchive' {Maybe Text
Text
HashedBody
body :: HashedBody
accountId :: Text
vaultName :: Text
archiveDescription :: Maybe Text
checksum :: Maybe Text
$sel:body:UploadArchive' :: UploadArchive -> HashedBody
$sel:accountId:UploadArchive' :: UploadArchive -> Text
$sel:vaultName:UploadArchive' :: UploadArchive -> Text
$sel:archiveDescription:UploadArchive' :: UploadArchive -> Maybe Text
$sel:checksum:UploadArchive' :: UploadArchive -> Maybe Text
..} = HashedBody -> RequestBody
forall a. ToBody a => a -> RequestBody
Core.toBody HashedBody
body

instance Core.ToHeaders UploadArchive where
  toHeaders :: UploadArchive -> ResponseHeaders
toHeaders UploadArchive' {Maybe Text
Text
HashedBody
body :: HashedBody
accountId :: Text
vaultName :: Text
archiveDescription :: Maybe Text
checksum :: Maybe Text
$sel:body:UploadArchive' :: UploadArchive -> HashedBody
$sel:accountId:UploadArchive' :: UploadArchive -> Text
$sel:vaultName:UploadArchive' :: UploadArchive -> Text
$sel:archiveDescription:UploadArchive' :: UploadArchive -> Maybe Text
$sel:checksum:UploadArchive' :: UploadArchive -> Maybe Text
..} =
    [ResponseHeaders] -> ResponseHeaders
forall a. Monoid a => [a] -> a
Prelude.mconcat
      [ HeaderName
"x-amz-sha256-tree-hash" HeaderName -> Maybe Text -> ResponseHeaders
forall a. ToHeader a => HeaderName -> a -> ResponseHeaders
Core.=# Maybe Text
checksum,
        HeaderName
"x-amz-archive-description"
          HeaderName -> Maybe Text -> ResponseHeaders
forall a. ToHeader a => HeaderName -> a -> ResponseHeaders
Core.=# Maybe Text
archiveDescription
      ]

instance Core.ToPath UploadArchive where
  toPath :: UploadArchive -> ByteString
toPath UploadArchive' {Maybe Text
Text
HashedBody
body :: HashedBody
accountId :: Text
vaultName :: Text
archiveDescription :: Maybe Text
checksum :: Maybe Text
$sel:body:UploadArchive' :: UploadArchive -> HashedBody
$sel:accountId:UploadArchive' :: UploadArchive -> Text
$sel:vaultName:UploadArchive' :: UploadArchive -> Text
$sel:archiveDescription:UploadArchive' :: UploadArchive -> Maybe Text
$sel:checksum:UploadArchive' :: UploadArchive -> Maybe Text
..} =
    [ByteString] -> ByteString
forall a. Monoid a => [a] -> a
Prelude.mconcat
      [ ByteString
"/",
        Text -> ByteString
forall a. ToByteString a => a -> ByteString
Core.toBS Text
accountId,
        ByteString
"/vaults/",
        Text -> ByteString
forall a. ToByteString a => a -> ByteString
Core.toBS Text
vaultName,
        ByteString
"/archives"
      ]

instance Core.ToQuery UploadArchive where
  toQuery :: UploadArchive -> QueryString
toQuery = QueryString -> UploadArchive -> QueryString
forall a b. a -> b -> a
Prelude.const QueryString
forall a. Monoid a => a
Prelude.mempty