Public API: Example Automatic Test creation script

Overview

Requirements

Contents

Script example

Attachments

Overview

This article describes an API script that, based on your config, will create a Continuous Test in your environment using the RDP connector with multiple host functions, and attaches an Account and Launcher Group if configured. The example below uses the PowerShell script, which can be downloaded under the Attachments.

Requirements

To get this script working, you need the following information:

  • Login Enterprise URL ($BaseURL)
  • API Token ($ConfigToken)
    • Needs to be a configuration permission
  • Account Group Name ($AccountGroupName)
  • Launcher Group Name ($LauncherGroupName)

These are placed at the top of the script:

## Script Variables 
$baseUrl = "loginenterprise.domain.lab"
$ConfigToken = "BifSUhkFfEKkLb7tqnYUtYiNBkaJA4OgrdemwoLM71Q"
$AccountGroupName = "Account Group Name"
$LauncherGroupName = "Launcher Group Name"

The section after the script variables contains the certificate and SSL connection setup. By default, it ignores certificate errors, so it works with self-signed and official certificates. For more information on certificates, see Managing Certificates.

Contents

The script is built using three (3) functions:

  • get-AccountGroupID - checks if the configured Account Group name exists in the Login Enterprise Appliance, and if found, will return its ID. 
  • get-LauncherGroupID - checks if the configured Launcher Group name exists in the Login Enterprise Appliance, and if found, will return its ID. 
  • Create-Test - sends the API request to create the Test entry based on its own $CreateBody and the retrieved Account and Launcher Group ID. 

To modify the test-type, connector, and other items of a test case, modify the $CreateBody variable:

$CreateBody = @"
{
"type": "ContinuousTest",
"name": "Your Test Name",
"description": "My New Test",
"connector": {
"type": "Rdp",
"hostList": [
{"enabled": true, "endpoint": "Host1"},
{"enabled": true, "endpoint": "Host2"},
],
"suppressCertWarn": "True",
},
"accountGroups": [$Global:AccountGroupId],
"launcherGroups": [$Global:LauncherGroupId]
}
"@

There are two global definitions other than the variables and functions, called $header and $Body. The $header is used for all API requests, as this defines the authorization method and is the same for all. The $body is used to find the AccountGroupID and LauncherGroupID.

Script example

## Script Variables
$baseUrl = "loginenterprise.domain.lab"
$ConfigToken = "BifSUhkFfEKkLb7tqnYUtYiNBkaJA4OgrdemwoLM71Q"
$AccountGroupName = "Account Group Name"
$LauncherGroupName = "Launcher Group Name"

# WARNING: ignoring SSL/TLS certificate errors is a security risk
$code = @"
public class SSLHandler
{public static System.Net.Security.RemoteCertificateValidationCallback GetSSLHandler()
{return new System.Net.Security.RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => { return true; });}
}
"@
Add-Type -TypeDefinition $code

# WARNING: ignoring SSL/TLS certificate errors is a security risk
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = [SSLHandler]::GetSSLHandler()

# this is only required for older version of PowerShell/.NET
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12

# Define global authorization header for the script
$Header = @{
"Accept" = "application/json"
"Authorization" = "Bearer $ConfigToken"
}

#Define global Body statements for finding the Launcher and Account group name
$Body = @{
"orderBy" = "name"
"direction" = "desc"
"count" = "100"
}

## The Get-AccountGroupID function grabs all of the existing account groups and compares the configured name ($AccountGroupName) with the list
## If the configured name is found in the list it returns the ID of that account group ($global:AccountGroupID)
function get-AccountGroupID {

#Parameters of the API command that combines all values and selects the target API
$Parameters = @{
Uri = 'https://' + $baseUrl + '/publicApi/v6/account-groups/'
Headers = $Header
Method = 'GET'
body = $Body
ContentType = 'application/json'
}

#Execute the command using the parameters and placing them in the $request object.
$request = Invoke-RestMethod @Parameters

#setting counter to 0
$i=0

#Go through the array of items generated by the rest command
ForEach ($item in $request.items)
{
$i++
# Check each entry in array to check if the configured name exists in the array, if not it does nothing.
If($item.name -like $AccountGroupName){
$global:AccountGroupID = '"' + $item.groupId + '"'
Write-Host 'Found Account group match:'$item.groupId
Break
}

# if the end of the array is reached and no match is found, report and continue
ElseIf($i -ge $request.items.count){
Write-Host 'No Account Group name match found for'$AccountGroupName ', Continuing script...'
# set value to null so test is still created without an account group
$global:AccountGroupID = $null
}

}

}

## The Get-LauncherGroupID function grabs all of the existing launcher groups and compares the configured name ($LauncherGroupName) with the list
## If the configured name is found in the list it returns the ID of that launcher group ($global:LauncherGroupID)
function get-LauncherGroupID {

#Parameters of the API command that combines all values and selects the target API
$Parameters = @{
Uri = 'https://' + $baseUrl + '/publicApi/v6/launcher-groups/'
Headers = $Header
Method = 'GET'
body = $Body
ContentType = 'application/json'
}

#Execute the command using the parameters and placing them in the $request object.
$request = Invoke-RestMethod @Parameters

#setting counter to 0
$i=0

#Go through the list of items generated by the rest command
ForEach ($item in $request.items)
{
$i++
# Check each entry in array to check if the configured name exists in the array, if not it does nothing.
If($item.name -like $LauncherGroupName){
[string]$global:LauncherGroupID = '"' + $item.id + '"'
Write-Host 'Found Launcher group match:'$item.id
Break
}
# if the end of the array is reached and no match is found, report and continue
ElseIf($i -ge $request.items.count){
Write-Host 'No Launcher Group name match found for'$LauncherGroupName ', Continuing script...'
# set value to null so test is still created without an account group
$global:LauncherGroupID = $null
}

}

}

function create-test {
## Create Body of API to create the new test, contains all required fields to create a new test.
## This example makes use of the host function of the RDP connector
## for more info check the API documentation on the External Notifications > Public API Page.

$CreateBody = @"
{
"type": "ContinuousTest",
"name": "Your Test Name",
"description": "My New Test",
"connector": {
"type": "Rdp",
"hostList": [
{"enabled": true, "endpoint": "Host1"},
{"enabled": true, "endpoint": "Host2"},
],
"suppressCertWarn": "True",
},
"accountGroups": [$Global:AccountGroupId],
"launcherGroups": [$Global:LauncherGroupId]
}
"@

#Parameters of the API command that combines all values and selects the target API
$Parameters = @{
Uri = 'https://' + $baseUrl + '/publicApi/v6/tests/'
Headers = $Header
Method = 'POST'
body = $CreateBody
ContentType = 'application/json'
}

#Execute the rest command, if a test with the same name already exists it will generate an error. This command does not need an AccountGroupId or LauncherGroupId to work
Invoke-RestMethod @Parameters
}

#execute each function
get-AccountGroupID
get-LauncherGroupID
Create-test

Attachments