@@ -2,6 +2,7 @@
22{-# LANGUAGE ExtendedDefaultRules #-}
33{-# LANGUAGE FlexibleInstances #-}
44{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5+{-# LANGUAGE LambdaCase #-}
56{-# LANGUAGE OverloadedStrings #-}
67{-# LANGUAGE TemplateHaskell #-}
78{-# LANGUAGE TypeFamilies #-}
@@ -9,7 +10,7 @@
910{-# OPTIONS_GHC -fno-warn-type-defaults #-}
1011
1112-- Module : Network.PagerDuty.REST.Incidents
12−-- Copyright : (c) 2013-2014 Brendan Hay <brendan.g.hay@gmail.com>
13+-- Copyright : (c) 2013-2015 Brendan Hay <brendan.g.hay@gmail.com>
1314-- License : This Source Code Form is subject to the terms of
1415-- the Mozilla Public License, v. 2.0.
1516-- A copy of the MPL can be found in the LICENSE file or
@@ -48,21 +49,101 @@
4849-- /See:/ <http://developer.pagerduty.com/documentation/rest/incidents>
4950module Network.PagerDuty.REST.Incidents
5051 (
51−
52+ -- * List Incidents
53+ ListIncidents
54+ , listIncidents
55+ , lsSince
56+ , lsUntil
57+ , lsDateRange
58+ , lsStatus
59+ , lsIncidentKey
60+ , lsService
61+ , lsAssignedToUser
62+ , lsTimeZone
63+ , lsSortBy
64+
65+ -- * Count Incidents
66+ , CountIncidents
67+ , countIncidents
68+ , cSince
69+ , cUntil
70+ , cDateRange
71+ , cStatus
72+ , cIncidentKey
73+ , cService
74+ , cAssignedToUser
75+
76+ -- * Get Incident
77+ , getIncident
78+
79+ -- * Update Incidents
80+ , UpdateIncidents
81+ , updateIncidents
82+ , updateIncidentsBasic
83+ , uiIncidents
84+
85+ -- * Resolve Incident
86+ , resolveIncident
87+ , resolveIncidentBasic
88+
89+ -- * Acknowledge Incident
90+ , acknowledgeIncident
91+ , acknowledgeIncidentBasic
92+
93+ -- * Reassign Incident
94+ , ReassignIncident
95+ , reassignIncident
96+ , riEscalationPolicy
97+ , riEscalationLevel
98+ , riAssignedToUser
99+
100+ -- * Types
101+ , Sort (..)
102+ , _Desc
103+ , _Asc
104+
105+ , Field (..)
106+
107+ , IncidentStatus (..)
108+ , UpdateStatus (..)
109+
110+ , UpdateIncident
111+ , iiId
112+ , iiStatus
113+ , iiEscalationLevel
114+ , iiEscalationPolicy
115+ , iiAssignedToUser
116+
117+ , UpdatedIncidents
118+ , uiId
119+ , uiStatus
120+ , uiIncidentNumber
121+ , uiUrl
122+ , uiError
123+
124+ , Assignee
125+ , aAt
126+ , aObject
127+
128+ , HasIncident (..)
129+ , Incident
52130 ) where
53131
54−import Control.Applicative hiding (empty)
55−import Control.Lens
56−import Data.Aeson
57−import Data.ByteString.Conversion (ToByteString(..))
58−import Data.Default
59−import Data.Monoid hiding (All)
60−import Data.Text (Text)
61−import Data.Time
62−import Network.HTTP.Types
63−import Network.PagerDuty.Query
64−import Network.PagerDuty.TH
65−import Network.PagerDuty.Types
132+import Control.Applicative hiding (empty)
133+import Control.Lens hiding (Empty)
134+import Data.Aeson
135+import Data.ByteString.Conversion (ToByteString (..),
136+ toByteString')
137+import Data.Default.Class
138+import Data.Maybe
139+import Data.Monoid hiding (All)
140+import Data.Text (Text)
141+import qualified Data.Text.Encoding as Text
142+import Data.Time
143+import Network.HTTP.Types
144+import Network.PagerDuty.Internal.Query
145+import Network.PagerDuty.Internal.TH
146+import Network.PagerDuty.Internal.Types
66147
67148default (Path)
68149
@@ -101,19 +182,28 @@ instance QueryValues Range where
101182 queryValues All = ["all"]
102183
103184data IncidentStatus
104− = Triggered
105− | Acknowledged
106− | Resolved
107− | Other Text
185+ = ITriggered
186+ | IAcknowledged
187+ | IResolved
188+ | IOther Text
108189 deriving (Eq, Show)
109190
110−deriveNullary ''IncidentStatus
111−
112191instance ToByteString IncidentStatus where
113− builder Triggered = "triggered"
114− builder Acknowledged = "acknowledged"
115− builder Resolved = "resolved"
116− builder (Other t) = builder t
192+ builder = \case
193+ ITriggered -> "triggered"
194+ IAcknowledged -> "acknowledged"
195+ IResolved -> "resolved"
196+ IOther t -> builder t
197+
198+instance FromJSON IncidentStatus where
199+ parseJSON = withText "status" $ pure . \case
200+ "triggered" -> ITriggered
201+ "acknowledged" -> IAcknowledged
202+ "resolved" -> IResolved
203+ t -> IOther t
204+
205+instance ToJSON IncidentStatus where
206+ toJSON = String . Text.decodeUtf8 . toByteString'
117207
118208data Assignee = Assignee
119209 { _aAt :: Date
@@ -123,76 +213,104 @@ data Assignee = Assignee
123213deriveRecord ''Assignee
124214
125215data Incident = Incident
126− { _iIncidentNumber :: !Int
127− , _iStatus :: !IncidentStatus
128− , _iCreatedOn :: Date
129− , _iHtmlUrl :: Text
130− , _iIncidentKey :: Maybe IncidentKey
131− , _iService :: Object
132− , _iEscalationPolicy :: Maybe Object
133− , _iAssignedTo :: [Assignee]
134− , _iAcknowledgers :: [Assignee]
135− , _iLastStatusChangeBy :: Maybe Assignee
136− , _iLastStatusChangeOn :: Maybe Date
137− , _iTriggerSummaryData :: Object
138− , _iTriggerDetailsHtmlUrl :: Text
216+ { _iIncidentNumber' :: !Int
217+ , _iStatus' :: !IncidentStatus
218+ , _iCreatedOn' :: Date
219+ , _iHtmlUrl' :: Text
220+ , _iIncidentKey' :: Maybe IncidentKey
221+ , _iService' :: Object
222+ , _iEscalationPolicy' :: Maybe Object
223+ , _iAssignedTo' :: [Assignee]
224+ , _iAcknowledgers' :: [Assignee]
225+ , _iLastStatusChangeBy' :: Maybe Assignee
226+ , _iLastStatusChangeOn' :: Maybe Date
227+ , _iTriggerSummaryData' :: Object
228+ , _iTriggerDetailsHtmlUrl' :: Text
139229 } deriving (Eq, Show)
140230
141−deriveJSON ''Incident
142−
143−-- | The number of the incident. This is unique across your account.
144−makeLens "_iIncidentNumber" ''Incident
145−
146−-- | The current status of the incident. Valid statuses are:
147−makeLens "_iStatus" ''Incident
148−
149−-- | The date/time the incident was triggered.
150−iCreatedOn :: Lens' Incident UTCTime
151−iCreatedOn = lens _iCreatedOn (\i x -> i { _iCreatedOn = x }) . _D
152−
153−-- | The PagerDuty website URL where the incident can be viewed and further
154−-- actions taken. This is not the resource URL.
155−makeLens "_iHtmlUrl" ''Incident
156−
157−-- | The incident's de-duplication key. See the PagerDuty Integration API docs
158−-- for further details.
159−makeLens "_iIncidentKey" ''Incident
160−
161−-- | The PagerDuty service that the incident belongs to. The service will
162−-- contain fields of its own.
163−makeLens "_iService" ''Incident
164−
165−-- | The escalation policy that the incident belongs to. The policy will
166−-- contain fields of its own.
167−makeLens "_iEscalationPolicy" ''Incident
168−
169−-- | The list of assignments of the incident. An assignment is an object
170−-- containing the assigned user as well as the date/time the incident was assigned
171−-- to that user. The user will contain fields of its own. This list is empty if
172−-- the status of the incident is resolved.
173−makeLens "_iAssignedTo" ''Incident
174−
175−-- | The list of acknowledgements of the incident. An acknowledgement is an
176−-- object containing the acknowleding object (either a user or the integration
177−-- API) as well as the date/time the incident was acknowledged. This field is only
178−-- present if the status of the incident is acknowledged. This field is sorted in
179−-- ascending order by acknowledgement time.
180−makeLens "_iAcknowledgers" ''Incident
181−
182−-- | The user who is responsible for the incident's last status change. If the incident is in the acknowledged or resolved status, this will be the user that took the first acknowledged or resolved action. If the incident was automatically resolved (say through the Event Integration API), or if the incident is in the triggered state, this will be null. User fields are the same as in the assignedToUser field above.
183−makeLens "_iLastStatusChangeBy" ''Incident
184−
185−-- | The date/time the incident's status last changed.
186−iLastStatusChangeOn :: Lens' Incident (Maybe UTCTime)
187−iLastStatusChangeOn =
188− lens _iLastStatusChangeOn (\i x -> i { _iLastStatusChangeOn = x })
189− . mapping _D
190−
191−-- | Some condensed information regarding the initial event that triggered this incident. This data will be a set of key/value pairs that vary depending on what sort of event triggered the incident (email, Event API request, etc). For instance, if an email triggered the incident, then the triggerSummaryData will likely contain a subject. There are no guarantees on the full presence of this data for every incident.
192−makeLens "_iTriggerSummaryData" ''Incident
193−
194−-- | The PagerDuty website URL where the full details regarding the initial event that triggered this incident can be found. (This is not the resource URL.)
195−makeLens "_iTriggerDetailsHtmlUrl" ''Incident
231+deriveRecord ''Incident
232+
233+class HasIncident a where
234+ incident :: Lens' a Incident
235+
236+ -- | The number of the incident. This is unique across your account.
237+ iIncidentNumber :: Lens' a Int
238+
239+ -- | The current status of the incident. Valid statuses are:
240+ iStatus :: Lens' a IncidentStatus
241+
242+ -- | The date/time the incident was triggered.
243+ iCreatedOn :: Lens' a UTCTime
244+
245+ -- | The PagerDuty website URL where the incident can be viewed and further
246+ -- actions taken. This is not the resource URL.
247+ iHtmlUrl :: Lens' a Text
248+
249+ -- | The incident's de-duplication key. See the PagerDuty Integration API
250+ -- docs for further details.
251+ iIncidentKey :: Lens' a (Maybe IncidentKey)
252+
253+ -- | The PagerDuty service that the incident belongs to. The service will
254+ -- contain fields of its own.
255+ iService :: Lens' a Object
256+
257+ -- | The escalation policy that the incident belongs to. The policy will
258+ -- contain fields of its own.
259+ iEscalationPolicy :: Lens' a (Maybe Object)
260+
261+ -- | The list of assignments of the incident. An assignment is an object
262+ -- containing the assigned user as well as the date/time the incident was
263+ -- assigned to that user. The user will contain fields of its own.
264+ -- This list is empty if the status of the incident is resolved.
265+ iAssignedTo :: Lens' a [Assignee]
266+
267+ -- | The list of acknowledgements of the incident. An acknowledgement is an
268+ -- object containing the acknowleding object (either a user or the
269+ -- integration API) as well as the date/time the incident was acknowledged.
270+ -- This field is only present if the status of the incident is acknowledged.
271+ -- This field is sorted in ascending order by acknowledgement time.
272+ iAcknowledgers :: Lens' a [Assignee]
273+
274+ -- | The user who is responsible for the incident's last status change. If
275+ -- the incident is in the acknowledged or resolved status, this will be the user
276+ -- that took the first acknowledged or resolved action. If the incident was
277+ -- automatically resolved (say through the Event Integration API), or if the
278+ -- incident is in the triggered state, this will be null. User fields are the
279+ -- same as in the assignedToUser field above.
280+ iLastStatusChangeBy :: Lens' a (Maybe Assignee)
281+
282+ -- | The date/time the incident's status last changed.
283+ iLastStatusChangeOn :: Lens' a (Maybe UTCTime)
284+
285+ -- | Some condensed information regarding the initial event that triggered
286+ -- this incident. This data will be a set of key/value pairs that vary depending
287+ -- on what sort of event triggered the incident (email, Event API request,
288+ -- etc). For instance, if an email triggered the incident, then the
289+ -- triggerSummaryData will likely contain a subject. There are no guarantees
290+ -- o nthe full presence of this data for every incident.
291+ iTriggerSummaryData :: Lens' a Object
292+
293+ -- | The PagerDuty website URL where the full details regarding the initial
294+ -- event that triggered this incident can be found. (This is not the resource
295+ -- URL.)
296+ iTriggerDetailsHtmlUrl :: Lens' a Text
297+
298+ iIncidentNumber = incident.iIncidentNumber'
299+ iStatus = incident.iStatus'
300+ iCreatedOn = incident.iCreatedOn'._D
301+ iHtmlUrl = incident.iHtmlUrl'
302+ iIncidentKey = incident.iIncidentKey'
303+ iService = incident.iService'
304+ iEscalationPolicy = incident.iEscalationPolicy'
305+ iAssignedTo = incident.iAssignedTo'
306+ iAcknowledgers = incident.iAcknowledgers'
307+ iLastStatusChangeBy = incident.iLastStatusChangeBy'
308+ iLastStatusChangeOn = incident.iLastStatusChangeOn'.mapping _D
309+ iTriggerSummaryData = incident.iTriggerSummaryData'
310+ iTriggerDetailsHtmlUrl = incident.iTriggerDetailsHtmlUrl'
311+
312+instance HasIncident Incident where
313+ incident = id
196314
197315data ListIncidents = ListIncidents
198316 { _lsSince' :: Maybe Date
@@ -229,13 +347,10 @@ lsUntil = upd.lsUntil'.mapping _D
229347-- | When set, the since and until parameters and defaults are
230348-- ignored. Use this to get all incidents since the account was created.
231349lsDateRange :: Lens' (Request ListIncidents s b) Bool
232−lsDateRange = upd . lens get' set'
350+lsDateRange = upd.lens get_ (flip set_)
233351 where
234− get' x
235− | Just All <- _lsDateRange' x = True
236− | otherwise = False
237−
238− set' l x = l { _lsDateRange' = if x then Just All else Nothing }
352+ get_ = (Just All ==) . view lsDateRange'
353+ set_ x = lsDateRange' .~ listToMaybe [All | x]
239354
240355-- | Returns only the incidents currently in the passed status(es).
241356lsStatus :: Lens' (Request ListIncidents s b) (Maybe [IncidentStatus])
@@ -322,12 +437,12 @@ cSince = upd.cSince'.mapping _D
322437
323438-- | The end of the date range over which you want to search.
324439--
325−-- /Note: If you leave off either since or until, a 30 day default range is
440+-- /Note:/ If you leave off either since or until, a 30 day default range is
326441-- applied to your open ended range. Not including the since parameter will set
327−-- the date range to until - 30 days. Likewise, if you leave off until,
328−-- it is set to since + 30 days.
442+-- the date range to until @-30@ days. Likewise, if you leave off until,
443+-- it is set to since @+30@ days.
329444--
330−-- Defaults to the last 30 days if you leave off both. The size of the date
445+-- /Default:/ The last 30 days if you leave off both. The size of the date
331446-- range must be less than 180 days.
332447cUntil :: Lens' (Request CountIncidents s b) (Maybe UTCTime)
333448cUntil = upd.cUntil'.mapping _D
@@ -335,13 +450,10 @@ cUntil = upd.cUntil'.mapping _D
335450-- | When set, the since and until parameters and defaults are
336451-- ignored. Use this to get all counts since the account was created.
337452cDateRange :: Lens' (Request CountIncidents s b) Bool
338−cDateRange = upd . lens get' set'
453+cDateRange = upd.lens get_ (flip set_)
339454 where
340− get' x
341− | Just All <- _cDateRange' x = True
342− | otherwise = False
343−
344− set' l x = l { _cDateRange' = if x then Just All else Nothing }
455+ get_ = (Just All ==) . view cDateRange'
456+ set_ x = cDateRange' .~ listToMaybe [All | x]
345457
346458-- | Only counts the incidents currently in the passed status(es).
347459cStatus :: Lens' (Request CountIncidents s b) (Maybe [IncidentStatus])
@@ -392,30 +504,148 @@ countIncidents =
392504 , _cAssignedToUser' = Nothing
393505 } & path .~ incidents % "count"
394506
507+
508+data UpdateStatus
509+ = UAcknowledged
510+ | UResolved
511+ deriving (Eq, Show)
512+
513+deriveNullary ''UpdateStatus
514+
515+data UpdateIncident = UpdateIncident
516+ { _iiId :: IncidentId
517+ , _iiStatus :: Maybe UpdateStatus
518+ , _iiEscalationLevel :: Maybe Int
519+ , _iiEscalationPolicy :: Maybe EscalationPolicyId
520+ , _iiAssignedToUser :: Maybe (CSV UserId)
521+ } deriving (Eq, Show)
522+
523+deriveJSON ''UpdateIncident
524+
525+-- | The id of the incident to update.
526+makeLens "_iiId" ''UpdateIncident
527+
528+-- | The new status of the incident.
529+makeLens "_iiStatus" ''UpdateIncident
530+
531+-- | Escalate incident to this level in the escalation policy.
532+makeLens "_iiEscalationLevel" ''UpdateIncident
533+
534+-- | Delegate this incident to the specified escalation policy id.
535+-- This restarts the incident's escalation following the new policy.
536+makeLens "_iiEscalationPolicy" ''UpdateIncident
537+
538+-- | List of user IDs to assign this incident to.
539+makeLens "_iiAssignedToUser" ''UpdateIncident
540+
541+data UpdatedIncidents = UpdatedIncidents
542+ { _uiId :: IncidentId
543+ , _uiStatus :: IncidentStatus
544+ , _uiIncidentNumber :: !Int
545+ , _uiUrl :: Text
546+ , _uiError :: Maybe Object
547+ } deriving (Eq, Show)
548+
549+deriveRecord ''UpdatedIncidents
550+
551+data UpdateIncidents = UpdateIncidents
552+ { _uiIncidents' :: [UpdateIncident]
553+ } deriving (Eq, Show)
554+
555+jsonRequest ''UpdateIncidents
556+
557+uiIncidents :: Lens' (Request UpdateIncidents s b) [UpdateIncident]
558+uiIncidents = upd.uiIncidents'
559+
395560-- | Acknowledge, resolve, escalate or reassign one or more incidents.
396561--
397562-- @PUT \/incidents@
398563--
399564-- /See:/ <http://developer.pagerduty.com/documentation/rest/incidents/update>
400−updateIncidents = undefined
565+updateIncidents :: RequesterId -> Request UpdateIncidents s [UpdatedIncidents]
566+updateIncidents r = auth updateIncidentsBasic & query .~ [("requester_id", r)]
567+
568+-- | A version of 'updateIncidents' which uses HTTP Basic authentication and
569+-- doesn't require a 'RequesterId'.
570+updateIncidentsBasic :: Request UpdateIncidents 'Basic [UpdatedIncidents]
571+updateIncidentsBasic =
572+ mk UpdateIncidents
573+ { _uiIncidents' = []
574+ } & meth .~ PUT
575+ & path .~ incidents
401576
402577-- | Resolve an incident.
403578--
404579-- @PUT \/incidents\/\:id\/resolve@
405580--
406581-- /See:/ <http://developer.pagerduty.com/documentation/rest/incidents/resolve>
407−resolveIncident = undefined
582+resolveIncident :: IncidentId
583+ -> RequesterId
584+ -> Request Empty s Empty
585+resolveIncident i r =
586+ auth (resolveIncidentBasic i) & query .~ [("requester_id", r)]
587+
588+-- | A version of 'resolveIncident' which uses HTTP Basic authentication and
589+-- doesn't require a 'RequesterId'.
590+resolveIncidentBasic :: IncidentId -> Request Empty s Empty
591+resolveIncidentBasic i = empty & meth .~ PUT & path .~ incidents % i % "resolve"
408592
409593-- | Acknowledge an incident.
410594--
411595-- @PUT \/incidents\/\:id\/acknowledge@
412596--
413597-- /See:/ <http://developer.pagerduty.com/documentation/rest/incidents/acknowledge>
414−acknowledgeIncident = undefined
598+acknowledgeIncident :: IncidentId
599+ -> RequesterId
600+ -> Request Empty s Empty
601+acknowledgeIncident i r =
602+ auth (acknowledgeIncidentBasic i) & query .~ [("requester_id", r)]
603+
604+-- | A version of 'acknowledgeIncident' which uses HTTP Basic authentication and
605+-- doesn't require a 'RequesterId'.
606+acknowledgeIncidentBasic :: IncidentId -> Request Empty s Empty
607+acknowledgeIncidentBasic i =
608+ empty & meth .~ PUT & path .~ incidents % i % "acknowledge"
609+
610+data ReassignIncident = ReassignIncident
611+ { _riEscalationPolicy' :: Maybe EscalationPolicyId
612+ , _riEscalationLevel' :: Maybe Int
613+ , _riAssignedToUser' :: Maybe (CSV UserId)
614+ } deriving (Eq, Show)
615+
616+jsonRequest ''ReassignIncident
617+
618+-- | The ID of an escalation policy. Delegate the incident to this escalation
619+-- policy.
620+riEscalationPolicy :: Lens' (Request ReassignIncident s b) (Maybe EscalationPolicyId)
621+riEscalationPolicy = upd.riEscalationPolicy'
622+
623+-- | Escalate incident to this level in the escalation policy.
624+riEscalationLevel :: Lens' (Request ReassignIncident s b) (Maybe Int)
625+riEscalationLevel = upd.riEscalationLevel'
626+
627+-- | Comma separated list of user IDs to assign this incident to.
628+riAssignedToUser :: Lens' (Request ReassignIncident s b) (Maybe [UserId])
629+riAssignedToUser = upd.riAssignedToUser'.mapping _CSV
415630
416631-- | Reassign an incident.
417632--
418633-- @PUT \/incidents\/\:id\/reassign@
419634--
420635-- /See:/ <http://developer.pagerduty.com/documentation/rest/incidents/reassign>
421−reassignIncident = undefined
636+reassignIncident :: IncidentId
637+ -> RequesterId
638+ -> Request ReassignIncident s Empty
639+reassignIncident i r =
640+ auth (reassignIncidentBasic i) & query .~ [("requester_id", r)]
641+
642+-- | A version of 'reassignIncident' which uses HTTP Basic authentication and
643+-- doesn't require a 'RequesterId'.
644+reassignIncidentBasic :: IncidentId -> Request ReassignIncident s Empty
645+reassignIncidentBasic i =
646+ mk ReassignIncident
647+ { _riEscalationPolicy' = Nothing
648+ , _riEscalationLevel' = Nothing
649+ , _riAssignedToUser' = Nothing
650+ } & meth .~ PUT
651+ & path .~ incidents % i % "reassign"