Samples/ImportCallhomeDetails/lxco/lxcoResourcesGroup.ps1

# ------------------------------------------------------------------
# Lenovo Copyright
#
# (C) Copyright Lenovo 2024 - present.
#
# LIMITED AND RESTRICTED RIGHTS NOTICE:
# If data or software is delivered pursuant a General Services
# Administration (GSA) contract, use, reproduction, or disclosure
# is subject to restrictions set forth in Contract No. GS-35F-05925.
# ------------------------------------------------------------------

Import-Module LXCOPSTool

$baseUri = "/api/v1/groups-management/resourceGroups"

function GetLXCOResourceGroups()
{
    param (
        [Parameter(Mandatory = $true)] $Connection
    )

    $uri = "$($baseUri)?offset=0&limit=50000&includeAttributes=members"
    return (Invoke-LXCORestMethod -Connection $Connection -Method Get -ResourceUrl $uri)
}

function PostLXCOResourceGroups()
{
    param (
        [Parameter(Mandatory = $true)] $Connection,
        [Parameter(Mandatory = $true)] $GropName,
        [Parameter(Mandatory = $true)] $GroupDescription
    )
    # generate request body
    $json = @{
        name = $GropName;
        memberIDs = @();
        description = $GroupDescription;
        type = "DeviceGroup"} | ConvertTo-Json -Compress
    $result = Invoke-LXCORestMethod -Connection $Connection -Method Post -ResourceUrl $baseUri -RequestBody $json
    return $result
}

function PostContactsAssignment()
{
    param (
        [Parameter(Mandatory = $true)] $Connection,
        [Parameter(Mandatory = $true)] $GropID,
        [Parameter(Mandatory = $true)] $AddressID
    )
    $uri = "/api/v1/service/contactsAssignment"
    # generate request body
    $json = @{
        groupIDs = @($GropID);
        primaryContactID = $AddressID;
        resourceIDs = @();
        secondaryContactIDs = @()} | ConvertTo-Json -Compress
    $result = Invoke-LXCORestMethod -Connection $Connection -Method Post -ResourceUrl $uri -RequestBody $json
    return $result
}

function PatchLXCOResourceGroups()
{
    param (
        [Parameter(Mandatory = $true)] $Connection,
        [Parameter(Mandatory = $true)] $DeviceID,
        [Parameter(Mandatory = $true)] $GroupID
    )

    $uri = "$($baseUri)/$($GroupID)"
    # generate request body
    [string] $data = @{
        op = "add";
        path = "members";
        value = $DeviceID;
    } | ConvertTo-Json -Compress

    $json = $data.Insert($data.Length, "]").Insert(0, "[")
    $result = Invoke-LXCORestMethod -Connection $Connection -Method Patch -ResourceUrl $uri -RequestBody $json
    return $result
}

function GetGroupName()
{
    param (
        [Parameter(Mandatory = $true)] [string] $Prefix,
        [Parameter(Mandatory = $true)] [hashtable] $Groups
    )

    $name = ""
    $index = 1
    do
    {
        $name = "$Prefix $index"
        $index++
    }
    while ($null -ne $Groups[$name])
    return $name
}


function UpdateGroupsInfo()
{
    param (
        [Parameter(Mandatory = $true)] $Connection,
        [Parameter(Mandatory = $true)] [string[]]$RequiredAddresses,
        [Parameter(Mandatory = $true)] [hashtable] $Addresses,
        [Parameter(Mandatory = $true)] [hashtable] $Devices
    )
    $result = GetLXCOResourceGroups -Connection $Connection
    $lxcoGroups = $(ConvertFrom-Json -InputObject $result.ResponseBody).results
    [hashtable] $groups = @{}
    [hashtable] $groupsByName = @{}
    foreach ($group in $lxcoGroups)
    {
        $groups[$group.description] = $group
        $groupsByName[$group.name] = $group
    }
    foreach ($aHash in $RequiredAddresses)
    {
        $addr = $Addresses[$aHash]
        $gname = GetGroupName -Prefix $addr.city -Groups $groupsByName
        $groupsByName[$gname] = $gname
        $gDescription = "Call Home Group: $($addr.country) - $($addr.city) - $($addr.streetAddress) - $($addr.postalCode)"
        if ($null -eq $groups[$gDescription])
        {
            # create group
            $result = PostLXCOResourceGroups -Connection $Connection -GropName $gname -GroupDescription $gDescription
            $addr.groupID = $($($result.LocationHeaderValue) -split "/") | Select-Object -Last 1
            # assign contacts
            PostContactsAssignment -Connection $Connection -GropID $addr.groupID -AddressID $addr.id
        }
        else
        {
            $addr.groupID = $groups[$gDescription].id
        }
    }

    # find groupID for each device
    [hashtable] $groupsDevicesMap = @{}
    foreach ($device in $Devices.GetEnumerator())
    {
        if ($true -eq $device.Value.isManaged)
        {
            $device.Value.groupID = $Addresses[$device.Value.addressHash].groupID
            if (($null -ne $device.Value.groupID) -and ($null -eq $groupsDevicesMap[$device.Value.groupID]))
            {
                $groupsDevicesMap[$device.Value.groupID] = @()
            }
            $groupsDevicesMap[$device.Value.groupID] += $device.Value.id
        }
    }

    # populate LXCO groups
    [hashtable] $lxcoGroupsMap = @{}
    foreach ($grp in $lxcoGroups)
    {
        $lxcoGroupsMap[$grp.id] = $grp
    }
    $mapKeys = $groupsDevicesMap.Keys
    foreach ($grpID in $mapKeys)
    {
        $desiredDevices = $groupsDevicesMap[$grpID]
        if ($null -ne $lxcoGroupsMap[$grpID])
        {
            $currentDevices = $lxcoGroupsMap[$grpID]
        }
        else
        {
            $currentDevices = @()
        }
        $missingDevices = $desiredDevices | Where-Object {$_ -notin $currentDevices.members}
        if ($null -ne $missingDevices)
        {
            PatchLXCOResourceGroups -Connection $Connection -DeviceID $missingDevices -GroupID $grpID
        }
    }

}
# EOF