{-# 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.CloudWatch.PutDashboard
-- 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 dashboard if it does not already exist, or updates an existing
-- dashboard. If you update a dashboard, the entire contents are replaced
-- with what you specify here.
--
-- All dashboards in your account are global, not region-specific.
--
-- A simple way to create a dashboard using @PutDashboard@ is to copy an
-- existing dashboard. To copy an existing dashboard using the console, you
-- can load the dashboard and then use the View\/edit source command in the
-- Actions menu to display the JSON block for that dashboard. Another way
-- to copy a dashboard is to use @GetDashboard@, and then use the data
-- returned within @DashboardBody@ as the template for the new dashboard
-- when you call @PutDashboard@.
--
-- When you create a dashboard with @PutDashboard@, a good practice is to
-- add a text widget at the top of the dashboard with a message that the
-- dashboard was created by script and should not be changed in the
-- console. This message could also point console users to the location of
-- the @DashboardBody@ script or the CloudFormation template used to create
-- the dashboard.
module Amazonka.CloudWatch.PutDashboard
  ( -- * Creating a Request
    PutDashboard (..),
    newPutDashboard,

    -- * Request Lenses
    putDashboard_dashboardName,
    putDashboard_dashboardBody,

    -- * Destructuring the Response
    PutDashboardResponse (..),
    newPutDashboardResponse,

    -- * Response Lenses
    putDashboardResponse_dashboardValidationMessages,
    putDashboardResponse_httpStatus,
  )
where

import Amazonka.CloudWatch.Types
import qualified Amazonka.Core as Core
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:/ 'newPutDashboard' smart constructor.
data PutDashboard = PutDashboard'
  { -- | The name of the dashboard. If a dashboard with this name already exists,
    -- this call modifies that dashboard, replacing its current contents.
    -- Otherwise, a new dashboard is created. The maximum length is 255, and
    -- valid characters are A-Z, a-z, 0-9, \"-\", and \"_\". This parameter is
    -- required.
    PutDashboard -> Text
dashboardName :: Prelude.Text,
    -- | The detailed information about the dashboard in JSON format, including
    -- the widgets to include and their location on the dashboard. This
    -- parameter is required.
    --
    -- For more information about the syntax, see
    -- <https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/CloudWatch-Dashboard-Body-Structure.html Dashboard Body Structure and Syntax>.
    PutDashboard -> Text
dashboardBody :: Prelude.Text
  }
  deriving (PutDashboard -> PutDashboard -> Bool
(PutDashboard -> PutDashboard -> Bool)
-> (PutDashboard -> PutDashboard -> Bool) -> Eq PutDashboard
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PutDashboard -> PutDashboard -> Bool
$c/= :: PutDashboard -> PutDashboard -> Bool
== :: PutDashboard -> PutDashboard -> Bool
$c== :: PutDashboard -> PutDashboard -> Bool
Prelude.Eq, ReadPrec [PutDashboard]
ReadPrec PutDashboard
Int -> ReadS PutDashboard
ReadS [PutDashboard]
(Int -> ReadS PutDashboard)
-> ReadS [PutDashboard]
-> ReadPrec PutDashboard
-> ReadPrec [PutDashboard]
-> Read PutDashboard
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [PutDashboard]
$creadListPrec :: ReadPrec [PutDashboard]
readPrec :: ReadPrec PutDashboard
$creadPrec :: ReadPrec PutDashboard
readList :: ReadS [PutDashboard]
$creadList :: ReadS [PutDashboard]
readsPrec :: Int -> ReadS PutDashboard
$creadsPrec :: Int -> ReadS PutDashboard
Prelude.Read, Int -> PutDashboard -> ShowS
[PutDashboard] -> ShowS
PutDashboard -> String
(Int -> PutDashboard -> ShowS)
-> (PutDashboard -> String)
-> ([PutDashboard] -> ShowS)
-> Show PutDashboard
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PutDashboard] -> ShowS
$cshowList :: [PutDashboard] -> ShowS
show :: PutDashboard -> String
$cshow :: PutDashboard -> String
showsPrec :: Int -> PutDashboard -> ShowS
$cshowsPrec :: Int -> PutDashboard -> ShowS
Prelude.Show, (forall x. PutDashboard -> Rep PutDashboard x)
-> (forall x. Rep PutDashboard x -> PutDashboard)
-> Generic PutDashboard
forall x. Rep PutDashboard x -> PutDashboard
forall x. PutDashboard -> Rep PutDashboard x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep PutDashboard x -> PutDashboard
$cfrom :: forall x. PutDashboard -> Rep PutDashboard x
Prelude.Generic)

