Forked from
DM / dm-docs
261 commits behind, 505 commits ahead of the upstream repository.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
dmRestSessionCli.py 2.32 KiB
#!/usr/bin/env python
from dm.common.cli.dmRestCli import DmRestCli
from dm.common.utility.configurationManager import ConfigurationManager
class DmRestSessionCli(DmRestCli):
""" Base dm session cli class. """
def __init__(self, validArgCount=0):
DmRestCli.__init__(self, validArgCount)
self.username = None
self.password = None
loginGroup = 'Login Options'
self.addOptionGroup(loginGroup, None)
self.addOptionToGroup(loginGroup, '', '--login-username', dest='loginUsername', help='Login username.')
self.addOptionToGroup(loginGroup, '', '--login-password', dest='loginPassword', help='Login password.')
self.addOptionToGroup(loginGroup, '', '--login-file', dest='loginFile', help='Login file, contains "<login username>|<login password>" pair. It can be specified using DM_LOGIN_FILE environment variable. This option cannot be used with --login-username and --login-password options.')
def parseArgs(self, usage=None):
DmRestCli.parseArgs(self, usage)
self.loginUsername = self.options.loginUsername
self.loginPassword = self.options.loginPassword
self.loginFile = self.options.loginFile
self.parseLoginFile()
return (self.options, self.args)
def getLoginUsername(self):
return self.loginUsername
def getLoginPassword(self):
return self.loginPassword
def getLoginFile(self):
if not self.loginFile:
configManager = ConfigurationManager.getInstance()
self.loginFile = configManager.getLoginFile()
return self.loginFile
def parseLoginFile(self):
if self.getLoginFile() and not self.getLoginUsername() and not self.getLoginPassword():
try:
# Assume form <username>|<password>
tokenList = open(self.loginFile).readline().split('|')
if len(tokenList) == 2:
self.loginUsername = tokenList[0].strip()
self.loginPassword = tokenList[1].strip()
except:
# Ignore invalid login file
pass
def hasCredentials(self):
return (self.getLoginUsername() != None and self.getLoginPassword() != None)
#######################################################################
# Testing
if __name__ == '__main__':
pass