{-# 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.RestoreServer
-- 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)
--
-- Restores a backup to a server that is in a @CONNECTION_LOST@, @HEALTHY@,
-- @RUNNING@, @UNHEALTHY@, or @TERMINATED@ state. When you run
-- RestoreServer, the server\'s EC2 instance is deleted, and a new EC2
-- instance is configured. RestoreServer maintains the existing server
-- endpoint, so configuration management of the server\'s client devices
-- (nodes) should continue to work.
--
-- Restoring from a backup is performed by creating a new EC2 instance. If
-- restoration is successful, and the server is in a @HEALTHY@ state, AWS
-- OpsWorks CM switches traffic over to the new instance. After restoration
-- is finished, the old EC2 instance is maintained in a @Running@ or
-- @Stopped@ state, but is eventually terminated.
--
-- This operation is asynchronous.
--
-- An @InvalidStateException@ is thrown when the server is not in a valid
-- state. A @ResourceNotFoundException@ is thrown when the server does not
-- exist. A @ValidationException@ is raised when parameters of the request
-- are not valid.
module Amazonka.OpsWorksCM.RestoreServer
  ( -- * Creating a Request
    RestoreServer (..),
    newRestoreServer,

    -- * Request Lenses
    restoreServer_keyPair,
    restoreServer_instanceType,
    restoreServer_backupId,
    restoreServer_serverName,

    -- * Destructuring the Response
    RestoreServerResponse (..),
    newRestoreServerResponse,

    -- * Response Lenses
    restoreServerResponse_server,
    restoreServerResponse_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:/ 'newRestoreServer' smart constructor.
data RestoreServer = RestoreServer'
  { -- | The name of the key pair to set on the new EC2 instance. This can be
    -- helpful if the administrator no longer has the SSH key.
    RestoreServer -> Maybe Text
keyPair :: Prelude.Maybe Prelude.Text,
    -- | The type of instance to restore. Valid values must be specified in the
    -- following format: @^([cm][34]|t2).*@ For example, @m5.large@. Valid
    -- values are @m5.large@, @r5.xlarge@, and @r5.2xlarge@. If you do not
    -- specify this parameter, RestoreServer uses the instance type from the
    -- specified backup.
    RestoreServer -> Maybe Text
instanceType :: Prelude.Maybe Prelude.Text,
    -- | The ID of the backup that you want to use to restore a server.
    RestoreServer -> Text
backupId :: Prelude.Text,
    -- | The name of the server that you want to restore.
    RestoreServer -> Text
serverName :: Prelude.Text
  }
  deriving (RestoreServer -> RestoreServer -> Bool
(RestoreServer -> RestoreServer -> Bool)
-> (RestoreServer -> RestoreServer -> Bool) -> Eq RestoreServer
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RestoreServer -> RestoreServer -> Bool
$c/= :: RestoreServer -> RestoreServer -> Bool
== :: RestoreServer -> RestoreServer -> Bool
$c== :: RestoreServer -> RestoreServer -> Bool
Prelude.Eq, ReadPrec [RestoreServer]
ReadPrec RestoreServer
Int -> ReadS RestoreServer
ReadS [RestoreServer]
(Int -> ReadS RestoreServer)
-> ReadS [RestoreServer]
-> ReadPrec RestoreServer
-> ReadPrec [RestoreServer]
-> Read RestoreServer
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [RestoreServer]
$creadListPrec :: ReadPrec [RestoreServer]
readPrec :: ReadPrec RestoreServer
$creadPrec :: ReadPrec RestoreServer
readList :: ReadS [RestoreServer]
$creadList :: ReadS [RestoreServer]
readsPrec :: Int -> ReadS RestoreServer
$creadsPrec :: Int -> ReadS RestoreServer
Prelude.Read, Int -> RestoreServer -> ShowS
[RestoreServer] -> ShowS
RestoreServer -> String
(Int -> RestoreServer -> ShowS)
-> (RestoreServer -> String)
-> ([RestoreServer] -> ShowS)
-> Show RestoreServer
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RestoreServer] -> ShowS
$cshowList :: [RestoreServer] -> ShowS
show :: RestoreServer -> String
$cshow :: RestoreServer -> String
showsPrec :: Int -> RestoreServer -> ShowS
$cshowsPrec :: Int -> RestoreServer -> ShowS
Prelude.Show, (forall x. RestoreServer -> Rep RestoreServer x)
-> (forall x. Rep RestoreServer x -> RestoreServer)
-> Generic RestoreServer
forall x. Rep RestoreServer x -> RestoreServer
forall x. RestoreServer -> Rep RestoreServer x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep RestoreServer x -> RestoreServer
$cfrom :: forall x. RestoreServer -> Rep RestoreServer x
Prelude.Generic)

