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

CHEF-3309-InSpec GCP Http error fixes #568

Merged
merged 5 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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