summaryrefslogtreecommitdiffstats
path: root/server/codes.py
blob: 82bfb0a46e71d3d88f537cda982930625846c8a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/python
"""
Virt-factory backend code.

Copyright 2006, Red Hat, Inc
Michael DeHaan <mdehaan@redhat.com>
Scott Seago <sseago@redhat.com>

This software may be freely redistributed under the terms of the GNU
general public license.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
"""

import exceptions
import string
import sys
import traceback



# internal codes for the types of operations (used by validation logic)
OP_ADD = "add"
OP_EDIT = "edit"
OP_DELETE = "delete"
OP_LIST = "list"
OP_METHOD = "method"
OP_GET = "get"

# error codes for the web service.
SUCCESS = ERR_SUCCESS      = 0
ERR_TOKEN_EXPIRED          = 1
ERR_TOKEN_INVALID          = 2
ERR_USER_INVALID           = 3
ERR_PASSWORD_INVALID       = 4
ERR_INTERNAL_ERROR         = 5
ERR_INVALID_ARGUMENTS      = 6
ERR_NO_SUCH_OBJECT         = 7
ERR_ORPHANED_OBJECT        = 8
ERR_SQL                    = 9
ERR_MISCONFIGURED          = 10
ERR_UNCAUGHT               = 11 
ERR_INVALID_METHOD         = 12
ERR_TASK                   = 13
ERR_REG_TOKEN_INVALID      = 14
ERR_REG_TOKEN_EXHAUSTED    = 15
ERR_PUPPET_NODE_NOT_SIGNED = 16



class FuncException(exceptions.Exception):
   error_code = ERR_INTERNAL_ERROR

   def __init__(self, **kwargs):
       self.job_id         = self.load(kwargs,"job_id")
       self.stacktrace     = self.load(kwargs,"stacktrace")
       self.invalid_fields = self.load(kwargs,"invalid_fields")
       self.data           = self.load(kwargs,"data")
       self.comment        = self.load(kwargs,"comment")
       self.tb_data = traceback.extract_stack()
       exceptions.Exception.__init__(self)
       

   def format(self):
      msg = """
Exception Name: %s
Exception Comment: %s
Exception Data: %s
Stack Trace:
%s""" % (self.__class__, self.comment, self.data,
         string.join(traceback.format_list(self.tb_data)))

      return msg

   def ok(self):
       return self.error_code == 0
 
   def load(self,hash,key,default=None):
       if hash.has_key(key):
           return hash[key]
       else:
           return default

   def __get_additional_data(self):
       data = {}
       if not self.job_id is None:
           data["job_id"] = self.job_id
       if not self.stacktrace is None:
           data["stacktrace"] = self.stacktrace
       if not self.invalid_fields is None:
           data["invalid_fields"] = self.invalid_fields
       if not self.data is None:
           data["data"] = self.data
       if not self.comment is None:
           data["comment"] = self.comment
       return data

   def to_datastruct(self):
       return (self.error_code, self.__get_additional_data())

#FIXME: hack
VirtFactoryException = FuncException

class SuccessException(VirtFactoryException):
   """
   Not an error / return success and data to caller.
   """
   error_code = ERR_SUCCESS   

class TokenExpiredException(VirtFactoryException):
   """
   The user token that was passed in has been logged out 
   due to inactivity.  Call user_login again.
   """
   error_code = ERR_TOKEN_EXPIRED

class TokenInvalidException(VirtFactoryException):
   """
   The user token doesn't exist, so this function call isn't 
   permitted.  Call user_login to get a valid token.
   """
   error_code = ERR_TOKEN_INVALID

class RegTokenInvalidException(VirtFactoryException):
   """
   The registration token doesn't exist, so this function call isn't 
   permitted. 
   """
   error_code = ERR_REG_TOKEN_INVALID

class RegTokenExhaustedException(VirtFactoryException):
   """
   The registration token that was passed in has been used
   it allowed number of uses.
   """
   error_code = ERR_REG_TOKEN_EXHAUSTED

class UserInvalidException(VirtFactoryException):
   """
   Can't log in this user since the user account doesn't 
   exist in the database.
   """
   error_code = ERR_USER_INVALID

class PasswordInvalidException(VirtFactoryException):
   """
   Wrong password.  Bzzzt.  Try again.
   """
   error_code = ERR_PASSWORD_INVALID

class InternalErrorException(VirtFactoryException):
   """
   FIXME: This is a generic error code, and if something is 
   throwing that error, it probably
   should be changed to throw something more specific.
   """
   error_code = ERR_INTERNAL_ERROR

class InvalidArgumentsException(VirtFactoryException):
   """
   The arguments passed in to this function failed to pass 
   validation.  See additional_data for the
   names of which arguments were rejected.
   """
   error_code = ERR_INVALID_ARGUMENTS

class NoSuchObjectException(VirtFactoryException):
   """
   The id passed in doesn't refer to an object.
   """
   error_code = ERR_NO_SUCH_OBJECT

class InvalidMethodException(VirtFactoryException):
   """The method called does not exist"""
   error_code = ERR_INVALID_METHOD

class UncaughtException(VirtFactoryException):
   """
   The python code choked.  additional_data contains the 
   stacktrace, and it's ok to give the stacktrace
   since the user is already logged in.  user_login shouldn't 
   give stacktraces for security reasons.
   """
   error_code = ERR_UNCAUGHT

class OrphanedObjectException(VirtFactoryException):
   """
   A delete can't proceed because another object references 
   this one, or an add can't proceed because a
   referenced object doesn't exist.
   """
   error_code = ERR_ORPHANED_OBJECT

class SQLException(VirtFactoryException):
   """
   The code died inside a SQL call.  This is probably 
   a sign that the validation prior to making
   the call needs to be improved, or maybe SQL was just 
   more efficient (i.e. referential integrity).
   """
   error_code = ERR_SQL

class TaskException(VirtFactoryException):
   """
   Something went wrong with the background task engine
   """
   error_code = ERR_TASK

class MisconfiguredException(VirtFactoryException):
   """
   The virt-factory service isn't properly configured and 
   no calls can be processed until this is corrected on the 
   server side.  The UI/WUI/etc is non-functional and should 
   display a splash screen telling the user to finish their 
   setup of the virt-factory service by running "vf_server init", edit
   /var/lib/virt-factory/settings, and then run "vf_server import".
   """
   error_code = ERR_MISCONFIGURED


class PuppetNodeNotSignedException(VirtFactoryException):
   """
   The puppet node certificate could not be signed, either
   because there was no matching certificate requrest or
   due to another puppetca error.
   """
   error_code = ERR_PUPPET_NODE_NOT_SIGNED

def success(data=None,job_id=None):
   """
   Shortcut around success exception that returns equivalent data 
   w/o raise.
   """
   ret = SuccessException(data=data, job_id=job_id)
   return ret


if __name__ == "__main__":
   # run this module as main to generate a ruby compatible constants
   # file.
   module = sys.modules[__name__]
   for x in sorted(module.__dict__.keys()):
       obj = module.__dict__[x]
       if (type(obj) == int or type(obj) == str) and not x.startswith("__"):
           if type(obj) == int:
               print "%s = %s" % (x, obj)
           else:
               print "%s = \"%s\"" % (x, obj)