Search code examples
pythonazure-data-factorypython-behave

How do I authenticate with Azure in order to run a data factory pipeline test with python behave?


I have a pipeline on Azure Data Factory that I want to write a test for using python behave. For now I just want to run a test locally. The following command won't run just now as I haven't authenticated in any way.

get_client_from_cli_profile(DataFactoryManagementClient)

The error message says I need to run 'az login' to setup account.

knack.util.CLIError: Please run 'az login' to setup account.

Could somebody give an example of how I do this?

Feature

Feature: Run pipeline
    Scenario: Get pipeline
        Given we get the pipeline

Step

@given('we get the pipeline')
def get_pipeline(context):
    pipeline_name = "xxx"
    resource_group = "yyy"
    data_factory = "zzz"
    parameters={}
    pipeline = get_datafactory_pipeline(pipeline_name, resource_group, data_factory, parameters)

Code to get pipeline

from azure.common.client_factory import get_client_from_cli_profile
from azure.mgmt.datafactory import DataFactoryManagementClient

def get_datafactory_pipeline(pipeline_name, resource_group, data_factory, parameters):
    return get_client_from_cli_profile(DataFactoryManagementClient().pipelines.create_run(
        resource_group_name = resource_group,
        factory_name = data_factory,
        pipeline_name = pipeline_name,
        parameters = parameters)

Solution

  • Two way:

    1.install Azure CLI and then az login access to Azure.(download link:https://learn.microsoft.com/en-us/cli/azure/install-azure-cli)

    enter image description here

    2.no need to install Azure CLI but change your code like this:

    def get_datafactory_pipeline(subscription_id,credentials,pipeline_name, resource_group, data_factory, parameters):
        return DataFactoryManagementClient(credentials,subscription_id).pipelines.create_run(
            resource_group_name=resource_group,
            factory_name=data_factory,
            pipeline_name=pipeline_name,
            parameters=parameters)
    

    and your step like this:

    @given('we get the pipeline')
    def get_pipeline(context):
        subscription_id = '<Specify your Azure Subscription ID>'
        credentials = ServicePrincipalCredentials(client_id='<Active Directory application/client ID>', secret='<client secret>', tenant='<Active Directory tenant ID>')
        pipeline_name = "xxx"
        resource_group = "yyy"
        data_factory = "zzz"
        parameters={}
        pipeline = get_datafactory_pipeline(subscription_id,credentials,pipeline_name, resource_group, data_factory, parameters)