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=$false)][String]    $Enviroment,
        [parameter(Mandatory=$false)][String]    $Space = '',
        [parameter(Mandatory=$false)][String]   $WorkerPool = '',
        [parameter(Mandatory=$false)][String[]] $Roles = $null,
        [parameter(Mandatory=$false)][String[]] $Tenants = $null,
        [parameter(Mandatory=$false)][String[]] $TenantTags = $null
    )
    $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)
        $enviromentResource = $repositoryForSpace.Environments.FindByName($Enviroment)
        $endpoint = [Octopus.Client.Model.Endpoints.CloudRegionEndpointResource]::new()

        if(-not([String]::IsNullOrEmpty($WorkerPool))) {
            $wpoolResource = $repositoryForSpace.WorkerPools.FindByName($WorkerPool)
            $endpoint.DefaultWorkerPoolId = $wpoolResource.Id
        }
        $tenantResources = @()
        if(-not($Tenants -eq $null)) {
            $Tenants | foreach {            
                $tenantResources += $repositoryForSpace.Tenants.FindByName($_)
            }
        }    
        $tenantTagResources = @()
        if(-not($TenantTags -eq $null)){
            $TenantTags | foreach {
                $item = $_.split("/")            
                $repositoryForSpace.TagSets.FindByName($item[0]) | foreach {
                    $_.Tags | foreach{
                        if($_.Name -eq $item[1]){               
                            $tenantTagResources += $_
                        }
                    }
                }
            }
        }
        try{
            $repositoryForSpace.Machines.CreateOrModify($Name,$endpoint,$enviromentResource,$Roles,$tenantResources,$tenantTagResources,[Octopus.Client.Model.TenantedDeploymentMode]::TenantedOrUntenanted).Instance
            Write-Output "Added machine for cloud region $Name"
        }catch {
            Write-Error $_
        }
    }
}

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

    $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 = $Name
        $newWorkerPool.IsDefault = $false
        $newWorkerPool.Description = "$Enviroment enviroment worker pool"
        try{
            $repositoryForSpace.WorkerPools.Create($newWorkerPool)
            write-output "Added worker pool $Name to space $($currSpace.Name)"
        }catch {
            Write-Error $_
        }
    }
}