Public/ExampleData/New-JIMExampleDataTemplate.ps1

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

function New-JIMExampleDataTemplate {
    <#
    .SYNOPSIS
        Creates a new Data Generation Template in JIM.
 
    .DESCRIPTION
        Creates a new Data Generation Template: a definition of which Metaverse Object Types to generate
        test objects for, how many of each, and how each attribute's values are generated (patterns,
        expressions, Example Data Sets, weighted values, number/date ranges and reference assignment).
 
    .PARAMETER Name
        The name for the Data Generation Template.
 
    .PARAMETER ObjectType
        One hashtable per Object Type the template generates objects for. Each takes MetaverseObjectType (a
        Metaverse Object Type name or id; required), ObjectsToCreate (how many objects to generate; 1 if omitted)
        and Attributes, an optional array of attribute generation hashtables using the API's request field names
        (Pattern, Expression, PopulatedValuesPercentage, MinNumber, MaxNumber, MinDate, MaxDate,
        BoolTrueDistribution, SequentialNumbers, RandomNumbers, WeightedStringValues,
        ManagerDepthPercentage, MvaRefMinAssignments, MvaRefMaxAssignments, ReferenceMetaverseObjectTypeIds,
        AttributeDependency), except that MetaverseAttribute may be a
        Metaverse Attribute name or id, and ExampleDataSets entries may be Example Data Set names or ids, in order.
        Unknown keys throw, so a typo is caught before anything is sent.
 
    .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 created Data Generation Template.
 
    .OUTPUTS
        With -PassThru, the created Data Generation Template; otherwise nothing.
 
    .EXAMPLE
        New-JIMExampleDataTemplate -Name "Demo Users" -ObjectType @{
            MetaverseObjectType = "User"
            ObjectsToCreate = 500
            Attributes = @(
                @{ MetaverseAttribute = "Display Name"; Pattern = "{0} {1}"; ExampleDataSets = @("Firstnames Female", "Lastnames") }
            )
        }
 
        Creates a template generating 500 Users with pattern-based Display Names.
 
    .EXAMPLE
        New-JIMExampleDataTemplate -Name "Demo Users" -ObjectType @{ MetaverseObjectType = 1; ObjectsToCreate = 100 } -ChangeReason "Seeding demo data (CHG0200)" -PassThru
 
        Creates the template using a Metaverse Object Type id and records the reason on its configuration change history.
 
    .LINK
        Get-JIMExampleDataTemplate
        Set-JIMExampleDataTemplate
        Remove-JIMExampleDataTemplate
        Add-JIMExampleDataTemplateAttribute
        Invoke-JIMExampleDataTemplate
    #>

    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
    [OutputType('JIM.ExampleDataTemplateDto')]
    param(
        [Parameter(Mandatory, Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string]$Name,

        [Parameter(Mandatory)]
        [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
        }

        if ($PSCmdlet.ShouldProcess($Name, "Create Data Generation Template")) {
            Write-Verbose "Creating Data Generation Template: $Name"

            try {
                $body = @{
                    name        = $Name
                    objectTypes = @(ConvertTo-JIMExampleDataTemplateObjectTypes -ObjectType $ObjectType)
                }
                if ($ChangeReason) {
                    $body.changeReason = $ChangeReason
                }

                $response = Invoke-JIMApi -Endpoint "/api/v1/example-data/templates" -Method 'POST' -Body $body
                Write-Verbose "Created Data Generation Template: $($response.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 create Data Generation Template: $_"
            }
        }
    }
}