@@ -36,6 +36,11 @@ class BadRequest(ConsulException):
3636 pass
3737
3838
39+ class ClientError (ConsulException ):
40+ """Encapsulates 4xx Http error code"""
41+ pass
42+
43+
3944#
4045# Convenience to define checks
4146
@@ -158,24 +163,28 @@ def _compat(
158163
159164class CB (object ):
160165 @classmethod
161- def __status (klass , response , allow_404 = True ):
166+ def _status (klass , response , allow_404 = True ):
162167 # status checking
163- if response .code >= 500 and response .code < 600 :
168+ if 400 <= response .code < 500 :
169+ if response .code == 400 :
170+ raise BadRequest ('%d %s' % (response .code , response .body ))
171+ elif response .code == 401 :
172+ raise ACLDisabled (response .body )
173+ elif response .code == 403 :
174+ raise ACLPermissionDenied (response .body )
175+ elif response .code == 404 :
176+ if not allow_404 :
177+ raise NotFound (response .body )
178+ else :
179+ raise ClientError ("%d %s" % (response .code , response .body ))
180+ elif 500 <= response .code < 600 :
164181 raise ConsulException ("%d %s" % (response .code , response .body ))
165- if response .code == 400 :
166- raise BadRequest ('%d %s' % (response .code , response .body ))
167- if response .code == 401 :
168- raise ACLDisabled (response .body )
169- if response .code == 403 :
170- raise ACLPermissionDenied (response .body )
171- if response .code == 404 and not allow_404 :
172- raise NotFound (response .body )
173182
174183 @classmethod
175184 def bool (klass ):
176185 # returns True on successful response
177186 def cb (response ):
178- CB .__status (response )
187+ CB ._status (response )
179188 return response .code == 200
180189 return cb
181190
@@ -204,7 +213,7 @@ def json(
204213 *is_id* only the 'ID' field of the json object will be returned.
205214 """
206215 def cb (response ):
207- CB .__status (response , allow_404 = allow_404 )
216+ CB ._status (response , allow_404 = allow_404 )
208217 if response .code == 404 :
209218 return response .headers ['X-Consul-Index' ], None
210219
@@ -886,7 +895,7 @@ def deregister(self, service_id):
886895 take care of deregistering the service with the Catalog. If
887896 there is an associated check, that is also deregistered.
888897 """
889- return self .agent .http .get (
898+ return self .agent .http .put (
890899 CB .bool (), '/v1/agent/service/deregister/%s' % service_id )
891900
892901 def maintenance (self , service_id , enable , reason = None ):
@@ -999,7 +1008,7 @@ def deregister(self, check_id):
9991008 """
10001009 Remove a check from the local agent.
10011010 """
1002- return self .agent .http .get (
1011+ return self .agent .http .put (
10031012 CB .bool (),
10041013 '/v1/agent/check/deregister/%s' % check_id )
10051014
@@ -1012,7 +1021,7 @@ def ttl_pass(self, check_id, notes=None):
10121021 if notes :
10131022 params ['note' ] = notes
10141023
1015- return self .agent .http .get (
1024+ return self .agent .http .put (
10161025 CB .bool (),
10171026 '/v1/agent/check/pass/%s' % check_id ,
10181027 params = params )
@@ -1027,7 +1036,7 @@ def ttl_fail(self, check_id, notes=None):
10271036 if notes :
10281037 params ['note' ] = notes
10291038
1030- return self .agent .http .get (
1039+ return self .agent .http .put (
10311040 CB .bool (),
10321041 '/v1/agent/check/fail/%s' % check_id ,
10331042 params = params )
@@ -1042,7 +1051,7 @@ def ttl_warn(self, check_id, notes=None):
10421051 if notes :
10431052 params ['note' ] = notes
10441053
1045- return self .agent .http .get (
1054+ return self .agent .http .put (
10461055 CB .bool (),
10471056 '/v1/agent/check/warn/%s' % check_id ,
10481057 params = params )
0 commit comments