Public/ExampleData/Get-JIMExampleDataTemplate.ps1

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

function Get-JIMExampleDataTemplate {
    <#
    .SYNOPSIS
        Gets data generation templates from JIM.
 
    .DESCRIPTION
        Retrieves data generation templates that define how test data should be
        generated, including which object types to create and how many instances
        of each.
 
    .PARAMETER Id
        The Data Generation Template to get, in full.
 
    .PARAMETER Name
        The name of the Data Generation Template to get, in full.
 
    .PARAMETER Page
        The page of Data Generation Templates to return.
 
    .PARAMETER PageSize
        How many Data Generation Templates per page.
 
    .OUTPUTS
        A list returns each Data Generation Template's header; -Id and -Name return the template in full, with its
        Object Types and their attribute configuration.
 
    .EXAMPLE
        Get-JIMExampleDataTemplate
 
        Gets all data generation templates.
 
    .EXAMPLE
        Get-JIMExampleDataTemplate -Id 1
 
        Gets a specific template by ID.
 
    .EXAMPLE
        Get-JIMExampleDataTemplate -Name 'Test Users'
 
        Gets a specific template by name.
 
    .EXAMPLE
        Get-JIMExampleDataTemplate | Select-Object Id, Name, ObjectTypeCount
 
        Gets all templates with specific properties.
 
    .LINK
        Get-JIMExampleDataSet
        Invoke-JIMExampleDataTemplate
    #>

    [CmdletBinding(DefaultParameterSetName = 'List')]
    [OutputType('JIM.ExampleDataTemplateHeader', ParameterSetName = 'List')]
    [OutputType('JIM.ExampleDataTemplateDto', ParameterSetName = 'ById')]
    [OutputType('JIM.ExampleDataTemplateDto', ParameterSetName = 'ByName')]
    param(
        [Parameter(Mandatory, ParameterSetName = 'ById', ValueFromPipelineByPropertyName)]
        [int]$Id,

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

        [Parameter(ParameterSetName = 'List')]
        [ValidateRange(1, [int]::MaxValue)]
        [int]$Page = 1,

        [Parameter(ParameterSetName = 'List')]
        [ValidateRange(1, 1000)]
        [int]$PageSize = 100
    )

    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
        }

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

        switch ($PSCmdlet.ParameterSetName) {
            { $_ -in 'ById', 'ByName' } {
                Write-Verbose "Getting data generation template with ID: $Id"
                $result = Invoke-JIMApi -Endpoint "/api/v1/example-data/templates/$Id"
                $result
            }

            'List' {
                Write-Verbose "Getting all data generation templates"

                $queryParams = @(
                    "page=$Page",
                    "pageSize=$PageSize"
                )
                $queryString = $queryParams -join '&'

                $response = Invoke-JIMApi -Endpoint "/api/v1/example-data/templates?$queryString"

                # Handle paginated response
                $templates = if ($response.items) { $response.items } else { $response }

                # Output each template individually for pipeline support
                foreach ($template in $templates) {
                    $template
                }
            }
        }
    }
}