Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nil exception when no zones are returned for a region #67

Merged
merged 1 commit into from
Oct 23, 2018

Conversation

alexpop
Copy link
Contributor

@alexpop alexpop commented Oct 22, 2018

When running the cis-gcp-benchmark-level2 profile against my gcp project, I noticed errors like this for a few controls:

screen shot 2018-10-22 at 6 38 11 pm

I made two changes to the PR, one to replace false with []. Otherwise controls like these:

google_compute_region(project: gcp_project_id, name: region_name).zone_names.each do |zone_name|

will raise an error as well since they expect zone_names to be array and not boolean.

Second change is on the nil checking of @region.zones. It was not returning [], here's my pry play:

[1] pry(#<#<Class:0x00007f9d7f9b7bb0>>)> @region.zones
=> nil
[2] pry(#<#<Class:0x00007f9d7f9b7bb0>>)> defined?(@region.zones)
=> "method"
[3] pry(#<#<Class:0x00007f9d7f9b7bb0>>)> puts "bingo" if !defined?(@region.zones)
=> nil
[4] pry(#<#<Class:0x00007f9d7f9b7bb0>>)> myvar=nil
=> nil
[5] pry(#<#<Class:0x00007f9d7f9b7bb0>>)> defined?(myvar)
=> "local-variable"
[6] pry(#<#<Class:0x00007f9d7f9b7bb0>>)> defined?(myvar2)
=> nil
[7] pry(#<#<Class:0x00007f9d7f9b7bb0>>)> puts "bingo" if !defined?(myvar)
=> nil
[8] pry(#<#<Class:0x00007f9d7f9b7bb0>>)> puts "bingo" if !defined?(myvar2)
bingo
=> nil

Copy link

@skpaterson skpaterson left a comment

Choose a reason for hiding this comment

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

Thanks @alexpop 👍

Copy link

@skpaterson skpaterson left a comment

Choose a reason for hiding this comment

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

Looks like rubocop is complaining about the below:

libraries/google_compute_region.rb:29:34: C: Style/NilComparison: Prefer the use of the nil? predicate.

Signed-off-by: Alex Pop <apop@chef.io>
@skpaterson skpaterson merged commit 1892fa2 into master Oct 23, 2018
@skpaterson skpaterson deleted the ap/nil-exception branch October 23, 2018 09:59
skpaterson pushed a commit that referenced this pull request Jan 21, 2019
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.

2 participants