Get Cloud Accounts Information

How can we get the relevant cloud accounts information for a particular user?

These code snippets demonstrate how the chalice server gets the cloud accounts information for a particular user. Note that this code will NOT be executed unless a valid Rosetta JWT has been presented at the API endpoint along with this request. Also note that cloud accounts are synonymous with target sites...the idea is that users are targeting the sites where their cloud accounts exist.

Once the Rosetta JWT has been validated, the chalice server simply queries the relevant information from DynamoDB. Since this behavior is part of the primary workflow for a user, we want it to be as fast as it can possibly be. Therefore, we limit the interaction to two DynamoDB interactions: (1) one to get the operational token identified by the Rosetta JWT and validate it; and (2) another to get the first X target sites for this user (nobody in our family currently has more than 100 cloud accounts, but you may need to increase that value). And, rather than shipping back all of the information associated wtih each target site, we only provide the names and the identifiers for those sites, since the user just wants to see a list of their cloud accounts so that they can choose the one they need to log in to.

--

app.py

--

from chalice import Chalice

from chalicelib import requestUtility

app = Chalice(app_name='rosetta-api-chalice')

#
# API endpoints - target-sites
#

@app.route('/target-sites')
def getTargetSites():
    userId = validateUser(app.current_request.headers)
    return requestUtility.RequestUtility().getTargetSites(userId)

--

chalicelib/requestUtility.py

--

from . dynamoDbUtility import DynamoDbUtility

class RequestUtility(object):

    # region - Public Methods

    def getTargetSites(self, userId):
        return DynamoDbUtility().getTargetSites(userId)

--

chalicelib/dynamoDbUtility.py

--

import boto3

from boto3.dynamodb.conditions import Key

ddbClient = boto3.resource('dynamodb')
ddbTargetSiteTable = ddbClient.Table('TargetSite')

def sortSiteMementoKey(siteMemento):
    siteName = siteMemento.get('siteName', '')
    return siteName.lower()

class DynamoDbUtility(object):

    # region - Public Methods - Target Site

    def getTargetSites(self, userId):
        # guard clause - problem
        itemsResponse = ddbTargetSiteTable.query(
            KeyConditionExpression=Key('userId').eq(userId),
            Limit=100
        )
        if itemsResponse is None or itemsResponse.get('Items') is None:
            return []

        items = itemsResponse.get('Items', [])
        siteMementos = list(map(
            lambda item: {
                'siteId': item.get('siteId', ''),
                'siteName': item.get('siteInfo', {}).get('siteName', '')
            },
            items
        ))
        siteMementos.sort(key=sortSiteMementoKey)
        return siteMementos

--