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

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

-- |
-- Module      : Amazonka.NetworkFirewall.Types.PortRange
-- Copyright   : (c) 2013-2023 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
module Amazonka.NetworkFirewall.Types.PortRange 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

-- | A single port range specification. This is used for source and
-- destination port ranges in the stateless rule MatchAttributes,
-- @SourcePorts@, and @DestinationPorts@ settings.
--
-- /See:/ 'newPortRange' smart constructor.
data PortRange = PortRange'
  { -- | The lower limit of the port range. This must be less than or equal to
    -- the @ToPort@ specification.
    PortRange -> Natural
fromPort :: Prelude.Natural,
    -- | The upper limit of the port range. This must be greater than or equal to
    -- the @FromPort@ specification.
    PortRange -> Natural
toPort :: Prelude.Natural
  }
  deriving (PortRange -> PortRange -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PortRange -> PortRange -> Bool
$c/= :: PortRange -> PortRange -> Bool
== :: PortRange -> PortRange -> Bool
$c== :: PortRange -> PortRange -> Bool
Prelude.Eq, ReadPrec [PortRange]
ReadPrec PortRange
Int -> ReadS PortRange
ReadS [PortRange]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [PortRange]
$creadListPrec :: ReadPrec [PortRange]
readPrec :: ReadPrec PortRange
$creadPrec :: ReadPrec PortRange
readList :: ReadS [PortRange]
$creadList :: ReadS [PortRange]
readsPrec :: Int -> ReadS PortRange
$creadsPrec :: Int -> ReadS PortRange
Prelude.Read, Int -> PortRange -> ShowS
[PortRange] -> ShowS
PortRange -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PortRange] -> ShowS
$cshowList :: [PortRange] -> ShowS
show :: PortRange -> String
$cshow :: PortRange -> String
showsPrec :: Int -> PortRange -> ShowS
$cshowsPrec :: Int -> PortRange -> ShowS
Prelude.Show, forall x. Rep PortRange x -> PortRange
forall x. PortRange -> Rep PortRange x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep PortRange x -> PortRange
$cfrom :: forall x. PortRange -> Rep PortRange x
Prelude.Generic)

-- |
-- Create a value of 'PortRange' 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:
--
-- 'fromPort', 'portRange_fromPort' - The lower limit of the port range. This must be less than or equal to
-- the @ToPort@ specification.
--
-- 'toPort', 'portRange_toPort' - The upper limit of the port range. This must be greater than or equal to
-- the @FromPort@ specification.
newPortRange ::
  -- | 'fromPort'
  Prelude.Natural ->
  -- | 'toPort'
  Prelude.Natural ->
  PortRange
newPortRange :: Natural -> Natural -> PortRange
newPortRange Natural
pFromPort_ Natural
pToPort_ =
  PortRange'
    { $sel:fromPort:PortRange' :: Natural
fromPort = Natural
pFromPort_,
      $sel:toPort:PortRange' :: Natural
toPort = Natural
pToPort_
    }

-- | The lower limit of the port range. This must be less than or equal to
-- the @ToPort@ specification.
portRange_fromPort :: Lens.Lens' PortRange Prelude.Natural
portRange_fromPort :: Lens' PortRange Natural
portRange_fromPort = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\PortRange' {Natural
fromPort :: Natural
$sel:fromPort:PortRange' :: PortRange -> Natural
fromPort} -> Natural
fromPort) (\s :: PortRange
s@PortRange' {} Natural
a -> PortRange
s {$sel:fromPort:PortRange' :: Natural
fromPort = Natural
a} :: PortRange)

-- | The upper limit of the port range. This must be greater than or equal to
-- the @FromPort@ specification.
portRange_toPort :: Lens.Lens' PortRange Prelude.Natural
portRange_toPort :: Lens' PortRange Natural
portRange_toPort = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\PortRange' {Natural
toPort :: Natural
$sel:toPort:PortRange' :: PortRange -> Natural
toPort} -> Natural
toPort) (\s :: PortRange
s@PortRange' {} Natural
a -> PortRange
s {$sel:toPort:PortRange' :: Natural
toPort = Natural
a} :: PortRange)

instance Data.FromJSON PortRange where
  parseJSON :: Value -> Parser PortRange
parseJSON =
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
Data.withObject
      String
"PortRange"
      ( \Object
x ->
          Natural -> Natural -> PortRange
PortRange'
            forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x forall a. FromJSON a => Object -> Key -> Parser a
Data..: Key
"FromPort")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser a
Data..: Key
"ToPort")
      )

instance Prelude.Hashable PortRange where
  hashWithSalt :: Int -> PortRange -> Int
hashWithSalt Int
_salt PortRange' {Natural
toPort :: Natural
fromPort :: Natural
$sel:toPort:PortRange' :: PortRange -> Natural
$sel:fromPort:PortRange' :: PortRange -> Natural
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Natural
fromPort
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Natural
toPort

instance Prelude.NFData PortRange where
  rnf :: PortRange -> ()
rnf PortRange' {Natural
toPort :: Natural
fromPort :: Natural
$sel:toPort:PortRange' :: PortRange -> Natural
$sel:fromPort:PortRange' :: PortRange -> Natural
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Natural
fromPort
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Natural
toPort

instance Data.ToJSON PortRange where
  toJSON :: PortRange -> Value
toJSON PortRange' {Natural
toPort :: Natural
fromPort :: Natural
$sel:toPort:PortRange' :: PortRange -> Natural
$sel:fromPort:PortRange' :: PortRange -> Natural
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ forall a. a -> Maybe a
Prelude.Just (Key
"FromPort" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Natural
fromPort),
            forall a. a -> Maybe a
Prelude.Just (Key
"ToPort" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Natural
toPort)
          ]
      )