{-# 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.OpsWorksCM.AssociateNode
-- Copyright   : (c) 2013-2023 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
--
-- Associates a new node with the server. For more information about how to
-- disassociate a node, see DisassociateNode.
--
-- On a Chef server: This command is an alternative to @knife bootstrap@.
--
-- Example (Chef):
-- @aws opsworks-cm associate-node --server-name @/@MyServer@/@ --node-name @/@MyManagedNode@/@ --engine-attributes \"Name=@/@CHEF_ORGANIZATION@/@,Value=default\" \"Name=@/@CHEF_NODE_PUBLIC_KEY@/@,Value=@/@public-key-pem@/@\"@
--
-- On a Puppet server, this command is an alternative to the
-- @puppet cert sign@ command that signs a Puppet node CSR.
--
-- Example (Puppet):
-- @aws opsworks-cm associate-node --server-name @/@MyServer@/@ --node-name @/@MyManagedNode@/@ --engine-attributes \"Name=@/@PUPPET_NODE_CSR@/@,Value=@/@csr-pem@/@\"@
--
-- A node can can only be associated with servers that are in a @HEALTHY@
-- state. Otherwise, an @InvalidStateException@ is thrown. A
-- @ResourceNotFoundException@ is thrown when the server does not exist. A
-- @ValidationException@ is raised when parameters of the request are not
-- valid. The AssociateNode API call can be integrated into Auto Scaling
-- configurations, AWS Cloudformation templates, or the user data of a
-- server\'s instance.
module Amazonka.OpsWorksCM.AssociateNode
  ( -- * Creating a Request
    AssociateNode (..),
    newAssociateNode,

    -- * Request Lenses
    associateNode_serverName,
    associateNode_nodeName,
    associateNode_engineAttributes,

    -- * Destructuring the Response
    AssociateNodeResponse (..),
    newAssociateNodeResponse,

    -- * Response Lenses
    associateNodeResponse_nodeAssociationStatusToken,
    associateNodeResponse_httpStatus,
  )
where

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

-- | /See:/ 'newAssociateNode' smart constructor.
data AssociateNode = AssociateNode'
  { -- | The name of the server with which to associate the node.
    AssociateNode -> Text
serverName :: Prelude.Text,
    -- | The name of the node.
    AssociateNode -> Text
nodeName :: Prelude.Text,
    -- | Engine attributes used for associating the node.
    --
    -- __Attributes accepted in a AssociateNode request for Chef__
    --
    -- -   @CHEF_ORGANIZATION@: The Chef organization with which the node is
    --     associated. By default only one organization named @default@ can
    --     exist.
    --
    -- -   @CHEF_NODE_PUBLIC_KEY@: A PEM-formatted public key. This key is
    --     required for the @chef-client@ agent to access the Chef API.
    --
    -- __Attributes accepted in a AssociateNode request for Puppet__
    --
    -- -   @PUPPET_NODE_CSR@: A PEM-formatted certificate-signing request (CSR)
    --     that is created by the node.
    AssociateNode -> [EngineAttribute]
engineAttributes :: [EngineAttribute]
  }
  deriving (AssociateNode -> AssociateNode -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AssociateNode -> AssociateNode -> Bool
$c/= :: AssociateNode -> AssociateNode -> Bool
== :: AssociateNode -> AssociateNode -> Bool
$c== :: AssociateNode -> AssociateNode -> Bool
Prelude.Eq, Int -> AssociateNode -> ShowS
[AssociateNode] -> ShowS
AssociateNode -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AssociateNode] -> ShowS
$cshowList :: [AssociateNode] -> ShowS
show :: AssociateNode -> String
$cshow :: AssociateNode -> String
showsPrec :: Int -> AssociateNode -> ShowS
$cshowsPrec :: Int -> AssociateNode -> ShowS
Prelude.Show, forall x. Rep AssociateNode x -> AssociateNode
forall x. AssociateNode -> Rep AssociateNode x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep AssociateNode x -> AssociateNode
$cfrom :: forall x. AssociateNode -> Rep AssociateNode x
Prelude.Generic)

