Skip to content

Commit

Permalink
Merge pull request #43 from ImMin5/master
Browse files Browse the repository at this point in the history
Add Guide for register DataSource
  • Loading branch information
ImMin5 authored Jan 30, 2024
2 parents 846b584 + 46383b3 commit 50b9204
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 19 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
# plugin-azure-cost-mgmt-cost-datasource

Plugin for collecting Azure Cost management data


---

## Azure Service Endpoint(in use)

<pre>
https://*.blob.core.windows.net
https://management.azure.com
https://login.microsoftonline.com
</pre>

----

## Schema Data

*Schema*
- billing_account_id (str):

- billing_account_id (str):
- tenant_id (str):
- client_id (str):
- client_secret (str):
- customer_tenants (list):(Optional) Customer's tenant id list

- customer_tenants (list):(Optional) Customer's tenant id list

*Example for EA*
<pre>
Expand All @@ -40,12 +45,13 @@ https://login.microsoftonline.com
"tenant_id": "*****",
"client_id": "*****",
"client_secret": "*****"
"customer_tenants":
"customer_tenants": #(optional)
- "*****"

}
</code>
</pre>

## Options

Currently, not required.
86 changes: 86 additions & 0 deletions docs/en/GUIDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Getting Started

All the following are executed with the `spacectl` command.
In this guide you will:

- Register the datasource with Azure credentials

## Pre-requisites

- [spacectl](https://github.com/cloudforet-io/spacectl)
- [Azure role assignment for access to ARM API](https://learn.microsoft.com/en-us/azure/cost-management-billing/automate/cost-management-api-permissions#assign-service-principal-access-to-azure-resource-manager-apis)
- Azure credentials for the billing account

---

## Register the datasource with Azure credentials

Prepare the yaml file for register the datasource with Azure credentials with name `register_azure_csp_datasource.yaml`

if you are installed cloudforet version `1.12`

```yaml
data_source_type: EXTERNAL
provider: azure
name: Azure
plugin_info:
plugin_id: plugin-azure-cost-mgmt-cost-datasource
secret_data:
client_id: { client_id }
client_secret: { client_secret }
tenant_id: { tenant_id }
billing_account_id: { billing_account_id }
options:
collect_resource_id: true
upgrade_mode: AUTO
tags: { }
template: { }
```
if you are installed cloudforet version `2.0`

```yaml
data_source_type: EXTERNAL
provider: azure
name: Azure
plugin_info:
schema_id: azure-client-secret-cost-management
plugin_id: plugin-azure-cost-mgmt-cost-datasource
secret_data:
client_id: { client_id }
client_secret: { client_secret }
tenant_id: { tenant_id }
billing_account_id: { billing_account_id }
options:
collect_resource_id: true
upgrade_mode: AUTO
tags: { }
template: { }
resource_group: DOMAIN
```

and execute the following command

```bash
spacectl exec register cost_analysis.DataSource -f register_azure_csp_datasource.yaml
```

## Sync with your Azure Cost Management data

We synchronize cost data via our scheduler every day at `16:00 UTC`.
If you want to sync immediately, you can use the following command.

before execute the following command, you must get the `data_source_id` from the previous step.

```bash
spacectl list cost_analysis.DataSource --minimal
```

and execute the following command <br>
If you are enterprise customer and have huge data, it may take a long time.

```bash
spacectl exec sync cost_analysis.DataSource -p data_source_id={ data_source_id }
```

39 changes: 24 additions & 15 deletions src/cloudforet/cost_analysis/info/cost_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,41 @@
from spaceone.core.pygrpc.message_type import *
from spaceone.core import utils

__all__ = ['CostInfo', 'CostsInfo']
__all__ = ["CostInfo", "CostsInfo"]

_LOGGER = logging.getLogger(__name__)


def CostInfo(cost_data):
try:
info = {
'cost': float(cost_data['cost']),
'usage_quantity': float(cost_data.get('usage_quantity')),
'usage_type': cost_data.get('usage_type'),
'usage_unit': cost_data.get('usage_unit'),
'provider': cost_data.get('provider'),
'region_code': cost_data.get('region_code'),
'product': cost_data.get('product'),
'billed_date': cost_data['billed_date'],
'additional_info': change_struct_type(cost_data['additional_info']) if 'additional_info' in cost_data else None,
'data': change_struct_type(cost_data['data']) if 'data' in cost_data else None,
'tags': change_struct_type(cost_data['tags']) if 'tags' in cost_data else None
"cost": float(cost_data["cost"]),
"usage_quantity": float(cost_data.get("usage_quantity")),
"usage_type": cost_data.get("usage_type"),
"usage_unit": cost_data.get("usage_unit"),
"provider": cost_data.get("provider"),
"region_code": cost_data.get("region_code"),
"product": cost_data.get("product"),
"billed_date": cost_data["billed_date"],
"additional_info": change_struct_type(cost_data["additional_info"])
if "additional_info" in cost_data
else None,
"data": change_struct_type(cost_data["data"])
if "data" in cost_data
else None,
"tags": change_struct_type(cost_data["tags"])
if "tags" in cost_data
else None,
}
return cost_pb2.CostInfo(**info)

except Exception as e:
_LOGGER.debug(f'[CostInfo] cost data: {cost_data}')
_LOGGER.debug(f'[CostInfo] error reason: {e}', exc_info=True)
_LOGGER.debug(f"[CostInfo] cost data: {cost_data}")
_LOGGER.debug(f"[CostInfo] error reason: {e}", exc_info=True)
raise e


def CostsInfo(costs_data, **kwargs):
return cost_pb2.CostsInfo(results=list(map(functools.partial(CostInfo, **kwargs), costs_data)))
return cost_pb2.CostsInfo(
results=list(map(functools.partial(CostInfo, **kwargs), costs_data))
)

0 comments on commit 50b9204

Please sign in to comment.