|
| 1 | +from typing import Generator |
| 2 | +from spaceone.cost_analysis.plugin.data_source.lib.server import DataSourcePluginServer |
| 3 | + |
| 4 | +from .manager import CostManager, DataSourceManager, JobManager |
| 5 | + |
| 6 | +app = DataSourcePluginServer() |
| 7 | + |
| 8 | + |
| 9 | +@app.route("DataSource.init") |
| 10 | +def data_source_init(params: dict) -> dict: |
| 11 | + """init plugin by options |
| 12 | +
|
| 13 | + Args: |
| 14 | + params (DataSourceInitRequest): { |
| 15 | + 'options': 'dict', # Required |
| 16 | + 'domain_id': 'str' # Required |
| 17 | + } |
| 18 | +
|
| 19 | + Returns: |
| 20 | + PluginResponse: { |
| 21 | + 'metadata': 'dict' |
| 22 | + } |
| 23 | + """ |
| 24 | + data_source_mgr = DataSourceManager() |
| 25 | + print(params) |
| 26 | + return data_source_mgr.init_response(**params) |
| 27 | + |
| 28 | + |
| 29 | +@app.route("DataSource.verify") |
| 30 | +def data_source_verify(params: dict) -> None: |
| 31 | + """Verifying data source plugin |
| 32 | +
|
| 33 | + Args: |
| 34 | + params (CollectorVerifyRequest): { |
| 35 | + 'options': 'dict', # Required |
| 36 | + 'secret_data': 'dict', # Required |
| 37 | + 'schema': 'str', |
| 38 | + 'domain_id': 'str' # Required |
| 39 | + } |
| 40 | +
|
| 41 | + Returns: |
| 42 | + None |
| 43 | + """ |
| 44 | + pass |
| 45 | + |
| 46 | + |
| 47 | +@app.route("Job.get_tasks") |
| 48 | +def job_get_tasks(params: dict) -> dict: |
| 49 | + """Get job tasks |
| 50 | +
|
| 51 | + Args: |
| 52 | + params (JobGetTaskRequest): { |
| 53 | + 'options': 'dict', # Required |
| 54 | + 'secret_data': 'dict', # Required |
| 55 | + 'linked_accounts': 'list', # optional |
| 56 | + 'schema': 'str', |
| 57 | + 'start': 'str', |
| 58 | + 'last_synchronized_at': 'datetime', |
| 59 | + 'domain_id': 'str' # Required |
| 60 | + } |
| 61 | +
|
| 62 | + Returns: |
| 63 | + TasksResponse: { |
| 64 | + 'tasks': 'list', |
| 65 | + 'changed': 'list' |
| 66 | + 'synced_accounts': 'list' |
| 67 | + } |
| 68 | +
|
| 69 | + """ |
| 70 | + job_mgr = JobManager() |
| 71 | + params["schema"] = params.pop("schema_name", None) |
| 72 | + return job_mgr.get_tasks(**params) |
| 73 | + |
| 74 | + |
| 75 | +@app.route("Cost.get_linked_accounts") |
| 76 | +def cost_get_linked_accounts(params: dict) -> dict: |
| 77 | + """Get linked accounts |
| 78 | +
|
| 79 | + Args: |
| 80 | + params (GetLinkedAccountsRequest): { |
| 81 | + 'options': 'dict', # Required |
| 82 | + 'schema': 'dict', |
| 83 | + 'secret_data': 'dict', # Required |
| 84 | + 'domain_id': 'str' # Required |
| 85 | + } |
| 86 | +
|
| 87 | + Returns: |
| 88 | + AccountsResponse: { |
| 89 | + 'results': 'list' |
| 90 | + } |
| 91 | + """ |
| 92 | + cost_mgr = CostManager() |
| 93 | + params["schema"] = params.pop("schema_name", None) |
| 94 | + return cost_mgr.get_linked_accounts(**params) |
| 95 | + |
| 96 | + |
| 97 | +@app.route("Cost.get_data") |
| 98 | +def cost_get_data(params: dict) -> Generator[dict, None, None]: |
| 99 | + """Get external cost data |
| 100 | +
|
| 101 | + Args: |
| 102 | + params (CostGetDataRequest): { |
| 103 | + 'options': 'dict', # Required |
| 104 | + 'secret_data': 'dict', # Required |
| 105 | + 'schema': 'str', |
| 106 | + 'task_options': 'dict', |
| 107 | + 'domain_id': 'str' # Required |
| 108 | + } |
| 109 | +
|
| 110 | + Returns: |
| 111 | + Generator[ResourceResponse, None, None] |
| 112 | + { |
| 113 | + 'cost': 'float', |
| 114 | + 'usage_quantity': 'float', |
| 115 | + 'usage_unit': 'str', |
| 116 | + 'provider': 'str', |
| 117 | + 'region_code': 'str', |
| 118 | + 'product': 'str', |
| 119 | + 'usage_type': 'str', |
| 120 | + 'resource': 'str', |
| 121 | + 'tags': 'dict' |
| 122 | + 'additional_info': 'dict' |
| 123 | + 'data': 'dict' |
| 124 | + 'billed_date': 'str' |
| 125 | + } |
| 126 | + """ |
| 127 | + cost_mgr = CostManager() |
| 128 | + options = params.get("options", {}) |
| 129 | + task_options = params.get("task_options", {}) |
| 130 | + params["schema"] = params.pop("schema_name", None) |
| 131 | + |
| 132 | + if options.get("cost_metric") == "AmortizedCost" and task_options.get( |
| 133 | + "is_benefit_job", False |
| 134 | + ): |
| 135 | + for cost_response in cost_mgr.get_benefit_data(**params): |
| 136 | + yield {"results": cost_response} |
| 137 | + else: |
| 138 | + for cost_response in cost_mgr.get_data(**params): |
| 139 | + yield {"results": cost_response} |
0 commit comments