def getCurrentUserInfo()

in visualizeConnectData/scripts/quicksight/deploy.py [0:0]


def getCurrentUserInfo():
    stsResp = boto3.client('sts').get_caller_identity()
    logger.info(stsResp)
    accountId = stsResp['Account']
    currentUserArn = stsResp['Arn']
    
    userNameIndexInd = 'assumed-role/'
    if (userNameIndexInd not in currentUserArn):
        userNameIndexInd = 'user/'
        if (userNameIndexInd not in currentUserArn):
            raise RuntimeError('userNameIndexInd is not in the currentUserArn: {0}'.format(currentUserArn))
    
    name = currentUserArn[currentUserArn.index(userNameIndexInd) + len(userNameIndexInd):]
    
    qsListUsersResp = None
    qsNamespace = 'default'
    try:
        qsAdminRegion = 'us-east-1' #This is the default region
        qsClientLocal = boto3.client('quicksight', region_name=qsAdminRegion)
        qsListUsersResp = qsClientLocal.list_users(
            AwsAccountId=accountId,
            Namespace=qsNamespace
        )
        logger.info(qsListUsersResp)
        if ('NextToken' in qsListUsersResp):
            raise RuntimeError('Code does not support paginating')

    except ClientError as e:
        if e.response['Error']['Code'] == 'AccessDeniedException':
            message = e.response['Message']
            logger.info(message)
            
            regexPattern = '^.*(Please use the)(.*)(endpoint).*$'
            matchObj = re.match(regexPattern, message, re.I)
            qsAdminRegion = matchObj.group(2).strip()
            
            qsClientLocal = boto3.client('quicksight', region_name=qsAdminRegion)
            qsListUsersResp = qsClientLocal.list_users(
                AwsAccountId=accountId,
                Namespace=qsNamespace
            )
            if ('NextToken' in qsListUsersResp):
                raise RuntimeError('Code does not support paginating')
        else:
            raise RuntimeError(e)

    qsUserArn = None
    for user in qsListUsersResp['UserList']:
        if (user['UserName'] == name):
            logger.info('Found user {0}.  The role is {1}'.format(user['UserName'], user['Role']))
            if (user['Role'] == 'ADMIN'):
                qsUserArn = user['Arn']
                break

    if (qsUserArn is None):
        raise RuntimeError('QuickSight user {0} is not found or is not an ADMIN'.format(name))
    else:
        return accountId, qsUserArn