Repository: HewlettPackard/python-hpOneView

Issue:
HPOneViewException not raised when connection with paused VM fails 
Hi guys,

When I try to create the OneView client but my VM is paused, an **OSError** exception is raised instead of a HPOneViewException. The HPOneViewException should not cover this type of exception in this scenario? For treat this exception is only possible if I catch an **OSError**, but is seems odd for me since this also could be used as a HPOneViewException maybe.

**Code example:**

```python
#VM paused
config = {
	"ip": "x.x.x.x",
	"credentials": {
		"userName": "user_name",
		"password": "pass"
	}
}

oneview_client = OneViewClient(config)
```

**Result:**

```shell
Traceback (most recent call last):
  File "one_view.py", line 89, in <module>
    one_view = OneView(config)
  File "one_view.py", line 12, in __init__
    self.oneview_client = OneViewClient(config)
  File "/usr/local/lib/python3.5/dist-packages/hpOneView/oneview_client.py", line 118, in __init__
    self.__connection.login(config["credentials"])
  File "/usr/local/lib/python3.5/dist-packages/hpOneView/connection.py", line 483, in login
    self.validateVersion()
  File "/usr/local/lib/python3.5/dist-packages/hpOneView/connection.py", line 79, in validateVersion
    version = self.get(uri['version'])
  File "/usr/local/lib/python3.5/dist-packages/hpOneView/connection.py", line 324, in get
    resp, body = self.do_http('GET', uri, '')
  File "/usr/local/lib/python3.5/dist-packages/hpOneView/connection.py", line 126, in do_http
    conn.request(method, path, body, http_headers)
  File "/usr/lib/python3.5/http/client.py", line 1106, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python3.5/http/client.py", line 1151, in _send_request
    self.endheaders(body)
  File "/usr/lib/python3.5/http/client.py", line 1102, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python3.5/http/client.py", line 934, in _send_output
    self.send(msg)
  File "/usr/lib/python3.5/http/client.py", line 877, in send
    self.connect()
  File "/usr/lib/python3.5/http/client.py", line 1252, in connect
    super().connect()
  File "/usr/lib/python3.5/http/client.py", line 849, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "/usr/lib/python3.5/socket.py", line 711, in create_connection
    raise err
  File "/usr/lib/python3.5/socket.py", line 702, in create_connection
    sock.connect(sa)
OSError: [Errno 113] No route to host
```



Discussion:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1c4a8175..04f9040a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,8 +1,11 @@
-# v4.1.0 (Unreleased)
+# v4.1.0
 
 #### New Resources:
    - Appliance node information
 
+#### Bug fixes & Enhancements
+- [#309](https://github.com/HewlettPackard/python-hpOneView/issues/309) HPOneViewException not raised when connection with paused VM fails
+
 # v4.0.0
 #### Notes
 Major release which extends support of the SDK to OneView Rest API version 500 (OneView v3.10).
diff --git a/hpOneView/connection.py b/hpOneView/connection.py
index 4d32ad3a..bba6b8d0 100644
--- a/hpOneView/connection.py
+++ b/hpOneView/connection.py
@@ -14,6 +14,7 @@ from __future__ import unicode_literals
 from builtins import open
 from builtins import str
 from future import standard_library
+from future.utils import raise_from
 
 standard_library.install_aliases()
 
@@ -479,8 +480,11 @@ class connection(object):
     # Login/Logout to/from appliance
     ###########################################################################
     def login(self, cred, verbose=False):
-        if self._validateVersion is False:
-            self.validateVersion()
+        try:
+            if self._validateVersion is False:
+                self.validateVersion()
+        except Exception as e:
+            raise_from(HPOneViewException('Failure during login attempt.'), e)
 
         self._cred = cred
         try:
diff --git a/setup.py b/setup.py
index ff2ac945..3fe21c28 100644
--- a/setup.py
+++ b/setup.py
@@ -26,10 +26,10 @@ from setuptools import find_packages
 from setuptools import setup
 
 setup(name='hpOneView',
-      version='4.0.0',
+      version='4.1.0',
       description='HPE OneView Python Library',
       url='https://github.com/HewlettPackard/python-hpOneView',
-      download_url="https://github.com/HewlettPackard/python-hpOneView/tarball/v4.0.0",
+      download_url="https://github.com/HewlettPackard/python-hpOneView/tarball/v4.1.0",
       author='Hewlett Packard Enterprise Development LP',
       author_email='oneview-pythonsdk@hpe.com',
       license='MIT',


Test patch:
diff --git a/tests/unit/test_connection.py b/tests/unit/test_connection.py
index 9206ed11..4a5a9cdc 100644
--- a/tests/unit/test_connection.py
+++ b/tests/unit/test_connection.py
@@ -869,6 +869,13 @@ class ConnectionTest(unittest.TestCase):
         self.assertEqual(self.connection.get_session_id(), '123')
         self.assertEqual(self.connection.get_session(), True)
 
+    @patch.object(connection, 'get')
+    def test_login_catches_exceptions_as_hpOneView(self, mock_get):
+        mock_get.side_effect = [Exception('test')]
+
+        with self.assertRaises(HPOneViewException):
+            self.connection.login({})
+
     @patch.object(connection, 'get')
     @patch.object(connection, 'post')
     def test_login_with_exception_in_post(self, mock_post, mock_get):