Public/Settings.ps1

function New-TMSetting {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [string]$Name,

        [Parameter(Mandatory)]
        [psobject]$Value,

        [psobject]$TMSession = 'Default',
        [switch]$Passthru
    )

    begin {
        $TMSession = Get-TMSession $TMSession

        $bodyParams = @{
            category = 'SCRIPT_SETTING'
            key      = $Name
            type     = 'text'
            value    = $Value
        }
    }

    process {
        try {
            $response = Invoke-TMRestMethod -Api setting -Method Post -BodyParams $bodyParams
            return $Passthru.IsPresent ? [TMSetting]::new($response) : $null
        } catch {
            if ($_.Exception.Message -match '.*Response: A duplicate key was encountered$') {
                throw "Setting '$Name' already exists"
            }
        }
    }
}
function Set-TMSetting {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $false, Position = 0)]
        [PSObject]$TMSession = 'Default',

        [Parameter(ParameterSetName = "ByValue", Mandatory, Position = 0)]
        [TMSetting] $TMSetting,

        [Parameter(ParameterSetName = "ByProperty", Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [Object]$Id,

        [Parameter(ParameterSetName = "ByProperty", Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias('Key')]
        [String]$Name,

        [Parameter(ParameterSetName = "ByProperty", Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
        [AllowNull()]
        [Alias('Project')]
        [Object]$ProjectId,

        [ArgumentCompleter({ [TMSetting]::ValidCategories })]
        [ValidateScript( { $_ -in [TMSetting]::ValidCategories } )]
        [Parameter(ParameterSetName = "ByProperty", Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [String]$Category = 'SCRIPT_SETTING',

        [Parameter(ParameterSetName = "ByProperty", Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [Object]$Value,

        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
        [ValidateSet('json', 'text')]
        [String]$Format = 'json',

        [Parameter(Mandatory = $false)]
        [Switch]$Passthru
    )

    begin {

        if ($PSCmdlet.ParameterSetName -eq "ByValue") {
            $Id = $TMSetting.Id
            $ProjectId = $TMSetting.Project
            $Category = $TMSetting.Category
            $Name = $TMSetting.Key
            $Value = $TMSetting.Value
        }

        # Get the session configuration
        Write-Verbose "Checking for cached TMSession"
        $TMSession = Get-TMSession $TMSession
        Write-Debug "TMSession:"
        Write-Debug ($TMSession | ConvertTo-Json -Depth 5)
        $bodyParams = @{
            id       = $Id
            category = $Category
            key      = $Name
            type     = $Value -is [String] ? 'text' : 'json'
            value    = $Value
        }

        if ($ProjectId -is [TMReference]) {
            $ProjectId = $ProjectId.Id
        } else {
            $ProjectId = $TMSession.Project.Id
        }
    }

    process {
        $Response = Invoke-TMRestMethod -Api "setting/$Id.$Format" -ApiParams "project=$ProjectId" -BodyParams $bodyParams -Method Put
        if ($Passthru) {
            [TMSetting]::new($Response)
        }
    }
}


function Get-TMSetting {
    [CmdletBinding()]
    param(
        [Parameter()]
        [psobject]$TMSession = 'Default',

        [Parameter(
            ParameterSetName = 'ById',
            Mandatory,
            ValueFromPipelineByPropertyName
        )]
        [int]$Id,

        [Parameter(
            ParameterSetName = 'ByName',
            Mandatory,
            ValueFromPipelineByPropertyName
        )]
        [string]$Name
    )

    begin {
        $TMSession = Get-TMSession $TMSession
        switch ($PSCmdlet.ParameterSetName) {
            ById {
                $api = 'setting/{0}' -f $Id
            }
            ByName { 
                $api = 'setting/SCRIPT_SETTING/{0}.json' -f $Name.ToUpper()
            }
        }
        $uri = 'https://{0}/tdstm/api/{1}?project={2}' -f $TMSession.TMServer, $Api, $TMSession.UserContext.Project.Id
    }

    process {
        # cannot use invoke-tmrestmethod as this endpoint does not like
        # having a project passed in the body of the request
        try {
            $response = Invoke-RestMethod -Uri $uri -WebSession $TMSession.TMRestSession -Method Get
        } catch {
            if ($_.ErrorDetails.Message -match 'Requested information was not found') {
                switch ($PSCmdlet.ParameterSetName) {
                    'ById' { throw "Setting with Id '$Id' was not found" }
                    'ByName' { throw "Setting '$Name' was not found" }
                }
            }

            throw "Error getting TM Setting: $_"
        }
        return [TMSetting]::new($response)
    }
}

function Remove-TMSetting {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $false, Position = 0)]
        [PSObject]$TMSession = 'Default',

        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [Object]$Id,

        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias('Key')]
        [String]$Name,

        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
        [AllowNull()]
        [Alias('Project')]
        [Object]$ProjectId,

        [ArgumentCompleter({ [TMSetting]::ValidCategories })]
        [ValidateScript( { $_ -in [TMSetting]::ValidCategories } )]
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [String]$Category,

        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [Object]$Value

    )

    begin {
        # Get the session configuration
        Write-Verbose "Checking for cached TMSession"
        $TMSession = Get-TMSession $TMSession
        Write-Debug 'TMSession:'
        Write-Debug ($TMSession | ConvertTo-Json -Depth 5)
        $bodyParams = @{
            id       = $Id
            category = $Category
            key      = $Name
            type     = $Value -is [string] ? 'text' : 'json'
            value    = $Value
        }
    }

    process {
        if ($ProjectId -is [TMReference]) {
            $ProjectId = $ProjectId.Id
        } else {
            $ProjectId = $TMSession.Project.Id
        }

        $Response = Invoke-TMRestMethod -Api setting/$Id -Method Delete -BodyParams $bodyParams
        if ($Response.deleted -eq 1) {
            return
        }
    }
}