# Example - powershell -file TDMAPIExample.ps1 -username administrator -password marmite -url http://20.70.152.98:8443 -ProjectName "TOSCA_DemoWebShop_DataGen" -Version 1.0 -Environment Demowebshop # Define Parameters #---------------------- param( [string]$username, [string]$url, [string]$ProjectName, [string]$Version, [string]$Environment, [string]$password ) # $authurl="${url}/TestDataManager/user/login" $projecturl="${url}/TDMProjectService/api/ca/v1/projects" $environmenturl="${url}/TDMDataReservationService/api/ca/v1/environments" $generatorurl="${url}/TDMGeneratorService/api/ca/v1/generators" # Convert username and password (username:password) to Base64 #------------------------------------------------------------------ $stringtoencode="${username}:${password}" $EncodedText = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("$stringtoencode")) $Auth="Basic ${EncodedText}" # Ignore Certificates when using SSL Connection string #------------------------------------------------------------------ $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("Authorization",$Auth) $headers.Add("ContentType",'application/json') add-type @" using System.Net; using System.Security.Cryptography.X509Certificates; public class TrustAllCertsPolicy : ICertificatePolicy { public bool CheckValidationResult( ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) { return true; } } "@ [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy [Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls" # Use Base64 encoded string to generate authorization token #------------------------------------------------------------------ $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("Authorization",$Auth) $headers.Add("ContentType",'application/json') try { $response=Invoke-RestMethod -Method 'Post' -Uri $authurl -Headers $headers } catch [System.Net.WebException] { Write-Verbose "An exception was caught: $($_.Exception.Message)" $_.Exception.Response } $tokenorig = $response.token $token="Bearer ${tokenorig}" # Query TDM for all Projects #--------------------------- $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("Authorization",$token) $headers.Add("ContentType",'application/json') try { $projectresponse=Invoke-RestMethod -Method 'Get' -Uri $projecturl -Headers $headers } catch [System.Net.WebException] { Write-Verbose "An exception was caught: $($_.Exception.Message)" $_.Exception.Response } # Extract ProjectID for Project name specified on CLI #----------------------------------------------------- $projectID=($projectresponse | where {$_.name -eq $ProjectName}) $ProjectID=$projectID.id # Query TDM to return all versions for the selected project #---------------------------------------------------------- $versionurl="${url}/TDMProjectService/api/ca/v1/projects/$projectID/versions" $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("Authorization",$token) $headers.Add("ContentType",'application/json') try { $versionresponse=Invoke-RestMethod -Method 'Get' -Uri $versionurl -Headers $headers } catch [System.Net.WebException] { Write-Verbose "An exception was caught: $($_.Exception.Message)" $_.Exception.Response } # Extract VersionID for Project / Version name specified on CLI $versionID=($versionresponse | where {$_.name -eq $Version}) $VersionID=$versionID.id # Query TDM to return all environments for the selected project / version #----------------------------------------------------------------------------- $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("Authorization",$token) $headers.Add("ContentType",'application/json') $EnvBody = @{"projectId"="$ProjectID"; "versionId"="$VersionID"; } try { $environmentresponse=Invoke-RestMethod -Method 'Get' -Uri $environmenturl -Headers $headers -Body $EnvBody } catch [System.Net.WebException] { Write-Verbose "An exception was caught: $($_.Exception.Message)" $_.Exception.Response } # Extract EnvironmentID for Project / Version / Environment name specified on CLI #--------------------------------------------------------------------------------------- $environmentid=($environmentresponse.elements | where {$_.name -eq $Environment}) $environmentID=$environmentid.id $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("Authorization",$token) $headers.Add("ContentType",'application/json') $EnvBody = @{"projectId"="$ProjectID"; "versionId"="$VersionID"; } $environmentdetailurl="${url}/TDMDataReservationService/api/ca/v1/environments/$environmentID" try { $environmentdetailresponse=Invoke-RestMethod -Method 'Get' -Uri $environmentdetailurl -Headers $headers -Body $EnvBody } catch [System.Net.WebException] { Write-Verbose "An exception was caught: $($_.Exception.Message)" $_.Exception.Response } # Output ID's "`n" Write-Host -NoNewline "Project ID for Project name ${ProjectName} is" $ProjectID "`n" Write-Host -NoNewline "Version ID for Version ${Version} is" $VersionID "`n" Write-Host -NoNewline "Environment ID for Environment ${Environment} is" $environmentID "`n" # Debugging section - Un comment the following section to view response outputs when debugging # write-host Project Table # write-output $projectresponse | Sort-Object -Property name| Format-Table # write-host Version Table for project ${ProjectName} # write-output $versionresponse | Sort-Object -Property name| Format-Table # write-host Environment Table ${ProjectName} / Version $Version # write-output $environmentresponse.elements | Sort-Object -Property name| Format-Table # write-output $generatorresponse.elements | Sort-Object -Property name| Format-Table # write-output $generatortableresponse.tables | Sort-Object -Property name| Format-Table