Skip to content

Instantly share code, notes, and snippets.

@haydosw
Last active January 23, 2019 05:39
Show Gist options
  • Save haydosw/7351e00b07af8080f401 to your computer and use it in GitHub Desktop.
Save haydosw/7351e00b07af8080f401 to your computer and use it in GitHub Desktop.
Checks for missing octopus variables given a project and scope to test
<#
.SYNOPSIS
Using AppName the script will find all variables for that project.
It will then look at EnvironmentToCheck for variables that have
been configured in other environments but not the one specified
i.e.
A variable named "Test" under the scope of a "Test" environment
will be reported if a "Test" variable under the "Prod" environment
has not also been specified/scoped
.PARAMETER AppName
Application Name exactly as it appears in Octopus
.PARAMETER EnvironmentToCheck
Environment to test against
.PARAMETER APIKey
Octopus API Key. How to create an API Key = http://docs.octopusdeploy.com/display/OD/How+to+create+an+API+key
.PARAMETER OctopusURI
Base endpoint for octopus
.EXAMPLE
Check-MissingVariables -AppName "TestApp" -EnvironmentToCheck "Production" -OctopusURI "https://octopus" -APIKey "API-TBGDJSTAUOXXXXXXXXXXXX"
#>
Function Check-MissingOctoVariables
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true)]
[string]$AppName,
[Parameter(Mandatory=$true)]
[string]$EnvironmentToCheck,
[Parameter(Mandatory=$true)]
[string]$APIKey,
[Parameter(Mandatory=$true)]
[string]$OctopusURI
)
$headers = @{"X-Octopus-ApiKey"="$($apikey)";}
$environments = Call-OctoApi("/api/environments/all")
$project = Find-Project
if ($project -eq $null)
{
Write-Host "Failed to find project"
Break
}
$variabeAndScopes = Find-ProjectVariables
$notInEnviromnet = $variabeAndScopes | Where-Object { $_.Scopes.Count -ge 1 -and $_.Scopes.Name -ne $EnvironmentToCheck }
$inEnvironemnt = $variabeAndScopes | Where-Object { $_.Scopes.Count -ge 1 -and $_.Scopes.Name -eq $EnvironmentToCheck }
# Slow delta logic - but eh took 10 mins
$notInEnviromnet | ForEach-Object {
$testVariable = $_.variable
$found = $false
$inEnvironemnt | ForEach-Object {
$environmentVariable = $_.variable
if ($environmentVariable -eq $testVariable)
{
$found = $true
}
}
if ($found -ne $true)
{
$_
}
}
}
Function script:Fetch-PagedData ($pagedData, $output, $propertiesToSelect)
{
$pagedData.Links | ForEach-Object {
$nextPageOfData = $_.'Page.Next'
if ($nextPageOfData -ne $null)
{
$nextPage = Call-OctoApi($nextPageOfData)
Fetch-PagedData $nextPage $output $propertiesToSelect
}
}
$pagedData.Items | Select-Object -Property $propertiesToSelect | ForEach-Object {
$output += $_
}
return $output
}
Function script:Find-ProjectVariables
{
$variables = Call-OctoApi("/api/variables/$($project.VariableSetId)")
$variabeAndScopes = @()
$variables.Variables | ForEach-Object { $variabeAndScopes += Extract-ScopeVars }
return $variabeAndScopes
}
Function script:Find-Project
{
$projects = Call-OctoApi("/api/projects")
return Fetch-PagedData $projects @() @("Id", "Name", "VariableSetId") | Where-Object { $_.Name -eq $appName } | Sort -Property Name
}
Function script:Call-OctoApi($URIPart)
{
$requestParams = @{
Uri = "$($octopusuri)$URIPart"
Method = "Get"
Headers = $headers
}
return Invoke-Webrequest @requestParams | Select -Expandproperty Content| Convertfrom-Json
}
Function script:Extract-ScopeVars
{
$scope = @()
$_.Scope | Where-Object{$_.Environment -ne $null} | Select -expand Environment | ForEach-Object {
$env = $_
$scope += $environments | Where-Object {$_.Id -eq $env} | select Name
}
$variableAndEnvironments = New-Object psobject
Add-Member -InputObject $variableAndEnvironments -MemberType NoteProperty -Name Variable -Value $_.Name
Add-Member -InputObject $variableAndEnvironments -MemberType NoteProperty -Name Scopes -Value $scope
return $variableAndEnvironments
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment