diff --git a/samples/costmanagement/README.md b/samples/costmanagement/README.md new file mode 100644 index 0000000..e1d82e0 --- /dev/null +++ b/samples/costmanagement/README.md @@ -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 diff --git a/samples/costmanagement/manage_exports.py b/samples/costmanagement/manage_exports.py new file mode 100644 index 0000000..359a4a0 --- /dev/null +++ b/samples/costmanagement/manage_exports.py @@ -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() diff --git a/samples/costmanagement/requirements.txt b/samples/costmanagement/requirements.txt new file mode 100644 index 0000000..03c3133 --- /dev/null +++ b/samples/costmanagement/requirements.txt @@ -0,0 +1,4 @@ +azure-identity +azure-mgmt-resource==20.0.0 +azure-mgmt-costmanagement==3.0.0 +azure-mgmt-storage==20.0.0