Skip to content

Commit

Permalink
[COST-1881] - add cost-type to settings page ("DEVELOPMENT" mode only) (
Browse files Browse the repository at this point in the history
  • Loading branch information
ddonahue007 authored Oct 14, 2021
1 parent b9f8cd7 commit 2e13e75
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 14 deletions.
72 changes: 59 additions & 13 deletions koku/api/settings/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
from api.settings.utils import create_select
from api.settings.utils import create_subform
from api.settings.utils import generate_doc_link
from api.settings.utils import get_cost_type_options
from api.settings.utils import get_currency_options
from api.settings.utils import get_selected_cost_type_or_setup
from api.settings.utils import get_selected_currency_or_setup
from api.settings.utils import set_cost_type
from api.settings.utils import set_currency
from api.settings.utils import SETTINGS_PREFIX
from api.tags.aws.queries import AWSTagQueryHandler
Expand Down Expand Up @@ -145,6 +148,7 @@ def _build_components(self):
)
tag_key_text = create_plain_text_with_doc(tag_key_text_name, tag_key_text_context, doc_link)

sub_form_fields = []
avail_objs = []
enabled_objs = []
for providerName in obtainTagKeysProvidersParams:
Expand Down Expand Up @@ -176,31 +180,45 @@ def _build_components(self):
dual_list_name = f'{"api.settings.tag-management.enabled"}'
tags_and_labels = create_dual_list_select(dual_list_name, **dual_list_options)

for field in enable_tags_title, tag_key_text, tags_and_labels:
sub_form_fields.append(field)

# currency settings TODO: only show in dev mode right now
if settings.DEVELOPMENT:
currency_select_name = f'{"api.settings.currency"}'
currency_text_context = "Select the preferred currency view for your organization."
currency_title = create_plain_text(currency_select_name, "Currency", "h2")
currency_select_text = create_plain_text(currency_select_name, currency_text_context, "h4")
currency_select_text = create_plain_text(currency_select_name, currency_text_context, "p")
currency_options = {
"label": "Currency",
"options": get_currency_options(),
"initialValue": get_selected_currency_or_setup(self.schema),
"FormGroupProps": {"style": {"width": "400px"}},
}
currency = create_select(currency_select_name, **currency_options)

sub_form_fields = [
currency_title,
currency_select_text,
currency,
enable_tags_title,
tag_key_text,
tags_and_labels,
]
idx = 0
for field in currency_title, currency_select_text, currency:
sub_form_fields.insert(idx, field)
idx += 1

else:
sub_form_fields = [enable_tags_title, tag_key_text, tags_and_labels]
# cost_type plan settings TODO: only show in dev mode right now
if settings.DEVELOPMENT:
cost_type_select_name = f'{"api.settings.cost_type"}'
cost_type_text_context = (
"Select the preferred way of calculating upfront costs, either through savings "
"plans or subscription fees. This feature is available for Amazon Web Services cost only."
)
cost_type_title = create_plain_text(cost_type_select_name, "Show cost as (Amazon Web Services Only)", "h2")
cost_type_select_text = create_plain_text(cost_type_select_name, cost_type_text_context, "p")
cost_type_options = {
"options": get_cost_type_options(),
"initialValue": get_selected_cost_type_or_setup(self.schema),
"FormGroupProps": {"style": {"width": "400px"}},
}
cost_type = create_select(cost_type_select_name, **cost_type_options)

for field in cost_type_title, cost_type_select_text, cost_type:
sub_form_fields.append(field)

sub_form_name = f"{SETTINGS_PREFIX}.settings.subform"
sub_form_title = ""
Expand Down Expand Up @@ -294,6 +312,31 @@ def _currency_handler(self, settings):
invalidate_view_cache_for_tenant_and_source_type(self.schema, Provider.PROVIDER_OCP)
return True

def _cost_type_handler(self, settings):
if settings is None:
return False
else:
cost_type = settings

try:
stored_cost_type = get_selected_cost_type_or_setup(self.schema)
except Exception as exp:
LOG.warning(f"Failed to retrieve cost_type for schema {self.schema}. Reason: {exp}")
return False

if stored_cost_type == cost_type:
return False

try:
LOG.info(f"Updating cost_type to: " + settings)
set_cost_type(self.schema, settings)
except Exception as exp:
LOG.warning(f"Failed to store new cost_type settings for schema {self.schema}. Reason: {exp}")
return False

invalidate_view_cache_for_tenant_and_source_type(self.schema, Provider.PROVIDER_OCP)
return True

def build_settings(self):
"""
Generate tag management settings
Expand All @@ -317,10 +360,13 @@ def handle_settings(self, settings):
currency_settings = settings.get("api", {}).get("settings", {}).get("currency", None)
currency_change = self._currency_handler(currency_settings)

cost_type_settings = settings.get("api", {}).get("settings", {}).get("cost_type", None)
cost_type_change = self._cost_type_handler(cost_type_settings)

tg_mgmt_settings = settings.get("api", {}).get("settings", {}).get("tag-management", {})
tags_change = self._tag_key_handler(tg_mgmt_settings)

if tags_change or currency_change:
if tags_change or currency_change or cost_type_change:
return True

return False
6 changes: 5 additions & 1 deletion koku/api/settings/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,11 @@ def get_cost_type_options():
(dict) - options.
"""
return [
dict(value=cost_type.get("code"), label=f"{cost_type.get('name')} ({cost_type.get('description')})")
dict(
value=cost_type.get("code"),
label=f"{cost_type.get('name')}",
description=f"{cost_type.get('description')}",
)
for cost_type in COST_TYPES
]

Expand Down

0 comments on commit 2e13e75

Please sign in to comment.