{-# 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.EFS.CreateMountTarget
-- 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)
--
-- Creates a mount target for a file system. You can then mount the file
-- system on EC2 instances by using the mount target.
--
-- You can create one mount target in each Availability Zone in your VPC.
-- All EC2 instances in a VPC within a given Availability Zone share a
-- single mount target for a given file system. If you have multiple
-- subnets in an Availability Zone, you create a mount target in one of the
-- subnets. EC2 instances do not need to be in the same subnet as the mount
-- target in order to access their file system.
--
-- You can create only one mount target for an EFS file system using One
-- Zone storage classes. You must create that mount target in the same
-- Availability Zone in which the file system is located. Use the
-- @AvailabilityZoneName@ and @AvailabiltyZoneId@ properties in the
-- DescribeFileSystems response object to get this information. Use the
-- @subnetId@ associated with the file system\'s Availability Zone when
-- creating the mount target.
--
-- For more information, see
-- <https://docs.aws.amazon.com/efs/latest/ug/how-it-works.html Amazon EFS: How it Works>.
--
-- To create a mount target for a file system, the file system\'s lifecycle
-- state must be @available@. For more information, see
-- DescribeFileSystems.
--
-- In the request, provide the following:
--
-- -   The file system ID for which you are creating the mount target.
--
-- -   A subnet ID, which determines the following:
--
--     -   The VPC in which Amazon EFS creates the mount target
--
--     -   The Availability Zone in which Amazon EFS creates the mount
--         target
--
--     -   The IP address range from which Amazon EFS selects the IP
--         address of the mount target (if you don\'t specify an IP address
--         in the request)
--
-- After creating the mount target, Amazon EFS returns a response that
-- includes, a @MountTargetId@ and an @IpAddress@. You use this IP address
-- when mounting the file system in an EC2 instance. You can also use the
-- mount target\'s DNS name when mounting the file system. The EC2 instance
-- on which you mount the file system by using the mount target can resolve
-- the mount target\'s DNS name to its IP address. For more information,
-- see
-- <https://docs.aws.amazon.com/efs/latest/ug/how-it-works.html#how-it-works-implementation How it Works: Implementation Overview>.
--
-- Note that you can create mount targets for a file system in only one
-- VPC, and there can be only one mount target per Availability Zone. That
-- is, if the file system already has one or more mount targets created for
-- it, the subnet specified in the request to add another mount target must
-- meet the following requirements:
--
-- -   Must belong to the same VPC as the subnets of the existing mount
--     targets
--
-- -   Must not be in the same Availability Zone as any of the subnets of
--     the existing mount targets
--
-- If the request satisfies the requirements, Amazon EFS does the
-- following:
--
-- -   Creates a new mount target in the specified subnet.
--
-- -   Also creates a new network interface in the subnet as follows:
--
--     -   If the request provides an @IpAddress@, Amazon EFS assigns that
--         IP address to the network interface. Otherwise, Amazon EFS
--         assigns a free address in the subnet (in the same way that the
--         Amazon EC2 @CreateNetworkInterface@ call does when a request
--         does not specify a primary private IP address).
--
--     -   If the request provides @SecurityGroups@, this network interface
--         is associated with those security groups. Otherwise, it belongs
--         to the default security group for the subnet\'s VPC.
--
--     -   Assigns the description
--         @Mount target fsmt-id for file system fs-id @ where @ fsmt-id @
--         is the mount target ID, and @ fs-id @ is the @FileSystemId@.
--
--     -   Sets the @requesterManaged@ property of the network interface to
--         @true@, and the @requesterId@ value to @EFS@.
--
--     Each Amazon EFS mount target has one corresponding requester-managed
--     EC2 network interface. After the network interface is created,
--     Amazon EFS sets the @NetworkInterfaceId@ field in the mount
--     target\'s description to the network interface ID, and the
--     @IpAddress@ field to its address. If network interface creation
--     fails, the entire @CreateMountTarget@ operation fails.
--
-- The @CreateMountTarget@ call returns only after creating the network
-- interface, but while the mount target state is still @creating@, you can
-- check the mount target creation status by calling the
-- DescribeMountTargets operation, which among other things returns the
-- mount target state.
--
-- We recommend that you create a mount target in each of the Availability
-- Zones. There are cost considerations for using a file system in an
-- Availability Zone through a mount target created in another Availability
-- Zone. For more information, see <http://aws.amazon.com/efs/ Amazon EFS>.
-- In addition, by always using a mount target local to the instance\'s
-- Availability Zone, you eliminate a partial failure scenario. If the
-- Availability Zone in which your mount target is created goes down, then
-- you can\'t access your file system through that mount target.
--
-- This operation requires permissions for the following action on the file
-- system:
--
-- -   @elasticfilesystem:CreateMountTarget@
--
-- This operation also requires permissions for the following Amazon EC2
-- actions:
--
-- -   @ec2:DescribeSubnets@
--
-- -   @ec2:DescribeNetworkInterfaces@
--
-- -   @ec2:CreateNetworkInterface@
module Amazonka.EFS.CreateMountTarget
  ( -- * Creating a Request
    CreateMountTarget (..),
    newCreateMountTarget,

    -- * Request Lenses
    createMountTarget_ipAddress,
    createMountTarget_securityGroups,
    createMountTarget_fileSystemId,
    createMountTarget_subnetId,

    -- * Destructuring the Response
    MountTargetDescription (..),
    newMountTargetDescription,

    -- * Response Lenses
    mountTargetDescription_ipAddress,
    mountTargetDescription_availabilityZoneId,
    mountTargetDescription_vpcId,
    mountTargetDescription_availabilityZoneName,
    mountTargetDescription_networkInterfaceId,
    mountTargetDescription_ownerId,
    mountTargetDescription_mountTargetId,
    mountTargetDescription_fileSystemId,
    mountTargetDescription_subnetId,
    mountTargetDescription_lifeCycleState,
  )