-- |
-- Create a value of 'PutDashboard' 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:
--
-- 'dashboardName', 'putDashboard_dashboardName' - The name of the dashboard. If a dashboard with this name already exists,
-- this call modifies that dashboard, replacing its current contents.
-- Otherwise, a new dashboard is created. The maximum length is 255, and
-- valid characters are A-Z, a-z, 0-9, \"-\", and \"_\". This parameter is
-- required.
--
-- 'dashboardBody', 'putDashboard_dashboardBody' - The detailed information about the dashboard in JSON format, including
-- the widgets to include and their location on the dashboard. This
-- parameter is required.
--
-- For more information about the syntax, see
-- <https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/CloudWatch-Dashboard-Body-Structure.html Dashboard Body Structure and Syntax>.
newPutDashboard ::
  -- | 'dashboardName'
  Prelude.Text ->
  -- | 'dashboardBody'
  Prelude.Text ->
  PutDashboard
newPutDashboard :: Text -> Text -> PutDashboard
newPutDashboard Text
pDashboardName_ Text
pDashboardBody_ =
  PutDashboard' :: Text -> Text -> PutDashboard
PutDashboard'
    { $sel:dashboardName:PutDashboard' :: Text
dashboardName = Text
pDashboardName_,
      $sel:dashboardBody:PutDashboard' :: Text
dashboardBody = Text
pDashboardBody_
    }

-- | The name of the dashboard. If a dashboard with this name already exists,
-- this call modifies that dashboard, replacing its current contents.
-- Otherwise, a new dashboard is created. The maximum length is 255, and
-- valid characters are A-Z, a-z, 0-9, \"-\", and \"_\". This parameter is
-- required.
putDashboard_dashboardName :: Lens.Lens' PutDashboard Prelude.Text
putDashboard_dashboardName :: (Text -> f Text) -> PutDashboard -> f PutDashboard
putDashboard_dashboardName = (PutDashboard -> Text)
-> (PutDashboard -> Text -> PutDashboard)
-> Lens PutDashboard PutDashboard Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\PutDashboard' {Text
dashboardName :: Text
$sel:dashboardName:PutDashboard' :: PutDashboard -> Text
dashboardName} -> Text
dashboardName) (\s :: PutDashboard
s@PutDashboard' {} Text
a -> PutDashboard
s {$sel:dashboardName:PutDashboard' :: Text
dashboardName = Text
a} :: PutDashboard)

-- | The detailed information about the dashboard in JSON format, including
-- the widgets to include and their location on the dashboard. This
-- parameter is required.
--
-- For more information about the syntax, see
-- <https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/CloudWatch-Dashboard-Body-Structure.html Dashboard Body Structure and Syntax>.
putDashboard_dashboardBody :: Lens.Lens' PutDashboard Prelude.Text
putDashboard_dashboardBody :: (Text -> f Text) -> PutDashboard -> f PutDashboard
putDashboard_dashboardBody = (PutDashboard -> Text)
-> (PutDashboard -> Text -> PutDashboard)
-> Lens PutDashboard PutDashboard Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\PutDashboard' {Text
dashboardBody :: Text
$sel:dashboardBody:PutDashboard' :: PutDashboard -> Text
dashboardBody} -> Text
dashboardBody) (\s :: PutDashboard
s@PutDashboard' {} Text
a -> PutDashboard
s {$sel:dashboardBody:PutDashboard' :: Text
dashboardBody = Text
a} :: PutDashboard)

