Sqlx currency code fix (#1149)

* using currency code from fetch prices api

* formatting & linting fixes

* Update SqlX.rp.ts
This commit is contained in:
siddjoshi-ms 2021-11-08 10:34:22 -08:00 committed by GitHub
parent a3d88af175
commit 2dfabf3c69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 12 deletions

View File

@ -6,6 +6,7 @@ import { RefreshResult } from "../SelfServeTypes";
import SqlX from "./SqlX"; import SqlX from "./SqlX";
import { import {
FetchPricesResponse, FetchPricesResponse,
PriceMapAndCurrencyCode,
RegionsResponse, RegionsResponse,
SqlxServiceResource, SqlxServiceResource,
UpdateDedicatedGatewayRequestParameters, UpdateDedicatedGatewayRequestParameters,
@ -178,18 +179,18 @@ const getFetchPricesPathForRegion = (subscriptionId: string): string => {
return `/subscriptions/${subscriptionId}/providers/Microsoft.CostManagement/fetchPrices`; return `/subscriptions/${subscriptionId}/providers/Microsoft.CostManagement/fetchPrices`;
}; };
export const getPriceMap = async (regions: Array<string>): Promise<Map<string, Map<string, number>>> => { export const getPriceMapAndCurrencyCode = async (regions: Array<string>): Promise<PriceMapAndCurrencyCode> => {
const telemetryData = { const telemetryData = {
feature: "Calculate approximate cost", feature: "Calculate approximate cost",
function: "getPriceMap", function: "getPriceMapAndCurrencyCode",
description: "fetch prices API call", description: "fetch prices API call",
selfServeClassName: SqlX.name, selfServeClassName: SqlX.name,
}; };
const getPriceMapTimestamp = selfServeTraceStart(telemetryData); const getPriceMapAndCurrencyCodeTimestamp = selfServeTraceStart(telemetryData);
try { try {
const priceMap = new Map<string, Map<string, number>>(); const priceMap = new Map<string, Map<string, number>>();
let currencyCode;
for (const region of regions) { for (const region of regions) {
const regionPriceMap = new Map<string, number>(); const regionPriceMap = new Map<string, number>();
@ -207,17 +208,21 @@ export const getPriceMap = async (regions: Array<string>): Promise<Map<string, M
}); });
for (const item of response.result.Items) { for (const item of response.result.Items) {
if (currencyCode === undefined) {
currencyCode = item.currencyCode;
} else if (item.currencyCode !== currencyCode) {
throw Error("Currency Code Mismatch: Currency code not same for all regions / skus.");
}
regionPriceMap.set(item.skuName, item.retailPrice); regionPriceMap.set(item.skuName, item.retailPrice);
} }
priceMap.set(region, regionPriceMap); priceMap.set(region, regionPriceMap);
} }
selfServeTraceSuccess(telemetryData, getPriceMapTimestamp); selfServeTraceSuccess(telemetryData, getPriceMapAndCurrencyCodeTimestamp);
return priceMap; return { priceMap: priceMap, currencyCode: currencyCode };
} catch (err) { } catch (err) {
const failureTelemetry = { err, selfServeClassName: SqlX.name }; const failureTelemetry = { err, selfServeClassName: SqlX.name };
selfServeTraceFailure(failureTelemetry, getPriceMapTimestamp); selfServeTraceFailure(failureTelemetry, getPriceMapAndCurrencyCodeTimestamp);
return { priceMap: undefined, currencyCode: undefined };
return undefined;
} }
}; };

View File

@ -21,7 +21,7 @@ import { BladeType, generateBladeLink } from "../SelfServeUtils";
import { import {
deleteDedicatedGatewayResource, deleteDedicatedGatewayResource,
getCurrentProvisioningState, getCurrentProvisioningState,
getPriceMap, getPriceMapAndCurrencyCode,
getRegions, getRegions,
refreshDedicatedGatewayProvisioning, refreshDedicatedGatewayProvisioning,
updateDedicatedGatewayResource, updateDedicatedGatewayResource,
@ -207,6 +207,7 @@ const ApproximateCostDropDownInfo: Info = {
}; };
let priceMap: Map<string, Map<string, number>>; let priceMap: Map<string, Map<string, number>>;
let currencyCode: string;
let regions: Array<string>; let regions: Array<string>;
const calculateCost = (skuName: string, instanceCount: number): Description => { const calculateCost = (skuName: string, instanceCount: number): Description => {
@ -237,7 +238,7 @@ const calculateCost = (skuName: string, instanceCount: number): Description => {
selfServeTraceSuccess(telemetryData, calculateCostTimestamp); selfServeTraceSuccess(telemetryData, calculateCostTimestamp);
return { return {
textTKey: `${costPerHour} USD`, textTKey: `${costPerHour} ${currencyCode}`,
type: DescriptionType.Text, type: DescriptionType.Text,
}; };
} catch (err) { } catch (err) {
@ -346,7 +347,9 @@ export default class SqlX extends SelfServeBaseClass {
}); });
regions = await getRegions(); regions = await getRegions();
priceMap = await getPriceMap(regions); const priceMapAndCurrencyCode = await getPriceMapAndCurrencyCode(regions);
priceMap = priceMapAndCurrencyCode.priceMap;
currencyCode = priceMapAndCurrencyCode.currencyCode;
const response = await getCurrentProvisioningState(); const response = await getCurrentProvisioningState();
if (response.status && response.status !== "Deleting") { if (response.status && response.status !== "Deleting") {

View File

@ -36,9 +36,15 @@ export type FetchPricesResponse = {
Count: number; Count: number;
}; };
export type PriceMapAndCurrencyCode = {
priceMap: Map<string, Map<string, number>>;
currencyCode: string;
};
export type PriceItem = { export type PriceItem = {
retailPrice: number; retailPrice: number;
skuName: string; skuName: string;
currencyCode: string;
}; };
export type RegionsResponse = { export type RegionsResponse = {