77
88from spaceone .core .manager import BaseManager
99from cloudforet .cost_analysis .conf .cost_conf import SECRET_TYPE_DEFAULT
10+ from cloudforet .cost_analysis .connector import AzureCostMgmtConnector
1011from cloudforet .cost_analysis .error .cost import *
1112
1213_LOGGER = logging .getLogger ("spaceone" )
1718class JobManager (BaseManager ):
1819 def __init__ (self , * args , ** kwargs ):
1920 super ().__init__ (* args , ** kwargs )
20- self .azure_cm_connector = self . locator . get_connector ( " AzureCostMgmtConnector" )
21+ self .azure_cm_connector : AzureCostMgmtConnector = AzureCostMgmtConnector ( )
2122
2223 def get_tasks (
2324 self ,
@@ -33,14 +34,14 @@ def get_tasks(
3334
3435 self .azure_cm_connector .create_session (options , secret_data , schema )
3536 secret_type = options .get ("secret_type" , SECRET_TYPE_DEFAULT )
37+ billing_account_agreement_type = ""
3638
3739 if secret_type == "MANUAL" :
38- billing_account_info = self . azure_cm_connector . get_billing_account ()
39- billing_account_agreement_type = (
40- self .azure_cm_connector .get_agreement_type_from_billing_account_info (
40+ if not options . get ( "collect_scope" ) == "subscription_id" :
41+ billing_account_info = self . azure_cm_connector . get_billing_account ()
42+ billing_account_agreement_type = self .azure_cm_connector .get_agreement_type_from_billing_account_info (
4143 billing_account_info
4244 )
43- )
4445
4546 if billing_account_agreement_type == "MicrosoftPartnerAgreement" :
4647 tasks = []
@@ -67,17 +68,15 @@ def get_tasks(
6768 start + relativedelta (months = month + month_range_step - 1 ),
6869 "%Y-%m" ,
6970 )
70- tasks .append (
71- {
72- "task_options" : {
73- "start" : task_start_month ,
74- "end" : task_end_month ,
75- "account_agreement_type" : billing_account_agreement_type ,
76- "collect_scope" : "billing_account_id" ,
77- "billing_tenant_id" : secret_data ["tenant_id" ],
78- }
79- }
71+ task = self ._get_task_scope_billing_account_id (
72+ secret_data ,
73+ billing_account_agreement_type ,
74+ options ,
75+ task_start_month ,
76+ task_end_month ,
8077 )
78+ tasks .append (task )
79+
8180 if linked_accounts :
8281 synced_accounts = linked_accounts
8382 changed .append ({"start" : start_month })
@@ -137,7 +136,7 @@ def get_tasks(
137136 synced_accounts = self ._extend_synced_accounts (
138137 synced_accounts , first_sync_tenants
139138 )
140- else :
139+ elif billing_account_agreement_type == "EnterpriseAgreement" :
141140 tasks = [
142141 {
143142 "task_options" : {
@@ -153,6 +152,22 @@ def get_tasks(
153152 ]
154153 changed = [{"start" : start_month }]
155154 synced_accounts = []
155+ elif billing_account_agreement_type == "MicrosoftCustomerAgreement" :
156+ task = self ._get_task_scope_billing_account_id (
157+ secret_data , billing_account_agreement_type , options , start_month
158+ )
159+
160+ tasks = [task ]
161+ changed = [{"start" : start_month }]
162+ synced_accounts = []
163+ else :
164+ task = self ._get_task_scope_subscription (
165+ secret_data , options , start_month
166+ )
167+
168+ tasks = [task ]
169+ changed = [{"start" : start_month }]
170+ synced_accounts = []
156171
157172 # Benefit Job Task
158173 if options .get ("cost_metric" ) == "AmortizedCost" :
@@ -169,18 +184,9 @@ def get_tasks(
169184 )
170185
171186 elif secret_type == "USE_SERVICE_ACCOUNT_SECRET" :
172- subscription_id = secret_data .get ("subscription_id" , "" )
173- tenant_id = secret_data .get ("tenant_id" )
174- tasks = [
175- {
176- "task_options" : {
177- "collect_scope" : "subscription_id" ,
178- "start" : start_month ,
179- "subscription_id" : subscription_id ,
180- "billing_tenant_id" : tenant_id ,
181- }
182- }
183- ]
187+ task = self ._get_task_scope_subscription (secret_data , options , start_month )
188+
189+ tasks = [task ]
184190 changed = [{"start" : start_month }]
185191 synced_accounts = []
186192
@@ -190,6 +196,45 @@ def get_tasks(
190196 tasks = {"tasks" : tasks , "changed" : changed , "synced_accounts" : synced_accounts }
191197 return tasks
192198
199+ @staticmethod
200+ def _get_task_scope_billing_account_id (
201+ secret_data : dict ,
202+ billing_account_agreement_type : str ,
203+ options : dict ,
204+ start_month : str ,
205+ end_month : str = None ,
206+ ) -> dict :
207+ task_options = {
208+ "start" : start_month ,
209+ "account_agreement_type" : billing_account_agreement_type ,
210+ "collect_scope" : "billing_account_id" ,
211+ "billing_tenant_id" : secret_data ["tenant_id" ],
212+ }
213+
214+ if end_month :
215+ task_options ["end" ] = end_month
216+
217+ task = {"task_options" : task_options }
218+
219+ return task
220+
221+ @staticmethod
222+ def _get_task_scope_subscription (
223+ secret_data : dict , options : dict , start_month : str
224+ ) -> dict :
225+ subscription_id = secret_data ["subscription_id" ]
226+ tenant_id = secret_data .get ("tenant_id" )
227+ task = {
228+ "task_options" : {
229+ "collect_scope" : "subscription_id" ,
230+ "start" : start_month ,
231+ "subscription_id" : subscription_id ,
232+ "tenant_id" : tenant_id ,
233+ }
234+ }
235+
236+ return task
237+
193238 def _get_tenants_from_billing_account (self ):
194239 tenants = []
195240 for (
0 commit comments