Skip to content

Add 'subscription id' scope at MANUAL TYPE secret #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def init_response(options: dict, domain_id: str) -> dict:
"data_source_rules": _DEFAULT_DATA_SOURCE_RULES,
"supported_secret_types": ["MANUAL"],
"currency": "KRW",
"collect_resource_id": True,
"use_account_routing": False,
"exclude_license_cost": False,
"include_credit_cost": False,
Expand Down
101 changes: 73 additions & 28 deletions src/cloudforet/cost_analysis/manager/job_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from spaceone.core.manager import BaseManager
from cloudforet.cost_analysis.conf.cost_conf import SECRET_TYPE_DEFAULT
from cloudforet.cost_analysis.connector import AzureCostMgmtConnector
from cloudforet.cost_analysis.error.cost import *

_LOGGER = logging.getLogger("spaceone")
Expand All @@ -17,7 +18,7 @@
class JobManager(BaseManager):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.azure_cm_connector = self.locator.get_connector("AzureCostMgmtConnector")
self.azure_cm_connector: AzureCostMgmtConnector = AzureCostMgmtConnector()

def get_tasks(
self,
Expand All @@ -33,14 +34,14 @@ def get_tasks(

self.azure_cm_connector.create_session(options, secret_data, schema)
secret_type = options.get("secret_type", SECRET_TYPE_DEFAULT)
billing_account_agreement_type = ""

if secret_type == "MANUAL":
billing_account_info = self.azure_cm_connector.get_billing_account()
billing_account_agreement_type = (
self.azure_cm_connector.get_agreement_type_from_billing_account_info(
if not options.get("collect_scope") == "subscription_id":
billing_account_info = self.azure_cm_connector.get_billing_account()
billing_account_agreement_type = self.azure_cm_connector.get_agreement_type_from_billing_account_info(
billing_account_info
)
)

if billing_account_agreement_type == "MicrosoftPartnerAgreement":
tasks = []
Expand All @@ -67,17 +68,15 @@ def get_tasks(
start + relativedelta(months=month + month_range_step - 1),
"%Y-%m",
)
tasks.append(
{
"task_options": {
"start": task_start_month,
"end": task_end_month,
"account_agreement_type": billing_account_agreement_type,
"collect_scope": "billing_account_id",
"billing_tenant_id": secret_data["tenant_id"],
}
}
task = self._get_task_scope_billing_account_id(
secret_data,
billing_account_agreement_type,
options,
task_start_month,
task_end_month,
)
tasks.append(task)

if linked_accounts:
synced_accounts = linked_accounts
changed.append({"start": start_month})
Expand Down Expand Up @@ -137,7 +136,7 @@ def get_tasks(
synced_accounts = self._extend_synced_accounts(
synced_accounts, first_sync_tenants
)
else:
elif billing_account_agreement_type == "EnterpriseAgreement":
tasks = [
{
"task_options": {
Expand All @@ -153,6 +152,22 @@ def get_tasks(
]
changed = [{"start": start_month}]
synced_accounts = []
elif billing_account_agreement_type == "MicrosoftCustomerAgreement":
task = self._get_task_scope_billing_account_id(
secret_data, billing_account_agreement_type, options, start_month
)

tasks = [task]
changed = [{"start": start_month}]
synced_accounts = []
else:
task = self._get_task_scope_subscription(
secret_data, options, start_month
)

tasks = [task]
changed = [{"start": start_month}]
synced_accounts = []

# Benefit Job Task
if options.get("cost_metric") == "AmortizedCost":
Expand All @@ -169,18 +184,9 @@ def get_tasks(
)

elif secret_type == "USE_SERVICE_ACCOUNT_SECRET":
subscription_id = secret_data.get("subscription_id", "")
tenant_id = secret_data.get("tenant_id")
tasks = [
{
"task_options": {
"collect_scope": "subscription_id",
"start": start_month,
"subscription_id": subscription_id,
"billing_tenant_id": tenant_id,
}
}
]
task = self._get_task_scope_subscription(secret_data, options, start_month)

tasks = [task]
changed = [{"start": start_month}]
synced_accounts = []

Expand All @@ -190,6 +196,45 @@ def get_tasks(
tasks = {"tasks": tasks, "changed": changed, "synced_accounts": synced_accounts}
return tasks

@staticmethod
def _get_task_scope_billing_account_id(
secret_data: dict,
billing_account_agreement_type: str,
options: dict,
start_month: str,
end_month: str = None,
) -> dict:
task_options = {
"start": start_month,
"account_agreement_type": billing_account_agreement_type,
"collect_scope": "billing_account_id",
"billing_tenant_id": secret_data["tenant_id"],
}

if end_month:
task_options["end"] = end_month

task = {"task_options": task_options}

return task

@staticmethod
def _get_task_scope_subscription(
secret_data: dict, options: dict, start_month: str
) -> dict:
subscription_id = secret_data["subscription_id"]
tenant_id = secret_data.get("tenant_id")
task = {
"task_options": {
"collect_scope": "subscription_id",
"start": start_month,
"subscription_id": subscription_id,
"tenant_id": tenant_id,
}
}

return task

def _get_tenants_from_billing_account(self):
tenants = []
for (
Expand Down
Loading