where

import qualified Amazonka.Core as Core
import Amazonka.EFS.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

-- |
--
-- /See:/ 'newCreateMountTarget' smart constructor.
data CreateMountTarget = CreateMountTarget'
  { -- | Valid IPv4 address within the address range of the specified subnet.
    CreateMountTarget -> Maybe Text
ipAddress :: Prelude.Maybe Prelude.Text,
    -- | Up to five VPC security group IDs, of the form @sg-xxxxxxxx@. These must
    -- be for the same VPC as subnet specified.
    CreateMountTarget -> Maybe [Text]
securityGroups :: Prelude.Maybe [Prelude.Text],
    -- | The ID of the file system for which to create the mount target.
    CreateMountTarget -> Text
fileSystemId :: Prelude.Text,
    -- | The ID of the subnet to add the mount target in. For file systems that
    -- use One Zone storage classes, use the subnet that is associated with the
    -- file system\'s Availability Zone.
    CreateMountTarget -> Text
subnetId :: Prelude.Text
  }
  deriving (CreateMountTarget -> CreateMountTarget -> Bool
(CreateMountTarget -> CreateMountTarget -> Bool)
-> (CreateMountTarget -> CreateMountTarget -> Bool)
-> Eq CreateMountTarget
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CreateMountTarget -> CreateMountTarget -> Bool
$c/= :: CreateMountTarget -> CreateMountTarget -> Bool
== :: CreateMountTarget -> CreateMountTarget -> Bool
$c== :: CreateMountTarget -> CreateMountTarget -> Bool
Prelude.Eq, ReadPrec [CreateMountTarget]
ReadPrec CreateMountTarget
Int -> ReadS CreateMountTarget
ReadS [CreateMountTarget]
(Int -> ReadS CreateMountTarget)
-> ReadS [CreateMountTarget]
-> ReadPrec CreateMountTarget
-> ReadPrec [CreateMountTarget]
-> Read CreateMountTarget
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [CreateMountTarget]
$creadListPrec :: ReadPrec [CreateMountTarget]
readPrec :: ReadPrec CreateMountTarget
$creadPrec :: ReadPrec CreateMountTarget
readList :: ReadS [CreateMountTarget]
$creadList :: ReadS [CreateMountTarget]
readsPrec :: Int -> ReadS CreateMountTarget
$creadsPrec :: Int -> ReadS CreateMountTarget
Prelude.Read, Int -> CreateMountTarget -> ShowS
[CreateMountTarget] -> ShowS
CreateMountTarget -> String
(Int -> CreateMountTarget -> ShowS)
-> (CreateMountTarget -> String)
-> ([CreateMountTarget] -> ShowS)
-> Show CreateMountTarget
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CreateMountTarget] -> ShowS
$cshowList :: [CreateMountTarget] -> ShowS
show :: CreateMountTarget -> String
$cshow :: CreateMountTarget -> String
showsPrec :: Int -> CreateMountTarget -> ShowS
$cshowsPrec :: Int -> CreateMountTarget -> ShowS
Prelude.Show, (forall x. CreateMountTarget -> Rep CreateMountTarget x)
-> (forall x. Rep CreateMountTarget x -> CreateMountTarget)
-> Generic CreateMountTarget
forall x. Rep CreateMountTarget x -> CreateMountTarget
forall x. CreateMountTarget -> Rep CreateMountTarget x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep CreateMountTarget x -> CreateMountTarget
$cfrom :: forall x. CreateMountTarget -> Rep CreateMountTarget x
Prelude.Generic)

