I created an ADF pipeline using the Azure Data Factory UI that triggers a run once every day. Is there a way to get latest pipeline run id & monitor that run using .NET SDK? Please provide a console application that does the same with an existing ADF pipeline run?
This is what I tried where a pipeline is created using .NET SDK and monitor run (https://learn.microsoft.com/en-us/azure/data-factory/quickstart-create-data-factory-dot-net):
static async Task Main(string[] args)
{
// Authenticate and 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);
using (var client = new DataFactoryManagementClient(cred) {
SubscriptionId = subscriptionId
})
{
RunQueryFilter filter1 = new RunQueryFilter("PipelineName", "Equals", new List<string> { "Pipeline" });
DateTime before = DateTime.UtcNow;
DateTime after = before.AddHours(-24);
RunFilterParameters param = new RunFilterParameters(after, before, null, new List<RunQueryFilter> { filter1 }, null);
PipelineRunsQueryResponse pipelineResponse = client.PipelineRuns.QueryByFactory(
resourceGroup,
dataFactoryName, param
);
}
// Monitor the pipeline run
Console.WriteLine("Checking pipeline run status...");
PipelineRun pipelineRun;
while (true)
{
pipelineRun = client.PipelineRuns.Get(
resourceGroup, dataFactoryName, runResponse.RunId);
Console.WriteLine("Status: " + pipelineRun.Status);
if (pipelineRun.Status == "InProgress" || pipelineRun.Status == "Queued")
System.Threading.Thread.Sleep(15000);
else
break;
}
}
but is it possible to monitor the run by getting latest run id from ADF?
Step1,please see IPipelineRunsOperations.QueryByFactoryWithHttpMessagesAsync method.
Step2: Navigate to RunFilterParameters Class, you could find the property named OrderBy.
Step3: Find RunQueryOrderBy Class and you could see it accepts 2 parameters. Here, you could set them as RunEnd and DESC.
Step4: Just get the first element of the Pipeline Runs List.
Related
I have a .net core api deployed on IIS on a Windows server. This api is executing a powershell command (command is updating a text file and also restarting a windows service).
Whenever I am running the code using VS on my local machine or running the same code using VS in the windows server where api is deployed, the powershell command is executing fine. But whenever I am deploying the same changes on IIS and then hitting the api, the same powershell command is not executing and also I am not seeing any exception.
Below is my code through which I am executing the powershell command in the api code.
public async Task SaveCredentials()
{
List<Credential> ls = new List<Credential>();
Credential c1 = new Credential();
c1.Username = "username1";
c1.Password = "8407EC85-3E02-4CED-922F-893A3676786F";
Credential c2 = new Credential();
c2.Username = "username2";
c2.Password = "C6BC830C-570D-429C-BD5E-AE521FDBC952";
Credential c3 = new Credential();
c3.Username = "username3";
c3.Password = "63724EDC-C392-4D69-ADD5-5D5075CD4DA2";
ls.Add(c1);
ls.Add(c2);
ls.Add(c3);
if (ls.Count > 0)
{
var usernames = new string[ls.Count];
var passwords = new string[ls.Count];
foreach (var item in ls)
{
int index = ls.IndexOf(item);
usernames[index] = item.Username;
passwords[index] = item.Password;
}
InitialSessionState initialSessionState = InitialSessionState.CreateDefault();
initialSessionState.ExecutionPolicy = ExecutionPolicy.Unrestricted;
using (Runspace rs = RunspaceFactory.CreateRunspace())
{
rs.Open();
using (PowerShell powershell = PowerShell.Create())
{
var scriptPath = $#"{AppContext.BaseDirectory}Powershell\pscommand.ps1";
powershell.Runspace = rs;
// specify the script code to run.
powershell.AddScript(File.ReadAllText(scriptPath));
// specify the parameters to pass into the script.
powershell.AddParameter("Username", usernames);
powershell.AddParameter("Password", passwords);
var pipelineObjects = await powershell.InvokeAsync().ConfigureAwait(false);
// print the resulting pipeline objects to the console.
foreach (var item in pipelineObjects)
{
_logger.LogInformation(item.BaseObject.ToString());
}
}
// Close the runspace and release any resources.
rs.Close();
}
}
}
Can someone help me here or give some guidance?
I am trying to incorporate "Checkmarx" Static code scans as a stage into my devops pipeline. Currently our code uses "cake" files to excute the stages (invoked by PowerShell).
I was checking the cake support for Checkmarx.Api but could not find any neither in the Checkmarx site or in the Cake website. The NuGet gallery has a tab for the cake addin - https://www.nuget.org/packages/Checkmarx.API/
but does not share any information on the contracts exposed.
So reaching out to the community to see if anyone has done any work on this or has any references. Any other way you have incorporated "Checkmarx" into your build pipeline (without directly using the plugin rather using the CxCLi) would also be helpful as well.
As answered in the GitHub discussion where you asked the same question:
Cake scripts based on "normal" C#, so whatever the usage of Checkmarx.API, you can simply incorporate that in your cake scripts. Probably something like:
Task("Scan")
.Does(() =>
{
// place your code here..
});
As for using Checkmarx.API, I would suggest asking in the Checkmarx.API repo.
Alternatively, it seems that there is a CLI available. You can use that using the one of the process aliases.
Probably something like:
Task("Scan")
.Does(() =>
{
StartProcess("runCxConsole.cmd", new ProcessSettings
{
Arguments = #"Scan -v -ProjectName ""CxServer/bookname j2"" -CxServer http://localhost -CxUser username -CxPassword admin -LocationType folder -LocationPath ""C:\Data\Projects\Java\bs java"" -preset ""Checkmarx Default"""
});
});
(Note: I took the Arguments to runCxConsole.cmd from the documentation - I did not test that.)
I will mark this as closed as I have been able to get around this using .net HttpClient but unfortunately could not implement using Checkmarx cake addin.
I will paste the sample code, i was getting some ssl eerror until i added the "ServerCertificateCustomValidationCallback" to return true
string accessToken = string.Empty;
try
{
using (var httpClientHandler = new HttpClientHandler())
{
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
using (var client = new HttpClient(httpClientHandler))
{
client.BaseAddress = new Uri(CXUrl+"/auth/identity/connect/token");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Add("Accept", "*/*");
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("scope", "access_control_api sast_api"),
new KeyValuePair<string, string>("username", username),
new KeyValuePair<string, string>("password", pwd),
new KeyValuePair<string, string>("client_id", "resource_owner_sast_client"),
new KeyValuePair<string, string>("client_secret", "****************************"),
});
var response = client.PostAsync("", content);
var result = JsonConvert.DeserializeObject<CXAccessToken>(response.Result.Content.ReadAsStringAsync().Result);
accessToken = result.access_token;
}
}
}
How can I get the list of test results for a given test case using azure devops API ?
var testResultsQuery = new TestResultsQuery
{
ResultsFilter = new ResultsFilter
{
TestCaseId = validTestCaseId
}
};
var testCaseResults = await _testClient.GetTestResultsByQueryAsync(testResultsQuery, projectName).ConfigureAwait(false);
This code results in an internal error (500) from the API.
You need to specify the GroupBy and AutomatedTestName properties for ResultsFilter too. Please specify them in ResultsFilter. See below example:
var testResultsQuery = new TestResultsQuery
{
ResultsFilter = new ResultsFilter
{
AutomatedTestName="AutoTestName",
GroupBy = "branch",
TestCaseId = 149
}
};
I'd like to get a list of all the iterations for a given project in a Azure DevOps repository, using the .NET API.
Is there any example of how to do this? The current documentation (https://learn.microsoft.com/en-us/dotnet/api/microsoft.teamfoundation.work.webapi.workhttpclientbase.getteamiterationsasync?view=azure-devops-dotnet) is pretty thin.
Below is a working example of how to achieve this.
You need to reference Microsoft.TeamFoundation.Work.WebApi.
public async Task<List<TeamSettingsIteration>> GetProjectIterations(string serverUrl, string projectName)
{
var uri = new Uri(serverUrl);
var creds = new VssClientCredentials(new WindowsCredential(true), new VssFederatedCredential(true), CredentialPromptType.PromptIfNeeded);
var azureDevopsConnection = new VssConnection(uri, creds);
await azureDevopsConnection.ConnectAsync();
WorkHttpClient azureDevOpsWorkHttpClient = azureDevopsConnection.GetClient<WorkHttpClient>();
TeamContext teamContext = new TeamContext(projectName);
List<TeamSettingsIteration> results= await azureDevOpsWorkHttpClient.GetTeamIterationsAsync(teamContext);
return results;
}
I have a WorkFlow Service hosted in a server: http://myServer.net/MyWorkflowService.xamlx
and it's working normally, I called it from a Windows Phone app before and working.
Now, I wanted to call it from a PCL Project (profile 78) for Xamarin.
I got this error:
A correlation query yielded an empty result set. Please ensure
correlation queries for the endpoint are correctly configured.
I added it as a service reference, and I call an Async Method, and subscribes for completed event:
example
TaskCompletionSource<MyResponse> tsk = new TaskCompletionSource<MyResponse>();
WorkFlowService.SubmitModel serviceModel = new WorkFlowService.SubmitModel()
{
List = MyList.ToArray<string>(),
Guid = Guid,
Description = Description,
userid = UserId
};
WorkFlowClient.SubmitCompleted += (sender, eventArgs) => {
if (eventArgs.Error != null)
{
Debug.WriteLine("Exception : DataService : Adding New" + eventArgs.Error.Message);
tsk.TrySetResult(new MyResponse() {
HasError = true
});
}
else
{
tsk.TrySetResult(new MyResponse()
{
HasError = false
});
}
};
WorkFlowClient.SubmitAsync(new WorkFlowService.SubmitRequest((serviceModel)));
return tsk.Task;
I should send an array of strings with my request, Should I provide ServiceReferences.ClientConfig file and what is the build action for it inside the PCL?!