Skip to content

Commit

Permalink
feat: add subscriptionids to recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinDawson authored and camcash17 committed Oct 17, 2023
1 parent 2fc7dfa commit 49948b5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 38 deletions.
4 changes: 3 additions & 1 deletion packages/app/src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ export default class App {
if (AZURE?.USE_BILLING_DATA) {
const azureAccount = new AzureAccount()
await azureAccount.initializeAccount()
const recommendations = await azureAccount.getDataFromAdvisorManagement()
const recommendations = await azureAccount.getDataFromAdvisorManagement(
request.accounts,
)
AzureRecommendations.push(recommendations)
}
allRecommendations.push(AzureRecommendations.flat())
Expand Down
1 change: 1 addition & 0 deletions packages/app/src/CreateValidRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface EstimationRequest {

export interface RecommendationRequest {
awsRecommendationTarget?: AWS_RECOMMENDATIONS_TARGETS
accounts?: string[]
}

interface FormattedEstimationRequest {
Expand Down
1 change: 1 addition & 0 deletions packages/app/src/RawRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface FootprintEstimatesRawRequest {

export interface RecommendationsRawRequest {
awsRecommendationTarget?: string
accounts?: string[]
}

export interface Tags {
Expand Down
84 changes: 47 additions & 37 deletions packages/azure/src/application/AzureAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ export default class AzureAccount extends CloudProviderAccount {
}
}

public async getDataFromAdvisorManagement(): Promise<RecommendationResult[]> {
const subscriptions = await this.getSubscriptions()
public async getDataFromAdvisorManagement(
subscriptionIds: string[],
): Promise<RecommendationResult[]> {
const subscriptions = await this.getSubscriptions(subscriptionIds)
const recommendations = await Promise.all(
subscriptions.map(async (subscription: Subscription) => {
try {
Expand All @@ -80,17 +82,10 @@ export default class AzureAccount extends CloudProviderAccount {
startDate: Date,
endDate: Date,
grouping: GroupBy,
subscriptionIds: string[] = [],
subscriptionIds: string[],
): Promise<EstimationResult[]> {
const AZURE = configLoader().AZURE
const defaultAzureSubscriptionIds = subscriptionIds.length
? subscriptionIds
: AZURE.SUBSCRIPTIONS

const subscriptions = defaultAzureSubscriptionIds?.length
? await this.getSubscriptionsByIds(defaultAzureSubscriptionIds)
: await this.getSubscriptions()

const subscriptions = await this.getSubscriptions(subscriptionIds)
const requests = this.createSubscriptionRequests(
subscriptions,
startDate,
Expand All @@ -116,40 +111,55 @@ export default class AzureAccount extends CloudProviderAccount {
return R.flatten(estimationResults)
}

public async getSubscriptions(): Promise<Subscription[]> {
const subscriptions = []
for await (const subscription of this.subscriptionClient.subscriptions.list()) {
subscriptions.push(subscription)
}

if (subscriptions.length === 0) {
this.logger.warn(
'No subscription returned for these Azure credentials, be sure the registered application has ' +
'enough permissions. Go to https://www.cloudcarbonfootprint.org/docs/azure/ for more information.',
)
}

return subscriptions
}

private async getSubscriptionsByIds(
subscriptionIds: string[],
private async getSubscriptions(
subscriptionIds: string[] = [],
): Promise<Subscription[]> {
const subscriptions = []
const AZURE = configLoader().AZURE
const defaultAzureSubscriptionIds = subscriptionIds.length
? subscriptionIds
: AZURE.SUBSCRIPTIONS

for (const subscriptionId of subscriptionIds) {
try {
const subscription = await this.subscriptionClient.subscriptions.get(
subscriptionId,
)
const getSubscriptions = async (): Promise<Subscription[]> => {
const subscriptions = []
for await (const subscription of this.subscriptionClient.subscriptions.list()) {
subscriptions.push(subscription)
} catch (error) {
}

if (subscriptions.length === 0) {
this.logger.warn(
`Unable to fetch subscription details for: "${subscriptionId}". Reason: ${error.message}`,
'No subscription returned for these Azure credentials, be sure the registered application has ' +
'enough permissions. Go to https://www.cloudcarbonfootprint.org/docs/azure/ for more information.',
)
}

return subscriptions
}

const getSubscriptionsByIds = async (
subscriptionIds: string[],
): Promise<Subscription[]> => {
const subscriptions = []

for (const subscriptionId of subscriptionIds) {
try {
const subscription = await this.subscriptionClient.subscriptions.get(
subscriptionId,
)
subscriptions.push(subscription)
} catch (error) {
this.logger.warn(
`Unable to fetch subscription details for: "${subscriptionId}". Reason: ${error.message}`,
)
}
}

return subscriptions
}

const subscriptions = defaultAzureSubscriptionIds?.length
? await getSubscriptionsByIds(defaultAzureSubscriptionIds)
: await getSubscriptions()

return subscriptions
}

Expand Down

0 comments on commit 49948b5

Please sign in to comment.