htools: add a compatibility module
[ganeti-local] / htools / Ganeti / Jobs.hs
1 {-| Implementation of the job information.
2
3 -}
4
5 {-
6
7 Copyright (C) 2009, 2010, 2011 Google Inc.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 02110-1301, USA.
23
24 -}
25
26 module Ganeti.Jobs
27     ( OpStatus(..)
28     , JobStatus(..)
29     ) where
30
31 import Text.JSON (readJSON, showJSON, JSON)
32 import qualified Text.JSON as J
33
34 import qualified Ganeti.Constants as C
35
36 -- | Our ADT for the OpCode status at runtime (while in a job).
37 data OpStatus = OP_STATUS_QUEUED
38               | OP_STATUS_WAITLOCK
39               | OP_STATUS_CANCELING
40               | OP_STATUS_RUNNING
41               | OP_STATUS_CANCELED
42               | OP_STATUS_SUCCESS
43               | OP_STATUS_ERROR
44                 deriving (Eq, Enum, Bounded, Show, Read)
45
46 instance JSON OpStatus where
47     showJSON os = showJSON w
48       where w = case os of
49               OP_STATUS_QUEUED -> C.jobStatusQueued
50               OP_STATUS_WAITLOCK -> C.jobStatusWaitlock
51               OP_STATUS_CANCELING -> C.jobStatusCanceling
52               OP_STATUS_RUNNING -> C.jobStatusRunning
53               OP_STATUS_CANCELED -> C.jobStatusCanceled
54               OP_STATUS_SUCCESS -> C.jobStatusSuccess
55               OP_STATUS_ERROR -> C.jobStatusError
56     readJSON s = case readJSON s of
57       J.Ok v | v == C.jobStatusQueued -> J.Ok OP_STATUS_QUEUED
58              | v == C.jobStatusWaitlock -> J.Ok OP_STATUS_WAITLOCK
59              | v == C.jobStatusCanceling -> J.Ok OP_STATUS_CANCELING
60              | v == C.jobStatusRunning -> J.Ok OP_STATUS_RUNNING
61              | v == C.jobStatusCanceled -> J.Ok OP_STATUS_CANCELED
62              | v == C.jobStatusSuccess -> J.Ok OP_STATUS_SUCCESS
63              | v == C.jobStatusError -> J.Ok OP_STATUS_ERROR
64              | otherwise -> J.Error ("Unknown opcode status " ++ v)
65       _ -> J.Error ("Cannot parse opcode status " ++ show s)
66
67 -- | The JobStatus data type. Note that this is ordered especially
68 -- such that greater\/lesser comparison on values of this type makes
69 -- sense.
70 data JobStatus = JOB_STATUS_QUEUED
71                | JOB_STATUS_WAITLOCK
72                | JOB_STATUS_RUNNING
73                | JOB_STATUS_SUCCESS
74                | JOB_STATUS_CANCELING
75                | JOB_STATUS_CANCELED
76                | JOB_STATUS_ERROR
77                  deriving (Eq, Enum, Ord, Bounded, Show, Read)
78
79 instance JSON JobStatus where
80     showJSON js = showJSON w
81         where w = case js of
82                 JOB_STATUS_QUEUED -> "queued"
83                 JOB_STATUS_WAITLOCK -> "waiting"
84                 JOB_STATUS_CANCELING -> "canceling"
85                 JOB_STATUS_RUNNING -> "running"
86                 JOB_STATUS_CANCELED -> "canceled"
87                 JOB_STATUS_SUCCESS -> "success"
88                 JOB_STATUS_ERROR -> "error"
89     readJSON s = case readJSON s of
90       J.Ok "queued" -> J.Ok JOB_STATUS_QUEUED
91       J.Ok "waiting" -> J.Ok JOB_STATUS_WAITLOCK
92       J.Ok "canceling" -> J.Ok JOB_STATUS_CANCELING
93       J.Ok "running" -> J.Ok JOB_STATUS_RUNNING
94       J.Ok "success" -> J.Ok JOB_STATUS_SUCCESS
95       J.Ok "canceled" -> J.Ok JOB_STATUS_CANCELED
96       J.Ok "error" -> J.Ok JOB_STATUS_ERROR
97       _ -> J.Error ("Unknown job status " ++ show s)