]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Improve ODL ML2 Exception Handling
authorDave Tucker <dave@dtucker.co.uk>
Wed, 23 Apr 2014 16:01:38 +0000 (17:01 +0100)
committerDave Tucker <dave@dtucker.co.uk>
Fri, 25 Apr 2014 18:15:27 +0000 (19:15 +0100)
Raise a MechanismDriverError when authentication with ODL fails.
Re-raise HTTPError in "sync_resources" for exceptions whose status code is not 404.

Closes-Bug: #1311758
Change-Id: I99d2c4f270007c369024525ba4e6188d232a0b13

neutron/plugins/ml2/drivers/mechanism_odl.py

index cecc06c01aa4b1be94f298d666bdac4e681e1b53..d4e92334f1d2b2425889a98020e0c8ab3618492c 100644 (file)
@@ -68,6 +68,10 @@ def try_del(d, keys):
             pass
 
 
+class OpendaylightAuthError(n_exc.NeutronException):
+    message = '%(msg)s'
+
+
 class JsessionId(requests.auth.AuthBase):
 
     """Attaches the JSESSIONID and JSESSIONIDSSO cookies to an HTTP Request.
@@ -95,8 +99,16 @@ class JsessionId(requests.auth.AuthBase):
     def obtain_auth_cookies(self):
         """Make a REST call to obtain cookies for ODL authenticiation."""
 
-        r = requests.get(self.url, auth=(self.username, self.password))
-        r.raise_for_status()
+        try:
+            r = requests.get(self.url, auth=(self.username, self.password))
+            r.raise_for_status()
+        except requests.exceptions.HTTPError as e:
+            raise OpendaylightAuthError(msg=_("Failed to authenticate with "
+                                              "OpenDaylight: %s") % e)
+        except requests.exceptions.Timeout as e:
+            raise OpendaylightAuthError(msg=_("Authentication Timed"
+                                              " Out: %s") % e)
+
         jsessionid = r.cookies.get('JSESSIONID')
         jsessionidsso = r.cookies.get('JSESSIONIDSSO')
         if jsessionid and jsessionidsso:
@@ -203,9 +215,11 @@ class OpenDaylightMechanismDriver(api.MechanismDriver):
                 urlpath = collection_name + '/' + resource['id']
                 self.sendjson('get', urlpath, None)
             except requests.exceptions.HTTPError as e:
-                if e.response.status_code == 404:
-                    attr_filter(resource, context, dbcontext)
-                    to_be_synced.append(resource)
+                with excutils.save_and_reraise_exception() as ctx:
+                    if e.response.status_code == 404:
+                        attr_filter(resource, context, dbcontext)
+                        to_be_synced.append(resource)
+                        ctx.reraise = False
 
         key = resource_name if len(to_be_synced) == 1 else collection_name