Skip to content
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

[CostManagement]Create a python sample for CostManagement #93

Merged
merged 5 commits into from
May 18, 2022
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
87 changes: 87 additions & 0 deletions samples/costmanagement/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
page_type: sample
languages:
- python
products:
- azure
description: "These code samples will show you how to manage Consumption using Azure SDK for Python."
urlFragment: costmanagement
---

# Getting started - Managing Consumption using Azure Python SDK

These code samples will show you how to manage costmanagement using Azure SDK for Python.

## Features

This project framework provides examples for the following services:

### costmanagement
* [] Using the Azure SDK for Python - costmanagement Management Library [azure-mgmt-costmanagement](https://pypi.org/project/azure-mgmt-costmanagement/) for the [costmanagement API](https://docs.microsoft.com/en-us/rest/api/cost-management/)

## Getting Started

### Prerequisites

1. Before we run the samples, we need to make sure we have setup the credentials. Follow the instructions in [register a new application using Azure portal](https://docs.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-portal) to obtain `subscription id`,`client id`,`client secret`, and `application id`

2. Store your credentials an environment variables.
For example, in Linux-based OS, you can do
```bash
export AZURE_TENANT_ID="xxx"
export AZURE_CLIENT_ID="xxx"
export AZURE_CLIENT_SECRET="xxx"
export SUBSCRIPTION_ID="xxx"
```

### Installation

1. If you don't already have it, [install Python](https://www.python.org/downloads/).

This sample (and the SDK) is compatible with Python 3.6+.

2. General recommendation for Python development is to use a Virtual Environment.
For more information, see https://docs.python.org/3/tutorial/venv.html

Install and initialize the virtual environment with the "venv" module on Python 3.6+:

```
python -m venv mytestenv # Might be "python3" or "py3.6" depending on your Python installation
cd mytestenv
source bin/activate # Linux shell (Bash, ZSH, etc.) only
./scripts/activate # PowerShell only
./scripts/activate.bat # Windows CMD only
```

### Quickstart

1. Clone the repository.

```
git clone https://github.com/Azure-Samples/azure-samples-python-management.git
```

2. Install the dependencies using pip.

```
cd azure-samples-python-management/samples/costmanagement
pip install -r requirements.txt
```

## Demo

A demo app is included to show how to use the project.

To run the complete demo, execute `python example.py`

To run each individual demo, point directly to the file. For example (i.e. not complete list):

1. `python manage_workspace.py`

If the script starts with `disable_***.py`, it means that it is unavailable now.

The sample files do not have dependency each other and each file represents an individual end-to-end scenario. Please look at the sample that contains the scenario you are interested in

## Resources

- https://github.com/Azure/azure-sdk-for-python
112 changes: 112 additions & 0 deletions samples/costmanagement/manage_exports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.costmanagement import CostManagementClient
from azure.mgmt.storage import StorageManagementClient

def main():
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
credentials = DefaultAzureCredential()
GROUP_NAME = "test"
export_name="exportxxyyzz"
scope=f'subscriptions/{subscription_id}/resourceGroups/{GROUP_NAME}'
STORAGE_ACCOUNT = "storageaccountxxyyzz"

resource_client = ResourceManagementClient(
credentials,
subscription_id
)
storage_client = StorageManagementClient(
credentials,
subscription_id
)
costmanagement_client = CostManagementClient(
credentials
)
resource_client.resource_groups.create_or_update(
GROUP_NAME,
{"location": "eastus"}

)
storage_account = storage_client.storage_accounts.begin_create(
GROUP_NAME,
STORAGE_ACCOUNT,
{
"sku": {
"name": "Standard_GRS"
},
"kind": "StorageV2",
"location": "eastus",
"encryption": {
"services": {
"file": {
"key_type": "Account",
"enabled": True
},
"blob": {
"key_type": "Account",
"enabled": True
}
},
"key_source": "Microsoft.Storage"
},
"tags": {
"key1": "value1",
"key2": "value2"
}
}
).result()
costmanagement=costmanagement_client.exports.create_or_update(
scope,
export_name,
{
'id':storage_account.id,
'name':STORAGE_ACCOUNT,
'type':'Daily',
"format": "Csv",
"delivery_info": {
"destination":{
"resourceId":storage_account.id,
"container": "exports",
"rootFolderPath": "ad-hoc"
}
},
"definition": {
"type": "Usage",
"timeframe": "MonthToDate",
},
"schedule": {
"status": "Active",
"recurrence": "Weekly",
"recurrencePeriod": {
"from": "2022-05-13T12:00:00Z",
"to": "2022-10-31T00:00:00Z"
}
}

}

)
print("Create consumption:\n{}\n".format(costmanagement))

costmanagement_client.exports.delete(
scope,
export_name
)
print("Delete exports.\n")


storage_client.storage_accounts.delete(
GROUP_NAME,
STORAGE_ACCOUNT
)
print("Delete storage.\n")

resource_client.resource_groups.begin_delete(
GROUP_NAME
).result()



if __name__ == "__main__":
main()
4 changes: 4 additions & 0 deletions samples/costmanagement/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
azure-identity
azure-mgmt-resource==20.0.0
azure-mgmt-costmanagement==3.0.0
azure-mgmt-storage==20.0.0