This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

PowerShell samples on using LRC Public APIs

LoadRunner Cloud provides a long list of Public APIs to allow users to do various operations on LRC.

Below are two simplified examples in PowerShell. They demostrate how to do authentications (via both API access keys and user/password) and invoke target APIs. Also, there are some sample codes which might be useful in your scenarios. For example: configure web proxy, conversion between Epoch time and local time etc.

1.  retrieve license usage on your tenant in last one month

 

//For simplicity, basic information(tenant Id, project Id etc.) are configured in codes. $LRC = "">loadrunner-cloud.saas.microfocus.com" $tenantId = "652261341" $projectId = 115 //It's recommended to use API access keys instead of user/password. $useAPIKey = $true # read credentials from environment variables $username = $Env:LRC_USER $password = $Env:LRC_PASSWORD $client_id = $Env:LRC_CLIENT_ID $client_secret = $Env:LRC_CLIENT_SECRET # Write-host "tenant Id: $tenantId" Write-host "project Id: $projectId" Write-host "use API key: $useAPIKey" //License usage data will be saved into a .csv file generated in the same folder as this file $scriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent $csvFile = Join-Path -Path $scriptDir -ChildPath "licenses.csv" //If accessing LRC Public APIs requires web proxy, we can get proxy details from system and configure it accordingly. # get proxy settings from system $proxy = [System.Net.WebRequest]::GetSystemWebProxy() $proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials //Using API access keys and user/password are very similar. The payloads are in JSON format. if ($useAPIKey) { $loginUrl = "$LRC/v1/auth-client?TENANTID=$tenantId" $credentials = @" { "client_id": "$client_id", "client_secret": "$client_secret" } "@ } else { $loginUrl = "$LRC/v1/auth?TENANTID=$tenantId" $credentials = @" { "user": "$username", "password": "$password" } "@ } # create session and send login request $session = new-object microsoft.powershell.commands.webrequestsession $proxyUri = $proxy.GetProxy($loginUrl) Write-host "login" $response = Invoke-RestMethod -Uri $loginUrl -Method Post -Body $credentials -WebSession $session -Proxy $proxyUri -ProxyUseDefaultCredentials -ContentType 'application/json' if ($response.token.Length -le 0) { Write-Host "Failed to Login !!" return 1 } $token = $response.token if ($useAPIKey) { //when using API access keys, returned token needs to be added into bearer authorization header # Bearer authorization header $headers = @{Authorization = "Bearer $token"} } else { //when using user/password, returned token needs to be added into a cookie # add token into session cookie $cookie = new-object system.net.cookie $cookie.name = "LWSSO_COOKIE_KEY" $cookie.value = $token $cookie.domain = "loadrunner-cloud.saas.microfocus.com" $session.Cookies.Add($cookie) } GET /license/usage API accepts two Epoch time parameters to specify the target time range. #date times $Now = Get-Date $OneMinuteAgo = $Now.AddMinutes(-1) $OneMonthAgo = $Now.AddMonths(-1) #convert to Epoch milliseconds $StartTime = [Long]([Double](Get-Date $OneMonthAgo.ToUniversalTime() -UFormat %s) * 1000) $EndTime = [Long]([Double](Get-Date $OneMinuteAgo.ToUniversalTime() -UFormat %s) * 1000) Write-Host $StartTime, $EndTime $licenseUsageUrl = "$LRC/v1/license/usage?TENANTID=${tenantId}&projectIds=$projectId&startTime=$StartTime&endTime=$EndTime" Write-host "retrieving license usage between $LastOneMonth and $Now $licenseUsageUrl" $usages = Invoke-RestMethod -Uri $licenseUsageUrl -Method Get -Headers $headers -WebSession $session -Proxy $proxyUri -ProxyUseDefaultCredentials -ContentType 'application/json' #Write-host $usages //Before saving the data into .csv file, we update the 'startTime' field to use local time (for easier usage in Excel) $usages | foreach-object { $item = $_ #convert Epoch milliseconds to local time $item.startTime = ([System.DateTimeOffset]::FromUnixTimeSeconds([Double]$_.startTime/1000)).DateTime.ToLocalTime() } # output to csv file $usages | select-object | export-csv ${csvFile} -NoTypeInformation Write-Host "csv file generated: ${csvFile}"

 

 

Full sample can be found at "get-license-usage.ps1".

2. bulk updates on test script settings via csv file

If your test has lots of scripts and the scripts have many different scheduling settings, you might want to manage and update them via csv file. This can be easily achieved via below 2 APIs:

We create two scripts. The first one ("list-test-scripts.ps1") is to retrieve the details about test scripts via GET /projects/${projectId}/load-tests/${testId}/scripts and save data into a .csv file.. With the generated .csv file, you can open the file with Excel to make bulk changes. Then, run the second script ("update-test-scripts.ps1") to quickly apply the settings on all scripts.

 

For details about LRC Public APIs, refer to LRC  DOC and give it a try!