Public/ExampleData/Set-JIMExampleDataTemplate.ps1

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

function Set-JIMExampleDataTemplate {
    <#
    .SYNOPSIS
        Updates a Data Generation Template in JIM.
 
    .DESCRIPTION
        Renames a Data Generation Template and/or replaces its Object Type configuration.
        Built-in Data Generation Templates cannot be updated.
 
    .PARAMETER Id
        The Data Generation Template to update.
 
    .PARAMETER Name
        The name of the Data Generation Template to update.
 
    .PARAMETER NewName
        The new name.
 
    .PARAMETER ObjectType
        Replaces the template's whole Object Type graph: every Object Type and attribute configuration not in these
        hashtables is removed. Add-JIMExampleDataTemplateAttribute adds one attribute without restating the rest.
        The hashtables take the same shape as New-JIMExampleDataTemplate -ObjectType.
 
    .PARAMETER ChangeReason
        A reason for the change, recorded on its Activity and shown in the Data Generation Template's Configuration
        Change History.
 
    .PARAMETER PassThru
        Returns the updated Data Generation Template.
 
    .OUTPUTS
        With -PassThru, the updated Data Generation Template; otherwise nothing.
 
    .EXAMPLE
        Set-JIMExampleDataTemplate -Id 7 -NewName "Demo Users v2"
 
        Renames the Data Generation Template.
 
    .EXAMPLE
        Set-JIMExampleDataTemplate -Name "Demo Users" -ObjectType @{ MetaverseObjectType = "User"; ObjectsToCreate = 1000 } -PassThru
 
        Replaces the template's entire Object Type graph with a single Object Type configuration.
 
    .EXAMPLE
        Set-JIMExampleDataTemplate -Id 7 -NewName "Demo Users v2" -ChangeReason "Renaming for clarity (CHG0201)"
 
        Renames the template and records the reason on its configuration change history.
 
    .LINK
        Get-JIMExampleDataTemplate
        New-JIMExampleDataTemplate
        Remove-JIMExampleDataTemplate
        Add-JIMExampleDataTemplateAttribute
    #>

    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium', DefaultParameterSetName = 'ById')]
    [OutputType('JIM.ExampleDataTemplateDto')]
    param(
        [Parameter(Mandatory, ParameterSetName = 'ById', ValueFromPipelineByPropertyName)]
        [int]$Id,

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

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]$NewName,

        [Parameter()]
        [hashtable[]]$ObjectType,

        [ValidateNotNullOrEmpty()]
        [string]$ChangeReason,

        [switch]$PassThru
    )

    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
        }

        # Only send what the caller named. An omitted setting must reach the API as absent rather than
        # as a default, or every call would silently rewrite settings it was never asked about.
        if (-not $PSBoundParameters.ContainsKey('NewName') -and -not $PSBoundParameters.ContainsKey('ObjectType')) {
            Write-Error "No settings were supplied to change. Supply at least one of -NewName or -ObjectType."
            return
        }

        # Resolve name to ID if using ByName parameter set
        $displayName = if ($PSCmdlet.ParameterSetName -eq 'ByName') { $Name } else { $Id }
        if ($PSCmdlet.ParameterSetName -eq 'ByName') {
            try {
                $resolvedTemplate = Resolve-JIMExampleDataTemplate -Name $Name
                $Id = $resolvedTemplate.id
            }
            catch {
                Write-Error $_
                return
            }
        }

        if ($PSCmdlet.ShouldProcess($displayName, "Update Data Generation Template")) {
            Write-Verbose "Updating Data Generation Template: $Id"

            try {
                $body = @{}
                if ($PSBoundParameters.ContainsKey('NewName')) {
                    $body.name = $NewName
                }
                if ($PSBoundParameters.ContainsKey('ObjectType')) {
                    $body.objectTypes = @(ConvertTo-JIMExampleDataTemplateObjectTypes -ObjectType $ObjectType)
                }
                if ($ChangeReason) {
                    $body.changeReason = $ChangeReason
                }

                $response = Invoke-JIMApi -Endpoint "/api/v1/example-data/templates/$Id" -Method 'PUT' -Body $body
                Write-Verbose "Updated Data Generation Template: $Id"

                # What the save said about the template's expressions without refusing it (#2055 Phase 3): each
                # written as the server phrased it.
                foreach ($saveWarning in @($response.Warnings | Where-Object { $_ })) {
                    Write-Warning $saveWarning
                }

                if ($PassThru) {
                    $response
                }
            }
            catch {
                Write-Error "Failed to update Data Generation Template: $_"
            }
        }
    }
}