OctopusMachines.psm1

Import-Module PowershellOctopusClientPS
(Get-Module -ListAvailable PowershellOctopusClientPS).ModuleBase

try
{
    $octoclientpath = Join-Path -Path (Get-Module -ListAvailable PowershellOctopusClientPS).ModuleBase -ChildPath "lib\Octopus.Client.dll"
    $newtonsoftPath = Join-Path -Path (Get-Module -ListAvailable PowershellOctopusClientPS).ModuleBase -ChildPath "lib\Newtonsoft.Json.dll"
    Add-Type -Path $newtonsoftPath
    Add-Type -Path $octoclientpath
}
catch [System.Reflection.ReflectionTypeLoadException]
{    #system.attonations always throws
    #Write-Host "Message: $($_.Exception.Message)"
    #Write-Host "StackTrace: $($_.Exception.StackTrace)"
    #Write-Host "LoaderExceptions: $($_.Exception.LoaderExceptions)"
}
<#
    .SYNOPSIS
    return a setup client
#>

function LoadOctopusClientAndDependancies {
[OutputType([Octopus.Client.OctopusClient])]
    Param(
        [parameter(Mandatory=$true)][String] $url,
        [parameter(Mandatory=$true)][String] $apiKey
    )
    
    $client = [Octopus.Client.OctopusClient]::new([Octopus.Client.OctopusServerEndpoint]::new($url, $apiKey))
    $client
}

function CreateCloudRegion {
    Param(
        [parameter(Mandatory=$true)][String] $octopusURL,
        [parameter(Mandatory=$true)][String] $octopusAPIKey,
        [parameter(Mandatory=$true)][String] $Name,
        [parameter(Mandatory=$true)][String] $Space,
        [parameter(Mandatory=$true)][String] $Enviroment
    )
    <#
        $newWorkerPool = [Octopus.Client.Model.WorkerPoolResource]::new()
        if($createWorkerPool -eq $true){
            $newWorkerPool.Name = $enviromentName
            $newWorkerPool.IsDefault = $false
            $newWorkerPool.Description = "$enviromentName enviroment worker pool"
            $newWorkerPool = $repositoryForSpace.WorkerPools.Create($newWorkerPool)
            write-output "Added worker pool $enviromentName to space $currSpace with id $($newWorkerPool.Id)"
        }
        
    #>

}

function CreateWorkerPool {
    Param(
        [parameter(Mandatory=$true)][String] $octopusURL,
        [parameter(Mandatory=$true)][String] $octopusAPIKey,
        [parameter(Mandatory=$true)][String] $Name,
        [parameter(Mandatory=$true)][String] $Space,
        [parameter(Mandatory=$true)][String] $Enviroment
    )

    $client = LoadOctopusClientAndDependancies -url $octopusURL -apiKey $octopusAPIKey   
    $repository = $client.ForSystem()
    $spaces = $null

    if([string]::IsNullOrEmpty($Space)) {        
        $spaces = $repository.Spaces.GetAll()        
    }else{
        $spaces = $repository.Spaces.FindByName($Space)        
    }
    
    $spaces | foreach {
        $currSpace = $_
        $repositoryForSpace = $client.ForSpace($currSpace)
        $newWorkerPool = [Octopus.Client.Model.WorkerPoolResource]::new()
        $newWorkerPool.Name = $Enviroment
        $newWorkerPool.IsDefault = $false
        $newWorkerPool.Description = "$Enviroment enviroment worker pool"
        $repositoryForSpace.WorkerPools.Create($newWorkerPool)
        write-output "Added worker pool $Enviroment to space $($currSpace.Name) with id $($newWorkerPool.Id)"
    }
}