{-# 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.XRay.CreateSamplingRule
-- Copyright   : (c) 2013-2023 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
--
-- Creates a rule to control sampling behavior for instrumented
-- applications. Services retrieve rules with
-- <https://docs.aws.amazon.com/xray/latest/api/API_GetSamplingRules.html GetSamplingRules>,
-- and evaluate each rule in ascending order of /priority/ for each
-- request. If a rule matches, the service records a trace, borrowing it
-- from the reservoir size. After 10 seconds, the service reports back to
-- X-Ray with
-- <https://docs.aws.amazon.com/xray/latest/api/API_GetSamplingTargets.html GetSamplingTargets>
-- to get updated versions of each in-use rule. The updated rule contains a
-- trace quota that the service can use instead of borrowing from the
-- reservoir.
module Amazonka.XRay.CreateSamplingRule
  ( -- * Creating a Request
    CreateSamplingRule (..),
    newCreateSamplingRule,

    -- * Request Lenses
    createSamplingRule_tags,
    createSamplingRule_samplingRule,

    -- * Destructuring the Response
    CreateSamplingRuleResponse (..),
    newCreateSamplingRuleResponse,

    -- * Response Lenses
    createSamplingRuleResponse_samplingRuleRecord,
    createSamplingRuleResponse_httpStatus,
  )
where

import qualified Amazonka.Core as Core
import qualified Amazonka.Core.Lens.Internal as Lens
import qualified Amazonka.Data as Data
import qualified Amazonka.Prelude as Prelude
import qualified Amazonka.Request as Request
import qualified Amazonka.Response as Response
import Amazonka.XRay.Types

-- | /See:/ 'newCreateSamplingRule' smart constructor.
data CreateSamplingRule = CreateSamplingRule'
  { -- | A map that contains one or more tag keys and tag values to attach to an
    -- X-Ray sampling rule. For more information about ways to use tags, see
    -- <https://docs.aws.amazon.com/general/latest/gr/aws_tagging.html Tagging Amazon Web Services resources>
    -- in the /Amazon Web Services General Reference/.
    --
    -- The following restrictions apply to tags:
    --
    -- -   Maximum number of user-applied tags per resource: 50
    --
    -- -   Maximum tag key length: 128 Unicode characters
    --
    -- -   Maximum tag value length: 256 Unicode characters
    --
    -- -   Valid values for key and value: a-z, A-Z, 0-9, space, and the
    --     following characters: _ . : \/ = + - and \@
    --
    -- -   Tag keys and values are case sensitive.
    --
    -- -   Don\'t use @aws:@ as a prefix for keys; it\'s reserved for Amazon
    --     Web Services use.
    CreateSamplingRule -> Maybe [Tag]
tags :: Prelude.Maybe [Tag],
    -- | The rule definition.
    CreateSamplingRule -> SamplingRule
samplingRule :: SamplingRule
  }
  deriving (CreateSamplingRule -> CreateSamplingRule -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CreateSamplingRule -> CreateSamplingRule -> Bool
$c/= :: CreateSamplingRule -> CreateSamplingRule -> Bool
== :: CreateSamplingRule -> CreateSamplingRule -> Bool
$c== :: CreateSamplingRule -> CreateSamplingRule -> Bool
Prelude.Eq, ReadPrec [CreateSamplingRule]
ReadPrec CreateSamplingRule
Int -> ReadS CreateSamplingRule
ReadS [CreateSamplingRule]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [CreateSamplingRule]
$creadListPrec :: ReadPrec [CreateSamplingRule]
readPrec :: ReadPrec CreateSamplingRule
$creadPrec :: ReadPrec CreateSamplingRule
readList :: ReadS [CreateSamplingRule]
$creadList :: ReadS [CreateSamplingRule]
readsPrec :: Int -> ReadS CreateSamplingRule
$creadsPrec :: Int -> ReadS CreateSamplingRule
Prelude.Read, Int -> CreateSamplingRule -> ShowS
[CreateSamplingRule] -> ShowS
CreateSamplingRule -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CreateSamplingRule] -> ShowS
$cshowList :: [CreateSamplingRule] -> ShowS
show :: CreateSamplingRule -> String
$cshow :: CreateSamplingRule -> String
showsPrec :: Int -> CreateSamplingRule -> ShowS
$cshowsPrec :: Int -> CreateSamplingRule -> ShowS
Prelude.Show, forall x. Rep CreateSamplingRule x -> CreateSamplingRule
forall x. CreateSamplingRule -> Rep CreateSamplingRule x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep CreateSamplingRule x -> CreateSamplingRule
$cfrom :: forall x. CreateSamplingRule -> Rep CreateSamplingRule x
Prelude.Generic)

-- |
-- Create a value of 'CreateSamplingRule' 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:
--
-- 'tags', 'createSamplingRule_tags' - A map that contains one or more tag keys and tag values to attach to an
-- X-Ray sampling rule. For more information about ways to use tags, see
-- <https://docs.aws.amazon.com/general/latest/gr/aws_tagging.html Tagging Amazon Web Services resources>
-- in the /Amazon Web Services General Reference/.
--
-- The following restrictions apply to tags:
--
-- -   Maximum number of user-applied tags per resource: 50
--
-- -   Maximum tag key length: 128 Unicode characters
--
-- -   Maximum tag value length: 256 Unicode characters
--
-- -   Valid values for key and value: a-z, A-Z, 0-9, space, and the
--     following characters: _ . : \/ = + - and \@
--
-- -   Tag keys and values are case sensitive.
--
-- -   Don\'t use @aws:@ as a prefix for keys; it\'s reserved for Amazon
--     Web Services use.
--
-- 'samplingRule', 'createSamplingRule_samplingRule' - The rule definition.
newCreateSamplingRule ::
  -- | 'samplingRule'
  SamplingRule ->
  CreateSamplingRule
newCreateSamplingRule :: SamplingRule -> CreateSamplingRule
newCreateSamplingRule SamplingRule
pSamplingRule_ =
  CreateSamplingRule'
    { $sel:tags:CreateSamplingRule' :: Maybe [Tag]
tags = forall a. Maybe a
Prelude.Nothing,
      $sel:samplingRule:CreateSamplingRule' :: SamplingRule
samplingRule = SamplingRule
pSamplingRule_
    }

-- | A map that contains one or more tag keys and tag values to attach to an
-- X-Ray sampling rule. For more information about ways to use tags, see
-- <https://docs.aws.amazon.com/general/latest/gr/aws_tagging.html Tagging Amazon Web Services resources>
-- in the /Amazon Web Services General Reference/.
--
-- The following restrictions apply to tags:
--
-- -   Maximum number of user-applied tags per resource: 50
--
-- -   Maximum tag key length: 128 Unicode characters
--
-- -   Maximum tag value length: 256 Unicode characters
--
-- -   Valid values for key and value: a-z, A-Z, 0-9, space, and the
--     following characters: _ . : \/ = + - and \@
--
-- -   Tag keys and values are case sensitive.
--
-- -   Don\'t use @aws:@ as a prefix for keys; it\'s reserved for Amazon
--     Web Services use.
createSamplingRule_tags :: Lens.Lens' CreateSamplingRule (Prelude.Maybe [Tag])
createSamplingRule_tags :: Lens' CreateSamplingRule (Maybe [Tag])
createSamplingRule_tags = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CreateSamplingRule' {Maybe [Tag]
tags :: Maybe [Tag]
$sel:tags:CreateSamplingRule' :: CreateSamplingRule -> Maybe [Tag]
tags} -> Maybe [Tag]
tags) (\s :: CreateSamplingRule
s@CreateSamplingRule' {} Maybe [Tag]
a -> CreateSamplingRule
s {$sel:tags:CreateSamplingRule' :: Maybe [Tag]
tags = Maybe [Tag]
a} :: CreateSamplingRule) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
Lens.mapping forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | The rule definition.
createSamplingRule_samplingRule :: Lens.Lens' CreateSamplingRule SamplingRule
createSamplingRule_samplingRule :: Lens' CreateSamplingRule SamplingRule
createSamplingRule_samplingRule = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CreateSamplingRule' {SamplingRule
samplingRule :: SamplingRule
$sel:samplingRule:CreateSamplingRule' :: CreateSamplingRule -> SamplingRule
samplingRule} -> SamplingRule
samplingRule) (\s :: CreateSamplingRule
s@CreateSamplingRule' {} SamplingRule
a -> CreateSamplingRule
s {$sel:samplingRule:CreateSamplingRule' :: SamplingRule
samplingRule = SamplingRule
a} :: CreateSamplingRule)

instance Core.AWSRequest CreateSamplingRule where
  type
    AWSResponse CreateSamplingRule =
      CreateSamplingRuleResponse
  request :: (Service -> Service)
-> CreateSamplingRule -> Request CreateSamplingRule
request Service -> Service
overrides =
    forall a. (ToRequest a, ToJSON a) => Service -> a -> Request a
Request.postJSON (Service -> Service
overrides Service
defaultService)
  response :: forall (m :: * -> *).
