Skip to content

Add null checks to prevent premature script exit in GCP sizing#107

Open
visharma28 wants to merge 1 commit into
masterfrom
gcp-sizing-null-checks
Open

Add null checks to prevent premature script exit in GCP sizing#107
visharma28 wants to merge 1 commit into
masterfrom
gcp-sizing-null-checks

Conversation

@visharma28

@visharma28 visharma28 commented Nov 13, 2025

Copy link
Copy Markdown
Contributor

Summary:

This PR adds comprehensive null checks and error handling to the GCP sizing PowerShell script to prevent premature script termination when encountering API errors, permission issues, or null values during instance and disk processing.

What?

  • Added null check for $instanceInfo to skip instance processing when Get-GceInstance API call fails
  • Added try-catch wrapper around entire instance processing loop (lines 264-348) to handle null reference errors gracefully
  • Added try-catch block around individual disk retrieval operations (Get-GceDisk) with proper error handling and continue statements
  • Added null check for $diskInfo after disk retrieval to skip failed disk operations
  • Added null check for $allDisks to skip unattached disk processing when Get-GceDisk API call fails
  • Fixed bug on line 333 where $vm.Status was incorrectly used instead of $instance.Status

Why?

  • The script was crashing with "You cannot call a method on a null-valued expression" errors when encountering null $disk.Source values or API failures
  • Projects with disabled APIs (e.g., Compute Engine API not enabled) would cause the entire script to exit prematurely, preventing data collection from other accessible projects
  • Permission denied errors on some projects would stop the script from processing remaining projects in the list
  • The bug using $vm.Status instead of $instance.Status caused incorrect status reporting in the output CSV
  • Without proper null checks, a single failed disk retrieval would terminate instance processing instead of gracefully skipping the problematic disk
  • This fix enables the script to collect maximum available data across all accessible projects, even when some projects have API or permission issues

How?

  • Implemented null check pattern matching the reference implementation: wrapping instance processing in try-catch blocks to catch any null reference exceptions
  • Used if ($variable -eq $null) checks before processing $instanceInfo and $allDisks collections to prevent iteration over null objects
  • Added try-catch blocks around Get-GceDisk calls with continue statements to skip failed disk retrievals without stopping the loop
  • Added null validation for $diskInfo after retrieval attempts, logging warnings and using continue to skip to next disk
  • Replaced incorrect variable reference $vm.Status with correct $instance.Status to access the instance object's Status property
  • All error conditions now log descriptive warning messages (yellow) or error messages (red) before gracefully continuing execution

Test Plan:

  • PowerShell syntax validation passed using [System.Management.Automation.Language.Parser]::ParseFile()
  • Tested with project spark-dev-100 that has Compute Engine API disabled - script logged "No instance information available" and continued without crashing
  • Verified script handles null $instanceInfo gracefully by skipping instance processing and logging appropriate warning message
  • Verified script handles null $allDisks gracefully by skipping unattached disk processing and logging appropriate warning message
  • Confirmed try-catch wrapper around instance processing catches null reference errors and logs error details before continuing to next instance
  • Verified disk retrieval failures are caught, logged, and skipped using continue statement without terminating the script
  • Confirmed the Status field bug fix correctly references $instance.Status instead of undefined $vm.Status

https://phabricator.rubrik.com/P19812

JIRA Issues:

SIZENG-48

Revert Plan:

…n GCP sizing script

- Add null check for \ to skip instance processing when API calls fail
- Add try-catch wrapper around entire instance processing loop to handle null reference errors
- Add try-catch blocks around individual disk retrieval operations with graceful error handling
- Add null check for \ to skip failed disk operations
- Add null check for \ to skip disk processing when API calls fail
- Fix bug where \.Status should be \.Status (line 333)

This prevents the script from crashing when encountering API errors, permission issues,
or null disk sources, allowing it to continue processing other projects and instances.
Write-Progress -ID 2 -Activity "Processing GCE VM Instance: $($instance.Name)" -Status "Project: $($instanceCounter) of $($instanceInfo.Count)" -PercentComplete (($instanceCounter / $instanceInfo.Count) * 100)
$instanceCounter++
# Skip processing if instanceInfo is null (due to API failure)
if ($instanceInfo -eq $null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: if ($null -eq $instanceInfo)

continue
}

if ($diskInfo -eq $null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: if ($null -eq $diskInfo)

@mtellin

mtellin commented May 7, 2026

Copy link
Copy Markdown
Member

This has merge conflicts with master. Could you rebase and resolve? Happy to review once updated.

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.

3 participants