{-# 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-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)
--
-- 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.Lens as Lens
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
(AssociateNode -> AssociateNode -> Bool)
-> (AssociateNode -> AssociateNode -> Bool) -> Eq AssociateNode
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
(Int -> AssociateNode -> ShowS)
-> (AssociateNode -> String)
-> ([AssociateNode] -> ShowS)
-> Show AssociateNode
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. AssociateNode -> Rep AssociateNode x)
-> (forall x. Rep AssociateNode x -> AssociateNode)
-> Generic AssociateNode
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' :: Text -> Text -> [EngineAttribute] -> AssociateNode
AssociateNode'
    { $sel:serverName:AssociateNode' :: Text
serverName = Text
pServerName_,
      $sel:nodeName:AssociateNode' :: Text
nodeName = Text
pNodeName_,
      $sel:engineAttributes:AssociateNode' :: [EngineAttribute]
engineAttributes = [EngineAttribute]
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 :: (Text -> f Text) -> AssociateNode -> f AssociateNode
associateNode_serverName = (AssociateNode -> Text)
-> (AssociateNode -> Text -> AssociateNode)
-> Lens AssociateNode AssociateNode Text Text
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 :: (Text -> f Text) -> AssociateNode -> f AssociateNode
associateNode_nodeName = (AssociateNode -> Text)
-> (AssociateNode -> Text -> AssociateNode)
-> Lens AssociateNode AssociateNode Text Text
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 :: ([EngineAttribute] -> f [EngineAttribute])
-> AssociateNode -> f AssociateNode
associateNode_engineAttributes = (AssociateNode -> [EngineAttribute])
-> (AssociateNode -> [EngineAttribute] -> AssociateNode)
-> Lens
     AssociateNode AssociateNode [EngineAttribute] [EngineAttribute]
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) (([EngineAttribute] -> f [EngineAttribute])
 -> AssociateNode -> f AssociateNode)
-> (([EngineAttribute] -> f [EngineAttribute])
    -> [EngineAttribute] -> f [EngineAttribute])
-> ([EngineAttribute] -> f [EngineAttribute])
-> AssociateNode
-> f AssociateNode
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. ([EngineAttribute] -> f [EngineAttribute])
-> [EngineAttribute] -> f [EngineAttribute]
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 :: AssociateNode -> Request AssociateNode
request = Service -> AssociateNode -> Request AssociateNode
forall a. (ToRequest a, ToJSON a) => Service -> a -> Request a
Request.postJSON Service
defaultService
  response :: Logger
-> Service
-> Proxy AssociateNode
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse AssociateNode)))
response =
    (Int
 -> ResponseHeaders
 -> Object
 -> Either String (AWSResponse AssociateNode))
-> Logger
-> Service
-> Proxy AssociateNode
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse AssociateNode)))
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 ->
          Maybe Text -> Int -> AssociateNodeResponse
AssociateNodeResponse'
            (Maybe Text -> Int -> AssociateNodeResponse)
-> Either String (Maybe Text)
-> Either String (Int -> AssociateNodeResponse)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x Object -> Text -> Either String (Maybe Text)
forall a. FromJSON a => Object -> Text -> Either String (Maybe a)
Core..?> Text
"NodeAssociationStatusToken")
            Either String (Int -> AssociateNodeResponse)
-> Either String Int -> Either String AssociateNodeResponse
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Int -> Either String Int
forall (f :: * -> *) a. Applicative f => a -> f a
Prelude.pure (Int -> Int
forall a. Enum a => a -> Int
Prelude.fromEnum Int
s))
      )

instance Prelude.Hashable AssociateNode

instance Prelude.NFData AssociateNode

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

instance Core.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
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"ServerName" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Text
serverName),
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"NodeName" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Text
nodeName),
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just
              (Text
"EngineAttributes" Text -> [EngineAttribute] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= [EngineAttribute]
engineAttributes)
          ]
      )

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

instance Core.ToQuery AssociateNode where
  toQuery :: AssociateNode -> QueryString
toQuery = QueryString -> AssociateNode -> QueryString
forall a b. a -> b -> a
Prelude.const QueryString
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
(AssociateNodeResponse -> AssociateNodeResponse -> Bool)
-> (AssociateNodeResponse -> AssociateNodeResponse -> Bool)
-> Eq AssociateNodeResponse
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]
(Int -> ReadS AssociateNodeResponse)
-> ReadS [AssociateNodeResponse]
-> ReadPrec AssociateNodeResponse
-> ReadPrec [AssociateNodeResponse]
-> Read 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
(Int -> AssociateNodeResponse -> ShowS)
-> (AssociateNodeResponse -> String)
-> ([AssociateNodeResponse] -> ShowS)
-> Show AssociateNodeResponse
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. AssociateNodeResponse -> Rep AssociateNodeResponse x)
-> (forall x. Rep AssociateNodeResponse x -> AssociateNodeResponse)
-> Generic AssociateNodeResponse
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' :: Maybe Text -> Int -> AssociateNodeResponse
AssociateNodeResponse'
    { $sel:nodeAssociationStatusToken:AssociateNodeResponse' :: Maybe Text
nodeAssociationStatusToken =
        Maybe Text
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 :: (Maybe Text -> f (Maybe Text))
-> AssociateNodeResponse -> f AssociateNodeResponse
associateNodeResponse_nodeAssociationStatusToken = (AssociateNodeResponse -> Maybe Text)
-> (AssociateNodeResponse -> Maybe Text -> AssociateNodeResponse)
-> Lens
     AssociateNodeResponse
     AssociateNodeResponse
     (Maybe Text)
     (Maybe Text)
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 :: (Int -> f Int) -> AssociateNodeResponse -> f AssociateNodeResponse
associateNodeResponse_httpStatus = (AssociateNodeResponse -> Int)
-> (AssociateNodeResponse -> Int -> AssociateNodeResponse)
-> Lens AssociateNodeResponse AssociateNodeResponse Int Int
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