With Microsoft Fabric becoming GA in November last year (2023), I wanted to start and explore this new shiny Software-as-a-service (SaaS) solution as well.
With only a limited budget for exploring and testing new tools, I had to figure out how to use my budget efficiently. Therefore, before making any decisions, I looked at the Microsoft Fabric pricing and possibilities.
If you want to take a look at the Microsoft Fabric pricing models, you can find an overview via the following link: Microsoft Fabric – Pricing | Microsoft Azure
To avoid any surprises and to be as cost-effective as possible, I created an easy Python script that I can use to pause and start my Microsoft Fabric capacity, or better said resume and suspend.
In this blog post, I will be going through my solution. Before we get started, you need to know that I’m not a Python expert and there probably is room for improvement in my solution.
Recipe Overview
Preparation time: 20 minutes
Ingredients
- Microsoft Fabric
- Visual Studio Code
- Limited Budget
- Additional tip
Authentication
To be able to use the Microsoft Fabric REST APIs, we need to be authenticated. To get this done, we can make use of MSAL (Microsoft Authentication Library). If you want to find out more about MSAL, you can use the following URL: Overview of the Microsoft Authentication Library (MSAL) – Microsoft identity platform | Microsoft Learn
To avoid storing my credentials directly into my Python script, I’m using a FabricConfig.ini file.
As you can see, I defined 3 different variables in my config file. The client_id, the client_secret and the tenant_id
FabricConfig.ini
[AUTH]
client_id = ########-####-####-####-############
client_secret = #####~##################################
tenant_id = ########-####-####-####-############
Now that we have defined our configuration file, we can start to write our code to authenticate. As a first step, we need to import the required libraries. For msal, we need to import the msal library and while we are configuring, we will already define the other libraries we will be using later in the script.
import msal
import requests
import configparser
import sys
To continue, we need to read the information from the configuration file. To read a config file successfully, we are using the configparser library. As soon as we write the first 2 lines as I have done in the code block below, we can define variables in our code that will contain the configured values as defined in the config file.
config = configparser.ConfigParser()
config.read(r'./FabricConfig.ini')
client_id = config['AUTH']['client_id']
client_secret = config['AUTH']['client_secret']
tenant_id = config['AUTH']['tenant_id']
Now that we have read the configuration file successfully, we can write the code to request a token to use to resume and suspend our Microsoft Fabric Capacity.
authority_url = 'https://login.microsoftonline.com/' + tenant_id
scope = ["https://management.core.windows.net//.default"]
#Use MSAL to grab token
app = msal.ConfidentialClientApplication(client_id, authority=authority_url, client_credential=client_secret)
result = app.acquire_token_for_client(scopes=scope)
Fabric REST API
Now that we have configured the basics to achieve a valid token, we can continue by adding the specific configuration in the FabricConfig.ini file to communicate with the Fabric REST API.
[API]
subscription_id = ########-####-####-####-############
resource_group_name = rg_########
capacity_name = #######
api_version = 2022-07-01-preview
As a next step, we retrieve the configured values in the main Python script.
subscription_id = config['API']['subscription_id']
resource_group = config['API']['resource_group_name']
capacity_name = config['API']['capacity_name']
api_version = config['API']['api_version']
Since we want our script to be executed with a specific parameter, in this case, resume / suspend, we’re capturing the parameter used by using the following syntax:
argAction = sys.argv[1]
By using this syntax we can execute the Python script with a parameter. which will be looking similar to:
python ./StartStopMicrosoftFabric.py "resume"
After configuring the parameter, we can build the final API URL. As you will be able to see in the code sample below, we’re building the URL based on the values from the configuration file and the parameter.
url = "https://management.azure.com/subscriptions/" + subscription_id
url += "/resourceGroups/" + resource_group
url += "/providers/Microsoft.Fabric/"
url += "capacities/" + capacity_name
url += "/" + argAction #resume or suspend
url += "?api-version=" + api_version
As a final step, we’re adding a check that we received an access token and we finally use a POST API call to resume or suspend our Microsoft Fabric instance.
if 'access_token' in result:
access_token = result['access_token']
header = {'Content-Type':'application/json',
'Authorization':f'Bearer {access_token}'}
api_call = requests.post(url=url, headers=header)
If you want to compare your Python script with the complete script we went through in this data recipe, you can find the Python script in my Github repository: blogging/Fabric/Resume – Suspend Microsoft Fabric at main · Oliviervs/blogging (github.com)
Additional tip
If you succesfully want to execute the Python script that has been created, make sure to add the Service Principal as a Contributor to your Microsoft Fabric instance.
To get this done, navigate to portal.azure.com and search for the Microsoft Fabric instance. To continue, go to the Access Control (IAM) section on the left and click on this section. And add your Registered app as a Contributor. (This is the minimal required role to execute the created code.)
Enjoy!
Bon appétit!