module Alerta.Types
(
Resource
, Event
, Service
, Environment
, Group
, Origin
, AlertType
, UserName
, CustomerName
, Tag
, Email
, Password
, Provider
, UUID
, Href
, ShouldReverse
, Limit
, PageNo
, QueryString
, IsRepeat
, FieldQuery
, MatchType(..)
, (=.), (!=), (~.), (!~)
, Resp(..)
, Severity(..)
, Status(..)
, TrendIndication(..)
, Alert(..)
, mkAlert
, AlertInfo(..)
, AlertAttr(..)
, QueryAttr(..)
, HistoryItem(..)
, ExtendedHistoryItem(..)
, Tags(..)
, Attributes(..)
, StatusChange(..)
, CreateAlertResp(..)
, AlertResp(..)
, AlertsResp(..)
, AlertCountResp(..)
, ResourceInfo(..)
, Top10Info(..)
, Top10Resp(..)
, AlertHistoryResp(..)
, EnvironmentInfo(..)
, EnvironmentsResp(..)
, ServiceInfo(..)
, ServicesResp(..)
, Blackout(..)
, blackout
, BlackoutInfo(..)
, BlackoutStatus(..)
, ExtendedBlackoutInfo(..)
, BlackoutResp(..)
, BlackoutsResp(..)
, Heartbeat(..)
, HeartbeatInfo(..)
, CreateHeartbeatResp(..)
, HeartbeatResp(..)
, HeartbeatsResp(..)
, ApiKey(..)
, CreateApiKey(..)
, ApiKeyType(..)
, ApiKeyInfo(..)
, CreateApiKeyResp(..)
, ApiKeysResp(..)
, User(..)
, user
, UserAttr(..)
, IsEmpty(..)
, emptyUserAttr
, checkNonempty
, withUserName
, withUserLogin
, withUserPassword
, withUserProvider
, withUserText
, withUserEmailVerified
, UserInfo(..)
, RoleType(..)
, ExtendedUserInfo(..)
, UserResp(..)
, UsersResp(..)
, Customer(..)
, CustomerInfo(..)
, CustomerResp(..)
, CustomersResp(..)
) where
import Alerta.Util
import Control.Applicative (empty)
import Data.Aeson
import qualified Data.Aeson.Encoding as E
import Data.Aeson.Types
import Data.Aeson.TH
import Data.Coerce (coerce)
import Data.Default
import Data.Ix
import Data.Map (Map)
import Data.Monoid ((<>))
import Data.String (IsString(..))
import qualified Data.Text as T
import Data.Text (Text)
import Data.Time (UTCTime)
import GHC.Generics
import Web.HttpApiData
type Resource = Text
type Event = Text
type Service = Text
type Environment = Text
type Group = Text
type Origin = Text
type AlertType = Text
type UserName = Text
type CustomerName = Text
type Tag = Text
type Email = Text
type Password = Text
type Provider = Text
type ShouldReverse = Bool
type Limit = Int
type PageNo = Int
type UUID = Text
type Href = Text
type QueryString = Text
type IsRepeat = Bool
type FieldQuery = (QueryAttr, Text, MatchType, Bool)
data MatchType = Regex | Literal deriving (Eq, Enum, Show, Read, Generic)
(=.), (!=), (~.), (!~) :: QueryAttr -> Text -> FieldQuery
k =. v = (k, v, Literal, True)
k != v = (k, v, Literal, False)
k ~. v = (k, v, Regex, True)
k !~ v = (k, v, Regex, False)
data QueryAttr =
EventQueryAttr
| EnvironmentQueryAttr
| SeverityQueryAttr
| CorrelateQueryAttr
| StatusQueryAttr
| ServiceQueryAttr
| GroupQueryAttr
| ValueQueryAttr
| TextQueryAttr
| TagsQueryAttr
| AttributesQueryAttr
| OriginQueryAttr
| TypeQueryAttr
| CreateTimeQueryAttr
| TimeoutQueryAttr
| RawDataQueryAttr
| CustomerQueryAttr
| RepeatQueryAttr
| PreviousSeverityQueryAttr
| TrendIndicationQueryAttr
| ReceiveTimeQueryAttr
| LastReceiveIdQueryAttr
| LastReceiveTimeQueryAttr
| HistoryQueryAttr
| HrefQueryAttr
deriving (Eq, Enum, Show, Generic)
instance IsString QueryAttr where
fromString "event" = EventQueryAttr
fromString "environment" = EnvironmentQueryAttr
fromString "severity" = SeverityQueryAttr
fromString "correlate" = CorrelateQueryAttr
fromString "status" = StatusQueryAttr
fromString "service" = ServiceQueryAttr
fromString "group" = GroupQueryAttr
fromString "value" = ValueQueryAttr
fromString "text" = TextQueryAttr
fromString "tags" = TagsQueryAttr
fromString "attributes" = AttributesQueryAttr
fromString "origin" = OriginQueryAttr
fromString "type" = TypeQueryAttr
fromString "createTime" = CreateTimeQueryAttr
fromString "timeout" = TimeoutQueryAttr
fromString "rawData" = RawDataQueryAttr
fromString "customer" = CustomerQueryAttr
fromString "repeat" = RepeatQueryAttr
fromString "previousSeverity" = PreviousSeverityQueryAttr
fromString "trendIndication" = TrendIndicationQueryAttr
fromString "receiveTime" = ReceiveTimeQueryAttr
fromString "lastReceiveId" = LastReceiveIdQueryAttr
fromString "lastReceiveTime" = LastReceiveTimeQueryAttr
fromString "history" = HistoryQueryAttr
fromString "href" = HrefQueryAttr
fromString other = error $ "\"" ++ other ++ "\" is not a valid QueryAttr"
instance ToHttpApiData QueryAttr where
toQueryParam = T.pack . uncapitalise . onCamelComponents (dropRight 2) . show
data Severity =
Unknown
| Trace
| Debug
| Informational
| Ok
| Normal
| Cleared
| Indeterminate
| Warning
| Minor
| Major
| Critical
| Security
deriving (Eq, Ord, Bounded, Enum, Ix, Show, Read, Generic)
instance ToHttpApiData Severity where
toUrlPiece = T.pack . show
instance ToJSONKey Severity where
toJSONKey = toJSONKeyText (T.toLower . T.pack . show)
instance FromJSONKey Severity where
fromJSONKey = FromJSONKeyTextParser $ \case
"security" -> pure Security
"critical" -> pure Critical
"major" -> pure Major
"minor" -> pure Minor
"warning" -> pure Warning
"indeterminate" -> pure Indeterminate
"cleared" -> pure Cleared
"normal" -> pure Normal
"ok" -> pure Ok
"informational" -> pure Informational
"debug" -> pure Debug
"trace" -> pure Trace
"unknown" -> pure Unknown
other -> fail $ "Could not parse key \"" ++ T.unpack other ++ "\" as Severity"
data Status =
OpenStatus
| AssignStatus
| AckStatus
| ClosedStatus
| ExpiredStatus
| UnknownStatus
deriving (Eq, Ord, Bounded, Enum, Ix, Show, Read, Generic)
instance ToHttpApiData Status where
toUrlPiece = showTextLowercase
instance ToJSONKey Status where
toJSONKey = toJSONKeyText (T.toLower . T.pack . show)
instance FromJSONKey Status where
fromJSONKey = FromJSONKeyTextParser $ \case
"open" -> pure OpenStatus
"assign" -> pure AssignStatus
"ack" -> pure AckStatus
"closed" -> pure ClosedStatus
"expired" -> pure ExpiredStatus
"unknown" -> pure UnknownStatus
other -> fail $ "Could not parse key \"" ++ T.unpack other ++ "\" as Status"
data TrendIndication = NoChange | LessSevere | MoreSevere
deriving (Eq, Ord, Bounded, Enum, Ix, Show, Generic)
instance FromHttpApiData TrendIndication where
parseQueryParam = parseBoundedTextData
data Resp = OkResp | ErrorResp { respMessage :: Text }
deriving (Eq, Show, Generic)
data Alert = Alert
{ alertResource :: Resource
, alertEvent :: Event
, alertEnvironment :: Maybe Environment
, alertSeverity :: Maybe Severity
, alertCorrelate :: Maybe [Event]
, alertStatus :: Maybe Status
, alertService :: Maybe [Service]
, alertGroup :: Maybe Group
, alertValue :: Maybe Value
, alertText :: Maybe Text
, alertTags :: Maybe [Tag]
, alertAttributes :: Maybe (Map Text Text)
, alertOrigin :: Maybe Origin
, alertType :: Maybe AlertType
, alertCreateTime :: Maybe UTCTime
, alertTimeout :: Maybe Int
, alertRawData :: Maybe Text
, alertCustomer :: Maybe CustomerName
} deriving (Eq, Show, Generic)
mkAlert :: Resource -> Event -> Service -> Alert
mkAlert r e s = Alert
{ alertResource = r
, alertEvent = e
, alertEnvironment = Just "Development"
, alertSeverity = Nothing
, alertCorrelate = Nothing
, alertStatus = Nothing
, alertService = Just [s]
, alertGroup = Nothing
, alertValue = Nothing
, alertText = Nothing
, alertTags = Nothing
, alertAttributes = Nothing
, alertOrigin = Nothing
, alertType = Nothing
, alertCreateTime = Nothing
, alertTimeout = Nothing
, alertRawData = Nothing
, alertCustomer = Nothing
}
data AlertInfo = AlertInfo
{ alertInfoId :: UUID
, alertInfoResource :: Resource
, alertInfoEvent :: Event
, alertInfoEnvironment :: Environment
, alertInfoSeverity :: Maybe Severity
, alertInfoCorrelate :: [Event]
, alertInfoStatus :: Maybe Status
, alertInfoService :: [Service]
, alertInfoGroup :: Group
, alertInfoValue :: Value
, alertInfoText :: Text
, alertInfoTags :: [Tag]
, alertInfoAttributes :: Map Text Text
, alertInfoOrigin :: Origin
, alertInfoType :: AlertType
, alertInfoCreateTime :: UTCTime
, alertInfoTimeout :: Int
, alertInfoRawData :: Maybe Text
, alertInfoCustomer :: Maybe CustomerName
, alertInfoDuplicateCount :: Maybe Int
, alertInfoRepeat :: Maybe Bool
, alertInfoPreviousSeverity :: Maybe Severity
, alertInfoTrendIndication :: Maybe TrendIndication
, alertInfoReceiveTime :: UTCTime
, alertInfoLastReceiveId :: Maybe UUID
, alertInfoLastReceiveTime :: UTCTime
, alertInfoHistory :: [HistoryItem]
, alertInfoHref :: Href
} deriving (Eq, Show, Generic)
data AlertAttr =
IdAlertAttr
| ResourceAlertAttr
| EventAlertAttr
| EnvironmentAlertAttr
| SeverityAlertAttr
| CorrelateAlertAttr
| StatusAlertAttr
| ServiceAlertAttr
| GroupAlertAttr
| ValueAlertAttr
| TextAlertAttr
| TagsAlertAttr
| AttributesAlertAttr
| OriginAlertAttr
| TypeAlertAttr
| CreateTimeAlertAttr
| TimeoutAlertAttr
| RawDataAlertAttr
| CustomerAlertAttr
| DuplicateCountAlertAttr
| RepeatAlertAttr
| PreviousSeverityAlertAttr
| TrendIndicationAlertAttr
| ReceiveTimeAlertAttr
| LastReceiveIdAlertAttr
| LastReceiveTimeAlertAttr
| HistoryAlertAttr
| HrefAlertAttr
deriving (Eq, Enum, Show, Generic)
instance IsString AlertAttr where
fromString "id" = IdAlertAttr
fromString "resource" = ResourceAlertAttr
fromString "event" = EventAlertAttr
fromString "environment" = EnvironmentAlertAttr
fromString "severity" = SeverityAlertAttr
fromString "correlate" = CorrelateAlertAttr
fromString "status" = StatusAlertAttr
fromString "service" = ServiceAlertAttr
fromString "group" = GroupAlertAttr
fromString "value" = ValueAlertAttr
fromString "text" = TextAlertAttr
fromString "tags" = TagsAlertAttr
fromString "attributes" = AttributesAlertAttr
fromString "origin" = OriginAlertAttr
fromString "type" = TypeAlertAttr
fromString "createTime" = CreateTimeAlertAttr
fromString "timeout" = TimeoutAlertAttr
fromString "rawData" = RawDataAlertAttr
fromString "customer" = CustomerAlertAttr
fromString "duplicateCount" = DuplicateCountAlertAttr
fromString "repeat" = RepeatAlertAttr
fromString "previousSeverity" = PreviousSeverityAlertAttr
fromString "trendIndication" = TrendIndicationAlertAttr
fromString "receiveTime" = ReceiveTimeAlertAttr
fromString "lastReceiveId" = LastReceiveIdAlertAttr
fromString "lastReceiveTime" = LastReceiveTimeAlertAttr
fromString "history" = HistoryAlertAttr
fromString "href" = HrefAlertAttr
fromString other = error $ "\"" ++ other ++ "\" is not a valid AlertAttr"
instance ToHttpApiData AlertAttr where
toQueryParam = T.pack . uncapitalise . onCamelComponents (dropRight 2) . show
data HistoryItem = StatusHistoryItem
{ historyItemEvent :: Event
, historyItemStatus :: Status
, historyItemText :: Text
, historyItemId :: UUID
, historyItemUpdateTime :: UTCTime
} | SeverityHistoryItem {
historyItemEvent :: Event
, historyItemSeverity :: Severity
, historyItemText :: Text
, historyItemId :: UUID
, historyItemUpdateTime :: UTCTime
, historyItemValue :: Value
} deriving (Eq, Show, Generic)
data ExtendedHistoryItem = StatusExtendedHistoryItem
{ statusExtendedHistoryItemId :: UUID
, statusExtendedHistoryItemResource :: Resource
, statusExtendedHistoryItemEvent :: Event
, statusExtendedHistoryItemEnvironment :: Environment
, statusExtendedHistoryItemStatus :: Status
, statusExtendedHistoryItemService :: [Service]
, statusExtendedHistoryItemGroup :: Group
, statusExtendedHistoryItemText :: Text
, statusExtendedHistoryItemTags :: [Tag]
, statusExtendedHistoryItemAttributes :: Map Text Text
, statusExtendedHistoryItemOrigin :: Origin
, statusExtendedHistoryItemUpdateTime :: UTCTime
, statusExtendedHistoryItemCustomer :: Maybe CustomerName
} | SeverityExtendedHistoryItem
{ severityExtendedHistoryItemId :: UUID
, severityExtendedHistoryItemResource :: Resource
, severityExtendedHistoryItemEvent :: Event
, severityExtendedHistoryItemEnvironment :: Environment
, severityExtendedHistoryItemSeverity :: Severity
, severityExtendedHistoryItemService :: [Service]
, severityExtendedHistoryItemGroup :: Group
, severityExtendedHistoryItemValue :: Value
, severityExtendedHistoryItemText :: Text
, severityExtendedHistoryItemTags :: [Tag]
, severityExtendedHistoryItemAttributes :: Map Text Text
, severityExtendedHistoryItemOrigin :: Origin
, severityExtendedHistoryItemUpdateTime :: UTCTime
, severityExtendedHistoryItemCustomer :: Maybe CustomerName
} deriving (Eq, Show, Generic)
newtype Tags = Tags { tags :: [Tag] } deriving (Eq, Show, Generic)
newtype Attributes = Attributes { attributes :: Map Text Text } deriving (Eq, Show, Generic)
data StatusChange = StatusChange
{ statusChangeStatus :: Status
, statusChangeText :: Maybe Text
} deriving (Eq, Show, Generic)
data CreateAlertResp = OkCreateAlertResp
{ okCreateAlertRespId :: UUID
, okCreateAlertRespAlert :: Maybe AlertInfo
, okCreateAlertRespMessage :: Maybe Text
} | ErrorCreateAlertResp {
errorCreateAlertRespMessage :: Text
} deriving (Eq, Show, Generic)
data AlertResp = OkAlertResp
{ okAlertRespAlert :: AlertInfo
, okAlertRespTotal :: Int
} | ErrorAlertResp {
errorAlertRespMessage :: Text
} deriving (Eq, Show, Generic)
data AlertsResp = OkAlertsResp
{ okAlertsRespAlerts :: [AlertInfo]
, okAlertsRespTotal :: Int
, okAlertsRespPage :: PageNo
, okAlertsRespPageSize :: Int
, okAlertsRespPages :: Int
, okAlertsRespMore :: Bool
, okAlertsRespSeverityCounts :: Maybe (Map Severity Int)
, okAlertsRespStatusCounts :: Maybe (Map Status Int)
, okAlertsRespLastTime :: UTCTime
, okAlertsRespAutoRefresh :: Bool
, okAlertsRespMessage :: Maybe Text
} | ErrorAlertsResp
{ errorAlertsRespMessage :: Text
} deriving (Eq, Show, Generic)
data AlertCountResp = OkAlertCountResp
{ okAlertCountRespTotal :: Int
, okAlertCountRespSeverityCounts :: Int
, okAlertCountRespStatusCounts :: Int
, okAlertCountRespMessage :: Maybe Text
} | ErrorAlertCountResp
{ errorAlertCountRespMessage :: Text
} deriving (Eq, Show, Generic)
data ResourceInfo = ResourceInfo
{ resourceInfoId :: UUID
, resourceInfoResource :: Resource
, resourceInfoHref :: Href
} deriving (Eq, Show, Generic)
data Top10Info = Top10Info
{ top10InfoCount :: Int
, top10InfoDuplicateCount :: Int
, top10InfoEnvironments :: [Environment]
, top10InfoServices :: [Service]
, top10InfoResources :: [ResourceInfo]
} deriving (Eq, Show, Generic)
data Top10Resp = OkTop10Resp
{ okTop10RespTop10 :: [Top10Info]
, okTop10RespTotal :: Int
, okTop10RespMessage :: Maybe Text
} | ErrorTop10Resp
{ errorTop10RespMessage :: Text
} deriving (Eq, Show, Generic)
data AlertHistoryResp = OkAlertHistoryResp
{ okAlertHistoryRespHistory :: [ExtendedHistoryItem]
, okAlertHistoryRespLastTime :: UTCTime
, okAlertHistoryRespMessage :: Maybe Text
} | ErrorAlertHistoryResp
{ errorAlertHistoryResp :: Text
} deriving (Eq, Show, Generic)
data EnvironmentInfo = EnvironmentInfo
{ environmentInfoCount :: Int
, environmentInfoEnvironment :: Environment
} deriving (Eq, Show, Generic)
data EnvironmentsResp = OkEnvironmentsResp
{ okEnvironmentsRespMessage :: Maybe Text
, okEnvironmentsRespTotal :: Int
, okEnvironmentsRespEnvironments :: [EnvironmentInfo]
} | ErrorEnvironmentsResp
{ errorEnvironmentsRespMessage :: Text
} deriving (Eq, Show, Generic)
data ServiceInfo = ServiceInfo
{ serviceInfoCount :: Int
, serviceInfoEnvironment :: Environment
, serviceInfoService :: Service
} deriving (Eq, Show, Generic)
data ServicesResp = OkServicesResp
{ okServicesRespTotal :: Int
, okServicesRespServices :: [ServiceInfo]
, okServicesRespMessage :: Maybe Text
} | ErrorServicesResp
{ errorServicesRespMessage :: Text
} deriving (Eq, Show, Generic)
data Blackout = Blackout
{ blackoutEnvironment :: Environment
, blackoutResource :: Maybe Resource
, blackoutService :: Maybe Service
, blackoutEvent :: Maybe Event
, blackoutGroup :: Maybe Group
, blackoutTags :: Maybe [Tag]
, blackoutStartTime :: Maybe UTCTime
, blackoutEndTime :: Maybe UTCTime
, blackoutDuration :: Maybe Int
} deriving (Eq, Show, Generic)
blackout :: Environment -> Blackout
blackout env = Blackout env Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
data BlackoutInfo = BlackoutInfo
{ blackoutInfoId :: UUID
, blackoutInfoPriority :: Int
, blackoutInfoEnvironment :: Environment
, blackoutInfoResource :: Maybe Resource
, blackoutInfoService :: Maybe [Service]
, blackoutInfoEvent :: Maybe Event
, blackoutInfoGroup :: Maybe Group
, blackoutInfoTags :: Maybe [Tag]
, blackoutInfoCustomer :: Maybe CustomerName
, blackoutInfoStartTime :: UTCTime
, blackoutInfoEndTime :: UTCTime
, blackoutInfoDuration :: Int
} deriving (Eq, Show, Generic)
data BlackoutStatus = Expired | Pending | Active deriving (Eq, Ord, Bounded, Enum, Ix, Show, Generic)
data ExtendedBlackoutInfo = ExtendedBlackoutInfo
{ extendedBlackoutInfoId :: UUID
, extendedBlackoutInfoPriority :: Int
, extendedBlackoutInfoEnvironment :: Environment
, extendedBlackoutInfoResource :: Maybe Resource
, extendedBlackoutInfoService :: Maybe [Service]
, extendedBlackoutInfoEvent :: Maybe Event
, extendedBlackoutInfoGroup :: Maybe Group
, extendedBlackoutInfoTags :: Maybe [Tag]
, extendedBlackoutInfoCustomer :: Maybe CustomerName
, extendedBlackoutInfoStartTime :: UTCTime
, extendedBlackoutInfoEndTime :: UTCTime
, extendedBlackoutInfoDuration :: Int
, extendedBlackoutInfoRemaining :: Int
, extendedBlackoutInfoStatus :: BlackoutStatus
} deriving (Eq, Show, Generic)
data BlackoutResp = OkBlackoutResp
{ okBlackoutRespId :: UUID
, okBlackoutRespBlackout :: BlackoutInfo
} | ErrorBlackoutResp {
errorBlackoutRespMessage :: Text
} deriving (Eq, Show, Generic)
data BlackoutsResp = OkBlackoutsResp
{ okBlackoutsRespTotal :: Int
, okBlackoutsRespBlackouts :: [ExtendedBlackoutInfo]
, okBlackoutsRespMessage :: Maybe Text
, okBlackoutsRespTime :: UTCTime
} | ErrorBlackoutsResp {
errorBlackoutsRespMessage :: Text
} deriving (Eq, Show, Generic)
data Heartbeat = Heartbeat
{ heartbeatOrigin :: Maybe Origin
, heartbeatTags :: [Tag]
, heartbeatCreateTime :: Maybe UTCTime
, heartbeatTimeout :: Maybe Int
, heartbeatCustomer :: Maybe CustomerName
} deriving (Eq, Show, Generic, Default)
data HeartbeatInfo = HeartbeatInfo
{ heartbeatInfoCreateTime :: UTCTime
, heartbeatInfoCustomer :: Maybe CustomerName
, heartbeatInfoHref :: Href
, heartbeatInfoId :: UUID
, heartbeatInfoOrigin :: Origin
, heartbeatInfoReceiveTime :: UTCTime
, heartbeatInfoTags :: [Tag]
, heartbeatInfoTimeout :: Int
, heartbeatInfoType :: Text
} deriving (Eq, Show, Generic)
data CreateHeartbeatResp = OkCreateHeartbeatResp
{ createHeartbeatRespId :: UUID
, createHeartbeatRespHeartbeat :: HeartbeatInfo
} | ErrorCreateHeartbeatResp
{ createHeartbeatRespMessage :: Text
} deriving (Eq, Show, Generic)
data HeartbeatResp = OkHeartbeatResp
{ heartbeatRespHeartbeat :: HeartbeatInfo
, heartbeatRespTotal :: Int
} | ErrorHeartbeatResp
{ heartbeatRespMessage :: Text
} deriving (Eq, Show, Generic)
data HeartbeatsResp = OkHeartbeatsResp
{ heartbeatsRespHeartbeats :: [HeartbeatInfo]
, heartbeatsRespTime :: Maybe UTCTime
, heartbeatsRespTotal :: Int
, heartbeatsRespMessage :: Maybe Text
} | ErrorHeartbeatsResp
{ heartbeatsRespErrorMessage :: Text
} deriving (Eq, Show, Generic)
data ApiKey = ApiKey { unApiKey :: !Text }
deriving (Eq, Show, Generic)
instance IsString ApiKey where
fromString = ApiKey . T.pack
instance FromJSON ApiKey where
parseJSON = withText "API key" (pure . ApiKey)
instance ToJSON ApiKey where
toJSON (ApiKey k) = String k
toEncoding (ApiKey k) = E.text k
instance ToHttpApiData ApiKey where
toUrlPiece (ApiKey k) = k
data CreateApiKey = CreateApiKey
{ createApiKeyUser :: Maybe Email
, createApiKeyCustomer :: Maybe CustomerName
, createApiKeyType :: Maybe ApiKeyType
, createApiKeyText :: Maybe Text
} deriving (Eq, Show, Generic, Default)
data ApiKeyType = ReadOnly | ReadWrite deriving (Eq, Ord, Bounded, Enum, Ix, Generic)
instance Show ApiKeyType where
show ReadWrite = "read-write"
show ReadOnly = "read-only"
instance ToJSON ApiKeyType where
toJSON = genericToJSON $ defaultOptions { constructorTagModifier = camelTo2 '-'}
instance FromJSON ApiKeyType where
parseJSON = genericParseJSON $ defaultOptions { constructorTagModifier = camelTo2 '-'}
data ApiKeyInfo = ApiKeyInfo
{ apiKeyInfoUser :: Email
, apiKeyInfoKey :: ApiKey
, apiKeyInfoType :: ApiKeyType
, apiKeyInfoText :: Text
, apiKeyInfoExpireTime :: UTCTime
, apiKeyInfoCount :: Int
, apiKeyInfoLastUsedTime :: Maybe UTCTime
, apiKeyInfoCustomer :: Maybe CustomerName
} deriving (Eq, Show, Generic)
data CreateApiKeyResp = OkCreateApiKeyResp
{ okCreateApiKeyRespKey :: ApiKey
, okCreateApiKeyRespData :: ApiKeyInfo
} | ErrorCreateApiKeyResp
{ errorCreateApiKeyRespMessage :: Text
} deriving (Eq, Show, Generic)
data ApiKeysResp = OkApiKeysResp
{ okApiKeysRespKeys :: [ApiKeyInfo]
, okApiKeysRespTotal :: Int
, okApiKeysRespTime :: UTCTime
, okApiKeysRespMessage :: Maybe Text
} | ErrorApiKeysResp
{ errorApiKeysRespMessage :: Text
} deriving (Eq, Show, Generic)
data User = User
{ userName :: UserName
, userLogin :: Email
, userPassword :: Password
, userProvider :: Maybe Provider
, userText :: Maybe Text
, userEmailVerified :: Maybe Bool
} deriving (Show, Generic)
user :: UserName -> Email -> Password -> User
user name login password = User name login password Nothing Nothing Nothing
data IsEmpty = Empty | Nonempty | UnknownIfEmpty
data UserAttr (u :: IsEmpty) = UserAttr
{ userAttrName :: Maybe UserName
, userAttrLogin :: Maybe Email
, userAttrPassword :: Maybe Password
, userAttrProvider :: Maybe Provider
, userAttrText :: Maybe Text
, userAttrEmail_verified :: Maybe Bool
} deriving (Eq, Show, Generic)
emptyUserAttr :: UserAttr 'Empty
emptyUserAttr = UserAttr Nothing Nothing Nothing Nothing Nothing Nothing
checkNonempty :: UserAttr u -> Either (UserAttr 'Empty) (UserAttr 'Nonempty)
checkNonempty a@(UserAttr Nothing Nothing Nothing Nothing Nothing Nothing) = Left $ coerce a
checkNonempty a = Right $ coerce a
instance Default (UserAttr 'Empty) where
def = emptyUserAttr
instance ToJSON (UserAttr 'Nonempty) where
toJSON = genericToJSON $ defaultOptions { omitNothingFields = True }
toEncoding (UserAttr n l pw pr t ev) =
pairs $
"name" .= n <>
"login" .= l <>
"password" .= pw <>
"provider" .= pr <>
"text" .= t <>
"email_verified" .= ev
instance FromJSON (UserAttr 'UnknownIfEmpty) where
parseJSON (Object v) =
UserAttr <$>
v .:? "name" <*>
v .:? "login" <*>
v .:? "password" <*>
v .:? "provider" <*>
v .:? "text" <*>
v .:? "email_verified"
parseJSON _ = empty
withUserName :: UserAttr u -> UserName -> UserAttr 'Nonempty
withUserLogin :: UserAttr u -> Email -> UserAttr 'Nonempty
withUserPassword :: UserAttr u -> Password -> UserAttr 'Nonempty
withUserProvider :: UserAttr u -> Provider -> UserAttr 'Nonempty
withUserText :: UserAttr u -> Text -> UserAttr 'Nonempty
withUserEmailVerified :: UserAttr u -> Bool -> UserAttr u
withUserName u s = u { userAttrName = Just s }
withUserLogin u s = u { userAttrLogin = Just s }
withUserPassword u s = u { userAttrPassword = Just s, userAttrProvider = Just "basic" }
withUserProvider u s = u { userAttrProvider = Just s }
withUserText u s = u { userAttrText = Just s }
withUserEmailVerified u b = u { userAttrEmail_verified = Just b }
data UserInfo = UserInfo
{ userInfoCreateTime :: UTCTime
, userInfoId :: UUID
, userInfoName :: UserName
, userInfoProvider :: Provider
, userInfoLogin :: Email
, userInfoText :: Text
, userInfoEmail_verified :: Bool
} deriving (Show, Generic)
data RoleType = UserRoleType | AdminRoleType
deriving (Eq, Ord, Bounded, Enum, Ix, Show, Generic)
data ExtendedUserInfo = ExtendedUserInfo
{ extendedUserInfoCreateTime :: UTCTime
, extendedUserInfoId :: UUID
, extendedUserInfoName :: UserName
, extendedUserInfoLogin :: Email
, extendedUserInfoProvider :: Provider
, extendedUserInfoRole :: RoleType
, extendedUserInfoText :: Text
, extendedUserInfoEmail_verified :: Bool
} deriving (Show, Generic)
data UserResp = OkUserResp
{ okUserRespId :: UUID
, okUserRespUser :: UserInfo
} | ErrorUserResp {
errorUserRespMessage :: Text
} deriving (Show, Generic)
data UsersResp = OkUsersResp
{ okUsersRespUsers :: [ExtendedUserInfo]
, okUsersRespTotal :: Int
, okUsersRespDomains :: [Text]
, okUsersRespGroups :: [Text]
, okUsersRespOrgs :: [Text]
, okUsersRespRoles :: Maybe [Text]
, okUsersRespTime :: UTCTime
, okUsersRespMessage :: Maybe Text
} | ErrorUsersResp {
errorUsersResp :: Text
} deriving (Show, Generic)
data Customer = Customer
{ customerCustomer :: CustomerName
, customerMatch :: Text
} deriving (Show, Generic)
data CustomerInfo = CustomerInfo
{ customerInfoId :: UUID
, customerInfoCustomer :: CustomerName
, customerInfoMatch :: Text
} deriving (Show, Generic)
data CustomerResp = OkCustomerResp
{ okCustomerRespId :: UUID
, okCustomerRespCustomer :: CustomerInfo
} | ErrorCustomerResp {
errorCustomerRespMessage :: Text
} deriving (Show, Generic)
data CustomersResp = OkCustomersResp
{ okCustomersRespCustomers :: [CustomerInfo]
, okCustomersRespTotal :: Int
, okCustomersRespMessage :: Maybe Text
, okCustomersRespTime :: UTCTime
} | ErrorCustomersResp {
errorCustomersMessage :: Text
} deriving (Show, Generic)
$( deriveJSON (toOpts 0 0 def) ''Severity )
$( deriveJSON (toOpts 1 1 def) ''Status )
$( deriveJSON (toOpts 1 1 def) ''Alert )
$( deriveJSON (toOpts 0 0 def { unwrap = False }) ''Tags )
$( deriveJSON (toOpts 0 0 def { unwrap = False }) ''Attributes )
$( deriveJSON (toOpts 2 2 def) ''AlertAttr )
$( deriveJSON (toOpts 2 2 def) ''AlertInfo )
$( deriveJSON (toOpts 4 3 def) ''CreateAlertResp )
$( deriveJSON (toOpts 3 2 def) ''AlertResp )
$( deriveJSON (toOpts 3 2 def) ''AlertsResp )
$( deriveJSON (toOpts 2 2 def) ''ResourceInfo )
$( deriveJSON (toOpts 2 2 def) ''Top10Info )
$( deriveJSON (toOpts 3 2 def) ''Top10Resp )
$( deriveJSON (toOpts 4 3 def) ''AlertCountResp )
$( deriveJSON (toOpts 4 3 def) ''AlertHistoryResp )
$( deriveJSON (toOpts 2 2 def) ''StatusChange )
$( deriveJSON (toOpts 2 1 def) ''Resp )
$( deriveJSON (toOpts 0 0 def) ''TrendIndication )
$( deriveJSON (toOpts 2 2 def { tag = "type" }) ''HistoryItem )
$( deriveJSON (toOpts 4 3 def { tag = "type" }) ''ExtendedHistoryItem )
$( deriveJSON (toOpts 2 2 def) ''EnvironmentInfo )
$( deriveJSON (toOpts 3 2 def) ''EnvironmentsResp )
$( deriveJSON (toOpts 2 2 def) ''ServiceInfo )
$( deriveJSON (toOpts 3 2 def) ''ServicesResp )
$( deriveJSON (toOpts 1 1 def) ''Blackout )
$( deriveJSON (toOpts 2 2 def) ''BlackoutInfo )
$( deriveJSON (toOpts 2 0 def) ''BlackoutStatus )
$( deriveJSON (toOpts 3 3 def) ''ExtendedBlackoutInfo )
$( deriveJSON (toOpts 3 2 def) ''BlackoutResp )
$( deriveJSON (toOpts 3 2 def) ''BlackoutsResp )
$( deriveJSON (toOpts 1 1 def) ''Heartbeat )
$( deriveJSON (toOpts 2 2 def) ''HeartbeatInfo )
$( deriveJSON (toOpts 3 3 def) ''CreateHeartbeatResp )
$( deriveJSON (toOpts 2 2 def) ''HeartbeatResp )
$( deriveJSON (toOpts 2 2 def) ''HeartbeatsResp )
$( deriveJSON (toOpts 3 3 def) ''CreateApiKey )
$( deriveJSON (toOpts 3 3 def) ''ApiKeyInfo )
$( deriveJSON (toOpts 5 4 def) ''CreateApiKeyResp )
$( deriveJSON (toOpts 4 3 def) ''ApiKeysResp )
$( deriveJSON (toOpts 3 2 def) ''RoleType )
$( deriveJSON (toOpts 1 1 def) ''User )
$( deriveJSON (toOpts 2 2 def) ''UserInfo )
$( deriveJSON (toOpts 3 3 def) ''ExtendedUserInfo )
$( deriveJSON (toOpts 3 2 def) ''UserResp )
$( deriveJSON (toOpts 3 2 def) ''UsersResp )
$( deriveJSON (toOpts 1 1 def) ''Customer )
$( deriveJSON (toOpts 2 2 def) ''CustomerInfo )
$( deriveJSON (toOpts 3 2 def) ''CustomerResp )
$( deriveJSON (toOpts 3 2 def) ''CustomersResp )