Limit Azure datacenter discovery using mid.cloud.discovery.sonar.discover_all_azure_datacentersSummary<!-- /*NS Branding Styles*/ --> .ns-kb-css-body-editor-container { p { font-size: 12pt; font-family: Lato; color: #000000; } span { font-size: 12pt; font-family: Lato; color: #000000; } h2 { font-size: 24pt; font-family: Lato; color: black; } h3 { font-size: 18pt; font-family: Lato; color: black; } h4 { font-size: 14pt; font-family: Lato; color: black; } a { font-size: 12pt; font-family: Lato; color: #00718F; } a:hover { font-size: 12pt; color: #024F69; } a:target { font-size: 12pt; color: #032D42; } a:visited { font-size: 12pt; color: #00718f; } ul { font-size: 12pt; font-family: Lato; } li { font-size: 12pt; font-family: Lato; } img { display: ; max-width: ; width: ; height: ; } } Overview In Discovery and Service Mapping Patterns, an optimization exists to reduce Azure datacenter discovery scope so discovery focuses only on datacenters where resources are present for the service account. The switch for this behavior is the MID Server property mid.cloud.discovery.sonar.discover_all_azure_datacenters.It decides whether the Azure datacenter discovery pattern runs a filtering library. ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Where the property is used Pattern name ↳ Azure Datacenter discoveryPattern link ↳ https://<instance_name>.service-now.com/nav_to.do?uri=sa_pattern.do?sys_id=09d4ce4ddbb8230078f02b8705961962 Library name ↳ Azure Empty Datacenter SonarLibrary link ↳ https://<instance_name>.service-now.com/nav_to.do?uri=sa_pattern.do?sys_id=06094cba1bb88950d67afc4b8b4bcbee Key behaviour ↳ The property is a gate that decides whether the library runs. If the library does not run, there is no "active-only" filtering. ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Pattern story Step 1: Filter locations regionType equal to "Physical"↳ The pattern keeps only Physical Azure regions before any sonar filtering happens. Step 2: Fill cmdb_ci_logical_datacenter table↳ The pattern creates the working set of datacenters in cmdb_ci_azure_datacenter. Step 3: Check if we need to discover all datacenters↳ Reads mid.cloud.discovery.sonar.discover_all_azure_datacenters into discoverAllDatacenters.↳ The property read defaults to "false" if the property is missing. Step 4: Filter empty datacenters↳ Runs the library only when all conditions are true.↳ discoverAllDatacenters equals "false"↳ cmdb_ci_azure_datacenter is not empty↳ isManagementGroupAccount equals "false"↳ flow_type is empty Step 5: Gracefully terminate if no active datacenters found↳ If cmdb_ci_azure_datacenter becomes empty after filtering, the pattern ends gracefully with:"Active Sonar excluded all Azure datacenters with no resources. Terminating discovery." Step 6: Create Relation between datacenter and service account↳ Relationships are created only for whatever datacenters remain after filtering. ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ What true vs false changes Property = "true"↳ The library step is skipped.↳ Discovery continues with all Physical datacenters produced earlier. Property = "false"↳ The library runs (only if the other gates also pass).↳ The library keeps only datacenters it marks as active, and removes the rest from the working set.↳ If no active datacenters remain, the run terminates at the graceful terminate step with the message shown above. ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Library story (Azure Empty Datacenter Sonar) — what it calls and how it decides active vs passive What the library is trying to do For the service account's subscription, the library tries to build a list of Azure locations where it can see resources.It then marks datacenters as active only when the datacenter region exists in that location list.Finally, it removes every datacenter not marked active. Library step 1: Create Resource Inventory Query ↳ Builds this query (subscription resources list):https://management.<resource_url>/subscriptions/<service_account[1].account_id>/resources?$top=100&api-version=2018-05-01 Library step 2: Get Resource Inventory for Service Account ↳ Executes the query above and stores the response into resources_sa. Library step 3: Create Resource Groups Query ↳ Builds this query (resource groups list):https://management.<resource_url>/subscriptions/<service_account[1].account_id>/resourcegroups?api-version=2021-04-01 Library step 4: Get Resource Inventory for Resource Groups ↳ Executes the resource groups query and stores the response into resources_rg. Library step 5: Extract Locations of Resources This step builds the list activeLDCs (Active Logical Datacenters), which is a list of region names in quotes (example format "eastus").The library only runs this extraction when at least one of these is not empty.↳ resources_sa is not empty, or resources_rg is not empty How locations are extracted↳ For each response payload in resources_sa and resources_rg↳ Parse JSON and read payloadObj.value↳ For every item in value, if item.location exists↳ Add the location to a set (so duplicates are removed)↳ Convert the set into a list and store it as activeLDCs Important detail↳ Locations are stored as quoted strings like ""eastus"" and later compared the same way. Library step 6: Add Active or Passive State to Datacenters in comment field This step updates fields on each cmdb_ci_azure_datacenter row, but only if activeLDCs is not empty. How sonar_state is calculated for each datacenter↳ Take the datacenter region from cmdb_ci_azure_datacenter[].region↳ Default state = "passive"↳ If activeLDCs contains ""<region>"" then set state = "active"↳ Write that value into cmdb_ci_azure_datacenter.sonar_state Important detail↳ This transform also sets cmdb_ci_azure_datacenter.comments = "Active" for every row it touches, regardless of whether sonar_state became active or stayed passive.So comments is not a reliable indicator of active vs passive in this library; sonar_state is the reliable indicator. Library step 7: Extract Only Active Datacenters This is the actual filtering step.↳ It filters cmdb_ci_azure_datacenter to keep only rows where sonar_state contains "active"↳ Every other datacenter row is removed from the working set ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Exact active vs passive decision A datacenter becomes active when↳ The library successfully extracted at least one location into activeLDCs↳ The datacenter’s region value exists inside activeLDCs A datacenter becomes passive when↳ activeLDCs exists (not empty), but the datacenter’s region is not present in activeLDCs↳ The library defaults sonar_state to passive and only flips to active when a match is found A datacenter may remain unclassified (no sonar_state update) when↳ activeLDCs is empty, because the transform step that sets sonar_state does not run↳ In that case, the later filter step still runs and keeps only rows whose sonar_state contains active↳ If sonar_state is blank everywhere, the filter results in an empty working set and the pattern terminates with the "Active Sonar excluded..." message ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Troubleshooting Symptom 1: Property is set but it still scans all Azure datacenters Check 1 ↳ Confirm the library step executed in the pattern run↳ In the pattern execution, verify the step "Filter empty datacenters" ran Check 2 ↳ Confirm the library gates allowed execution↳ isManagementGroupAccount must be "false"↳ flow_type must be empty↳ cmdb_ci_azure_datacenter must be not empty before the library runs↳ discoverAllDatacenters must evaluate to the literal string "false" Check 3 ↳ Confirm filtering actually reduced the working set↳ The library keeps only rows with sonar_state containing "active"↳ If many rows ended up active, the scan scope will still be large by design Symptom 2: Property is "false" and discovery terminates with "Active Sonar excluded all Azure datacenters with no resources. Terminating discovery."This means the post-library cmdb_ci_azure_datacenter working set became empty.The library can lead to this outcome in these exact situations.↳ resources_sa and resources_rg were empty or did not produce any item.location values, so activeLDCs became empty↳ activeLDCs had values, but none matched the datacenter region strings (mismatch between region names and extracted location strings)↳ sonar_state was not set to active for any datacenter, so the filter "keep only active" removed everything Symptom 3: Customer expects comments to show Active/Passive but it always says "Active" This is expected from the library code.↳ The library sets comments = "Active" in the same transform where it calculates sonar_state↳ It does not set comments based on the calculated stateUse sonar_state to validate what is active vs passive.