Skip to content
Snippets Groups Projects
Forked from DM / dm-docs
261 commits behind, 807 commits ahead of the upstream repository.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
getUserCli.py 1.63 KiB
#!/usr/bin/env python

from dm.common.exceptions.invalidRequest import InvalidRequest
from dm.ds_web_service.api.userRestApi import UserRestApi
from dsWebServiceSessionCli import DsWebServiceSessionCli

class GetUserCli(DsWebServiceSessionCli):
    def __init__(self):
        DsWebServiceSessionCli.__init__(self)
        self.addOption('', '--id', dest='id', help='User id. Either id or username must be provided. If both are provided, id takes precedence.')
        self.addOption('', '--username', dest='username', help='User username. Either id or username must be provided. If both are provided, id takes precedence.')

    def checkArgs(self):
        if self.options.id is None and self.options.username is None:
            raise InvalidRequest('Either user id or username must be provided.')

    def getId(self):
        return self.options.id

    def getUsername(self):
        return self.options.username

    def runCommand(self):
        self.parseArgs(usage="""
    dm-get-user --id=ID|--username=USERNAME

Description:
    Retrieves user information.
        """)
        self.checkArgs()
        api = UserRestApi(self.getLoginUsername(), self.getLoginPassword(), self.getServiceHost(), self.getServicePort(), self.getServiceProtocol())
        if self.getId() is not None:
            userInfo = api.getUserById(self.getId())
        else:
            userInfo = api.getUserByUsername(self.getUsername())
        print userInfo.getDisplayString(self.getDisplayKeys(), self.getDisplayFormat())

#######################################################################
# Run command.
if __name__ == '__main__':
    cli = GetUserCli()
    cli.run()