Skip to content

Accept China Prisma location #12

@sm-palo

Description

@sm-palo

Hi here is my code to accept China Prisma API calls.

Parameter definitions

param (
[string]$region = "US", # Default to US
[string]$api_key = $null,
[string]$environment = "prod", # Default to prod
[string]$dataType = "EgressIPs",
[string]$outputFile = $null
)

Prompt for API Key if not provided

if ([string]::IsNullOrEmpty($api_key)) {
$api_key = Read-Host "Enter API Key"
if ([string]::IsNullOrWhiteSpace($api_key)) {
Write-Host "Please enter a valid API key."
exit
}
}

Define headers for API requests

$headers = @{
"header-api-key" = $api_key
}

Define data payloads

$dataPayloads = @{
"EgressIPs" = '{"serviceType": "all", "addrType": "all", "location": "all"}'
"ActiveReservedOnboardedMobileUserLocations" = '{"serviceType": "gp_gateway", "addrType": "all", "location": "deployed"}'
"ActiveIPOnboardedMobileUserLocations" = '{"serviceType": "gp_gateway", "addrType": "active", "location": "deployed"}'
"ActiveMobileUserAddresses" = '{"serviceType": "gp_gateway", "addrType": "all", "location": "all"}'
"RemoteNetworkAddresses" = '{"serviceType": "remote_network", "addrType": "all", "location": "all"}'
"CleanPipeAddresses" = '{"serviceType": "clean_pipe", "addrType": "all", "location": "all"}'
"ExplicitProxyAddresses" = '{"serviceType": "swg_proxy", "location": "deployed", "addrType": "auth_cache_service"}'
}

Define the base API URI based on the region and environment

if ($region -eq "CN") {
$uriBase = "https://api.$environment.datapath.prismaaccess.cn"
} else {
$uriBase = "https://api.$environment.datapath.prismaaccess.com"
}

Define function for sending API requests

function Send-APIRequest {
param (
[string]$uri,
[string]$method,
$body = $null, # Optional parameter
$headers
)
try {
$response = Invoke-RestMethod -Uri $uri -Method $method -Body $body -Headers $headers -ContentType "application/json"
return $response
} catch {
Write-Error "Failed to fetch data from $uri. Error: $_"
exit
}
}

Define function for displaying formatted results

function Display-FormattedResult {
param (
$result
)
$outputTable = @()
foreach ($item in $result) {
foreach ($detail in $item.address_details) {
$row = [PSCustomObject]@{
Zone = $item.zone
ServiceType = $detail.serviceType
Address = $detail.address
AddressType = $detail.addressType
}
$outputTable += $row
}
}
$outputTable | Format-Table -Property Zone, ServiceType, Address, AddressType -AutoSize
}

Define function for displaying Loopback IPs

function Display-LoopbackIps {
param (
$result
)
$outputTable = @()
foreach ($item in $result) {
$fwType = $item.result.fwType
foreach ($addr in $item.result.addrList) {
$splitAddr = $addr -split ':'
$row = [PSCustomObject]@{
Type = $fwType
Location = $splitAddr[0]
"Loopback IP" = $splitAddr[1]
}
$outputTable += $row
}
}
$outputTable | Format-Table -Property Type, Location, "Loopback IP" -AutoSize
}

Main Logic based on $dataType and region

if ($dataType -eq "loopback_ip") {
$loopbackResults = @()
foreach ($fwType in @('gpcs_gp_gw', 'gpcs_gp_portal', 'gpcs_remote_network')) {
$loopbackUri = "$uriBase/getAddrList/latest?fwType=$fwType&addrType=loopback_ip"
$result = Send-APIRequest -uri $loopbackUri -method 'GET' -headers $headers
$loopbackResults += $result
}

if ($outputFile) {
    $ext = [System.IO.Path]::GetExtension($outputFile).ToLower()
    switch ($ext) {
        ".json" { $loopbackResults | ConvertTo-Json -Depth 10 | Out-File $outputFile }
        ".csv"  { Display-LoopbackIps -result $loopbackResults | Export-Csv -Path $outputFile -NoTypeInformation }
        ".txt"  { $loopbackResults | ForEach-Object { $_.result.addrList | ForEach-Object { ($_ -split ':')[1] } } | Out-File $outputFile }
        default { Write-Error "Unsupported file extension: $ext" }
    }
} else {
    Display-LoopbackIps -result $loopbackResults
}

} else {
$apiUri = "$uriBase/getPrismaAccessIP/v2"
if ($dataPayloads.ContainsKey($dataType)) {
$body = $dataPayloads[$dataType]
$result = Send-APIRequest -uri $apiUri -method 'POST' -body $body -headers $headers
$resultData = $result.result

    if ($outputFile) {
        $ext = [System.IO.Path]::GetExtension($outputFile).ToLower()
        switch ($ext) {
            ".json" { $resultData | ConvertTo-Json -Depth 10 | Out-File $outputFile }
            ".csv"  { Display-FormattedResult -result $resultData | Export-Csv -Path $outputFile -NoTypeInformation }
            ".txt"  { $resultData | ForEach-Object { $_.address_details | ForEach-Object { $_.address } } | Out-File $outputFile }
            default { Write-Error "Unsupported file extension: $ext" }
        }
    } else {
        Display-FormattedResult -result $resultData
    }
} else {
    Write-Error "Unsupported data type: $dataType"
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions