Skip to content

Commit

Permalink
FOPTS-4349 Display recommendation in the local currency for rightsize…
Browse files Browse the repository at this point in the history
…_managed_disks (#2424)

* adding currency

* Update azure_rightsize_managed_disks.pt

Adding currency conversion datasources

* Update azure_rightsize_managed_disks.pt

* Update azure_rightsize_managed_disks.pt

* Update azure_rightsize_managed_disks.pt

Fixing currency conversion

* Update azure_rightsize_managed_disks.pt

* SQ-9018 Remove currencyConvertor

* Update azure_rightsize_managed_disks.pt

* adding currency support and update version

* Updates

Fixing comments
Better change log

---------

Co-authored-by: Davinder Singh <[email protected]>
Co-authored-by: midir99 <[email protected]>
  • Loading branch information
3 people authored Jul 19, 2024
1 parent 5d8a9ed commit ae88232
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 14 deletions.
4 changes: 4 additions & 0 deletions cost/azure/rightsize_managed_disks/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v2.3.0

- Implemented automatic currency detection and conversion, ensuring recommendations are displayed in the currency configured in the user's settings.

## v2.2.0

- Added a parameter to make recommendations or not for changing a disk tier to Standard HDD.
Expand Down
128 changes: 119 additions & 9 deletions cost/azure/rightsize_managed_disks/azure_rightsize_managed_disks.pt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ category "Cost"
severity "low"
default_frequency "monthly"
info(
version: "2.2.0",
version: "2.3.0",
provider: "Azure",
service: "Managed Disks",
policy_set: "Rightsize Storage",
Expand Down Expand Up @@ -720,12 +720,120 @@ datasource "ds_azure_md_pricing" do
end
end

# Gather local currency info
datasource "ds_currency_reference" do
request do
host "raw.githubusercontent.com"
path "/flexera-public/policy_templates/master/data/currency/currency_reference.json"
header "User-Agent", "RS Policies"
end
end

datasource "ds_currency_code" do
request do
auth $auth_flexera
host rs_optima_host
path join(["/bill-analysis/orgs/", rs_org_id, "/settings/currency_code"])
header "Api-Version", "0.1"
header "User-Agent", "RS Policies"
ignore_status [403]
end
result do
encoding "json"
field "id", jmes_path(response, "id")
field "value", jmes_path(response, "value")
end
end

datasource "ds_currency_target" do
run_script $js_currency_target, $ds_currency_reference, $ds_currency_code
end

script "js_currency_target", type:"javascript" do
parameters "ds_currency_reference", "ds_currency_code"
result "result"
code <<-EOS
// Default to USD if currency is not found
result = ds_currency_reference['USD']
if (ds_currency_code['value'] != undefined && ds_currency_reference[ds_currency_code['value']] != undefined) {
result = ds_currency_reference[ds_currency_code['value']]
}
EOS
end

# Branching logic:
# This datasource returns an empty array if the target currency is USD.
# This prevents ds_currency_conversion from running if it's not needed.
datasource "ds_conditional_currency_conversion" do
run_script $js_conditional_currency_conversion, $ds_currency_target
end

script "js_conditional_currency_conversion", type: "javascript" do
parameters "ds_currency_target"
result "result"
code <<-EOS
result = []
// Make the request only if the target currency is not USD
if (ds_currency_target['code'] != 'USD') {
result = [1]
}
EOS
end

datasource "ds_currency_conversion" do
# Only make a request if the target currency is not USD
iterate $ds_conditional_currency_conversion
request do
host "api.xe-auth.flexeraeng.com"
path "/prod/{proxy+}"
query "from", "USD"
query "to", val($ds_currency_target, 'code')
query "amount", "1"
# Ignore currency conversion if API has issues
ignore_status [400, 404, 502]
end
result do
encoding "json"
field "from", jmes_path(response, "from")
field "to", jmes_path(response, "to")
field "amount", jmes_path(response, "amount")
field "year", jmes_path(response, "year")
end
end

datasource "ds_currency" do
run_script $js_currency, $ds_currency_target, $ds_currency_conversion
end

script "js_currency", type:"javascript" do
parameters "ds_currency_target", "ds_currency_conversion"
result "result"
code <<-EOS
result = ds_currency_target
result['exchange_rate'] = 1
if (ds_currency_conversion.length > 0) {
currency_code = ds_currency_target['code']
current_month = parseInt(new Date().toISOString().split('-')[1])
conversion_block = _.find(ds_currency_conversion[0]['to'][currency_code], function(item) {
return item['month'] == current_month
})
if (conversion_block != undefined) {
result['exchange_rate'] = conversion_block['monthlyAverage']
}
}
EOS
end

datasource "ds_disk_rightsizing_recommendations" do
run_script $js_disk_rightsizing_recommendations, $ds_azure_disks_with_metrics_thresholds_filtered, $ds_azure_md_tier_types, $ds_azure_md_pricing, $ds_applied_policy, $ds_min_used_disk_space_pct, $param_stats_lookback, $param_min_savings, $param_min_used_disk_iops_pct, $param_min_used_disk_throughput_pct, $param_stats_aggregation_for_disk_iops, $param_stats_aggregation_for_disk_throughput, $param_recommend_hdd_tier
run_script $js_disk_rightsizing_recommendations, $ds_currency, $ds_azure_disks_with_metrics_thresholds_filtered, $ds_azure_md_tier_types, $ds_azure_md_pricing, $ds_applied_policy, $ds_min_used_disk_space_pct, $param_stats_lookback, $param_min_savings, $param_min_used_disk_iops_pct, $param_min_used_disk_throughput_pct, $param_stats_aggregation_for_disk_iops, $param_stats_aggregation_for_disk_throughput, $param_recommend_hdd_tier
end

script "js_disk_rightsizing_recommendations", type: "javascript" do
parameters "ds_azure_disks_with_metrics_thresholds_filtered", "ds_azure_md_tier_types", "ds_azure_md_pricing", "ds_applied_policy", "ds_min_used_disk_space_pct", "param_stats_lookback", "param_min_savings", "param_min_used_disk_iops_pct", "param_min_used_disk_throughput_pct", "param_stats_aggregation_for_disk_iops", "param_stats_aggregation_for_disk_throughput", "param_recommend_hdd_tier"
parameters "ds_currency", "ds_azure_disks_with_metrics_thresholds_filtered", "ds_azure_md_tier_types", "ds_azure_md_pricing", "ds_applied_policy", "ds_min_used_disk_space_pct", "param_stats_lookback", "param_min_savings", "param_min_used_disk_iops_pct", "param_min_used_disk_throughput_pct", "param_stats_aggregation_for_disk_iops", "param_stats_aggregation_for_disk_throughput", "param_recommend_hdd_tier"
result "result"
code <<-'EOS'
var hddDiskSizes = [32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32767]
Expand Down Expand Up @@ -965,19 +1073,19 @@ script "js_disk_rightsizing_recommendations", type: "javascript" do
resourceTypeGiB: currentDiskData.sizeGiB,
resourceTypeIOPS: currentDiskData.IOPS,
resourceTypeThroughput: currentDiskData.throughput,
resourceTypeMonthlyPrice: currentDiskPrice.pricePerUnit,
resourceTypePriceCurrency: currentDiskPrice.currencyCode,
resourceTypeMonthlyPrice: currentDiskPrice.pricePerUnit * ds_currency.exchange_rate,
resourceTypePriceCurrency: ds_currency.code,
newResourceType: newResourceType,
newResourceTypeRedundancy: newResourceTypeRedundancy,
newResourceTypeGiB: newResourceTypeGiB,
newResourceTypeIOPS: newResourceTypeIOPS,
newResourceTypeThroughput: newResourceTypeThroughput,
newResourceTypeMonthlyPrice: newResourceTypeMonthlyPrice,
newResourceTypePriceCurrency: newResourceTypePriceCurrency,
newResourceTypeMonthlyPrice: newResourceTypeMonthlyPrice * ds_currency.exchange_rate,
newResourceTypePriceCurrency: ds_currency.code,
resourceKind: disk.resourceType,
region: disk.region,
monthlySavings: monthlySavings.toFixed(2),
savingsCurrency: currentDiskPrice.currencyCode,
monthlySavings: monthlySavings.toFixed(2) * ds_currency.exchange_rate,
savingsCurrency: ds_currency.code,
iopsPctMaximum: Math.round(disk.iopsConsumedPctMax),
iopsPctAverage: Math.round(disk.iopsConsumedPctAvg),
iopsThreshold: param_min_used_disk_iops_pct,
Expand Down Expand Up @@ -1010,7 +1118,9 @@ script "js_disk_rightsizing_recommendations", type: "javascript" do
recommendations[0].message += "- This policy was applied with **Minimum used disk IOPS percentage** and **Minimum used disk throughput percentage** set to -1. As a result, any disk with any activity or throughput will be considered oversized.\n"
}
recommendations[0].message += "\nThe above settings can be modified by editing the applied policy and changing the appropriate parameters."
recommendations[0].totalSavings = totalMonthlySavings.toFixed(2)
recommendations[0].policyName = ds_applied_policy.name
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ category "Meta"
default_frequency "15 minutes"
info(
provider: "Azure",
version: "2.2.0", # This version of the Meta Parent Policy Template should match the version of the Child Policy Template as it appears in the Catalog for best reliability
version: "2.3.0", # This version of the Meta Parent Policy Template should match the version of the Child Policy Template as it appears in the Catalog for best reliability
)

##############################################################################
Expand Down Expand Up @@ -911,9 +911,6 @@ script "js_ds_disk_rightsizing_recommendations_combined_incidents", type: "javas
EOS
end




# Summary and a conditional incident which will show up if any policy is being applied, updated or deleted.
# Minimum of 1 incident, max of four
# Could swap the summary to only showing running
Expand All @@ -924,7 +921,7 @@ policy "policy_scheduled_report" do
validate $ds_disk_rightsizing_recommendations_combined_incidents do
summary_template "Consolidated Incident: {{ len data }} Azure Oversized Managed Disks Found"
escalate $esc_email

check eq(size(data), 0)
export do
resource_level true
Expand Down

0 comments on commit ae88232

Please sign in to comment.