Get Cloud Account Information

How can we get the information for a specific cloud account?

These code snippets demonstrate how the chalice server gets the cloud account information for a particular user and a particular cloud account. 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 a cloud account is synonymous with a target site...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 information for this particular cloud account/target site.

--

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/{siteId}')
def getTargetSite(siteId):
    userId = validateUser(app.current_request.headers)
    return requestUtility.RequestUtility().getTargetSite(userId, siteId)

--

chalicelib/requestUtility.py

--

from . dynamoDbUtility import DynamoDbUtility

class RequestUtility(object):

    # region - Public Methods

    def getTargetSite(self, userId, siteId):
        return DynamoDbUtility().getTargetSite(userId, siteId)

--

chalicelib/dynamoDbUtility.py

--

import boto3

from boto3.dynamodb.conditions import Key

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

class DynamoDbUtility(object):

    # region - Public Methods - Target Site

    def getTargetSite(self, userId, siteId):
        # guard clause - not found
        itemResponse = ddbTargetSiteTable.get_item(
            Key={ 'userId': userId, 'siteId': siteId }
        )
        if itemResponse is None or itemResponse.get('Item') is None:
            return None

        siteInfo = itemResponse.get('Item', {}).get('siteInfo', {})
        return siteInfo

--