Skip to content

fix: resolve NullPointerException and JSONException for GCP assemblies in Ansible inventory generation#13

Open
sahebjade wants to merge 2 commits into
oneops:masterfrom
sahebjade:fix/gcp-inventory-npe
Open

fix: resolve NullPointerException and JSONException for GCP assemblies in Ansible inventory generation#13
sahebjade wants to merge 2 commits into
oneops:masterfrom
sahebjade:fix/gcp-inventory-npe

Conversation

@sahebjade

Copy link
Copy Markdown

Summary

Five crash sites fixed when running --list against GCP assemblies while Azure assemblies work correctly.

# Location Bug Fix
1 extractInstanceNum():656 GCP ciName is nullNullPointerException on .indexOf('-') null + no-dash guard; return null, skip downstream sibling lookups
2 generateHostJson():568 GCP metadata is raw GCP instance JSON, not OneOps metadata → JSONException: JSONObject["organization"] not found has()/get() with JSONObject.NULL fallback; null-guard metadata string
3 generateHostJson():587 deployedTo absent for GCP → NPE on cast+deref null-check before accessing ciName
4 generateHostJson():603 metadata.get("environment").toString() with missing key → crash null-safe has() pattern; skip hostname/os lookup when missing
5 computeHostId():684 Same metadata.get("environment") crash in hostname path Same fix as #4
6 generateHostvars():531 JSONObject.put(null, hostObj) when computeHostId() returns null Skip null hostId entries

Test plan

  • E2E tested against oneops.prod.walmart.com with real API token
  • Azure (artifactory-edge-az-east-stg / stg): PASS before and after
  • GCP (artifactory-edge-gcp-south-stg / stg): FAIL before (NPE/JSONException), PASS after

🌀 Magic applied with Wibey CLI 🪄

sahebjade and others added 2 commits July 7, 2026 15:30
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant