Manage SquaredUp Workspaces with PowerShell

While I was tidying up a mess of unsorted scripts I’ve used for miscellaneous SquaredUp Cloud testing, troubleshooting, and playing around I found a couple functions that can be used to create, list, and delete workspaces using your API key.

I’ll work on getting things better organized, and hope to be getting some functions added to Dashboards and Scopes soon too.

function New-Workspace {
    param (
        [Parameter(Mandatory = $true)]
        [string]$apiKey,
        [Parameter(Mandatory = $true)]
        [string]$name,
        [Parameter(Mandatory = $false)]
        [string]$region = 'us',
        [Parameter(Mandatory = $false)]
        [string[]]$pluginLinks = @(),
        [Parameter(Mandatory = $false)]
        [string[]]$workspaceLinks = @(),
        [Parameter(Mandatory = $false)]
        [string]$description,
        [Parameter(Mandatory = $false)]
        [string[]]$tags = @(),
        [Parameter(Mandatory = $false)]
        [bool]$openAccess = $false,
        [Parameter(Mandatory = $false)]
        [string]$type = "other"      
    )

    $workspace = @{
        displayName      = $name
        links            = @{
            plugins    = $pluginLinks
            workspaces = $workspaceLinks
        }
        linkToWorkspaces = $workspaceLinks.Count -gt 0 ? $true : $false        
        acl              = $null
        properties       = @{
            description       = $description
            openAccessEnabled = $openAccess
            type              = $type
            tags              = $tags ?? @()
        }
    }

    $apiUrl = $region -eq 'eu' ? 'https://eu.api.squaredup.com' : 'https://api.squaredup.com'

    $payload = $workspace | ConvertTo-Json
    try {
        $workspaceResult = Invoke-RestMethod -Uri "$apiUrl/api/workspaces" -Method Post -Headers @{ 'apiKey' = $apiKey } -Body $payload -ContentType 'application/json'
        return $workspaceResult
    }
    catch {
        Write-Error "Failed to create workspace with name $name"
        Write-Error $_
    }
    
}

function Get-Workspace {
    param (
        [Parameter(Mandatory = $true)]
        [string]$apiKey,
        [Parameter(Mandatory = $false)]
        [string]$workspaceId = "",
        [Parameter(Mandatory = $false)]
        [string]$region = 'us'
    )
    
    $apiUrl = $region -eq 'eu' ? 'https://eu.api.squaredup.com' : 'https://api.squaredup.com'

    try {
        $workspaceResult = Invoke-RestMethod -Uri "$apiUrl/api/workspaces/$workspaceId" -Method Get -Headers @{ 'apiKey' = $apiKey } -ContentType 'application/json'
        return $workspaceResult
    }
    catch {
        Write-Error "Failed to get workspace with id $workspaceId"
        Write-Error $_
    }
}

function Remove-Workspace {
    param (
        [Parameter(Mandatory = $true)]
        [string]$apiKey,
        [Parameter(Mandatory = $true)]
        [string]$workspaceId,
        [Parameter(Mandatory = $false)]
        [string]$region = 'us'
    )
    
    $apiUrl = $region -eq 'eu' ? 'https://eu.api.squaredup.com' : 'https://api.squaredup.com'

    try {
        Invoke-RestMethod -Uri "$apiUrl/api/workspaces/$workspaceId" -Method Delete -Headers @{ 'apiKey' = $apiKey } -ContentType 'application/json'
        Write-Information "Workspace with id $workspaceId has been deleted"
    }
    catch {
        Write-Error "Failed to delete workspace with id $workspaceId"
        Write-Error $_
    }
}

Let me know if you’ve got any questions, comments, or suggestions.

The API is available in the Enterprise tier

2 Likes