instance Core.AWSRequest PutDashboard where
  type AWSResponse PutDashboard = PutDashboardResponse
  request :: PutDashboard -> Request PutDashboard
request = Service -> PutDashboard -> Request PutDashboard
forall a. ToRequest a => Service -> a -> Request a
Request.postQuery Service
defaultService
  response :: Logger
-> Service
-> Proxy PutDashboard
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse PutDashboard)))
response =
    Text
-> (Int
    -> ResponseHeaders
    -> [Node]
    -> Either String (AWSResponse PutDashboard))
-> Logger
-> Service
-> Proxy PutDashboard
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse PutDashboard)))
forall (m :: * -> *) a.
MonadResource m =>
Text
-> (Int
    -> ResponseHeaders -> [Node] -> Either String (AWSResponse a))
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
Response.receiveXMLWrapper
      Text
"PutDashboardResult"
      ( \Int
s ResponseHeaders
h [Node]
x ->
          Maybe [DashboardValidationMessage] -> Int -> PutDashboardResponse
PutDashboardResponse'
            (Maybe [DashboardValidationMessage] -> Int -> PutDashboardResponse)
-> Either String (Maybe [DashboardValidationMessage])
-> Either String (Int -> PutDashboardResponse)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> ( [Node]
x [Node] -> Text -> Either String (Maybe [Node])
forall a. FromXML a => [Node] -> Text -> Either String (Maybe a)
Core..@? Text
"DashboardValidationMessages"
                            Either String (Maybe [Node]) -> [Node] -> Either String [Node]
forall (f :: * -> *) a. Functor f => f (Maybe a) -> a -> f a
Core..!@ [Node]
forall a. Monoid a => a
Prelude.mempty
                            Either String [Node]
-> ([Node] -> Either String (Maybe [DashboardValidationMessage]))
-> Either String (Maybe [DashboardValidationMessage])
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
Prelude.>>= ([Node] -> Either String [DashboardValidationMessage])
-> [Node] -> Either String (Maybe [DashboardValidationMessage])
forall (f :: * -> *) a b.
Applicative f =>
([a] -> f b) -> [a] -> f (Maybe b)
Core.may (Text -> [Node] -> Either String [DashboardValidationMessage]
forall a. FromXML a => Text -> [Node] -> Either String [a]
Core.parseXMLList Text
"member")
                        )
            Either String (Int -> PutDashboardResponse)
-> Either String Int -> Either String PutDashboardResponse
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 PutDashboard

instance Prelude.NFData PutDashboard

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

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

instance Core.ToQuery PutDashboard where
  toQuery :: PutDashboard -> QueryString
toQuery PutDashboard' {Text
dashboardBody :: Text
dashboardName :: Text
$sel:dashboardBody:PutDashboard' :: PutDashboard -> Text
$sel:dashboardName:PutDashboard' :: PutDashboard -> Text
..} =
    [QueryString] -> QueryString
forall a. Monoid a => [a] -> a
Prelude.mconcat
      [ ByteString
"Action"
          ByteString -> ByteString -> QueryString
forall a. ToQuery a => ByteString -> a -> QueryString
Core.=: (ByteString
"PutDashboard" :: Prelude.ByteString),
        ByteString
"Version"
          ByteString -> ByteString -> QueryString
forall a. ToQuery a => ByteString -> a -> QueryString
Core.=: (ByteString
"2010-08-01" :: Prelude.ByteString),
        ByteString
"DashboardName" ByteString -> Text -> QueryString
forall a. ToQuery a => ByteString -> a -> QueryString
Core.=: Text
dashboardName,
        ByteString
"DashboardBody" ByteString -> Text -> QueryString
forall a. ToQuery a => ByteString -> a -> QueryString
Core.=: Text
dashboardBody
      ]

