src/Public/azuredevops.ps1


function Get-RedkiteAzDevOpsLibrary{
<#
.Description
Get a complete library from a Azure DevOps
#>

    [CmdletBinding()]
    param (
        [Parameter()]
        [string]
        $LibraryName
    )
    $AllLibraries = invokeDevOpsAPI "distributedtask/variablegroups/?groupName=$LibraryName&api-version=6.0-preview.2" 'GET'
    $Result = @{}
    foreach ($p in ($AllLibraries.value.Variables).PSObject.Properties){
        $Result[$p.Name] = $p.Value.Value
    }
    return $Result
}


function Get-RedkiteAzDevOpsLibraryKey{
<#
.Description
Get a single key from a Azure DevOps Library
#>

    [CmdletBinding()]
    param (
        [Parameter()] [string] $LibraryName,
        [Parameter()] [string] $KeyName
    )
    $AllKeys = Get-RedkiteAzDevOpsLibrary -LibraryName $LibraryName
    return $AllKeys[$KeyName]
}


function Set-RedkiteAzDevOpsLibraryKey{
<#
.Description
Add/Update a Key in a Azure DevOps Library
#>

    [CmdletBinding()]
    param (
        [Parameter()] [string] $LibraryName,
        [Parameter()] [string] $KeyName,
        [Parameter()] [string] $Value
    )

    $Existing = invokeDevOpsAPI "distributedtask/variablegroups/?groupName=$LibraryName&api-version=6.0-preview.2" 'GET'
    if (!($Existing)){
        throw "Library $LibraryName not found"
    }
    $GroupID = $Existing.value.id

    $NewObj = $Existing.value | Select-Object -Property * -ExcludeProperty isShared, variableGroupProjectReferences, id, createdBy, createdOn, modifiedOn, modifiedBy

    $NewKeyObjValue = New-Object pscustomobject
    $NewKeyObjValue | Add-Member NoteProperty "value" $Value

    $DoesExist = [bool]($NewObj.Variables.PSobject.Properties.name -match $KeyName)
    if ($DoesExist){
        $NewObj.Variables."$KeyName" = $NewKeyObjValue
    }
    else{
        $NewObj.Variables | Add-Member NoteProperty $KeyName $NewKeyObjValue
    }

    $Body = $NewObj | ConvertTo-Json -Depth 10
    invokeDevOpsAPIPut "distributedtask/variablegroups/$($GroupID)?api-version=6.0-preview.1" $Body

}