-- |
-- Create a value of 'RestoreServer' 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:
--
-- 'keyPair', 'restoreServer_keyPair' - The name of the key pair to set on the new EC2 instance. This can be
-- helpful if the administrator no longer has the SSH key.
--
-- 'instanceType', 'restoreServer_instanceType' - The type of instance to restore. Valid values must be specified in the
-- following format: @^([cm][34]|t2).*@ For example, @m5.large@. Valid
-- values are @m5.large@, @r5.xlarge@, and @r5.2xlarge@. If you do not
-- specify this parameter, RestoreServer uses the instance type from the
-- specified backup.
--
-- 'backupId', 'restoreServer_backupId' - The ID of the backup that you want to use to restore a server.
--
-- 'serverName', 'restoreServer_serverName' - The name of the server that you want to restore.
newRestoreServer ::
  -- | 'backupId'
  Prelude.Text ->
  -- | 'serverName'
  Prelude.Text ->
  RestoreServer
newRestoreServer :: Text -> Text -> RestoreServer
newRestoreServer Text
pBackupId_ Text
pServerName_ =
  RestoreServer' :: Maybe Text -> Maybe Text -> Text -> Text -> RestoreServer
RestoreServer'
    { $sel:keyPair:RestoreServer' :: Maybe Text
keyPair = Maybe Text
forall a. Maybe a
Prelude.Nothing,
      $sel:instanceType:RestoreServer' :: Maybe Text
instanceType = Maybe Text
forall a. Maybe a
Prelude.Nothing,
      $sel:backupId:RestoreServer' :: Text
backupId = Text
pBackupId_,
      $sel:serverName:RestoreServer' :: Text
serverName = Text
pServerName_
    }

-- | The name of the key pair to set on the new EC2 instance. This can be
-- helpful if the administrator no longer has the SSH key.
restoreServer_keyPair :: Lens.Lens' RestoreServer (Prelude.Maybe Prelude.Text)
restoreServer_keyPair :: (Maybe Text -> f (Maybe Text)) -> RestoreServer -> f RestoreServer
restoreServer_keyPair = (RestoreServer -> Maybe Text)
-> (RestoreServer -> Maybe Text -> RestoreServer)
-> Lens RestoreServer RestoreServer (Maybe Text) (Maybe Text)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\RestoreServer' {Maybe Text
keyPair :: Maybe Text
$sel:keyPair:RestoreServer' :: RestoreServer -> Maybe Text
keyPair} -> Maybe Text
keyPair) (\s :: RestoreServer
s@RestoreServer' {} Maybe Text
a -> RestoreServer
s {$sel:keyPair:RestoreServer' :: Maybe Text
keyPair = Maybe Text
a} :: RestoreServer)

-- | The type of instance to restore. Valid values must be specified in the
-- following format: @^([cm][34]|t2).*@ For example, @m5.large@. Valid
-- values are @m5.large@, @r5.xlarge@, and @r5.2xlarge@. If you do not
-- specify this parameter, RestoreServer uses the instance type from the
-- specified backup.
restoreServer_instanceType :: Lens.Lens' RestoreServer (Prelude.Maybe Prelude.Text)
restoreServer_instanceType :: (Maybe Text -> f (Maybe Text)) -> RestoreServer -> f RestoreServer
restoreServer_instanceType = (RestoreServer -> Maybe Text)
-> (RestoreServer -> Maybe Text -> RestoreServer)
-> Lens RestoreServer RestoreServer (Maybe Text) (Maybe Text)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\RestoreServer' {Maybe Text
instanceType :: Maybe Text
$sel:instanceType:RestoreServer' :: RestoreServer -> Maybe Text
instanceType} -> Maybe Text
instanceType) (\s :: RestoreServer
s@RestoreServer' {} Maybe Text
a -> RestoreServer
s {$sel:instanceType:RestoreServer' :: Maybe Text
instanceType = Maybe Text
a} :: RestoreServer)

-- | The ID of the backup that you want to use to restore a server.
restoreServer_backupId :: Lens.Lens' RestoreServer Prelude.Text
restoreServer_backupId :: (Text -> f Text) -> RestoreServer -> f RestoreServer
restoreServer_backupId = (RestoreServer -> Text)
-> (RestoreServer -> Text -> RestoreServer)
-> Lens RestoreServer RestoreServer Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\RestoreServer' {Text
backupId :: Text
$sel:backupId:RestoreServer' :: RestoreServer -> Text
backupId} -> Text
backupId) (\s :: RestoreServer
s@RestoreServer' {} Text
a -> RestoreServer
s {$sel:backupId:RestoreServer' :: Text
backupId = Text
a} :: RestoreServer)

-- | The name of the server that you want to restore.
restoreServer_serverName :: Lens.Lens' RestoreServer Prelude.Text
restoreServer_serverName :: (Text -> f Text) -> RestoreServer -> f RestoreServer
restoreServer_serverName = (RestoreServer -> Text)
-> (RestoreServer -> Text -> RestoreServer)
-> Lens RestoreServer RestoreServer Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\RestoreServer' {Text
serverName :: Text
$sel:serverName:RestoreServer' :: RestoreServer -> Text
serverName} -> Text
serverName) (\s :: RestoreServer
s@RestoreServer' {} Text
a -> RestoreServer
s {$sel:serverName:RestoreServer' :: Text
serverName = Text
a} :: RestoreServer)

instance Core.AWSRequest RestoreServer where
  type
    AWSResponse RestoreServer =
      RestoreServerResponse
  request :: RestoreServer -> Request RestoreServer
request = Service -> RestoreServer -> Request RestoreServer
forall a. (ToRequest a, ToJSON a) => Service -> a -> Request a
Request.postJSON Service
defaultService
  response :: Logger
-> Service
-> Proxy RestoreServer
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse RestoreServer)))
response =
    (Int
 -> ResponseHeaders
 -> Object
 -> Either String (AWSResponse RestoreServer))
-> Logger
-> Service
-> Proxy RestoreServer
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse RestoreServer)))
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 Server -> Int -> RestoreServerResponse
RestoreServerResponse'
            (Maybe Server -> Int -> RestoreServerResponse)
-> Either String (Maybe Server)
-> Either String (Int -> RestoreServerResponse)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x Object -> Text -> Either String (Maybe Server)
forall a. FromJSON a => Object -> Text -> Either String (Maybe a)
Core..?> Text
"Server")
            Either String (Int -> RestoreServerResponse)
-> Either String Int -> Either String RestoreServerResponse
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 RestoreServer

instance Prelude.NFData RestoreServer

instance Core.ToHeaders RestoreServer where
  toHeaders :: RestoreServer -> ResponseHeaders
toHeaders =
    ResponseHeaders -> RestoreServer -> 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.RestoreServer" ::
                          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 RestoreServer where
  toJSON :: RestoreServer -> Value
toJSON RestoreServer' {Maybe Text
Text
serverName :: Text
backupId :: Text
instanceType :: Maybe Text
keyPair :: Maybe Text
$sel:serverName:RestoreServer' :: RestoreServer -> Text
$sel:backupId:RestoreServer' :: RestoreServer -> Text
$sel:instanceType:RestoreServer' :: RestoreServer -> Maybe Text
$sel:keyPair:RestoreServer' :: RestoreServer -> Maybe Text
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Text
"KeyPair" 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
keyPair,
            (Text
"InstanceType" 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
instanceType,
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"BackupId" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Text
backupId),
            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)
          ]
      )

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

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

