Skip to content

Commit

Permalink
Fetch all usage plans in DevPortalLambda
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Chew committed Jun 26, 2019
1 parent 6cdc771 commit 34dd2f4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 34 deletions.
50 changes: 18 additions & 32 deletions lambdas/backend/_common/customers-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

'use strict'
const AWS = require('aws-sdk')
const { getAllUsagePlans } = require('../shared/get-all-usage-plans')

const dynamoDb = new AWS.DynamoDB.DocumentClient()
const apigateway = new AWS.APIGateway()
Expand Down Expand Up @@ -195,10 +196,9 @@ function getUsagePlansForCustomer(cognitoIdentityId, error, callback) {
keyId,
limit: 1000
}
apigateway.getUsagePlans(params, (err, usagePlansData) => {
if (err) error(err)
else callback(usagePlansData)
})
getAllUsagePlans(apigateway, params)
.then(usagePlansData => callback({ items: usagePlansData }))
.catch(err => error(err))
}
})
}
Expand All @@ -209,26 +209,22 @@ function getUsagePlanForProductCode(productCode, error, callback) {
// do a linear scan of usage plans for name matching productCode
var params = {
limit: 1000
};
apigateway.getUsagePlans(params, function(err, data) {
if (err) {
error(err)
} else {
console.log(`Got usage plans ${JSON.stringify(data.items)}`)
}
getAllUsagePlans(apigateway, params).then(usagePlans => {
console.log(`Got usage plans ${JSON.stringify(usagePlans)}`)

// note: ensure that only one usage plan maps to a given marketplace product code
const usageplan = data.items.find(function (item) {
return item.productCode !== undefined && item.productCode === productCode
})
if (usageplan !== undefined) {
console.log(`Found usage plan matching ${productCode}`)
callback(usageplan)
} else {
console.log(`Couldn't find usageplan matching product code ${productCode}`)
error(`Couldn't find usageplan matching product code ${productCode}`)
}
// note: ensure that only one usage plan maps to a given marketplace product code
const usageplan = usagePlans.find(function (item) {
return item.productCode !== undefined && item.productCode === productCode
})
if (usageplan !== undefined) {
console.log(`Found usage plan matching ${productCode}`)
callback(usageplan)
} else {
console.log(`Couldn't find usageplan matching product code ${productCode}`)
error(`Couldn't find usageplan matching product code ${productCode}`)
}
});
}).catch(err => error(err))
}

function updateCustomerMarketplaceId(cognitoIdentityId, marketplaceCustomerId, error, success) {
Expand Down Expand Up @@ -323,16 +319,6 @@ function updateCustomerApiKeyId(cognitoIdentityId, apiKeyId, error, success) {
})
}

// function getUsagePlans(error, callback) {
// const params = {
// limit: 1000
// }
// apigateway.getUsagePlans(params, (err, data) => {
// if (err) error(err)
// else callback(data)
// })
// }

module.exports = {
ensureCustomerItem,
subscribe,
Expand Down
5 changes: 3 additions & 2 deletions lambdas/backend/express-route-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const feedbackController = require('./_common/feedback-controller.js')
const AWS = require('aws-sdk')
const catalog = require('./catalog/index')
const hash = require('object-hash')
const { getAllUsagePlans } = require('./shared/get-all-usage-plans')

const Datauri = require('datauri')

Expand Down Expand Up @@ -421,7 +422,7 @@ async function getAdminCatalogVisibility(req, res) {
})
})

let usagePlans = await exports.apigateway.getUsagePlans().promise()
let usagePlans = await getAllUsagePlans(exports.apigateway)

// In the case of apiGateway APIs, the client doesn't know if there are usage plan associated or not
// so we need to provide that information. This can't be merged with the above loop:
Expand All @@ -431,7 +432,7 @@ async function getAdminCatalogVisibility(req, res) {
visibility.apiGateway.map((apiEntry) => {
apiEntry.subscribable = false

usagePlans.items.forEach((usagePlan) => {
usagePlans.forEach((usagePlan) => {
usagePlan.apiStages.forEach((apiStage) => {
if(apiEntry.id === apiStage.apiId && apiEntry.stage === apiStage.stage) {
apiEntry.subscribable = true
Expand Down
33 changes: 33 additions & 0 deletions lambdas/backend/shared/get-all-usage-plans.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Fetches all usage plans, combining all pages into a single array.
*
* @param apiGateway
* an instance of `AWS.APIGateway` to use for API calls
*
* @param paramOverrides
* a parameter object passed in calls to `APIGateway.getUsagePlans`
*
* @returns
* a Promise resolving with an array of items returned from
* `APIGateway.getUsagePlans` calls
*/
async function getAllUsagePlans(apiGateway, paramOverrides = {}) {
// The maximum allowed value of `limit` is 500 according to
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/APIGateway.html#getUsagePlans-property
const defaultParams = {limit: 500, ...paramOverrides}

console.log('Fetching first page of usage plans')
let response = await apiGateway.getUsagePlans(defaultParams).promise()
const usagePlans = response.items

while (response.position) {
console.log(`Fetching next page of usage plans, at position=[${response.position}]`)
const nextParams = {...defaultParams, position: response.position}
response = await apiGateway.getUsagePlans(nextParams).promise()
usagePlans.push(...response.items)
}

return usagePlans
}

module.exports = { getAllUsagePlans }

0 comments on commit 34dd2f4

Please sign in to comment.