-- |
-- Create a value of 'AssociateNode' 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:
--
-- 'serverName', 'associateNode_serverName' - The name of the server with which to associate the node.
--
-- 'nodeName', 'associateNode_nodeName' - The name of the node.
--
-- 'engineAttributes', 'associateNode_engineAttributes' - Engine attributes used for associating the node.
--
-- __Attributes accepted in a AssociateNode request for Chef__
--
-- -   @CHEF_ORGANIZATION@: The Chef organization with which the node is
--     associated. By default only one organization named @default@ can
--     exist.
--
-- -   @CHEF_NODE_PUBLIC_KEY@: A PEM-formatted public key. This key is
--     required for the @chef-client@ agent to access the Chef API.
--
-- __Attributes accepted in a AssociateNode request for Puppet__
--
-- -   @PUPPET_NODE_CSR@: A PEM-formatted certificate-signing request (CSR)
--     that is created by the node.
newAssociateNode ::
  -- | 'serverName'
  Prelude.Text ->
  -- | 'nodeName'
  Prelude.Text ->
  AssociateNode
newAssociateNode :: Text -> Text -> AssociateNode
newAssociateNode Text
pServerName_ Text
pNodeName_ =
  AssociateNode'
    { $sel:serverName:AssociateNode' :: Text
serverName = Text
pServerName_,
      $sel:nodeName:AssociateNode' :: Text
nodeName = Text
pNodeName_,
      $sel:engineAttributes:AssociateNode' :: [EngineAttribute]
engineAttributes = forall a. Monoid a => a
Prelude.mempty
    }

-- | The name of the server with which to associate the node.
associateNode_serverName :: Lens.Lens' AssociateNode Prelude.Text
associateNode_serverName :: Lens' AssociateNode Text
associateNode_serverName = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\AssociateNode' {Text
serverName :: Text
$sel:serverName:AssociateNode' :: AssociateNode -> Text
serverName} -> Text
serverName) (\s :: AssociateNode
s@AssociateNode' {} Text
a -> AssociateNode
s {$sel:serverName:AssociateNode' :: Text
serverName = Text
a} :: AssociateNode)

-- | The name of the node.
associateNode_nodeName :: Lens.Lens' AssociateNode Prelude.Text
associateNode_nodeName :: Lens' AssociateNode Text
associateNode_nodeName = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\AssociateNode' {Text
nodeName :: Text
$sel:nodeName:AssociateNode' :: AssociateNode -> Text
nodeName} -> Text
nodeName) (\s :: AssociateNode
s@AssociateNode' {} Text
a -> AssociateNode
s {$sel:nodeName:AssociateNode' :: Text
nodeName = Text
a} :: AssociateNode)

-- | Engine attributes used for associating the node.
--
-- __Attributes accepted in a AssociateNode request for Chef__
--
-- -   @CHEF_ORGANIZATION@: The Chef organization with which the node is
--     associated. By default only one organization named @default@ can
--     exist.
--
-- -   @CHEF_NODE_PUBLIC_KEY@: A PEM-formatted public key. This key is
--     required for the @chef-client@ agent to access the Chef API.
--
-- __Attributes accepted in a AssociateNode request for Puppet__
--
-- -   @PUPPET_NODE_CSR@: A PEM-formatted certificate-signing request (CSR)
--     that is created by the node.
associateNode_engineAttributes :: Lens.Lens' AssociateNode [EngineAttribute]
associateNode_engineAttributes :: Lens' AssociateNode [EngineAttribute]
associateNode_engineAttributes = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\AssociateNode' {[EngineAttribute]
engineAttributes :: [EngineAttribute]
$sel:engineAttributes:AssociateNode' :: AssociateNode -> [EngineAttribute]
engineAttributes} -> [EngineAttribute]
engineAttributes) (\s :: AssociateNode
s@AssociateNode' {} [EngineAttribute]
a -> AssociateNode
s {$sel:engineAttributes:AssociateNode' :: [EngineAttribute]
engineAttributes = [EngineAttribute]
a} :: AssociateNode) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