-- |
-- Create a value of 'CreateMountTarget' 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:
--
-- 'ipAddress', 'createMountTarget_ipAddress' - Valid IPv4 address within the address range of the specified subnet.
--
-- 'securityGroups', 'createMountTarget_securityGroups' - Up to five VPC security group IDs, of the form @sg-xxxxxxxx@. These must
-- be for the same VPC as subnet specified.
--
-- 'fileSystemId', 'createMountTarget_fileSystemId' - The ID of the file system for which to create the mount target.
--
-- 'subnetId', 'createMountTarget_subnetId' - The ID of the subnet to add the mount target in. For file systems that
-- use One Zone storage classes, use the subnet that is associated with the
-- file system\'s Availability Zone.
newCreateMountTarget ::
  -- | 'fileSystemId'
  Prelude.Text ->
  -- | 'subnetId'
  Prelude.Text ->
  CreateMountTarget
newCreateMountTarget :: Text -> Text -> CreateMountTarget
newCreateMountTarget Text
pFileSystemId_ Text
pSubnetId_ =
  CreateMountTarget' :: Maybe Text -> Maybe [Text] -> Text -> Text -> CreateMountTarget
CreateMountTarget'
    { $sel:ipAddress:CreateMountTarget' :: Maybe Text
ipAddress = Maybe Text
forall a. Maybe a
Prelude.Nothing,
      $sel:securityGroups:CreateMountTarget' :: Maybe [Text]
securityGroups = Maybe [Text]
forall a. Maybe a
Prelude.Nothing,
      $sel:fileSystemId:CreateMountTarget' :: Text
fileSystemId = Text
pFileSystemId_,
      $sel:subnetId:CreateMountTarget' :: Text
subnetId = Text
pSubnetId_
    }

-- | Valid IPv4 address within the address range of the specified subnet.
createMountTarget_ipAddress :: Lens.Lens' CreateMountTarget (Prelude.Maybe Prelude.Text)
createMountTarget_ipAddress :: (Maybe Text -> f (Maybe Text))
-> CreateMountTarget -> f CreateMountTarget
createMountTarget_ipAddress = (CreateMountTarget -> Maybe Text)
-> (CreateMountTarget -> Maybe Text -> CreateMountTarget)
-> Lens
     CreateMountTarget CreateMountTarget (Maybe Text) (Maybe Text)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CreateMountTarget' {Maybe Text
ipAddress :: Maybe Text
$sel:ipAddress:CreateMountTarget' :: CreateMountTarget -> Maybe Text
ipAddress} -> Maybe Text
ipAddress) (\s :: CreateMountTarget
s@CreateMountTarget' {} Maybe Text
a -> CreateMountTarget
s {$sel:ipAddress:CreateMountTarget' :: Maybe Text
ipAddress = Maybe Text
a} :: CreateMountTarget)

-- | Up to five VPC security group IDs, of the form @sg-xxxxxxxx@. These must
-- be for the same VPC as subnet specified.
createMountTarget_securityGroups :: Lens.Lens' CreateMountTarget (Prelude.Maybe [Prelude.Text])
createMountTarget_securityGroups :: (Maybe [Text] -> f (Maybe [Text]))
-> CreateMountTarget -> f CreateMountTarget
createMountTarget_securityGroups = (CreateMountTarget -> Maybe [Text])
-> (CreateMountTarget -> Maybe [Text] -> CreateMountTarget)
-> Lens
     CreateMountTarget CreateMountTarget (Maybe [Text]) (Maybe [Text])
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CreateMountTarget' {Maybe [Text]
securityGroups :: Maybe [Text]
$sel:securityGroups:CreateMountTarget' :: CreateMountTarget -> Maybe [Text]
securityGroups} -> Maybe [Text]
securityGroups) (\s :: CreateMountTarget
s@CreateMountTarget' {} Maybe [Text]
a -> CreateMountTarget
s {$sel:securityGroups:CreateMountTarget' :: Maybe [Text]
securityGroups = Maybe [Text]
a} :: CreateMountTarget) ((Maybe [Text] -> f (Maybe [Text]))
 -> CreateMountTarget -> f CreateMountTarget)
-> ((Maybe [Text] -> f (Maybe [Text]))
    -> Maybe [Text] -> f (Maybe [Text]))
-> (Maybe [Text] -> f (Maybe [Text]))
-> CreateMountTarget
-> f CreateMountTarget
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. AnIso [Text] [Text] [Text] [Text]
-> Iso (Maybe [Text]) (Maybe [Text]) (Maybe [Text]) (Maybe [Text])
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 AnIso [Text] [Text] [Text] [Text]
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | The ID of the file system for which to create the mount target.
createMountTarget_fileSystemId :: Lens.Lens' CreateMountTarget Prelude.Text
createMountTarget_fileSystemId :: (Text -> f Text) -> CreateMountTarget -> f CreateMountTarget
createMountTarget_fileSystemId = (CreateMountTarget -> Text)
-> (CreateMountTarget -> Text -> CreateMountTarget)
-> Lens CreateMountTarget CreateMountTarget Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CreateMountTarget' {Text
fileSystemId :: Text
$sel:fileSystemId:CreateMountTarget' :: CreateMountTarget -> Text
fileSystemId} -> Text
fileSystemId) (\s :: CreateMountTarget
s@CreateMountTarget' {} Text
a -> CreateMountTarget
s {$sel:fileSystemId:CreateMountTarget' :: Text
fileSystemId = Text
a} :: CreateMountTarget)

-- | The ID of the subnet to add the mount target in. For file systems that
-- use One Zone storage classes, use the subnet that is associated with the
-- file system\'s Availability Zone.
createMountTarget_subnetId :: Lens.Lens' CreateMountTarget Prelude.Text
createMountTarget_subnetId :: (Text -> f Text) -> CreateMountTarget -> f CreateMountTarget
createMountTarget_subnetId = (CreateMountTarget -> Text)
-> (CreateMountTarget -> Text -> CreateMountTarget)
-> Lens CreateMountTarget CreateMountTarget Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CreateMountTarget' {Text
subnetId :: Text
$sel:subnetId:CreateMountTarget' :: CreateMountTarget -> Text
subnetId} -> Text
subnetId) (\s :: CreateMountTarget
s@CreateMountTarget' {} Text
a -> CreateMountTarget
s {$sel:subnetId:CreateMountTarget' :: Text
subnetId = Text
a} :: CreateMountTarget)

instance Core.AWSRequest CreateMountTarget where
  type
    AWSResponse CreateMountTarget =
      MountTargetDescription
  request :: CreateMountTarget -> Request CreateMountTarget
request = Service -> CreateMountTarget -> Request CreateMountTarget
forall a. (ToRequest a, ToJSON a) => Service -> a -> Request a
Request.postJSON Service
defaultService
  response :: Logger
-> Service
-> Proxy CreateMountTarget
-> ClientResponse ClientBody
-> m (Either
        Error (ClientResponse (AWSResponse CreateMountTarget)))
response =
    (Int
 -> ResponseHeaders
 -> Object
 -> Either String (AWSResponse CreateMountTarget))
-> Logger
-> Service
-> Proxy CreateMountTarget
-> ClientResponse ClientBody
-> m (Either
        Error (ClientResponse (AWSResponse CreateMountTarget)))
forall (m :: * -> *) a.
MonadResource m =>
(Int -> ResponseHeaders -> Object -> Either String (AWSResponse a))
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
Response.receiveJSON
      (\Int
s ResponseHeaders
h Object
x -> Object -> Either String MountTargetDescription
forall a. FromJSON a => Object -> Either String a
Core.eitherParseJSON Object
x)

instance Prelude.Hashable CreateMountTarget

instance Prelude.NFData CreateMountTarget

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

instance Core.ToJSON CreateMountTarget where
  toJSON :: CreateMountTarget -> Value
toJSON CreateMountTarget' {Maybe [Text]
Maybe Text
Text
subnetId :: Text
fileSystemId :: Text
securityGroups :: Maybe [Text]
ipAddress :: Maybe Text
$sel:subnetId:CreateMountTarget' :: CreateMountTarget -> Text
$sel:fileSystemId:CreateMountTarget' :: CreateMountTarget -> Text
$sel:securityGroups:CreateMountTarget' :: CreateMountTarget -> Maybe [Text]
$sel:ipAddress:CreateMountTarget' :: CreateMountTarget -> Maybe Text
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Text
"IpAddress" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=) (Text -> Pair) -> Maybe Text -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Text
ipAddress,
            (Text
"SecurityGroups" Text -> [Text] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=)
              ([Text] -> Pair) -> Maybe [Text] -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe [Text]
securityGroups,
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"FileSystemId" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Text
fileSystemId),
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"SubnetId" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Text
subnetId)
          ]
      )

instance Core.ToPath CreateMountTarget where
  toPath :: CreateMountTarget -> ByteString
toPath = ByteString -> CreateMountTarget -> ByteString
forall a b. a -> b -> a
Prelude.const ByteString
"/2015-02-01/mount-targets"

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