diff --git a/octopus-samples-instances/api-scripts/call-enforce-space-standards-runbook.ps1 b/octopus-samples-instances/api-scripts/call-enforce-space-standards-runbook.ps1 index 276a5bf..7844eaf 100644 --- a/octopus-samples-instances/api-scripts/call-enforce-space-standards-runbook.ps1 +++ b/octopus-samples-instances/api-scripts/call-enforce-space-standards-runbook.ps1 @@ -5,7 +5,8 @@ param $AdminInstanceUrl, $AdminInstanceApiKey, $AdminEnvironmentName, - $AdminSpaceName + $AdminSpaceName, + $OptionalSpaceId ) function Invoke-OctopusApi @@ -77,8 +78,15 @@ function Invoke-OctopusApi } } -$spacesList = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "spaces?skip=0&take=1000" -spaceId $null -apiKey $OctopusApiKey -item $null -method "GET" -foreach ($space in $spacesList.Items) +$rawSpacesList = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "spaces?skip=0&take=1000" -spaceId $null -apiKey $OctopusApiKey -item $null -method "GET" +$spacesList = $rawSpacesList.Items + +if ([string]::IsNullOrWhiteSpace($OptionalSpaceId) -eq $false -and $OptionalSpaceId.ToLower().Trim() -ne "all") +{ + $spacesList = @($spacesList | Where-Object {$_.Id -eq $OptionalSpaceId}) +} + +foreach ($space in $spacesList) { Write-Host "Queueing a runbook run for $($space.Name) using the space id $($space.Id). The runbook will run on $AdminInstanceUrl for the environment $AdminEnvironmentName in the space $AdminSpaceName" Write-Host "Running command: octo run-runbook --project ""Standards"" --runbook ""Enforce Space Standards"" --environment ""$AdminEnvironmentName"" --variable=""Project.SpaceStandard.SpaceId:$($space.Id)"" --server=""$AdminInstanceUrl"" --apiKey=""$AdminInstanceApiKey"" --space=""$AdminSpaceName"" to queue the runbook" diff --git a/octopus-samples-instances/api-scripts/enforce-base-environments.ps1 b/octopus-samples-instances/api-scripts/enforce-base-environments.ps1 index cd62e8d..dc4bffd 100644 --- a/octopus-samples-instances/api-scripts/enforce-base-environments.ps1 +++ b/octopus-samples-instances/api-scripts/enforce-base-environments.ps1 @@ -2,7 +2,8 @@ param ( $OctopusUrl, $OctopusApiKey, - $EnvironmentsCsv + $EnvironmentsCsv, + $SpaceIdFilter ) function Invoke-OctopusApi @@ -74,35 +75,32 @@ function Invoke-OctopusApi } } -$spacesList = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "spaces?skip=0&take=1000" -spaceId $null -apiKey $OctopusApiKey -item $null -method "GET" +$space = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "spaces/$SpaceIdFilter" -spaceId $null -apiKey $OctopusApiKey -item $null -method "GET" $environmentsToCheckList = @($EnvironmentsCsv -split ",") -foreach ($space in $spacesList.Items) -{ - $environmentsList = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "environments?skip=0&take=1000" -spaceId $space.Id -apiKey $OctopusApiKey -item $null -method "GET" +$environmentsList = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "environments?skip=0&take=1000" -spaceId $space.Id -apiKey $OctopusApiKey -item $null -method "GET" - foreach ($environmentToCheck in $environmentsToCheckList) +foreach ($environmentToCheck in $environmentsToCheckList) +{ + $found = $false + foreach ($environment in $environmentsList.Items) { - $found = $false - foreach ($environment in $environmentsList.Items) + Write-Verbose "Checking $($environment.Name) with $environmentToCheck" + if ($environment.Name.ToLower().Trim() -eq $environmentToCheck.ToLower().Trim()) { - Write-Verbose "Checking $($environment.Name) with $environmentToCheck" - if ($environment.Name.ToLower().Trim() -eq $environmentToCheck.ToLower().Trim()) - { - Write-Host "Environment $environmentToCheck already exists on space $($space.Name)" - $found = $true - break - } + Write-Host "Environment $environmentToCheck already exists on space $($space.Name)" + $found = $true + break } + } - if ($found -eq $false) - { - $environmentToAdd = @{ - Name = $environmentToCheck.Trim() - } - - $newEnvironment = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "environments" -spaceId $space.Id -apiKey $OctopusApiKey -item $environmentToAdd -method "POST" - Write-Host "Added the environment $environmentToCheck with the new id $($newEnvironment.Id)" + if ($found -eq $false) + { + $environmentToAdd = @{ + Name = $environmentToCheck.Trim() } + + $newEnvironment = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "environments" -spaceId $space.Id -apiKey $OctopusApiKey -item $environmentToAdd -method "POST" + Write-Host "Added the environment $environmentToCheck with the new id $($newEnvironment.Id)" } } diff --git a/octopus-samples-instances/api-scripts/enforce-lifecycle-policies.ps1 b/octopus-samples-instances/api-scripts/enforce-lifecycle-policies.ps1 index 27e7e3e..78372af 100644 --- a/octopus-samples-instances/api-scripts/enforce-lifecycle-policies.ps1 +++ b/octopus-samples-instances/api-scripts/enforce-lifecycle-policies.ps1 @@ -3,66 +3,65 @@ param $OctopusUrl, $OctopusApiKey, $MaxReleaseRetentionDaysAllowed, - $UnitToUse + $UnitToUse, + $SpaceIdFilter ) $header = @{ "X-Octopus-ApiKey" = $octopusAPIKey } # Get spaces -$spaces = Invoke-RestMethod -Uri "$octopusURL/api/spaces/all" -Headers $header -Write-Output "Space Count: $($spaces.Length)" -foreach ($space in $spaces) { - Write-Output "Working on space $($space.Name)" +$space = Invoke-RestMethod -Uri "$octopusURL/api/spaces/$SpaceIdFilter" -Headers $header + +Write-Output "Working on space $($space.Name)" + +$lifecycleList = Invoke-RestMethod -Uri "$octopusUrl/api/$($space.Id)/lifecycles/all" -headers $header +foreach ($lifecycle in $lifecycleList) +{ + Write-Output " Checking lifecycle $($lifecycle.Name)" + $changeLifecycle = $false + if ($lifecycle.ReleaseRetentionPolicy.QuantityToKeep -gt $maxReleaseRetentionDaysAllowed -or $lifecycle.ReleaseRetentionPolicy.Unit -ne $unitToUse) + { + Write-Host " Release retention policy is set to $($lifecycle.ReleaseRetentionPolicy.QuantityToKeep) $($lifecycle.ReleaseRetentionPolicy.Unit), updating to $maxReleaseRetentionDaysAllowed $unitToUse" + $changeLifecycle = $true + $lifeCycle.ReleaseRetentionPolicy.QuantityToKeep = $maxReleaseRetentionDaysAllowed + $lifeCycle.ReleaseRetentionPolicy.Unit = $unitToUse + $lifeCycle.ReleaseRetentionPolicy.ShouldKeepForever = $false + } - $lifecycleList = Invoke-RestMethod -Uri "$octopusUrl/api/$($space.Id)/lifecycles/all" -headers $header - foreach ($lifecycle in $lifecycleList) + if ($lifecycle.TentacleRetentionPolicy.QuantityToKeep -gt $maxReleaseRetentionDaysAllowed -or $lifecycle.TentacleRetentionPolicy.Unit -ne $unitToUse) { - Write-Output " Checking lifecycle $($lifecycle.Name)" - $changeLifecycle = $false - if ($lifecycle.ReleaseRetentionPolicy.QuantityToKeep -gt $maxReleaseRetentionDaysAllowed -or $lifecycle.ReleaseRetentionPolicy.Unit -ne $unitToUse) - { - Write-Host " Release retention policy is set to $($lifecycle.ReleaseRetentionPolicy.QuantityToKeep) $($lifecycle.ReleaseRetentionPolicy.Unit), updating to $maxReleaseRetentionDaysAllowed $unitToUse" - $changeLifecycle = $true - $lifeCycle.ReleaseRetentionPolicy.QuantityToKeep = $maxReleaseRetentionDaysAllowed - $lifeCycle.ReleaseRetentionPolicy.Unit = $unitToUse - $lifeCycle.ReleaseRetentionPolicy.ShouldKeepForever = $false - } - - if ($lifecycle.TentacleRetentionPolicy.QuantityToKeep -gt $maxReleaseRetentionDaysAllowed -or $lifecycle.TentacleRetentionPolicy.Unit -ne $unitToUse) + Write-Host " Tentacle retention policy is set to $($lifecycle.TentacleRetentionPolicy.QuantityToKeep) $($lifecycle.TentacleRetentionPolicy.Unit), updating to $maxReleaseRetentionDaysAllowed $unitToUse" + $changeLifecycle = $true + $lifeCycle.TentacleRetentionPolicy.QuantityToKeep = $maxReleaseRetentionDaysAllowed + $lifeCycle.TentacleRetentionPolicy.Unit = $unitToUse + $lifeCycle.TentacleRetentionPolicy.ShouldKeepForever = $false + } + + foreach ($phase in $lifecycle.Phases) + { + if ($null -ne $phase.ReleaseRetentionPolicy -and ($phase.ReleaseRetentionPolicy.QuantityToKeep -gt $maxReleaseRetentionDaysAllowed -or $phase.ReleaseRetentionPolicy.Unit -ne $unitToUse)) { - Write-Host " Tentacle retention policy is set to $($lifecycle.TentacleRetentionPolicy.QuantityToKeep) $($lifecycle.TentacleRetentionPolicy.Unit), updating to $maxReleaseRetentionDaysAllowed $unitToUse" + Write-Host " Release retention policy for $($phase.Name) is set to $($phase.ReleaseRetentionPolicy.QuantityToKeep) $($phase.ReleaseRetentionPolicy.Unit), updating to $maxReleaseRetentionDaysAllowed $unitToUse" $changeLifecycle = $true - $lifeCycle.TentacleRetentionPolicy.QuantityToKeep = $maxReleaseRetentionDaysAllowed - $lifeCycle.TentacleRetentionPolicy.Unit = $unitToUse - $lifeCycle.TentacleRetentionPolicy.ShouldKeepForever = $false + $phase.ReleaseRetentionPolicy.QuantityToKeep = $maxReleaseRetentionDaysAllowed + $phase.ReleaseRetentionPolicy.Unit = $unitToUse + $phase.ReleaseRetentionPolicy.ShouldKeepForever = $false } - - foreach ($phase in $lifecycle.Phases) - { - if ($null -ne $phase.ReleaseRetentionPolicy -and ($phase.ReleaseRetentionPolicy.QuantityToKeep -gt $maxReleaseRetentionDaysAllowed -or $phase.ReleaseRetentionPolicy.Unit -ne $unitToUse)) - { - Write-Host " Release retention policy for $($phase.Name) is set to $($phase.ReleaseRetentionPolicy.QuantityToKeep) $($phase.ReleaseRetentionPolicy.Unit), updating to $maxReleaseRetentionDaysAllowed $unitToUse" - $changeLifecycle = $true - $phase.ReleaseRetentionPolicy.QuantityToKeep = $maxReleaseRetentionDaysAllowed - $phase.ReleaseRetentionPolicy.Unit = $unitToUse - $phase.ReleaseRetentionPolicy.ShouldKeepForever = $false - } - if ($null -ne $phease.TentacleRetentionPolicy -and ($phase.TentacleRetentionPolicy.QuantityToKeep -gt $maxReleaseRetentionDaysAllowed -or $phase.TentacleRetentionPolicy.Unit -ne $unitToUse)) - { - Write-Host " Tentacle retention policy for $($phase.Name) is set to $($phase.TentacleRetentionPolicy.QuantityToKeep) $($phase.TentacleRetentionPolicy.Unit), updating to $maxReleaseRetentionDaysAllowed $unitToUse" - $changeLifecycle = $true - $phase.TentacleRetentionPolicy.QuantityToKeep = $maxReleaseRetentionDaysAllowed - $phase.TentacleRetentionPolicy.Unit = $unitToUse - $phase.TentacleRetentionPolicy.ShouldKeepForever = $false - } - } - - if ($changeLifecycle -eq $true) + if ($null -ne $phease.TentacleRetentionPolicy -and ($phase.TentacleRetentionPolicy.QuantityToKeep -gt $maxReleaseRetentionDaysAllowed -or $phase.TentacleRetentionPolicy.Unit -ne $unitToUse)) { - Write-Verbose " Invoking a PUT on $octopusURL/api/$($space.Id)/lifecycles/$($lifecycle.Id)" - Write-Verbose " $($lifecycle | ConvertTo-Json -Depth 10)" - $lifecycleResponse = Invoke-RestMethod -Method Put -Uri "$octopusURL/api/$($space.Id)/lifecycles/$($lifecycle.Id)" -Body ($lifecycle | ConvertTo-Json -Depth 10) -Headers $header + Write-Host " Tentacle retention policy for $($phase.Name) is set to $($phase.TentacleRetentionPolicy.QuantityToKeep) $($phase.TentacleRetentionPolicy.Unit), updating to $maxReleaseRetentionDaysAllowed $unitToUse" + $changeLifecycle = $true + $phase.TentacleRetentionPolicy.QuantityToKeep = $maxReleaseRetentionDaysAllowed + $phase.TentacleRetentionPolicy.Unit = $unitToUse + $phase.TentacleRetentionPolicy.ShouldKeepForever = $false } - } -} \ No newline at end of file + } + + if ($changeLifecycle -eq $true) + { + Write-Verbose " Invoking a PUT on $octopusURL/api/$($space.Id)/lifecycles/$($lifecycle.Id)" + Write-Verbose " $($lifecycle | ConvertTo-Json -Depth 10)" + $lifecycleResponse = Invoke-RestMethod -Method Put -Uri "$octopusURL/api/$($space.Id)/lifecycles/$($lifecycle.Id)" -Body ($lifecycle | ConvertTo-Json -Depth 10) -Headers $header + } +} \ No newline at end of file diff --git a/octopus-samples-instances/api-scripts/enforce-runbook-retention-policies.ps1 b/octopus-samples-instances/api-scripts/enforce-runbook-retention-policies.ps1 index e0cfa42..e7f882f 100644 --- a/octopus-samples-instances/api-scripts/enforce-runbook-retention-policies.ps1 +++ b/octopus-samples-instances/api-scripts/enforce-runbook-retention-policies.ps1 @@ -2,47 +2,45 @@ param ( $OctopusUrl, $OctopusApiKey, - $RunbookMaxRetentionRunPerEnvironment + $RunbookMaxRetentionRunPerEnvironment, + $spaceIdFilter ) $header = @{ "X-Octopus-ApiKey" = $octopusAPIKey } -$spaces = Invoke-RestMethod -Uri "$octopusURL/api/spaces/all" -Headers $header -Write-Output "Space Count: $($spaces.Length)" +$space = Invoke-RestMethod -Uri "$octopusURL/api/spaces/$spaceIdFilter" -Headers $header -foreach ($space in $spaces) { - Write-Verbose "Working on space $($space.Name)" +Write-Verbose "Working on space $($space.Name)" - try { - $projects = Invoke-RestMethod -Uri "$octopusURL/api/$($space.Id)/projects/all" -Headers $header - } - catch { - Write-Warning "$($_.Exception.ToString())" - continue; - } - Write-Output "Project Count: $($projects.Length)" +try { + $projects = Invoke-RestMethod -Uri "$octopusURL/api/$($space.Id)/projects/all" -Headers $header +} +catch { + Write-Warning "$($_.Exception.ToString())" + continue; +} +Write-Output "Project Count: $($projects.Length)" - foreach ($project in $projects) { - Write-Verbose "Working on project $($project.Name)" +foreach ($project in $projects) { + Write-Verbose "Working on project $($project.Name)" + + $projectRunbooks = Invoke-RestMethod -Uri "$octopusURL/api/$($space.Id)/projects/$($project.Id)/runbooks" -Headers $header + $runbooks = $projectRunbooks.Items + Write-Output "Runbook Count: $($runbooks.Length)" + + foreach ($runbook in $runbooks) { + Write-Verbose "Working on runbook $($runbook.Name)" + $currentRetentionQuantityToKeep = $runbook.RunRetentionPolicy.QuantityToKeep - $projectRunbooks = Invoke-RestMethod -Uri "$octopusURL/api/$($space.Id)/projects/$($project.Id)/runbooks" -Headers $header - $runbooks = $projectRunbooks.Items - Write-Output "Runbook Count: $($runbooks.Length)" - - foreach ($runbook in $runbooks) { - Write-Verbose "Working on runbook $($runbook.Name)" - $currentRetentionQuantityToKeep = $runbook.RunRetentionPolicy.QuantityToKeep - - if ($currentRetentionQuantityToKeep -gt $runbookMaxRetentionRunPerEnvironment) { - Write-Output "Runbook '$($runbook.Name)' ($($runbook.Id)) has a retention run policy to keep of: $($currentRetentionQuantityToKeep) which is greater than $($runbookMaxRetentionRunPerEnvironment)" - $runbook.RunRetentionPolicy.QuantityToKeep = $runbookMaxRetentionRunPerEnvironment - Write-Output "Updating runbook run quantity to keep for '$($runbook.Name)' ($($runbook.Id)) to $runbookMaxRetentionRunPerEnvironment" + if ($currentRetentionQuantityToKeep -gt $runbookMaxRetentionRunPerEnvironment) { + Write-Output "Runbook '$($runbook.Name)' ($($runbook.Id)) has a retention run policy to keep of: $($currentRetentionQuantityToKeep) which is greater than $($runbookMaxRetentionRunPerEnvironment)" + $runbook.RunRetentionPolicy.QuantityToKeep = $runbookMaxRetentionRunPerEnvironment + Write-Output "Updating runbook run quantity to keep for '$($runbook.Name)' ($($runbook.Id)) to $runbookMaxRetentionRunPerEnvironment" - $runbookResponse = Invoke-RestMethod -Method Put -Uri "$octopusURL/api/$($space.Id)/runbooks/$($runbook.Id)" -Body ($runbook | ConvertTo-Json -Depth 10) -Headers $header - if ($runbookResponse.RunRetentionPolicy.QuantityToKeep -ne $runbookMaxRetentionRunPerEnvironment) { - throw "Update for '$($runbook.Name)' ($($runbook.Id)) doesnt look like it worked. QtyToKeep is: $($runbookResponse.RunRetentionPolicy.QuantityToKeep)" - } + $runbookResponse = Invoke-RestMethod -Method Put -Uri "$octopusURL/api/$($space.Id)/runbooks/$($runbook.Id)" -Body ($runbook | ConvertTo-Json -Depth 10) -Headers $header + if ($runbookResponse.RunRetentionPolicy.QuantityToKeep -ne $runbookMaxRetentionRunPerEnvironment) { + throw "Update for '$($runbook.Name)' ($($runbook.Id)) doesnt look like it worked. QtyToKeep is: $($runbookResponse.RunRetentionPolicy.QuantityToKeep)" } } } -} +} \ No newline at end of file diff --git a/octopus-samples-instances/api-scripts/enforce-system-team-permissions.ps1 b/octopus-samples-instances/api-scripts/enforce-system-team-permissions.ps1 index 459cf05..981bb02 100644 --- a/octopus-samples-instances/api-scripts/enforce-system-team-permissions.ps1 +++ b/octopus-samples-instances/api-scripts/enforce-system-team-permissions.ps1 @@ -3,7 +3,8 @@ param $OctopusUrl, $OctopusApiKey, $RolesAllowedCsv, - $TeamName + $TeamName, + $OptionalSpaceId ) function Invoke-OctopusApi @@ -110,29 +111,37 @@ foreach ($team in $teamList.Items) $teamScopedUserRoles = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "teams/$($teamToUpdate.Id)/scopeduserroles?skip=0&take=1000" -spaceId $null -apiKey $OctopusApiKey -item $null -method "GET" $spaceIdList = @() -foreach ($userRole in $teamScopedUserRoles.Items) + +if ([string]::IsNullOrWhiteSpace($OptionalSpaceId) -eq $false -and $OptionalSpaceId.ToLower().Trim() -ne "all") +{ + $spaceIdList += $OptionalSpaceId +} +else { - if ($null -eq $userRole.SpaceId) + foreach ($userRole in $teamScopedUserRoles.Items) { - continue - } + if ($null -eq $userRole.SpaceId) + { + continue + } - if ($spaceIdList -notcontains $userRole.SpaceId) - { - Write-Verbose "Adding the space $($userRole.SpaceId) to the space list" - $spaceIdList += $userRole.SpaceId + if ($spaceIdList -notcontains $userRole.SpaceId) + { + Write-Verbose "Adding the space $($userRole.SpaceId) to the space list" + $spaceIdList += $userRole.SpaceId + } } -} -$spacesList = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "spaces?skip=0&take=1000" -spaceId $null -apiKey $OctopusApiKey -item $null -method "GET" -foreach ($space in $spacesList.Items) -{ - Write-Verbose "Checking to see if $($space.Id) is already in the list" - if ($spaceIdList -notcontains $space.Id) - { - Write-Verbose "Adding the space $($space.SpaceId) to the space list" - $spaceIdList += $space.Id - } + $spacesList = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "spaces?skip=0&take=1000" -spaceId $null -apiKey $OctopusApiKey -item $null -method "GET" + foreach ($space in $spacesList.Items) + { + Write-Verbose "Checking to see if $($space.Id) is already in the list" + if ($spaceIdList -notcontains $space.Id) + { + Write-Verbose "Adding the space $($space.SpaceId) to the space list" + $spaceIdList += $space.Id + } + } } foreach ($spaceId in $spaceIdList) diff --git a/octopus-samples-instances/api-scripts/manage-community-step-templates.ps1 b/octopus-samples-instances/api-scripts/manage-community-step-templates.ps1 index f9b00d7..32f561d 100644 --- a/octopus-samples-instances/api-scripts/manage-community-step-templates.ps1 +++ b/octopus-samples-instances/api-scripts/manage-community-step-templates.ps1 @@ -2,7 +2,8 @@ param ( $OctopusUrl, $OctopusApiKey, - $CommunityStepTemplatesCsv + $CommunityStepTemplatesCsv, + $SpaceIdFilter ) function Invoke-OctopusApi @@ -75,81 +76,79 @@ function Invoke-OctopusApi } $communityActionTemplatesList = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "communityactiontemplates?skip=0&take=1000" -spaceId $null -apiKey $OctopusApiKey -item $null -method "GET" -$spacesList = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "spaces?skip=0&take=1000" -spaceId $null -apiKey $OctopusApiKey -item $null -method "GET" +$space = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "spaces/$SpaceIdFilter" -spaceId $null -apiKey $OctopusApiKey -item $null -method "GET" $expectedCommunityStepTemplatesList = @($CommunityStepTemplatesCsv -split ",") -foreach ($space in $spacesList.Items) +Write-Verbose "Checking $($space.Name) for the expected pre-installed community step templates" + +foreach ($expectedCommunityStepTemplate in $expectedCommunityStepTemplatesList) { - Write-Verbose "Checking $($space.Name) for the expected pre-installed community step templates" - - foreach ($expectedCommunityStepTemplate in $expectedCommunityStepTemplatesList) + $installStepTemplate = $true + $stepTemplatesList = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "actiontemplates?skip=0&take=1000&partialName=$([uri]::EscapeDataString($expectedCommunityStepTemplate.Name))" -spaceId $space.Id -apiKey $OctopusApiKey -item $null -method "GET" + + foreach ($stepTemplate in $stepTemplatesList.Items) { - $installStepTemplate = $true - $stepTemplatesList = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "actiontemplates?skip=0&take=1000&partialName=$([uri]::EscapeDataString($expectedCommunityStepTemplate.Name))" -spaceId $space.Id -apiKey $OctopusApiKey -item $null -method "GET" + if ($null -eq $stepTemplate.CommunityActionTemplateId) + { + Write-Host "The step template $($stepTemplate.Name) is not a community step template, moving on." + continue + } - foreach ($stepTemplate in $stepTemplatesList.Items) + if ($stepTemplate.Name.ToLower().Trim() -eq $expectedCommunityStepTemplate.ToLower().Trim()) { - if ($null -eq $stepTemplate.CommunityActionTemplateId) - { - Write-Host "The step template $($stepTemplate.Name) is not a community step template, moving on." - continue - } + Write-Host "The step template $($stepTemplate.Name) matches $expectedCommunityStepTemplate. No need to install the step template." + + $communityActionTemplate = $communityActionTemplatesList.Items | Where-Object {$_.Id -eq $stepTemplate.CommunityActionTemplateId} - if ($stepTemplate.Name.ToLower().Trim() -eq $expectedCommunityStepTemplate.ToLower().Trim()) + if ($null -eq $communityActionTemplate) { - Write-Host "The step template $($stepTemplate.Name) matches $expectedCommunityStepTemplate. No need to install the step template." - - $communityActionTemplate = $communityActionTemplatesList.Items | Where-Object {$_.Id -eq $stepTemplate.CommunityActionTemplateId} - - if ($null -eq $communityActionTemplate) - { - Write-Host "Unable to find the community step template in the library, skipping the version check." - $installStepTemplate = $false - break - } - - if ($communityActionTemplate.Version -eq $stepTemplate.Version) - { - Write-Host "The step template $($stepTemplate.Name) is on version $($stepTemplate.Version) while the matching community template is on version $($communityActionTemplate.Version). The versions match. Leaving the step template alone." - $installStepTemplate = $false - } - else - { - Write-Host "The step template $($stepTemplate.Name) is on version $($stepTemplate.Version) while the matching community template is on version $($communityActionTemplate.Version). Updating the step template." - - $actionTemplate = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "communityactiontemplates/$($communityActionTemplate.Id)/installation/$($space.Id)" -spaceId $null -apiKey $OctopusApiKey -item $null -method "PUT" - Write-Host "Succesfully updated the step template. The version is now $($actionTemplate.Version)" - - $installStepTemplate = $false - } - + Write-Host "Unable to find the community step template in the library, skipping the version check." + $installStepTemplate = $false break } - } - if ($installStepTemplate -eq $true) - { - $communityActionTemplateToInstall = $null - foreach ($communityStepTemplate in $communityActionTemplatesList.Items) + if ($communityActionTemplate.Version -eq $stepTemplate.Version) { - if ($communityStepTemplate.Name.ToLower().Trim() -eq $expectedCommunityStepTemplate.ToLower().Trim()) - { - $communityActionTemplateToInstall = $communityStepTemplate - break - } + Write-Host "The step template $($stepTemplate.Name) is on version $($stepTemplate.Version) while the matching community template is on version $($communityActionTemplate.Version). The versions match. Leaving the step template alone." + $installStepTemplate = $false } + else + { + Write-Host "The step template $($stepTemplate.Name) is on version $($stepTemplate.Version) while the matching community template is on version $($communityActionTemplate.Version). Updating the step template." + + $actionTemplate = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "communityactiontemplates/$($communityActionTemplate.Id)/installation/$($space.Id)" -spaceId $null -apiKey $OctopusApiKey -item $null -method "PUT" + Write-Host "Succesfully updated the step template. The version is now $($actionTemplate.Version)" - if ($null -eq $communityActionTemplateToInstall) + $installStepTemplate = $false + } + + break + } + } + + if ($installStepTemplate -eq $true) + { + $communityActionTemplateToInstall = $null + foreach ($communityStepTemplate in $communityActionTemplatesList.Items) + { + if ($communityStepTemplate.Name.ToLower().Trim() -eq $expectedCommunityStepTemplate.ToLower().Trim()) { - Write-Host -Message "Unable to find $expectedCommunityStepTemplate. Please either re-sync the community library or check the names. Exiting." -ForegroundColor Red - exit 1 + $communityActionTemplateToInstall = $communityStepTemplate + break } + } - Write-Host "Installing the step template $expectedCommunityStepTemplate to $($space.Name)." - $actionTemplate = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "communityactiontemplates/$($communityActionTemplateToInstall.Id)/installation/$($space.Id)" -spaceId $null -apiKey $OctopusApiKey -item $null -method "POST" - Write-Host "Succesfully installed the step template. The Id of the new action template is $($actionTemplate.Id)" + if ($null -eq $communityActionTemplateToInstall) + { + Write-Host -Message "Unable to find $expectedCommunityStepTemplate. Please either re-sync the community library or check the names. Exiting." -ForegroundColor Red + exit 1 } + + Write-Host "Installing the step template $expectedCommunityStepTemplate to $($space.Name)." + $actionTemplate = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "communityactiontemplates/$($communityActionTemplateToInstall.Id)/installation/$($space.Id)" -spaceId $null -apiKey $OctopusApiKey -item $null -method "POST" + Write-Host "Succesfully installed the step template. The Id of the new action template is $($actionTemplate.Id)" } } + Write-Host "Finished verifying the community step templates have all been installed." \ No newline at end of file