instance Core.AWSRequest AssociateNode where
  type
    AWSResponse AssociateNode =
      AssociateNodeResponse
  request :: (Service -> Service) -> AssociateNode -> Request AssociateNode
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 AssociateNode
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse AssociateNode)))
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 Text -> Int -> AssociateNodeResponse
AssociateNodeResponse'
            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
"NodeAssociationStatusToken")
            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 AssociateNode where
  hashWithSalt :: Int -> AssociateNode -> Int
hashWithSalt Int
_salt AssociateNode' {[EngineAttribute]
Text
engineAttributes :: [EngineAttribute]
nodeName :: Text
serverName :: Text
$sel:engineAttributes:AssociateNode' :: AssociateNode -> [EngineAttribute]
$sel:nodeName:AssociateNode' :: AssociateNode -> Text
$sel:serverName:AssociateNode' :: AssociateNode -> Text
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Text
serverName
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Text
nodeName
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` [EngineAttribute]
engineAttributes

instance Prelude.NFData AssociateNode where
  rnf :: AssociateNode -> ()
rnf AssociateNode' {[EngineAttribute]
Text
engineAttributes :: [EngineAttribute]
nodeName :: Text
serverName :: Text
$sel:engineAttributes:AssociateNode' :: AssociateNode -> [EngineAttribute]
$sel:nodeName:AssociateNode' :: AssociateNode -> Text
$sel:serverName:AssociateNode' :: AssociateNode -> Text
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Text
serverName
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Text
nodeName
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf [EngineAttribute]
engineAttributes

instance Data.ToHeaders AssociateNode where
  toHeaders :: AssociateNode -> ResponseHeaders
toHeaders =
    forall a b. a -> b -> a
Prelude.const
      ( forall a. Monoid a => [a] -> a
Prelude.mconcat
          [ HeaderName
"X-Amz-Target"
              forall a. ToHeader a => HeaderName -> a -> ResponseHeaders
Data.=# ( ByteString
"OpsWorksCM_V2016_11_01.AssociateNode" ::
                          Prelude.ByteString
                      ),
            HeaderName
"Content-Type"
              forall a. ToHeader a => HeaderName -> a -> ResponseHeaders
Data.=# ( ByteString
"application/x-amz-json-1.1" ::
                          Prelude.ByteString
                      )
          ]
      )

instance Data.ToJSON AssociateNode where
  toJSON :: AssociateNode -> Value
toJSON AssociateNode' {[EngineAttribute]
Text
engineAttributes :: [EngineAttribute]
nodeName :: Text
serverName :: Text
$sel:engineAttributes:AssociateNode' :: AssociateNode -> [EngineAttribute]
$sel:nodeName:AssociateNode' :: AssociateNode -> Text
$sel:serverName:AssociateNode' :: AssociateNode -> Text
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ forall a. a -> Maybe a
Prelude.Just (Key
"ServerName" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Text
serverName),
            forall a. a -> Maybe a
Prelude.Just (Key
"NodeName" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Text
nodeName),
            forall a. a -> Maybe a
Prelude.Just
              (Key
"EngineAttributes" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= [EngineAttribute]
engineAttributes)
          ]
      )

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

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

-- | /See:/ 'newAssociateNodeResponse' smart constructor.
data AssociateNodeResponse = AssociateNodeResponse'
  { -- | Contains a token which can be passed to the
    -- @DescribeNodeAssociationStatus@ API call to get the status of the
    -- association request.
    AssociateNodeResponse -> Maybe Text
nodeAssociationStatusToken :: Prelude.Maybe Prelude.Text,
    -- | The response's http status code.
    AssociateNodeResponse -> Int
httpStatus :: Prelude.Int
  }
  deriving (AssociateNodeResponse -> AssociateNodeResponse -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AssociateNodeResponse -> AssociateNodeResponse -> Bool
$c/= :: AssociateNodeResponse -> AssociateNodeResponse -> Bool
== :: AssociateNodeResponse -> AssociateNodeResponse -> Bool
$c== :: AssociateNodeResponse -> AssociateNodeResponse -> Bool
Prelude.Eq, ReadPrec [AssociateNodeResponse]
ReadPrec AssociateNodeResponse
Int -> ReadS AssociateNodeResponse
ReadS [AssociateNodeResponse]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [AssociateNodeResponse]
$creadListPrec :: ReadPrec [AssociateNodeResponse]
readPrec :: ReadPrec AssociateNodeResponse
$creadPrec :: ReadPrec AssociateNodeResponse
readList :: ReadS [AssociateNodeResponse]
$creadList :: ReadS [AssociateNodeResponse]
readsPrec :: Int -> ReadS AssociateNodeResponse
$creadsPrec :: Int -> ReadS AssociateNodeResponse
Prelude.Read, Int -> AssociateNodeResponse -> ShowS
[AssociateNodeResponse] -> ShowS
AssociateNodeResponse -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AssociateNodeResponse] -> ShowS
$cshowList :: [AssociateNodeResponse] -> ShowS
show :: AssociateNodeResponse -> String
$cshow :: AssociateNodeResponse -> String
showsPrec :: Int -> AssociateNodeResponse -> ShowS
$cshowsPrec :: Int -> AssociateNodeResponse -> ShowS
Prelude.Show, forall x. Rep AssociateNodeResponse x -> AssociateNodeResponse
forall x. AssociateNodeResponse -> Rep AssociateNodeResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep AssociateNodeResponse x -> AssociateNodeResponse
$cfrom :: forall x. AssociateNodeResponse -> Rep AssociateNodeResponse x
Prelude.Generic)

-- |
-- Create a value of 'AssociateNodeResponse' 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:
--
-- 'nodeAssociationStatusToken', 'associateNodeResponse_nodeAssociationStatusToken' - Contains a token which can be passed to the
-- @DescribeNodeAssociationStatus@ API call to get the status of the
-- association request.
--
-- 'httpStatus', 'associateNodeResponse_httpStatus' - The response's http status code.
newAssociateNodeResponse ::
  -- | 'httpStatus'
  Prelude.Int ->
  AssociateNodeResponse
newAssociateNodeResponse :: Int -> AssociateNodeResponse
newAssociateNodeResponse Int
pHttpStatus_ =
  AssociateNodeResponse'
    { $sel:nodeAssociationStatusToken:AssociateNodeResponse' :: Maybe Text
nodeAssociationStatusToken =
        forall a. Maybe a
Prelude.Nothing,
      $sel:httpStatus:AssociateNodeResponse' :: Int
httpStatus = Int
pHttpStatus_
    }

-- | Contains a token which can be passed to the
-- @DescribeNodeAssociationStatus@ API call to get the status of the
-- association request.
associateNodeResponse_nodeAssociationStatusToken :: Lens.Lens' AssociateNodeResponse (Prelude.Maybe Prelude.Text)
associateNodeResponse_nodeAssociationStatusToken :: Lens' AssociateNodeResponse (Maybe Text)
associateNodeResponse_nodeAssociationStatusToken = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\AssociateNodeResponse' {Maybe Text
nodeAssociationStatusToken :: Maybe Text
$sel:nodeAssociationStatusToken:AssociateNodeResponse' :: AssociateNodeResponse -> Maybe Text
nodeAssociationStatusToken} -> Maybe Text
nodeAssociationStatusToken) (\s :: AssociateNodeResponse
s@AssociateNodeResponse' {} Maybe Text
a -> AssociateNodeResponse
s {$sel:nodeAssociationStatusToken:AssociateNodeResponse' :: Maybe Text
nodeAssociationStatusToken = Maybe Text
a} :: AssociateNodeResponse)

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

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