Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
XOmniverse committed Oct 11, 2024
1 parent 0416ccb commit 9a23a76
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 35 deletions.
4 changes: 4 additions & 0 deletions cost/flexera/cco/currency_conversion/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v5.0.0

- Multiple dimension/value pairs can now be used when applying currency conversion

## v4.0.0

- Policy now accepts any arbitrary cost dimension/value instead of requiring that currency conversion be done for a specific cloud provider
Expand Down
5 changes: 3 additions & 2 deletions cost/flexera/cco/currency_conversion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## What It Does

This policy template creates adjustment rules that convert the currency of the costs associated with the cost dimension of choice. It utilizes xe.com to retrieve the latest exchange rates.
This policy template creates adjustment rules that convert the currency of the costs associated with the cost dimensions of choice. It utilizes xe.com to retrieve the latest exchange rates.

## How It Works

Expand All @@ -20,7 +20,8 @@ This policy has the following input parameters required when launching the polic
- *Backfill Adjustments* - Whether to add/modify currency conversion to just the current month or to backfill previous months.
- *Backfill Start Date* - The month and year in YYYY-MM format to backfill adjustments to. Only applicable if `Backfill Previous Months` is selected for the `Backfill Adjustments` parameter.
- *Backfill Exchange Rates* - Whether or not to use the current exchange rate, or the exchange rate at the time, when applying currency conversion to previous months. Only applicable if `Backfill Previous Months` is selected for the `Backfill Adjustments` parameter.
- *Dimension* - The Flexera CCO cost dimension name/id and value to apply the currency conversion to in 'Dimension=Value' format. Example: Cloud Vendor=AWS
- *Dimensions* - The Flexera CCO cost dimension names/ids and values to apply the currency conversion to in 'Dimension=Value' format. Example: Cloud Vendor=AWS
- *Dimensions Boolean* - Whether to apply the currency conversion to costs that match any of the criteria in the `Dimensions` parameter or only those that match all of them. Only applicable if more than one value is specified for the `Dimensions` parameter.
- *Currency From* - Currency you want to convert from (based on ISO 4217 codes - e.g., 'USD' for US Dollar)
- *Currency To* - Currency you want to convert to (based on ISO 4217 codes - e.g., 'EUR' for Euro)
- *Set Organization Currency* - Whether or not to configure the Flexera CCO user interface to present costs in the currency specified in the `Currency To` parameter. If set to `Yes`, this will be done, and all costs in Flexera CCO for this organization will be presented in the currency costs are converted to.
Expand Down
105 changes: 74 additions & 31 deletions cost/flexera/cco/currency_conversion/currency_conversion.pt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ severity "low"
default_frequency "monthly"
category "Cost"
info(
version: "4.0.0",
version: "5.0.0",
provider: "Flexera",
service: "Cloud Cost Optimization",
policy_set: "Cloud Cost Optimization"
Expand All @@ -17,16 +17,25 @@ info(
# Parameters
###############################################################################

parameter "param_dimension" do
type "string"
parameter "param_dimensions" do
type "list"
category "Policy Settings"
label "Dimension"
description "The Flexera CCO cost dimension name/id and value to apply the currency conversion to in 'Dimension=Value' format. Example: Cloud Vendor=AWS"
label "Dimensions"
description "The Flexera CCO cost dimension names/ids and values to apply the currency conversion to in 'Dimension=Value' format. Example: Cloud Vendor=AWS"
allowed_pattern /.+=.+/
min_length 1
# No default value, user input required
end

parameter "param_dimensions_boolean" do
type "string"
category "Policy Settings"
label "Dimensions Boolean"
description "Whether to apply the currency conversion to costs that match any of the criteria in the 'Dimensions' parameter or only those that match all of them. Only applicable if more than one value is specified for the 'Dimensions' parameter."
allowed_values "Any", "All"
default "All"
end

parameter "param_backfill" do
type "string"
category "Policy Settings"
Expand Down Expand Up @@ -112,31 +121,35 @@ datasource "ds_dimensions" do
end
end

datasource "ds_dimension" do
run_script $js_dimension, $ds_dimensions, $param_dimension
datasource "ds_filter_dimensions" do
run_script $js_filter_dimensions, $ds_dimensions, $param_dimensions
end

script "js_dimension", type: "javascript" do
parameters "ds_dimensions", "param_dimension"
script "js_filter_dimensions", type: "javascript" do
parameters "ds_dimensions", "param_dimensions"
result "result"
code <<-'EOS'
result = {}
result = _.map(param_dimensions, function(item) {
key = item.split('=')[0]
value = item.split('=')[1]
key = param_dimension.split('=')[0]
value = param_dimension.split('=')[1]
dimension = _.find(ds_dimensions, function(dimension) {
return dimension['id'] == key || dimension['name'] == key
})
dimension = _.find(ds_dimensions, function(dimension) {
return dimension['id'] == key || dimension['name'] == key
if (dimension) {
return {
id: dimension['id'],
name: dimension['name'],
type: dimension['type'],
value: value
}
} else {
return null
}
})
if (dimension) {
result = {
id: dimension['id'],
name: dimension['name'],
type: dimension['type'],
value: value
}
}
result = _.compact(result)
EOS
end

Expand Down Expand Up @@ -302,11 +315,11 @@ datasource "ds_current_adjustments" do
end

datasource "ds_updated_adjustments" do
run_script $js_updated_adjustments, $ds_exchange_rates, $ds_current_adjustments, $ds_month_list, $ds_dimension, $param_currency_from, $param_currency_to, $param_backfill_exchange_rate, $param_backfill, $param_backfill_start_date
run_script $js_updated_adjustments, $ds_exchange_rates, $ds_current_adjustments, $ds_month_list, $ds_filter_dimensions, $param_currency_from, $param_currency_to, $param_backfill_exchange_rate, $param_backfill, $param_backfill_start_date, $param_dimensions_boolean
end

script "js_updated_adjustments", type: "javascript" do
parameters "ds_exchange_rates", "ds_current_adjustments", "ds_month_list", "ds_dimension", "param_currency_from", "param_currency_to", "param_backfill_exchange_rate", "param_backfill", "param_backfill_start_date"
parameters "ds_exchange_rates", "ds_current_adjustments", "ds_month_list", "ds_filter_dimensions", "param_currency_from", "param_currency_to", "param_backfill_exchange_rate", "param_backfill", "param_backfill_start_date", "param_dimensions_boolean"
result "result"
code <<-'EOS'
// Get most recent exchange rate
Expand All @@ -325,7 +338,14 @@ script "js_updated_adjustments", type: "javascript" do
})
// Dimension details
pretty_dimension = ds_dimension['name'] + '=' + ds_dimension['value']
if (ds_filter_dimensions.length > 1) {
pretty_dimension = param_dimensions_boolean + ": " + _.map(ds_filter_dimensions, function(item) {
return item['name'] + '=' + item['value']
}).join(" | ")
} else {
pretty_dimension = ds_filter_dimensions[0]['name'] + '=' + ds_filter_dimensions[0]['value']
}
new_adj_name = "Currency Conversion - " + pretty_dimension
// Rule information stored for incident
Expand All @@ -341,11 +361,33 @@ script "js_updated_adjustments", type: "javascript" do
}
// Create the currency conversion rule
if (ds_filter_dimensions.length > 1) {
boolean_type = "or"
if (param_dimensions_boolean == "All") { boolean_type = "and" }
condition = {
type: boolean_type,
expressions: _.map(ds_filter_dimensions, function(item) {
return {
type: "dimension_equals",
dimension: item['id'],
value: item['value']
}
})
}
} else {
condition = {
type: "dimension_equals",
dimension: ds_filter_dimensions[0]['id'],
value: ds_filter_dimensions[0]['value']
}
}
currency_conversion_rule = {
name: new_adj_name,
rules: [
{
condition: { type: "dimension_equals", dimension: ds_dimension['id'], value: ds_dimension['value'] },
condition: condition,
cost_multiplier: conversion_rate - 1,
label: param_currency_from + " to " + param_currency_to
}
Expand Down Expand Up @@ -402,24 +444,25 @@ script "js_updated_adjustments", type: "javascript" do
result = {
list: { dated_adjustment_lists: new_adjustments },
message: message + message_rules.join("\n")
message: message + message_rules.join("\n"),
dimension: pretty_dimension
}
EOS
end

# Include ds_set_org_currency to ensure it executes, not because it's used by this script
datasource "ds_updated_adjustments_payload" do
run_script $js_updated_adjustments_payload, $ds_updated_adjustments, $ds_set_org_currency, $ds_dimension
run_script $js_updated_adjustments_payload, $ds_updated_adjustments, $ds_set_org_currency
end

script "js_updated_adjustments_payload", type: "javascript" do
parameters "ds_updated_adjustments", "ds_set_org_currency", "ds_dimension"
parameters "ds_updated_adjustments", "ds_set_org_currency"
result "result"
code <<-'EOS'
result = [{
list: JSON.stringify(ds_updated_adjustments['list']),
message: ds_updated_adjustments['message'],
dimension: ds_dimension['name'] + '=' + ds_dimension['value']
dimension: ds_updated_adjustments['dimension']
}]
EOS
end
Expand Down Expand Up @@ -449,7 +492,7 @@ end

policy "pol_currency_conversion" do
validate_each $ds_new_adjustments do
summary_template "Currency Conversion - {{ with index data 0 }}{{ .dimension }}{{ end }} - {{ parameters.param_currency_from }} to {{ parameters.param_currency_to }}"
summary_template "Currency Conversion - {{ parameters.param_currency_from }} to {{ parameters.param_currency_to }} - {{ with index data 0 }}{{ .dimension }}{{ end }}"
detail_template "{{ with index data 0 }}{{ .message }}{{ end }}"
check eq(0, 1)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4700,7 +4700,7 @@
{
"id": "./cost/flexera/cco/currency_conversion/currency_conversion.pt",
"name": "Currency Conversion",
"version": "4.0.0",
"version": "5.0.0",
"providers": [
{
"name": "flexera",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2718,7 +2718,7 @@
required: true
- id: "./cost/flexera/cco/currency_conversion/currency_conversion.pt"
name: Currency Conversion
version: 4.0.0
version: 5.0.0
:providers:
- :name: flexera
:permissions:
Expand Down

0 comments on commit 9a23a76

Please sign in to comment.