ashwini0723
New Contributor II

@knutasmI have build the solution for it. The way to create DLT pipeline using SPN is to write a code wherein via databricks API a new DLT pipeline will be created and you mentioned owner as a service principal in the API code as shown below. Below method worked for me 🙂

import requests

import requests
from azure.identity import ClientSecretCredential

databricks_instance = ""
client_id = dbutils.secrets.get(scope="", key="")
client_secret = dbutils.secrets.get(scope="", key="")
tenant_id = ""
#pipeline_id = ""

# Obtain token
token_response = requests.post(
    f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token",
    data={
        'grant_type': 'client_credentials',
        'client_id': client_id,
        'client_secret': client_secret,
        'scope': '2ff814a6-3304-4ab8-85cb-cd0e6f879c1d/.default'
    }
)

access_token = token_response.json().get("access_token")

if not access_token:
    print(f"Error obtaining token: {token_response.text}")
else:
    headers = {
        'Authorization': f'Bearer {access_token}',
        'Content-Type': 'application/json'
    }

    # Define the pipeline creation payload using Unity Catalog
    create_pipeline_payload = {
        "name": "",
        "catalog": "",  # Specify the Unity Catalog name
        "target": "",    # Specify the schema within the Unity Catalog
        "clusters": [
            {
            "label": "default",
            "node_type_id": "Standard_DS3_v2",
            "autoscale": {
                "min_workers": 1,
                "max_workers": 2,
                "mode": "ENHANCED"
            }
            }
        ],
        "libraries": [
            {
            "notebook": {
                "path": ""
            }
            }
        ],
        "permissions": [
            {
                "service_principal_name": '',
                "permission_level": "IS_OWNER"  # Set the desired permission level
            }
        ],
        "development": True  # Set to False for production mode
    }

    # Make the API request to create the pipeline
    response = requests.post(
        f'{databricks_instance}/api/2.0/pipelines',
        headers=headers,
        json=create_pipeline_payload
    )

    # Check the response
    if response.status_code == 200:
        pipeline_id = response.json().get("pipeline_id")
        print(f"Pipeline created successfully with ID: {pipeline_id}")
    else:
        print(f"Failed to create pipeline: {response.status_code}, {response.text}")