Public/New-DuneResourceProvider.ps1

<#
.SYNOPSIS
Create a new resource provider.

.DESCRIPTION
Creates a new `DuneResourceProvider`. Provide a name, type, default environment, allowed environments, and optional display name, description and configuration. Observes `ShouldProcess`.

.PARAMETER Name
The unique name for the resource provider. Mandatory.

.PARAMETER Type
The provider type string. Mandatory.

.PARAMETER DefaultEnvironment
Default environment (from `Environments` enum) for new resources created by this provider. Mandatory.

.PARAMETER AllowedEnvironments
Array of allowed environments for this provider. Mandatory.

.PARAMETER DisplayName
Optional human-friendly display name.

.PARAMETER Description
Optional description text.

.PARAMETER Config
Array of PSCustomObject entries representing provider configuration.

.PARAMETER InitializeForDeployments
If true, the provider will be initialized for deployments after creation.

.EXAMPLE
PS> New-DuneResourceProvider -Name "cust-azure" -Type "azure" -DefaultEnvironment Prod -AllowedEnvironments Prod,Dev -DisplayName "Customer Azure"
Creates a new resource provider named `cust-azure`.
#>

function New-DuneResourceProvider {
    [CmdletBinding(
        SupportsShouldProcess,
        ConfirmImpact = 'None'
    )]
    param (
        [Parameter(Mandatory)]
        [string]$Name,

        [Parameter(Mandatory)]
        [string]$Type,

        [Parameter(Mandatory)]
        [Environments]$DefaultEnvironment,

        [Parameter(Mandatory)]
        [Environments[]]$AllowedEnvironments,

        [Parameter()]
        [string]$DisplayName,

        [Parameter()]
        [string]$Description,

        [Parameter()]
        [PSCustomObject[]]$Config,

        [Parameter()]
        [bool]$InitializeForDeployments = $true
    )

    begin {}

    process {
        Write-Debug "$($MyInvocation.MyCommand)|process"
        $Body = @{
            Name                     = $Name
            DisplayName              = $DisplayName
            Description              = $Description
            AllowedEnvironments      = $AllowedEnvironments | ConvertTo-EnumString
            Type                     = $Type
            Config                   = $Config
            InitializeForDeployments = $InitializeForDeployments
        }
        if ($PSBoundParameters.ContainsKey('DefaultEnvironment')) {
            $Body.DefaultEnvironment = $DefaultEnvironment.ToString()
        }
        if ($PSBoundParameters.ContainsKey('AllowedEnvironments')) {
            $Body.AllowedEnvironments = $AllowedEnvironments | ConvertTo-EnumString
        }
        if ($PSCmdlet.ShouldProcess($($Body | ConvertTo-Json -Depth 16 -Compress))) {
            $Return = Invoke-DuneApiRequest -Uri "resourceproviders" -Method POST -Body $Body
            $ReturnObject = if ($Return.Content) { $Return.Content | ConvertFrom-Json | ConvertTo-DuneClassObject -Class DuneResourceProvider }
            return $ReturnObject
        }
    }

    end {}
}