-- | /See:/ 'newRestoreServerResponse' smart constructor.
data RestoreServerResponse = RestoreServerResponse'
  { RestoreServerResponse -> Maybe Server
server :: Prelude.Maybe Server,
    -- | The response's http status code.
    RestoreServerResponse -> Int
httpStatus :: Prelude.Int
  }
  deriving (RestoreServerResponse -> RestoreServerResponse -> Bool
(RestoreServerResponse -> RestoreServerResponse -> Bool)
-> (RestoreServerResponse -> RestoreServerResponse -> Bool)
-> Eq RestoreServerResponse
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RestoreServerResponse -> RestoreServerResponse -> Bool
$c/= :: RestoreServerResponse -> RestoreServerResponse -> Bool
== :: RestoreServerResponse -> RestoreServerResponse -> Bool
$c== :: RestoreServerResponse -> RestoreServerResponse -> Bool
Prelude.Eq, Int -> RestoreServerResponse -> ShowS
[RestoreServerResponse] -> ShowS
RestoreServerResponse -> String
(Int -> RestoreServerResponse -> ShowS)
-> (RestoreServerResponse -> String)
-> ([RestoreServerResponse] -> ShowS)
-> Show RestoreServerResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RestoreServerResponse] -> ShowS
$cshowList :: [RestoreServerResponse] -> ShowS
show :: RestoreServerResponse -> String
$cshow :: RestoreServerResponse -> String
showsPrec :: Int -> RestoreServerResponse -> ShowS
$cshowsPrec :: Int -> RestoreServerResponse -> ShowS
Prelude.Show, (forall x. RestoreServerResponse -> Rep RestoreServerResponse x)
-> (forall x. Rep RestoreServerResponse x -> RestoreServerResponse)
-> Generic RestoreServerResponse
forall x. Rep RestoreServerResponse x -> RestoreServerResponse
forall x. RestoreServerResponse -> Rep RestoreServerResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep RestoreServerResponse x -> RestoreServerResponse
$cfrom :: forall x. RestoreServerResponse -> Rep RestoreServerResponse x
Prelude.Generic)

-- |
-- Create a value of 'RestoreServerResponse' 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:
--
-- 'server', 'restoreServerResponse_server' - Undocumented member.
--
-- 'httpStatus', 'restoreServerResponse_httpStatus' - The response's http status code.
newRestoreServerResponse ::
  -- | 'httpStatus'
  Prelude.Int ->
  RestoreServerResponse
newRestoreServerResponse :: Int -> RestoreServerResponse
newRestoreServerResponse Int
pHttpStatus_ =
  RestoreServerResponse' :: Maybe Server -> Int -> RestoreServerResponse
RestoreServerResponse'
    { $sel:server:RestoreServerResponse' :: Maybe Server
server = Maybe Server
forall a. Maybe a
Prelude.Nothing,
      $sel:httpStatus:RestoreServerResponse' :: Int
httpStatus = Int
pHttpStatus_
    }

-- | Undocumented member.
restoreServerResponse_server :: Lens.Lens' RestoreServerResponse (Prelude.Maybe Server)
restoreServerResponse_server :: (Maybe Server -> f (Maybe Server))
-> RestoreServerResponse -> f RestoreServerResponse
restoreServerResponse_server = (RestoreServerResponse -> Maybe Server)
-> (RestoreServerResponse -> Maybe Server -> RestoreServerResponse)
-> Lens
     RestoreServerResponse
     RestoreServerResponse
     (Maybe Server)
     (Maybe Server)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\RestoreServerResponse' {Maybe Server
server :: Maybe Server
$sel:server:RestoreServerResponse' :: RestoreServerResponse -> Maybe Server
server} -> Maybe Server
server) (\s :: RestoreServerResponse
s@RestoreServerResponse' {} Maybe Server
a -> RestoreServerResponse
s {$sel:server:RestoreServerResponse' :: Maybe Server
server = Maybe Server
a} :: RestoreServerResponse)

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

instance Prelude.NFData RestoreServerResponse