Last active
February 23, 2016 10:17
-
-
Save PeteGoo/8c922fa7d51274c31ec0 to your computer and use it in GitHub Desktop.
Copying Sumo Logic Collector Sources with Powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-Collector([string] $collectorName, [PSCredential] $credential) { | |
$response = iwr "https://api.sumologic.com/api/v1/collectors" -Credential $credential | |
$content = ConvertFrom-Json $response.Content | |
$match = $content.collectors | Where {$_.name -eq $collectorName} | Select -First 1 | |
return $match | |
} | |
function Get-CollectorSources([string] $collectorName, [PSCredential] $credential) { | |
$collector = Get-Collector $collectorName $credential | |
if(-not $collector){ | |
throw "Could not find collector $collectorName" | |
} | |
$response = iwr "https://api.sumologic.com/api/v1/collectors/$($collector.id)/sources" -Credential $credential | |
$content = ConvertFrom-Json $response.Content | |
return $content.sources | |
} | |
function Get-CollectorSource([string] $collectorname, [string] $sourceName, [PSCredential] $credential) { | |
$match = (Get-CollectorSources $collectorname $credential)| Where {$_.name -eq $sourceName} | Select -First 1 | |
return $match | |
} | |
function Copy-CollectorSource([string] $fromCollectorName, [string] $toCollectorName, [string] $sourceName, [PSCredential] $credential, [Switch] $updateIfExists) { | |
$source = Get-CollectorSource $fromCollectorName $sourceName $credential | |
if(-not $source){ | |
throw "Could not find source $sourceName on $fromCollectorName" | |
} | |
$toCollector = Get-Collector $toCollectorName $credential | |
if(-not $toCollector){ | |
throw "Could not find collector $toCollectorName" | |
} | |
$source.PSObject.Members.Remove("id") | |
$source.PSObject.Members.Remove("alive") | |
$existingToSource = Get-CollectorSource $toCollectorName $sourceName $credential | |
if($existingToSource){ | |
if($updateIfExists) { | |
$fetchExistingresponse = iwr "https://api.sumologic.com/api/v1/collectors/$($toCollector.id)/sources/$($existingToSource.id)" -Credential $credential | |
$response = Invoke-RestMethod -Method Put -Uri "https://api.sumologic.com/api/v1/collectors/$($toCollector.id)/sources/$($existingToSource.id)" -Credential $credential -Body (ConvertTo-Json @{ source= $source}) -ContentType 'application/json' -Headers @{"If-Match"="$($fetchExistingresponse.Headers.ETag)"} | |
} else { | |
throw "The source $sourceName already exists on $toCollectorName. Use the -UpdateIfExists switch to overwrite" | |
} | |
} else { | |
$response = Invoke-RestMethod -Method Post -Uri "https://api.sumologic.com/api/v1/collectors/$($toCollector.id)/sources" -Credential $credential -Body (ConvertTo-Json @{ source= $source}) -ContentType 'application/json' | |
} | |
} | |
function Copy-AllCollectorSources([string] $fromCollectorName, [string] $toCollectorName, [PSCredential] $credential, [Switch] $updateIfExists) { | |
$allSources = Get-CollectorSources $fromCollectorName $credential | |
Write-Host "Copying $($allSources.Length) sources" | |
foreach($source in $allSources) { | |
Try { | |
Copy-CollectorSource $fromCollectorName $toCollectorName $source.name $credential $updateIfExists | |
} | |
Catch { | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment