Skip to content

Commit 307e5f7

Browse files
tjobarowclaude
andcommitted
fix(segment): run asset discovery under -DryRun even for new groups
Dry-run previously short-circuited before subnet expansion/asset discovery whenever the target group didn't exist yet, so it reported zero information about what would actually happen. Discovery is read-only (GET calls only), so it now always runs; only the group membership check (which needs a real group ID) is skipped when the group doesn't exist, with discovered assets still counted and reported in that case. Verified end-to-end against a real tenant: -DryRun -Server correctly found the 2 expected server assets in 10.1.11.0/24. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 80bb322 commit 307e5f7

2 files changed

Lines changed: 34 additions & 28 deletions

File tree

Segment/Segment/Asset Management/Create Custom Group from Asset Subnets/New-CustomGroupsFromSubnets.ps1

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,10 +1181,16 @@ processing workflow and final summary reporting.
11811181
.OUTPUTS
11821182
Returns a PSCustomObject summarizing what happened for this group (for the final run summary).
11831183
.NOTES
1184+
All read-only work (subnet expansion, asset discovery) always runs, even under -DryRun and even
1185+
when the target group does not exist yet - only mutating API calls (group creation, member
1186+
add/remove) are skipped under -DryRun. The one exception is the group membership check itself
1187+
(GET .../successors), which requires a real group ID and so cannot run against a group that
1188+
doesn't exist yet (Add mode, -DryRun) or doesn't exist at all (Remove mode) - in both cases,
1189+
discovered assets are still counted and reported, just without a membership diff.
11841190
Always updates the local JSON record via Update-GroupRecordEntry, even when the group did not
11851191
already exist and -DryRun prevented it from actually being created (record reflects a null groupId
11861192
in that case, and is corrected on the next non-dry-run pass). In -RemoveAssets mode, a missing
1187-
group is skipped entirely - nothing is recorded, since there is no group to describe.
1193+
group is never recorded, since there is no group to describe.
11881194
#>
11891195
function Invoke-ProcessGroupSubnetMapping {
11901196
param(
@@ -1209,38 +1215,37 @@ function Invoke-ProcessGroupSubnetMapping {
12091215

12101216
if ($RemoveAssets) {
12111217
$existingGroup = Get-CustomGroupByName -GroupName $GroupName
1212-
if ($null -eq $existingGroup) {
1213-
Write-Host "Group '$GroupName' does not exist - nothing to remove. Skipping."
1214-
return [PSCustomObject]@{
1215-
GroupName = $GroupName
1216-
Subnet = $Subnet
1217-
Mode = "Remove"
1218-
AssetsFound = 0
1219-
AssetsAffected = 0
1220-
AssetsSkipped = 0
1221-
}
1222-
}
1223-
$GroupId = $existingGroup.id
1218+
$GroupId = if ($null -ne $existingGroup) { $existingGroup.id } else { $null }
12241219
}
12251220
else {
12261221
$GroupId = New-CustomGroupIfMissing -GroupName $GroupName -Subnet $Subnet -DryRun:$DryRun
1227-
1228-
if ([string]::IsNullOrWhiteSpace($GroupId)) {
1229-
Write-Host "[DRY RUN] Skipping asset discovery/assignment for '$GroupName' - group does not exist yet and would only be created in a non-dry-run pass"
1230-
return [PSCustomObject]@{
1231-
GroupName = $GroupName
1232-
Subnet = $Subnet
1233-
Mode = "Add"
1234-
AssetsFound = 0
1235-
AssetsAffected = 0
1236-
AssetsSkipped = 0
1237-
}
1238-
}
12391222
}
1223+
$GroupExists = -not [string]::IsNullOrWhiteSpace($GroupId)
12401224

1225+
# Asset discovery is entirely read-only (GET calls only) - it always runs, regardless of -DryRun
1226+
# or whether the group exists yet, so a dry run reports real matching-asset counts.
12411227
$HostAddresses = Get-SubnetHostAddresses -TargetSubnet $Subnet
12421228
[System.Collections.ArrayList]$Assets = [System.Collections.ArrayList]@(Get-AssetsByHostAddresses -AssetSubnetHostAddresses $HostAddresses -MaxConcurrentBatches $MaxConcurrentBatches)
12431229

1230+
if (-not $GroupExists) {
1231+
# Membership can't be checked against a group that doesn't exist (Remove mode: not at all;
1232+
# Add mode: only reachable here under -DryRun, since a non-dry-run always creates the group).
1233+
if ($RemoveAssets) {
1234+
Write-Host "Group '$GroupName' does not exist - nothing to remove ($($Assets.Count) matching asset(s) found, but there is no group to check membership against). Skipping."
1235+
}
1236+
else {
1237+
Write-Host "[DRY RUN] Group '$GroupName' does not exist yet - would create it and add all $($Assets.Count) matching asset(s) to it (a new group has no existing members to skip)"
1238+
}
1239+
return [PSCustomObject]@{
1240+
GroupName = $GroupName
1241+
Subnet = $Subnet
1242+
Mode = $RemoveAssets ? "Remove" : "Add"
1243+
AssetsFound = $Assets.Count
1244+
AssetsAffected = $RemoveAssets ? 0 : $Assets.Count
1245+
AssetsSkipped = 0
1246+
}
1247+
}
1248+
12441249
if ($RemoveAssets) {
12451250
$AssignResult = Remove-AssetsFromCustomGroup -GroupId $GroupId -Assets $Assets -DryRun:$DryRun
12461251
Update-GroupRecordEntry -GroupName $GroupName -GroupId $GroupId -Subnet $Subnet -RemovedAssetIds @($AssignResult.Removed | ForEach-Object { $_.id })

Segment/Segment/Asset Management/Create Custom Group from Asset Subnets/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,12 @@ Every run mirrors all console output to a timestamped log file at `logs\New-Cust
170170

171171
## Dry Run Mode
172172

173-
Use the `-DryRun` switch to preview what changes would be made without actually applying them:
173+
Use the `-DryRun` switch to preview what changes would be made without actually applying them. All read-only work still runs - group existence checks, subnet expansion, and asset discovery are never skipped, so the reported `AssetsFound`/`AssetsAffected` counts are always real:
174174

175175
- Group existence is still checked, but missing groups are **not** created
176-
- Matching assets are still discovered and checked against current group membership
177-
- The request body that would be sent to add members is displayed, but no mutating API calls are made
176+
- Matching assets are still discovered regardless of whether the group exists yet
177+
- If the group already exists, current membership is still checked (read-only) and the request body that would be sent to add/remove members is displayed, but no mutating API calls are made
178+
- If the group doesn't exist yet (Add mode) or doesn't exist at all (Remove mode), membership can't be checked against it - discovered assets are still counted and reported, just without a per-asset membership diff (Add mode assumes all of them would be added, since a new group has no existing members to skip)
178179
- The local JSON record is still updated for groups that already exist, but not for groups that would only be created in a non-dry-run pass
179180

180181
## Notes

0 commit comments

Comments
 (0)