Skip to content

Commit

Permalink
Merge pull request #568 from inspec/bs/gcp-http-error-fixes
Browse files Browse the repository at this point in the history
CHEF-3309-InSpec GCP Http error fixes
  • Loading branch information
sa-progress authored Jan 18, 2024
2 parents 465871f + 83fc588 commit 273916f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,23 @@ control 'gcp-projects-zones-vm-label-loop-1.0' do
end
end
```
This example verifies there are sufficient privileges to list all regions.

```
next unless google_compute_regions(project: gcp_project_id).resource_failed?
google_compute_regions(project: gcp_project_id).region_names.each do |region_name|
describe google_compute_region(project: gcp_project_id, region: region_name) do
it { should be_up }
end
end

if google_compute_regions(project: gcp_project_id).resource_failed?
puts google_compute_regions(project: gcp_project_id).resource_exception_message
puts google_compute_regions(project: gcp_project_id,name: region_name).pretty_inspect
end
```
This example assumes there are sufficient privileges to list all GCP projects.
Expand Down
36 changes: 24 additions & 12 deletions libraries/gcp_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def initialize(opts)

# Magic Modules generated resources use an alternate transport method
# In the future this will be moved into the train-gcp plugin itself
@connection = GcpApiConnection.new if opts[:use_http_transport]
@connection = GcpApiConnection.new(self) if opts[:use_http_transport]
end

def failed_resource?
Expand Down Expand Up @@ -194,7 +194,10 @@ def camel_case(data)
end

class GcpApiConnection
def initialize
attr_reader :resource

def initialize(resource)
@resource = resource
config_name = Inspec::Config.cached.unpack_train_credentials[:host]
ENV['CLOUDSDK_ACTIVE_CONFIG_NAME'] = config_name
@google_application_credentials = config_name.blank? && ENV['GOOGLE_APPLICATION_CREDENTIALS']
Expand Down Expand Up @@ -237,27 +240,34 @@ def next_page(uri, request_type, token = nil)
fetch_auth,
request_type,
)
result = JSON.parse(get_request.send.body)
result = return_if_object(get_request.send)
next_page_token = result['nextPageToken']
return [result] if next_page_token.nil?

[result] + next_page(uri, request_type, next_page_token)
end

def return_if_object(response)
raise "Bad response: #{response.body}" \
if response.is_a?(Net::HTTPBadRequest)
raise "Bad response: #{response}" \
unless response.is_a?(Net::HTTPResponse)
return if response.is_a?(Net::HTTPNotFound)
return if response.is_a?(Net::HTTPNoContent)
result = JSON.parse(response.body)
raise_if_errors result, %w{error errors}, 'message'
raise "Bad response: #{response}" unless response.is_a?(Net::HTTPOK)
unless response.is_a?(Net::HTTPSuccess)
if response.is_a?(Net::HTTPResponse)
body = response.body
else
body = response
end
result = parser(body)
raise_if_errors result, %w{error errors}, 'message'
end
result = parser(response.body)
fetch_id result
result
end

def parser(json)
JSON.parse(json)
rescue JSON::ParserError
raise StandardError, "Bad response: #{json}" \
end

def fetch_id(result)
@resource_id = if result.key?('id')
result['id']
Expand All @@ -269,6 +279,8 @@ def fetch_id(result)

def raise_if_errors(response, err_path, msg_field)
errors = self.class.navigate(response, err_path)
resource.fail_resource errors
resource.failed_resource = true
raise_error(errors, msg_field) unless errors.nil?
end

Expand Down

0 comments on commit 273916f

Please sign in to comment.