From ffe7afa9f49d894cd5d45a3591c1808360bb6008 Mon Sep 17 00:00:00 2001 From: "ZiWei Chen (WICRESOFT NORTH AMERICA LTD)" Date: Mon, 16 May 2022 13:35:53 +0800 Subject: [PATCH 1/5] A python sample for CostManagement --- samples/costmanagement/README.md | 88 ++++++++++++++++++ samples/costmanagement/manage_exports.py | 112 +++++++++++++++++++++++ samples/costmanagement/requirements.txt | 4 + 3 files changed, 204 insertions(+) create mode 100644 samples/costmanagement/README.md create mode 100644 samples/costmanagement/manage_exports.py create mode 100644 samples/costmanagement/requirements.txt diff --git a/samples/costmanagement/README.md b/samples/costmanagement/README.md new file mode 100644 index 0000000..0f6510b --- /dev/null +++ b/samples/costmanagement/README.md @@ -0,0 +1,88 @@ +--- +--- +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/costmanagement/) + +## 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 (you must install [virtualenv](https://pypi.python.org/pypi/virtualenv) for Python 3.6+): + + ``` + python -m venv mytestenv # Might be "python3" or "py -3.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 From cc9aa8305a96cc905157d2cbffcffa3fd22580ee Mon Sep 17 00:00:00 2001 From: Yuchao Yan Date: Mon, 16 May 2022 16:38:03 +0800 Subject: [PATCH 2/5] Update README.md --- samples/costmanagement/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/samples/costmanagement/README.md b/samples/costmanagement/README.md index 0f6510b..dc3cd39 100644 --- a/samples/costmanagement/README.md +++ b/samples/costmanagement/README.md @@ -1,5 +1,4 @@ --- ---- page_type: sample languages: - python From 1a82de8ece5fb72ccc86d6ed19a39c93c6f1a52d Mon Sep 17 00:00:00 2001 From: kazrael2119 <98569699+kazrael2119@users.noreply.github.com> Date: Tue, 17 May 2022 10:47:17 +0800 Subject: [PATCH 3/5] Update samples/costmanagement/README.md Co-authored-by: Yuchao Yan --- samples/costmanagement/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/costmanagement/README.md b/samples/costmanagement/README.md index dc3cd39..95da258 100644 --- a/samples/costmanagement/README.md +++ b/samples/costmanagement/README.md @@ -43,7 +43,7 @@ export SUBSCRIPTION_ID="xxx" 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 (you must install [virtualenv](https://pypi.python.org/pypi/virtualenv) for Python 3.6+): + Install and initialize the virtual environment with the "venv" module on Python 3.6+: ``` python -m venv mytestenv # Might be "python3" or "py -3.6" depending on your Python installation From af28ffed67d7c3ee6c437da43d94383b2fc14ce8 Mon Sep 17 00:00:00 2001 From: kazrael2119 <98569699+kazrael2119@users.noreply.github.com> Date: Tue, 17 May 2022 10:47:32 +0800 Subject: [PATCH 4/5] Update samples/costmanagement/README.md Co-authored-by: Yuchao Yan --- samples/costmanagement/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/costmanagement/README.md b/samples/costmanagement/README.md index 95da258..7009e5c 100644 --- a/samples/costmanagement/README.md +++ b/samples/costmanagement/README.md @@ -46,7 +46,7 @@ export SUBSCRIPTION_ID="xxx" Install and initialize the virtual environment with the "venv" module on Python 3.6+: ``` - python -m venv mytestenv # Might be "python3" or "py -3.6" depending on your Python installation + 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 From c1940fab7d06c6d1220e33c58643edf635995d14 Mon Sep 17 00:00:00 2001 From: kazrael2119 <98569699+kazrael2119@users.noreply.github.com> Date: Tue, 17 May 2022 14:30:49 +0800 Subject: [PATCH 5/5] Update README.md --- samples/costmanagement/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/costmanagement/README.md b/samples/costmanagement/README.md index 7009e5c..e1d82e0 100644 --- a/samples/costmanagement/README.md +++ b/samples/costmanagement/README.md @@ -17,7 +17,7 @@ These code samples will show you how to manage costmanagement using Azure SDK fo 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/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