3
3
from typing import Tuple , Union
4
4
5
5
from mongoengine import QuerySet
6
+ from spaceone .core import config
6
7
from spaceone .core .service import *
7
8
from spaceone .cost_analysis .error import *
8
9
from spaceone .cost_analysis .model .data_source .request import *
@@ -61,7 +62,8 @@ def register(
61
62
'secret_filter': 'dict',
62
63
'template': 'dict',
63
64
'plugin_info': 'dict',
64
- 'tags': 'dict',
65
+ 'schedule;: 'dict', # required
66
+ 'tags': 'dict', # required
65
67
'resource_group': 'str # required
66
68
'workspace_id': 'str'
67
69
'domain_id': 'str' # injected from auth
@@ -85,6 +87,16 @@ def register(
85
87
else :
86
88
params ["workspace_id" ] = "*"
87
89
90
+ if not params .get ("schedule" ):
91
+ schedule = {
92
+ "state" : "ENABLED" ,
93
+ "hour" : config .get_global ("DATA_SOURCE_SYNC_HOUR" , 16 ),
94
+ }
95
+
96
+ params ["schedule" ] = schedule
97
+ else :
98
+ self ._check_schedule (params ["schedule" ])
99
+
88
100
if data_source_type == "EXTERNAL" :
89
101
params ["template" ] = None
90
102
@@ -194,6 +206,7 @@ def update(self, params):
194
206
'name': 'str',
195
207
'secret_filter': 'dict',
196
208
'template': 'dict',
209
+ 'schedule': 'dict',
197
210
'tags': 'dict'
198
211
'domain_id': 'str' # injected from auth
199
212
}
@@ -207,6 +220,9 @@ def update(self, params):
207
220
data_source_id , domain_id
208
221
)
209
222
223
+ if schedule := params .get ("schedule" ):
224
+ self ._check_schedule (schedule )
225
+
210
226
if "secret_filter" in params :
211
227
if data_source_vo .secret_type == "USE_SERVICE_ACCOUNT_SECRET" :
212
228
self .validate_secret_filter (
@@ -431,62 +447,6 @@ def update_plugin(self, params):
431
447
432
448
return data_source_vo
433
449
434
- @transaction (
435
- permission = "cost-analysis:DataSource.write" ,
436
- role_types = ["DOMAIN_ADMIN" , "WORKSPACE_OWNER" ],
437
- )
438
- @check_required (["data_source_id" , "domain_id" ])
439
- def enable (self , params ):
440
- """Enable data source
441
-
442
- Args:
443
- params (dict): {
444
- 'data_source_id': 'str', # required
445
- 'domain_id': 'str' # injected from auth
446
- }
447
-
448
- Returns:
449
- data_source_vo (object)
450
- """
451
-
452
- data_source_id = params ["data_source_id" ]
453
- domain_id = params ["domain_id" ]
454
- data_source_vo : DataSource = self .data_source_mgr .get_data_source (
455
- data_source_id , domain_id
456
- )
457
-
458
- return self .data_source_mgr .update_data_source_by_vo (
459
- {"state" : "ENABLED" }, data_source_vo
460
- )
461
-
462
- @transaction (
463
- permission = "cost-analysis:DataSource.write" ,
464
- role_types = ["DOMAIN_ADMIN" , "WORKSPACE_OWNER" ],
465
- )
466
- @check_required (["data_source_id" , "domain_id" ])
467
- def disable (self , params ):
468
- """Disable data source
469
-
470
- Args:
471
- params (dict): {
472
- 'data_source_id': 'str', # required
473
- 'domain_id': 'str' # injected from auth
474
- }
475
-
476
- Returns:
477
- data_source_vo (object)
478
- """
479
-
480
- data_source_id = params ["data_source_id" ]
481
- domain_id = params ["domain_id" ]
482
- data_source_vo : DataSource = self .data_source_mgr .get_data_source (
483
- data_source_id , domain_id
484
- )
485
-
486
- return self .data_source_mgr .update_data_source_by_vo (
487
- {"state" : "DISABLED" }, data_source_vo
488
- )
489
-
490
450
@transaction (
491
451
permission = "cost-analysis:DataSource.write" ,
492
452
role_types = ["DOMAIN_ADMIN" , "WORKSPACE_OWNER" ],
@@ -570,7 +530,7 @@ def sync(self, params):
570
530
data_source_id , domain_id , workspace_id
571
531
)
572
532
573
- if data_source_vo .state == "DISABLED" :
533
+ if data_source_vo .schedule . state == "DISABLED" :
574
534
raise ERROR_DATA_SOURCE_STATE (data_source_id = data_source_id )
575
535
576
536
if data_source_vo .data_source_type == "LOCAL" :
@@ -625,7 +585,6 @@ def get(self, params: DataSourceGetRequest) -> Union[DataSourceResponse, dict]:
625
585
[
626
586
"data_source_id" ,
627
587
"name" ,
628
- "state" ,
629
588
"data_source_type" ,
630
589
"provider" ,
631
590
"workspace_id" ,
@@ -645,7 +604,6 @@ def list(
645
604
'query': 'dict (spaceone.api.core.v1.Query)'
646
605
'data_source_id': 'str',
647
606
'name': 'str',
648
- 'state': 'str',
649
607
'data_source_type': 'str',
650
608
'provider': 'str',
651
609
'connected_workspace_id': str,
@@ -969,6 +927,14 @@ def _filter_cost_data_keys_with_permissions_by_data_source_vo(
969
927
data_source_vo .cost_data_keys = cost_data_keys
970
928
return data_source_vo
971
929
930
+ @staticmethod
931
+ def _check_schedule (schedule : dict ) -> None :
932
+ if schedule .get ("state" , "ENABLED" ) == "ENABLED" :
933
+ if not schedule .get ("hour" ):
934
+ raise ERROR_INVALID_PARAMETER (
935
+ key = "schedule.hour" , reason = "Need to set an hour when the state is ENABLED."
936
+ )
937
+
972
938
@staticmethod
973
939
def _check_only_fields_for_permissions (query : dict ) -> None :
974
940
only_fields = query .get ("only" , [])
0 commit comments