Public/ExampleData/Remove-JIMExampleDataSet.ps1

# Copyright (c) Tetron Limited. All rights reserved.
# Licensed under the Tetron Commercial License. See LICENSE file in the project root.

function Remove-JIMExampleDataSet {
    <#
    .SYNOPSIS
        Removes an Example Data Set from JIM.
 
    .DESCRIPTION
        Deletes an Example Data Set. Built-in Example Data Sets cannot be removed.
        This action cannot be undone.
 
    .PARAMETER Id
        The Example Data Set to delete.
 
    .PARAMETER ChangeReason
        A reason for the change, recorded on its Activity and shown in the Example Data Set's Configuration Change
        History.
 
    .PARAMETER Force
        Suppresses the confirmation prompt.
 
    .OUTPUTS
        None.
 
    .EXAMPLE
        Remove-JIMExampleDataSet -Id 5
 
        Removes the specified Example Data Set (with confirmation).
 
    .EXAMPLE
        Remove-JIMExampleDataSet -Id 5 -Force
 
        Removes the specified Example Data Set without confirmation.
 
    .EXAMPLE
        Remove-JIMExampleDataSet -Id 5 -Force -ChangeReason "Retiring obsolete set (CHG0102)"
 
        Removes the Example Data Set and records the reason on its configuration change history.
 
    .LINK
        Get-JIMExampleDataSet
        New-JIMExampleDataSet
        Set-JIMExampleDataSet
    #>

    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]
    [OutputType([void])]
    param(
        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [int]$Id,

        [ValidateNotNullOrEmpty()]
        [string]$ChangeReason,

        [switch]$Force
    )

    process {
        # Check connection first
        if (-not $script:JIMConnection) {
            Write-Error "You are not connected to JIM. Run Connect-JIM -Url <your JIM URL> to authenticate, then try again."
            return
        }

        # Get data set name for confirmation message
        $dataSetName = $Id
        try {
            $dataSet = Invoke-JIMApi -Endpoint "/api/v1/example-data/example-data-sets/$Id"
            $dataSetName = $dataSet.name
        }
        catch {
            # Continue with ID if we can't get the name
        }

        if (Confirm-JIMAction -Cmdlet $PSCmdlet -Target $dataSetName -Action "Remove Example Data Set" -Force:$Force) {
            Write-Verbose "Removing Example Data Set: $Id ($dataSetName)"

            try {
                $endpoint = "/api/v1/example-data/example-data-sets/$Id"
                if ($ChangeReason) {
                    $endpoint += "?changeReason=$([uri]::EscapeDataString($ChangeReason))"
                }
                Invoke-JIMApi -Endpoint $endpoint -Method 'DELETE'
                Write-Verbose "Removed Example Data Set: $Id"
            }
            catch {
                Write-Error "Failed to remove Example Data Set: $_"
            }
        }
    }
}