-- | /See:/ 'newPutDashboardResponse' smart constructor.
data PutDashboardResponse = PutDashboardResponse'
  { -- | If the input for @PutDashboard@ was correct and the dashboard was
    -- successfully created or modified, this result is empty.
    --
    -- If this result includes only warning messages, then the input was valid
    -- enough for the dashboard to be created or modified, but some elements of
    -- the dashboard might not render.
    --
    -- If this result includes error messages, the input was not valid and the
    -- operation failed.
    PutDashboardResponse -> Maybe [DashboardValidationMessage]
dashboardValidationMessages :: Prelude.Maybe [DashboardValidationMessage],
    -- | The response's http status code.
    PutDashboardResponse -> Int
httpStatus :: Prelude.Int
  }
  deriving (PutDashboardResponse -> PutDashboardResponse -> Bool
(PutDashboardResponse -> PutDashboardResponse -> Bool)
-> (PutDashboardResponse -> PutDashboardResponse -> Bool)
-> Eq PutDashboardResponse
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PutDashboardResponse -> PutDashboardResponse -> Bool
$c/= :: PutDashboardResponse -> PutDashboardResponse -> Bool
== :: PutDashboardResponse -> PutDashboardResponse -> Bool
$c== :: PutDashboardResponse -> PutDashboardResponse -> Bool
Prelude.Eq, ReadPrec [PutDashboardResponse]
ReadPrec PutDashboardResponse
Int -> ReadS PutDashboardResponse
ReadS [PutDashboardResponse]
(Int -> ReadS PutDashboardResponse)
-> ReadS [PutDashboardResponse]
-> ReadPrec PutDashboardResponse
-> ReadPrec [PutDashboardResponse]
-> Read PutDashboardResponse
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [PutDashboardResponse]
$creadListPrec :: ReadPrec [PutDashboardResponse]
readPrec :: ReadPrec PutDashboardResponse
$creadPrec :: ReadPrec PutDashboardResponse
readList :: ReadS [PutDashboardResponse]
$creadList :: ReadS [PutDashboardResponse]
readsPrec :: Int -> ReadS PutDashboardResponse
$creadsPrec :: Int -> ReadS PutDashboardResponse
Prelude.Read, Int -> PutDashboardResponse -> ShowS
[PutDashboardResponse] -> ShowS
PutDashboardResponse -> String
(Int -> PutDashboardResponse -> ShowS)
-> (PutDashboardResponse -> String)
-> ([PutDashboardResponse] -> ShowS)
-> Show PutDashboardResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PutDashboardResponse] -> ShowS
$cshowList :: [PutDashboardResponse] -> ShowS
show :: PutDashboardResponse -> String
$cshow :: PutDashboardResponse -> String
showsPrec :: Int -> PutDashboardResponse -> ShowS
$cshowsPrec :: Int -> PutDashboardResponse -> ShowS
Prelude.Show, (forall x. PutDashboardResponse -> Rep PutDashboardResponse x)
-> (forall x. Rep PutDashboardResponse x -> PutDashboardResponse)
-> Generic PutDashboardResponse
forall x. Rep PutDashboardResponse x -> PutDashboardResponse
forall x. PutDashboardResponse -> Rep PutDashboardResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep PutDashboardResponse x -> PutDashboardResponse
$cfrom :: forall x. PutDashboardResponse -> Rep PutDashboardResponse x
Prelude.Generic)

-- |
-- Create a value of 'PutDashboardResponse' 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:
--
-- 'dashboardValidationMessages', 'putDashboardResponse_dashboardValidationMessages' - If the input for @PutDashboard@ was correct and the dashboard was
-- successfully created or modified, this result is empty.
--
-- If this result includes only warning messages, then the input was valid
-- enough for the dashboard to be created or modified, but some elements of
-- the dashboard might not render.
--
-- If this result includes error messages, the input was not valid and the
-- operation failed.
--
-- 'httpStatus', 'putDashboardResponse_httpStatus' - The response's http status code.
newPutDashboardResponse ::
  -- | 'httpStatus'
  Prelude.Int ->
  PutDashboardResponse