MonadResource m =>
(ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy CreateSamplingRule
-> ClientResponse ClientBody
-> m (Either
        Error (ClientResponse (AWSResponse CreateSamplingRule)))
response =
    forall (m :: * -> *) a.
MonadResource m =>
(Int -> ResponseHeaders -> Object -> Either String (AWSResponse a))
-> (ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
Response.receiveJSON
      ( \Int
s ResponseHeaders
h Object
x ->
          Maybe SamplingRuleRecord -> Int -> CreateSamplingRuleResponse
CreateSamplingRuleResponse'
            forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x forall a. FromJSON a => Object -> Key -> Either String (Maybe a)
Data..?> Key
"SamplingRuleRecord")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (forall (f :: * -> *) a. Applicative f => a -> f a
Prelude.pure (forall a. Enum a => a -> Int
Prelude.fromEnum Int
s))
      )

instance Prelude.Hashable CreateSamplingRule where
  hashWithSalt :: Int -> CreateSamplingRule -> Int
hashWithSalt Int
_salt CreateSamplingRule' {Maybe [Tag]
SamplingRule
samplingRule :: SamplingRule
tags :: Maybe [Tag]
$sel:samplingRule:CreateSamplingRule' :: CreateSamplingRule -> SamplingRule
$sel:tags:CreateSamplingRule' :: CreateSamplingRule -> Maybe [Tag]
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe [Tag]
tags
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` SamplingRule
samplingRule

instance Prelude.NFData CreateSamplingRule where
  rnf :: CreateSamplingRule -> ()
rnf CreateSamplingRule' {Maybe [Tag]
SamplingRule
samplingRule :: SamplingRule
tags :: Maybe [Tag]
$sel:samplingRule:CreateSamplingRule' :: CreateSamplingRule -> SamplingRule
$sel:tags:CreateSamplingRule' :: CreateSamplingRule -> Maybe [Tag]
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe [Tag]
tags
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf SamplingRule
samplingRule

instance Data.ToHeaders CreateSamplingRule where
  toHeaders :: CreateSamplingRule -> ResponseHeaders
toHeaders = forall a b. a -> b -> a
Prelude.const forall a. Monoid a => a
Prelude.mempty

instance Data.ToJSON CreateSamplingRule where
  toJSON :: CreateSamplingRule -> Value
toJSON CreateSamplingRule' {Maybe [Tag]
SamplingRule
samplingRule :: SamplingRule
tags :: Maybe [Tag]
$sel:samplingRule:CreateSamplingRule' :: CreateSamplingRule -> SamplingRule
$sel:tags:CreateSamplingRule' :: CreateSamplingRule -> Maybe [Tag]
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Key
"Tags" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe [Tag]
tags,
            forall a. a -> Maybe a
Prelude.Just (Key
"SamplingRule" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= SamplingRule
samplingRule)
          ]
      )

instance Data.ToPath CreateSamplingRule where
  toPath :: CreateSamplingRule -> ByteString
toPath = forall a b. a -> b -> a
Prelude.const ByteString
"/CreateSamplingRule"

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

-- | /See:/ 'newCreateSamplingRuleResponse' smart constructor.
data CreateSamplingRuleResponse = CreateSamplingRuleResponse'
  { -- | The saved rule definition and metadata.
    CreateSamplingRuleResponse -> Maybe SamplingRuleRecord
samplingRuleRecord :: Prelude.Maybe SamplingRuleRecord,
    -- | The response's http status code.
    CreateSamplingRuleResponse -> Int
httpStatus :: Prelude.Int
  }
  deriving (CreateSamplingRuleResponse -> CreateSamplingRuleResponse -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CreateSamplingRuleResponse -> CreateSamplingRuleResponse -> Bool
$c/= :: CreateSamplingRuleResponse -> CreateSamplingRuleResponse -> Bool
== :: CreateSamplingRuleResponse -> CreateSamplingRuleResponse -> Bool
$c== :: CreateSamplingRuleResponse -> CreateSamplingRuleResponse -> Bool
Prelude.Eq, ReadPrec [CreateSamplingRuleResponse]
ReadPrec CreateSamplingRuleResponse
Int -> ReadS CreateSamplingRuleResponse
ReadS [CreateSamplingRuleResponse]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [CreateSamplingRuleResponse]
$creadListPrec :: ReadPrec [CreateSamplingRuleResponse]
readPrec :: ReadPrec CreateSamplingRuleResponse
$creadPrec :: ReadPrec CreateSamplingRuleResponse
readList :: ReadS [CreateSamplingRuleResponse]
$creadList :: ReadS [CreateSamplingRuleResponse]
readsPrec :: Int -> ReadS CreateSamplingRuleResponse
$creadsPrec :: Int -> ReadS CreateSamplingRuleResponse
Prelude.Read, Int -> CreateSamplingRuleResponse -> ShowS
[CreateSamplingRuleResponse] -> ShowS
CreateSamplingRuleResponse -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CreateSamplingRuleResponse] -> ShowS
$cshowList :: [CreateSamplingRuleResponse] -> ShowS
show :: CreateSamplingRuleResponse -> String
$cshow :: CreateSamplingRuleResponse -> String
showsPrec :: Int -> CreateSamplingRuleResponse -> ShowS
$cshowsPrec :: Int -> CreateSamplingRuleResponse -> ShowS
Prelude.Show, forall x.
Rep CreateSamplingRuleResponse x -> CreateSamplingRuleResponse
forall x.
CreateSamplingRuleResponse -> Rep CreateSamplingRuleResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x.
Rep CreateSamplingRuleResponse x -> CreateSamplingRuleResponse
$cfrom :: forall x.
CreateSamplingRuleResponse -> Rep CreateSamplingRuleResponse x
Prelude.Generic)

-- |
-- Create a value of 'CreateSamplingRuleResponse' 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:
--
-- 'samplingRuleRecord', 'createSamplingRuleResponse_samplingRuleRecord' - The saved rule definition and metadata.
--
-- 'httpStatus', 'createSamplingRuleResponse_httpStatus' - The response's http status code.
newCreateSamplingRuleResponse ::
  -- | 'httpStatus'
  Prelude.Int ->
  CreateSamplingRuleResponse
newCreateSamplingRuleResponse :: Int -> CreateSamplingRuleResponse
newCreateSamplingRuleResponse Int
pHttpStatus_ =
  CreateSamplingRuleResponse'
    { $sel:samplingRuleRecord:CreateSamplingRuleResponse' :: Maybe SamplingRuleRecord
samplingRuleRecord =
        forall a. Maybe a
Prelude.Nothing,
      $sel:httpStatus:CreateSamplingRuleResponse' :: Int
httpStatus = Int
pHttpStatus_
    }

-- | The saved rule definition and metadata.
createSamplingRuleResponse_samplingRuleRecord :: Lens.Lens' CreateSamplingRuleResponse (Prelude.Maybe SamplingRuleRecord)
createSamplingRuleResponse_samplingRuleRecord :: Lens' CreateSamplingRuleResponse (Maybe SamplingRuleRecord)
createSamplingRuleResponse_samplingRuleRecord = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CreateSamplingRuleResponse' {Maybe SamplingRuleRecord
samplingRuleRecord :: Maybe SamplingRuleRecord
$sel:samplingRuleRecord:CreateSamplingRuleResponse' :: CreateSamplingRuleResponse -> Maybe SamplingRuleRecord
samplingRuleRecord} -> Maybe SamplingRuleRecord
samplingRuleRecord) (\s :: CreateSamplingRuleResponse
s@CreateSamplingRuleResponse' {} Maybe SamplingRuleRecord
a -> CreateSamplingRuleResponse
s {$sel:samplingRuleRecord:CreateSamplingRuleResponse' :: Maybe SamplingRuleRecord
samplingRuleRecord = Maybe SamplingRuleRecord
a} :: CreateSamplingRuleResponse)

-- | The response's http status code.
createSamplingRuleResponse_httpStatus :: Lens.Lens' CreateSamplingRuleResponse Prelude.Int
createSamplingRuleResponse_httpStatus :: Lens' CreateSamplingRuleResponse Int
createSamplingRuleResponse_httpStatus = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CreateSamplingRuleResponse' {Int
httpStatus :: Int
$sel:httpStatus:CreateSamplingRuleResponse' :: CreateSamplingRuleResponse -> Int
httpStatus} -> Int
httpStatus) (\s :: CreateSamplingRuleResponse
s@CreateSamplingRuleResponse' {} Int
a -> CreateSamplingRuleResponse
s {$sel:httpStatus:CreateSamplingRuleResponse' :: Int
httpStatus = Int
a} :: CreateSamplingRuleResponse)

instance Prelude.NFData CreateSamplingRuleResponse where
  rnf :: CreateSamplingRuleResponse -> ()
rnf CreateSamplingRuleResponse' {Int
Maybe SamplingRuleRecord
httpStatus :: Int
samplingRuleRecord :: Maybe SamplingRuleRecord
$sel:httpStatus:CreateSamplingRuleResponse' :: CreateSamplingRuleResponse -> Int
$sel:samplingRuleRecord:CreateSamplingRuleResponse' :: CreateSamplingRuleResponse -> Maybe SamplingRuleRecord
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe SamplingRuleRecord
samplingRuleRecord
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Int
httpStatus