Public/ConnectedSystems/Set-JIMConnectedSystemObjectType.ps1

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

function Set-JIMConnectedSystemObjectType {
    <#
    .SYNOPSIS
        Updates properties of a Connected System Object Type in JIM.
 
    .DESCRIPTION
        Updates properties of an object type within a Connected System's schema.
        Use this to mark object types as selected for management by JIM.
 
    .PARAMETER ConnectedSystemId
        The unique identifier of the Connected System.
 
    .PARAMETER ObjectTypeId
        The unique identifier of the Object Type to update.
 
    .PARAMETER Selected
        Whether the object type should be managed by JIM.
        When set to $true, JIM will synchronise objects of this type.
        When set to $false, the object type is taken out of management: the next
        Full Import no longer returns its objects, so it marks them as deleted, and
        the following synchronisation disconnects them from their Metaverse Objects
        and applies the Metaverse Object deletion rules, exactly as deselecting a
        Partition does.
        Deselecting is refused while an enabled Synchronisation Rule is bound to the
        object type; disable those Synchronisation Rules first.
 
    .PARAMETER RemoveContributedAttributesOnObsoletion
        Whether to remove contributed attributes from the Metaverse object
        when a Connected System object is obsoleted.
 
    .PARAMETER PassThru
        If specified, returns the updated object type.
 
    .OUTPUTS
        If -PassThru is specified, returns the updated Object Type object.
 
    .EXAMPLE
        Set-JIMConnectedSystemObjectType -ConnectedSystemId 1 -ObjectTypeId 5 -Selected $true
 
        Marks the object type as selected for management by JIM.
 
    .EXAMPLE
        Set-JIMConnectedSystemObjectType -ConnectedSystemId 1 -ObjectTypeId 5 -Selected $true -PassThru
 
        Marks the object type as selected and returns the updated object.
 
    .EXAMPLE
        Get-JIMConnectedSystem -Id 1 -ObjectTypes | Where-Object { $_.name -eq "User" } |
            ForEach-Object { Set-JIMConnectedSystemObjectType -ConnectedSystemId 1 -ObjectTypeId $_.id -Selected $true }
 
        Selects the User object type for management.
 
    .EXAMPLE
        Get-JIMSyncRule -ConnectedSystemId 1 | Where-Object { $_.connectedSystemObjectTypeId -eq 5 } | Set-JIMSyncRule -Disable
        Set-JIMConnectedSystemObjectType -ConnectedSystemId 1 -ObjectTypeId 5 -Selected $false
 
        Takes object type 5 out of management. The first line disables every Synchronisation Rule
        bound to it, which deselecting requires. On the next Full Import every Connected System Object
        of that type is marked as deleted, and the following synchronisation disconnects them all and
        applies their Metaverse Objects' deletion rules. Run the first line without the final pipeline
        stage to see which Synchronisation Rules it would disable, and preview the deselection first with
        New-JIMConfigurationChangePreview -ConnectedSystemId 1 -SchemaObjectType @(@{ objectTypeId = 5; selected = $false }) -Wait
 
    .LINK
        Get-JIMConnectedSystem
        Set-JIMConnectedSystemAttribute
        Set-JIMSyncRule
        New-JIMConfigurationChangePreview
    #>

    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
    [OutputType([PSCustomObject])]
    param(
        [Parameter(Mandatory)]
        [int]$ConnectedSystemId,

        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [Alias('Id')]
        [int]$ObjectTypeId,

        [Parameter()]
        [bool]$Selected,

        [Parameter()]
        [bool]$RemoveContributedAttributesOnObsoletion,

        [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
        }

        # Build update body
        $body = @{}

        if ($PSBoundParameters.ContainsKey('Selected')) {
            $body.selected = $Selected
        }

        if ($PSBoundParameters.ContainsKey('RemoveContributedAttributesOnObsoletion')) {
            $body.removeContributedAttributesOnObsoletion = $RemoveContributedAttributesOnObsoletion
        }

        if ($body.Count -eq 0) {
            Write-Warning "No updates specified."
            return
        }

        if ($PSCmdlet.ShouldProcess("Object Type $ObjectTypeId in Connected System $ConnectedSystemId", "Update")) {
            Write-Verbose "Updating Object Type: $ObjectTypeId in Connected System: $ConnectedSystemId"

            try {
                $result = Invoke-JIMApi -Endpoint "/api/v1/synchronisation/connected-systems/$ConnectedSystemId/object-types/$ObjectTypeId" -Method 'PUT' -Body $body

                Write-Verbose "Updated Object Type: $ObjectTypeId"

                if ($PassThru) {
                    $result
                }
            }
            catch {
                Write-Error "Failed to update Object Type: $_"
            }
        }
    }
}