saurabh18cs
Honored Contributor III

Let me share process to authenticate and then execute and test your workflow: offcource you can make below pseudocode more industralized which suits you better

import requests
import json
import os

# Define environment variables and parameters
env = os.getenv('env', 'dev') # Replace with the actual way to get the environment variable
sp_app_id_dev = 'your_sp_app_id_dev'
sp_app_id_acc = 'your_sp_app_id_acc'
sp_app_id_prd = 'your_sp_app_id_prd'
SP_SECRET_DEV = 'your_sp_secret_dev'
SP_SECRET_ACC = 'your_sp_secret_acc'
SP_SECRET_PRD = 'your_sp_secret_prd'
databricks_wrkspc_url_dev = 'your_databricks_wrkspc_url_dev'
databricks_wrkspc_url_acc = 'your_databricks_wrkspc_url_acc'
databricks_wrkspc_url_prd = 'your_databricks_wrkspc_url_prd'
databricks_token = 'your_databricks_token' # Replace with the actual way to get the Databricks token

# Determine the environment and set the corresponding variables
if env == 'dev':
CLIENT_ID = sp_app_id_dev
CLIENT_SECRET = SP_SECRET_DEV
databricksWorkspaceUrl = databricks_wrkspc_url_dev
elif env == 'acc':
CLIENT_ID = sp_app_id_acc
CLIENT_SECRET = SP_SECRET_ACC
databricksWorkspaceUrl = databricks_wrkspc_url_acc
else:
CLIENT_ID = sp_app_id_prd
CLIENT_SECRET = SP_SECRET_PRD
databricksWorkspaceUrl = databricks_wrkspc_url_prd

# Get the OAuth token
token_url = "https://login.microsoftonline.com/<<TENANTID>>/oauth2/v2.0/token"
payload = {
'client_id': CLIENT_ID,
'grant_type': 'client_credentials',
'scope': '499b84ac-1321-427f-aa17-267ca6975798/.default',
'client_secret': CLIENT_SECRET
}

response = requests.post(token_url, data=payload)
response.raise_for_status() # Raise an error for bad status codes
sp_devops_token_val = response.json()
sp_devops_token = sp_devops_token_val.get('access_token')

print(f"SP DevOps Token: {sp_devops_token}")

# Set up Databricks Git credentials
DATABRICKS_GIT_URL = f"{databricksWorkspaceUrl}/api/2.0/git-credentials"
gitConfig = {
"personal_access_token": sp_devops_token,
"git_username": "gbr_sp",
"git_provider": "azureDevOpsServices"
}

headers = {
"Authorization": f"Bearer {databricks_token}",
"Accept": "application/json"
}

# Check if Git credentials already exist
git_exists_response = requests.get(DATABRICKS_GIT_URL, headers=headers)
git_exists_response.raise_for_status()
git_exists = git_exists_response.json().get('credentials', [])

if not git_exists:
# Create new Git credentials
create_response = requests.post(DATABRICKS_GIT_URL, headers=headers, json=gitConfig)
create_response.raise_for_status()
print("Git credentials created successfully.")
else:
# Update existing Git credentials
cred_id = git_exists[0].get('credential_id')
if not cred_id:
print("Credential Id is null")
exit(1)
else:
update_url = f"{DATABRICKS_GIT_URL}/{cred_id}"
update_response = requests.patch(update_url, headers=headers, json=gitConfig)
update_response.raise_for_status()
print("Git credentials updated successfully.")