如何使用托管服务标识从 azure 函数执行管道(adf v2)

时间:2021-03-19 19:26:00

标签: azure

我正在尝试从 azure 函数应用程序调用 adf v2,但使用管理服务标识但无法使用它。我知道如何使用如下使用的服务主体但无法使用 MSI 使用系统;

public static void Run(string myQueueItem, ILogger log) { log.LogInformation($"C# 队列触发函数已处理:{myQueueItem}");

        string tenantId = tenantId;
        string applicationId = applicationId;
        string authenticationKey = authenticationKey;
        string subscriptionId = subscriptionId;
        string resourceGroup = resourceGroup;
        string factoryName = factoryName;
        string pipelineName = "PL_TEST_AZURE_FN";

        //Check body for values
        if (
            tenantId == null ||
            applicationId == null ||
            authenticationKey == null ||
            subscriptionId == null ||
            factoryName == null ||
            pipelineName == null
            )
        {
            return new BadRequestObjectResult("Invalid request body, value missing.");
        }

        //Create a data factory management client
        var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
        ClientCredential cc = new ClientCredential(applicationId, authenticationKey);
        AuthenticationResult result = context.AcquireTokenAsync("https://management.azure.com/", cc).Result;
        ServiceClientCredentials cred = new TokenCredentials(result.AccessToken);
        var client = new DataFactoryManagementClient(cred)
        {
            SubscriptionId = subscriptionId
        };

        //Run pipeline
        CreateRunResponse runResponse;
        PipelineRun pipelineRun;

        if (data?.pipelineParameters == null)
        {
            log.LogInformation("Called pipeline without parameters.");

            runResponse = client.Pipelines.CreateRunWithHttpMessagesAsync(
                resourceGroup, factoryName, pipelineName).Result.Body;
        }
        else
        {
            log.LogInformation("Called pipeline with parameters.");

            JObject jObj = JObject.Parse(requestBody);
            Dictionary<string, object> parameters = jObj["pipelineParameters"].ToObject<Dictionary<string, object>>();

            log.LogInformation("Number of parameters provided: " + jObj.Count.ToString());

            runResponse = client.Pipelines.CreateRunWithHttpMessagesAsync(
                resourceGroup, factoryName, pipelineName, parameters: parameters).Result.Body;
        }

        log.LogInformation("Pipeline run ID: " + runResponse.RunId);
        
        //Wait and check for pipeline result
        log.LogInformation("Checking pipeline run status...");
        while (true)
        {
            pipelineRun = client.PipelineRuns.Get(
                resourceGroup, factoryName, runResponse.RunId);

            log.LogInformation("Status: " + pipelineRun.Status);

            if (pipelineRun.Status == "InProgress" || pipelineRun.Status == "Queued")
                System.Threading.Thread.Sleep(15000);
            else
                break;
        }

        //Final return detail
        string outputString = "{ \"PipelineName\": \"" + pipelineName + "\", \"RunIdUsed\": \"" + pipelineRun.RunId + "\", \"Status\": \"" + pipelineRun.Status + "\" }";

}

请帮助解决此问题以使用托管服务 ID

0 个答案:

没有答案