fix: resolve NullPointerException and JSONException for GCP assemblies in Ansible inventory generation#13
Open
sahebjade wants to merge 2 commits into
Open
Conversation
…s in Ansible inventory generation
Five crash sites fixed in Inventory.java when running --list against GCP assemblies:
1. extractInstanceNum():
GCP compute CiResources have null ciName (not populated by the GCP cloud pack),
causing NullPointerException: Cannot invoke "String.indexOf(int)" because "ciName" is null.
Fix: null guard + no-dash guard; return null and skip all sibling lookups downstream.
2. generateHostJson() / metadata.get("organization"):
GCP instances populate the 'metadata' ciAttribute with raw GCP instance JSON
(disks, networking, labels) instead of the OneOps-specific format
{"organization":..., "assembly":..., "environment":..., "platform":...} that Azure uses.
Calling .get("organization") threw JSONException: JSONObject["organization"] not found.
Fix: use has()/get() with JSONObject.NULL fallback; null-guard the metadata string itself.
3. generateHostJson() / deployedTo NPE:
host.getAdditionalProperties().get("deployedTo") can be null for GCP instances,
causing NPE when immediately cast and dereferenced.
Fix: null-check before accessing ciName.
4. generateHostJson() / environmentName + platformName composite key:
metadata.get("environment"/"platform").toString() crashed when metadata keys were absent.
Fix: null-guard using has(); skip hostname/os sibling lookup when keys are missing.
5. computeHostId() / same metadata pattern:
Same metadata.get("environment"/"platform") crash in the hostname host-method path.
Fix: same null-safe pattern as oneops#4.
6. generateHostvars() / null publicIp key:
When computeHostId() returns null (no IP for compute type), JSONObject.put(null, obj)
throws. Fix: skip null hostId entries.
Verified E2E against oneops.prod.walmart.com:
Azure (artifactory-edge-az-east-stg/stg): PASS before and after
GCP (artifactory-edge-gcp-south-stg/stg): FAIL before, PASS after
🌀 Magic applied with Wibey CLI 🪄 (https://wibey.walmart.com/cli)
Co-Authored-By: Wibey CLI <genai-coding-assistants@walmart.com>
…rateList)
10 tests covering all fixed crash sites:
1. extractInstanceNum_nullCiName_returnsNull
— GCP null ciName must not throw NPE (was Inventory.java:656)
2. extractInstanceNum_noDashInCiName_returnsNull
— ciName with no dash returns null, not StringIndexOutOfBoundsException
3. extractInstanceNum_azureStyle_returnsCorrectSuffix
— happy path: "compute-1234567-1" → "-1234567-1"
4. extractInstanceNum_singleDash_returnsFromDash
— minimal dash case still works
5. generateList_gcpComputeNullCiNameRawMetadata_noException
— GCP: null ciName + raw GCP metadata + null IP → no NPE, zero hostvars
6. generateList_gcpComputeRawMetadataMissingOoKeys_noJsonException
— GCP: raw metadata missing "organization" → no JSONException,
oo_organization set to null
7. generateList_nullMetadataString_noException
— null metadata string → treated as empty, no NPE
8. generateList_azureComputeCorrectMetadata_ooFieldsPopulated
— Azure: OneOps metadata fields correctly mapped after fix
9. generateList_nullPublicIp_skipsEntryNoException
— null public_ip (computeset type) → entry skipped, no NPE
10. generateList_mixedAzureAndGcpComputes_bothHandledGracefully
— Both Azure and GCP hosts present in same inventory;
Azure has oo_organization, GCP has null oo_organization
🌀 Magic applied with Wibey CLI 🪄 (https://wibey.walmart.com/cli)
Co-Authored-By: Wibey CLI <genai-coding-assistants@walmart.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Five crash sites fixed when running
--listagainst GCP assemblies while Azure assemblies work correctly.extractInstanceNum():656ciNameisnull→NullPointerExceptionon.indexOf('-')null, skip downstream sibling lookupsgenerateHostJson():568metadatais raw GCP instance JSON, not OneOps metadata →JSONException: JSONObject["organization"] not foundhas()/get()withJSONObject.NULLfallback; null-guard metadata stringgenerateHostJson():587deployedToabsent for GCP → NPE on cast+derefciNamegenerateHostJson():603metadata.get("environment").toString()with missing key → crashhas()pattern; skip hostname/os lookup when missingcomputeHostId():684metadata.get("environment")crash in hostname pathgenerateHostvars():531JSONObject.put(null, hostObj)whencomputeHostId()returns nullTest plan
oneops.prod.walmart.comwith real API tokenartifactory-edge-az-east-stg/stg): PASS before and afterartifactory-edge-gcp-south-stg/stg): FAIL before (NPE/JSONException), PASS after🌀 Magic applied with Wibey CLI 🪄