newPutDashboardResponse :: Int -> PutDashboardResponse
newPutDashboardResponse Int
pHttpStatus_ =
  PutDashboardResponse' :: Maybe [DashboardValidationMessage] -> Int -> PutDashboardResponse
PutDashboardResponse'
    { $sel:dashboardValidationMessages:PutDashboardResponse' :: Maybe [DashboardValidationMessage]
dashboardValidationMessages =
        Maybe [DashboardValidationMessage]
forall a. Maybe a
Prelude.Nothing,
      $sel:httpStatus:PutDashboardResponse' :: Int
httpStatus = Int
pHttpStatus_
    }

-- | If the input for @PutDashboard@ was correct and the dashboard was
-- successfully created or modified, this result is empty.
--
-- If this result includes only warning messages, then the input was valid
-- enough for the dashboard to be created or modified, but some elements of
-- the dashboard might not render.
--
-- If this result includes error messages, the input was not valid and the
-- operation failed.
putDashboardResponse_dashboardValidationMessages :: Lens.Lens' PutDashboardResponse (Prelude.Maybe [DashboardValidationMessage])
putDashboardResponse_dashboardValidationMessages :: (Maybe [DashboardValidationMessage]
 -> f (Maybe [DashboardValidationMessage]))
-> PutDashboardResponse -> f PutDashboardResponse
putDashboardResponse_dashboardValidationMessages = (PutDashboardResponse -> Maybe [DashboardValidationMessage])
-> (PutDashboardResponse
    -> Maybe [DashboardValidationMessage] -> PutDashboardResponse)
-> Lens
     PutDashboardResponse
     PutDashboardResponse
     (Maybe [DashboardValidationMessage])
     (Maybe [DashboardValidationMessage])
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\PutDashboardResponse' {Maybe [DashboardValidationMessage]
dashboardValidationMessages :: Maybe [DashboardValidationMessage]
$sel:dashboardValidationMessages:PutDashboardResponse' :: PutDashboardResponse -> Maybe [DashboardValidationMessage]
dashboardValidationMessages} -> Maybe [DashboardValidationMessage]
dashboardValidationMessages) (\s :: PutDashboardResponse
s@PutDashboardResponse' {} Maybe [DashboardValidationMessage]
a -> PutDashboardResponse
s {$sel:dashboardValidationMessages:PutDashboardResponse' :: Maybe [DashboardValidationMessage]
dashboardValidationMessages = Maybe [DashboardValidationMessage]
a} :: PutDashboardResponse) ((Maybe [DashboardValidationMessage]
  -> f (Maybe [DashboardValidationMessage]))
 -> PutDashboardResponse -> f PutDashboardResponse)
-> ((Maybe [DashboardValidationMessage]
     -> f (Maybe [DashboardValidationMessage]))
    -> Maybe [DashboardValidationMessage]
    -> f (Maybe [DashboardValidationMessage]))
-> (Maybe [DashboardValidationMessage]
    -> f (Maybe [DashboardValidationMessage]))
-> PutDashboardResponse
-> f PutDashboardResponse
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. AnIso
  [DashboardValidationMessage]
  [DashboardValidationMessage]
  [DashboardValidationMessage]
  [DashboardValidationMessage]
-> Iso
     (Maybe [DashboardValidationMessage])
     (Maybe [DashboardValidationMessage])
     (Maybe [DashboardValidationMessage])
     (Maybe [DashboardValidationMessage])
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
  [DashboardValidationMessage]
  [DashboardValidationMessage]
  [DashboardValidationMessage]
  [DashboardValidationMessage]
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

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

instance Prelude.NFData PutDashboardResponse