Public/Add-PegasusSyncObject.ps1

<#
.SYNOPSIS
Removes a position from the API.

.EXAMPLE
$Persons | Add-PegasusSyncObject -ObjectType person

#>

function Add-PegasusSyncObject {
    [CmdletBinding()]

    Param
    (
        [Parameter(Mandatory = $true, Position = 0)]
        [ValidateSet("person", "position", "orgunit", "genericdata")]
        [String] $ObjectType,
        
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        $InputObject
    )
    
    Process {
        $AnchorValue = ($ObjectType -eq "person") ? $InputObject.$($Script:SyncSessionPersonJoinAttribute) : $InputObject.sourceid

        if (!$AnchorValue) {
            Write-Error "Unable to determine anchor value for object: $($InputObject | ConvertTo-Json -Depth 10 -Compress)"
            return
        }

        if($AnchorValue.GetType().FullName -ne "System.String") {
            Write-Error "Anchor value for object must be a string, but got: $($AnchorValue.GetType().FullName)"
            return
        }

        if ($Script:SyncSessionObjects[$ObjectType].ContainsKey($AnchorValue)) {
            Write-Verbose "Object of type $ObjectType with anchor value $AnchorValue already exists: $($InputObject | ConvertTo-Json -Depth 10 -Compress)"
            return
        }

        if ($ObjectType -eq "position") {
            if (!$InputObject.person) {
                Write-Error "Position object does not have a person attribute: $($InputObject)"
                return
            }

            if (!$Script:SyncSessionObjects["person"].ContainsKey($InputObject.person)) {
                Write-Error "Position points to person with anchor value $($InputObject.person) that does not exist: $($InputObject | ConvertTo-Json -Depth 10 -Compress)"
                return
            }

            if (!$InputObject.orgunit) {
                Write-Error "Position object does not have an orgunit attribute: $($InputObject)"
                return
            }

            if (!$Script:SyncSessionObjects["orgunit"].ContainsKey($InputObject.orgunit)) {
                Write-Error "Orgunit object with anchor value $($InputObject.orgunit) does not exist: $($InputObject | ConvertTo-Json -Depth 10 -Compress)"
                return
            }
        }

        if ($ObjectType -eq "orgunit") {
            if ($InputObject.parent) {
                if (!$Script:SyncSessionObjects["orgunit"].ContainsKey($InputObject.parent)) {
                    if (!$InputObject.sourceid -contains $InputObject.parent) {
                        Write-Error "Parent orgunit object with anchor value $($InputObject.parent) does not exist: $($InputObject | ConvertTo-Json -Depth 10 -Compress)"
                        return
                    }
                }
            }

            if ($InputObject.managers) {
                $InputObject.managers | ForEach-Object {
                    if (!$Script:SyncSessionObjects["person"].ContainsKey($_)) {
                        Write-Warning "Manager person object with anchor value $_ does not exist: $($InputObject | ConvertTo-Json -Depth 10 -Compress)"
                    }
                }
            }
        }

        if ($ObjectType -eq "genericdata") {
            if (!$InputObject.person) {
                Write-Error "GenericData object does not have a person attribute: $($InputObject)"
                return
            }

            if (!$Script:SyncSessionObjects["person"].ContainsKey($InputObject.person)) {
                Write-Error "GenricData points to person with anchor value $($InputObject.person) that does not exist: $($InputObject | ConvertTo-Json -Depth 10 -Compress)"
                return
            }
        }

        Write-Verbose "Adding object of type $ObjectType with anchor value $AnchorValue"
        $Script:SyncSessionObjects[$ObjectType][$AnchorValue] = $InputObject
    }
}