Public/New-DuneRole.ps1

<#
.SYNOPSIS
Create a new role.

.DESCRIPTION
POSTs a new role to the authorization service. A role has a name, the config item type it applies to, and a set of privileges. Returns the created `DuneRole` object.

.PARAMETER Name
The name of the role. Required.

.PARAMETER AppliesTo
The type of config item the role can be granted on (`Tenant`, `Collection`, `Deployment`, `ResourceGroup`, `Resource`). Required.

.PARAMETER Privileges
One or more privileges the role provides. Required.

.PARAMETER Description
An optional description for the role.

.EXAMPLE
PS> New-DuneRole -Name "Auditor" -AppliesTo Deployment -Privileges Read
Creates a read-only role for deployments.

.EXAMPLE
PS> New-DuneRole -Name "Maintainer" -AppliesTo ResourceGroup -Privileges Read,Edit,Operate -Description "Day-2 operations"
Creates a role with multiple privileges and a description.
#>

function New-DuneRole {
    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory, Position = 0)]
        [string]$Name,

        [Parameter(Mandatory)]
        [ValidateSet('Tenant','Collection','Deployment','ResourceGroup','Resource')]
        [string]$AppliesTo,

        [Parameter(Mandatory)]
        [ValidateSet('Read','CreateChild','Operate','Edit','EditPermissions','EditTags','EditTemplate','CreateTemplate','Deploy','Move','Delete','DeleteTemplate','ReadInventory','CreateVulnerability','DeleteVulnerability','CreateDataPoint')]
        [string[]]$Privileges,

        [Parameter()]
        [string]$Description
    )

    begin {}

    process {
        Write-Debug "$($MyInvocation.MyCommand)|process"

        $Body = @{
            Name       = $Name
            AppliesTo  = $AppliesTo
            Privileges = $Privileges
        }
        if ($PSBoundParameters.ContainsKey('Description')) { $Body.Description = $Description }

        if ($PSCmdlet.ShouldProcess($Name, "Create Role")) {
            $Return = Invoke-DuneApiRequest -Uri 'authorization/roles' -Method POST -Body $Body -ErrorAction Stop
            $ReturnObject = if ($Return.Content) { $Return.Content | ConvertFrom-Json | ConvertTo-DuneClassObject -Class DuneRole }
            return $ReturnObject
        }
    }

    end {}
}