Skip to content

Commit 8476581

Browse files
committed
Add functionality to login with api-key
1 parent 0b79641 commit 8476581

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

cpapi/mgmt_api.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,15 @@ def save_debug_data(self):
139139
out_file = open(self.debug_file, 'w+')
140140
out_file.write(json.dumps(self.api_calls, indent=4, sort_keys=True))
141141

142-
def login(self, username, password, continue_last_session=False, domain=None, read_only=False,
143-
payload=None):
142+
def _build_payload(self, username=None, password=None, api_key=None):
143+
payload = {}
144+
if username: payload['user'] = username
145+
if password: payload['password'] = password
146+
if api_key: payload['api-key'] = api_key
147+
return payload
148+
149+
def login(self, username=None, password=None, continue_last_session=False, domain=None, read_only=False,
150+
payload=None, api_key=None):
144151
"""
145152
performs a 'login' API call to the management server
146153
@@ -155,7 +162,7 @@ def login(self, username, password, continue_last_session=False, domain=None, re
155162
:returns: APIResponse object
156163
:side-effects: updates the class's uid and server variables
157164
"""
158-
credentials = {"user": username, "password": password}
165+
credentials = self._build_payload(username, password, api_key)
159166

160167
if self.context == "web_api":
161168
credentials.update({"continue-last-session": continue_last_session,
@@ -310,7 +317,10 @@ def api_call(self, command, payload=None, sid=None, wait_for_task=True):
310317
# would not appear as plaintext in the debug file.
311318
if command == "login":
312319
json_data = compatible_loads(_data)
313-
json_data["password"] = "****"
320+
if "password" in json_data:
321+
json_data["password"] = "****"
322+
if "api-key" in json_data:
323+
json_data["api-key"] = "****"
314324
_data = json.dumps(json_data)
315325

316326
# Store the request and the reply (for debug purpose).

0 commit comments

Comments
 (0)