module Network.PagerDuty.REST.EscalationPolicies.EscalationRules
(
listRules
, CreateRule
, createRule
, crEscalationDelayInMinutes
, crTargets
, getRule
, UpdateRules
, updateRules
, urEscalationRules
, UpdateRule
, updateRule
, urEscalationDelayInMinutes
, urTargets
, deleteRule
, HasUserInfo (..)
, Target (..)
, _TSchedule
, _TUser
, ScheduleTarget
, stId
, stName
, Rule
, rId
, rEscalationDelayInMinutes
, rTargets
) where
import Control.Applicative ((<$>))
import Control.Lens hiding ((.=))
import Data.Aeson
import qualified Data.HashMap.Strict as Map
import Data.Text (Text)
import Network.HTTP.Types
import Network.PagerDuty.REST.Users
import Network.PagerDuty.Internal.TH
import Network.PagerDuty.Internal.Types
default (Path)
rules :: EscalationPolicyId -> Path
rules p = "escalation_policies" % p % "escalation_rules"
data TargetType
= TargetSchedule
| TargetUser
deriveJSONWith (dropped 6 underscored) ''TargetType
data TargetId
= ScheduleId ScheduleId
| UserId UserId
deriving (Eq, Show)
instance FromJSON TargetId where
parseJSON = withObject "target" $ \o -> do
t <- o .: "type"
case t of
TargetSchedule -> ScheduleId <$> o .: "id"
TargetUser -> UserId <$> o .: "id"
instance ToJSON TargetId where
toJSON (ScheduleId s) = object ["type" .= TargetSchedule, "id" .= s]
toJSON (UserId u) = object ["type" .= TargetUser, "id" .= u]
data ScheduleTarget = ScheduleTarget
{ _stId :: ScheduleId
, _stName :: Text
} deriving (Eq, Show)
deriveJSON ''ScheduleTarget
makeLens "_stId" ''ScheduleTarget
makeLens "_stName" ''ScheduleTarget
data Target
= TSchedule ScheduleTarget
| TUser UserInfo
deriving (Eq, Show)
makePrisms ''Target
instance FromJSON Target where
parseJSON = withObject "target" $ \o -> do
t <- o .: "type"
case t of
TargetSchedule -> TSchedule <$> parseJSON (Object o)
TargetUser -> TUser <$> parseJSON (Object o)
instance ToJSON Target where
toJSON t = Object (Map.insert "type" (String k) o)
where
(k, Object o) = case t of
TSchedule s -> ("schedule", toJSON s)
TUser u -> ("user", toJSON u)
data Rule = Rule
{ _rId :: EscalationRuleId
, _rEscalationDelayInMinutes :: !Int
, _rTargets :: [Target]
} deriving (Eq, Show)
deriveJSON ''Rule
makeLens "_rId" ''Rule
makeLens "_rEscalationDelayInMinutes" ''Rule
makeLens "_rTargets" ''Rule
listRules :: EscalationPolicyId -> Request Empty s [Rule]
listRules p = empty & path .~ rules p
getRule :: EscalationPolicyId -> EscalationRuleId -> Request Empty s Rule
getRule p r = empty & path .~ rules p % r
data CreateRule = CreateRule
{ _crEscalationDelayInMinutes :: !Int
, _crTargets :: [TargetId]
} deriving (Eq, Show)
deriveJSON ''CreateRule
instance QueryLike CreateRule where
toQuery = const []
makeLens "_crEscalationDelayInMinutes" ''CreateRule
makeLens "_crTargets" ''CreateRule
createRule :: EscalationPolicyId
-> Int
-> [TargetId]
-> Request CreateRule s Rule
createRule p n ts =
mk CreateRule
{ _crEscalationDelayInMinutes = n
, _crTargets = ts
} & meth .~ POST
& path .~ rules p
newtype UpdateRules = UpdateRules
{ _urEscalationRules :: [TargetId]
} deriving (Eq, Show)
deriveJSON ''UpdateRules
instance QueryLike UpdateRules where
toQuery = const []
makeLens "_urEscalationRules" ''UpdateRules
updateRules :: EscalationPolicyId
-> [TargetId]
-> Request UpdateRules s [Rule]
updateRules p rs =
mk UpdateRules
{ _urEscalationRules = rs
} & meth .~ PUT
& path .~ rules p
data UpdateRule = UpdateRule
{ _urEscalationDelayInMinutes :: Maybe Int
, _urTargets :: [TargetId]
} deriving (Eq, Show)
deriveJSON ''UpdateRule
instance QueryLike UpdateRule where
toQuery = const []
makeLens "_urEscalationDelayInMinutes" ''UpdateRule
makeLens "_urTargets" ''UpdateRule
updateRule :: EscalationPolicyId -> EscalationRuleId -> Request UpdateRule s Rule
updateRule p r =
mk UpdateRule
{ _urEscalationDelayInMinutes = Nothing
, _urTargets = []
} & meth .~ PUT
& path .~ rules p % r
deleteRule :: EscalationPolicyId -> EscalationRuleId -> Request Empty s Empty
deleteRule p r = empty & meth .~ DELETE & path .~ rules p % r