Skip to content
This repository was archived by the owner on Oct 15, 2020. It is now read-only.

Commit c9232e4

Browse files
Removing sessionID from credentials when not specified since it is sent in the POST body for authentication, updating README and adding other unit tests at test_oneview_client
1 parent 180d69b commit c9232e4

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

README.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Connection properties for accessing the OneView appliance can be set in a JSON f
6969
Before running the samples or your own scripts, you must create the JSON file.
7070
An example can be found at: [OneView configuration sample](examples/config-rename.json).
7171

72-
Note: If you have an active and valid login session and want to use it, define the sessionID in the Credentials. When sessionID is defined, username and password will be disregarded.
72+
Note: If you have an active and valid login session and want to use it, define the sessionID in the Credentials. When sessionID is defined, you can remove username and password from your JSON (they will be disregarded anyway).
7373

7474
Once you have created the JSON file, you can initialize the OneViewClient:
7575

@@ -101,7 +101,7 @@ export ONEVIEWSDK_PROXY='<proxy_host>:<proxy_port>'
101101

102102
:lock: Tip: Make sure no unauthorized person has access to the environment variables, since the password is stored in clear-text.
103103

104-
Note: If you have an active and valid login session and want to use it, define the ONEVIEWSDK_SESSIONID. When sessionID is defined, username and password will be disregarded.
104+
Note: If you have an active and valid login session and want to use it, define the ONEVIEWSDK_SESSIONID. When sessionID is defined, it will be used for authentication and username and password will be disregarded.
105105

106106
Once you have defined the environment variables, you can initialize the OneViewClient using the following code snippet:
107107

@@ -111,23 +111,33 @@ oneview_client = OneViewClient.from_environment_variables()
111111

112112
### Dictionary
113113

114-
You can also set the configuration using a dictionary:
114+
You can also set the configuration using a dictionary. As described above, for authentication you can use username/password:
115+
115116

116117
```python
117118
config = {
118119
"ip": "172.16.102.82",
119120
"credentials": {
120121
"userName": "Administrator",
121-
"password": "secret123",
122-
"sessionID": ""
122+
"password": "secret123"
123+
}
124+
}
125+
```
126+
127+
or if you have an active and valid login session and want to use it, define the sessionID in the Credentials:
128+
129+
130+
```python
131+
config = {
132+
"ip": "172.16.102.82",
133+
"credentials": {
134+
"sessionID": "123"
123135
}
124136
}
125137

126138
oneview_client = OneViewClient(config)
127139
```
128140

129-
Note: If you have an active and valid login session and want to use it, define the sessionID in the Credentials. When sessionID is defined, username and password will be disregarded.
130-
131141
:lock: Tip: Check the file permissions because the password is stored in clear-text.
132142

133143
### Proxy

hpOneView/connection.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ def login(self, cred, verbose=False):
443443
self.set_session_id(self._cred["sessionID"])
444444
task, body = self.put(uri['loginSessions'], None)
445445
else:
446+
self._cred.pop("sessionID", None)
446447
task, body = self.post(uri['loginSessions'], self._cred)
447448
except HPOneViewException:
448449
logger.exception('Login failed')

tests/unit/test_oneview_client.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,21 @@ def test_from_json_file_with_sessionID(self, mock_open, mock_login):
179179
self.assertIsInstance(oneview_client, OneViewClient)
180180
self.assertEqual("172.16.102.59", oneview_client.connection.get_host())
181181

182+
@mock.patch.object(connection, 'login')
183+
@mock.patch(mock_builtin('open'))
184+
def test_from_json_file_with_only_sessionID(self, mock_open, mock_login):
185+
json_config_content = u"""{
186+
"ip": "172.16.102.59",
187+
"credentials": {
188+
"sessionID": "123"
189+
}
190+
}"""
191+
mock_open.return_value = self.__mock_file_open(json_config_content)
192+
oneview_client = OneViewClient.from_json_file("config.json")
193+
194+
self.assertIsInstance(oneview_client, OneViewClient)
195+
self.assertEqual("172.16.102.59", oneview_client.connection.get_host())
196+
182197
@mock.patch.object(connection, 'login')
183198
@mock.patch(mock_builtin('open'))
184199
def test_default_api_version(self, mock_open, mock_login):
@@ -302,6 +317,21 @@ def test_from_environment_variables_is_passing_right_arguments_to_the_constructo
302317
'authLoginDomain': '',
303318
'sessionID': '123'}})
304319

320+
@mock.patch.dict('os.environ', OS_ENVIRON_CONFIG_MINIMAL_WITH_SESSIONID)
321+
@mock.patch.object(OneViewClient, '__init__')
322+
def test_from_environment_variables_is_passing_right_arguments_to_the_constructor_with_only_sessionID(self, mock_cls):
323+
mock_cls.return_value = None
324+
OneViewClient.from_environment_variables()
325+
mock_cls.assert_called_once_with({'api_version': 300,
326+
'proxy': '',
327+
'ip': '172.16.100.199',
328+
'image_streamer_ip': '',
329+
'credentials':
330+
{'userName': '',
331+
'password': '',
332+
'authLoginDomain': '',
333+
'sessionID': '123'}})
334+
305335
@mock.patch.object(connection, 'login')
306336
def test_create_image_streamer_client_without_image_streamer_ip(self, mock_login):
307337

0 commit comments

Comments
 (0)