diff options
author | John McCrae <john.mccrae@progress.com> | 2023-02-21 12:25:03 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-21 12:25:03 -0800 |
commit | 457778a73902592e3dca543b5a489290c72a5e16 (patch) | |
tree | deeeda6467eed31fd01e0d1e795314f900a47549 /lib/ohai/mixin/oci_metadata.rb | |
parent | 3fd46bd1f728ec1890d171da8e8149a859a1ffc4 (diff) | |
parent | dc7c760694906f3beebba8bc00755e946c0f2417 (diff) | |
download | ohai-457778a73902592e3dca543b5a489290c72a5e16.tar.gz |
Merge pull request #1786 from rnt/fix/calling-yajl-parser-only-once
Calling yajl parser only once
Diffstat (limited to 'lib/ohai/mixin/oci_metadata.rb')
-rw-r--r-- | lib/ohai/mixin/oci_metadata.rb | 25 |
1 files changed, 8 insertions, 17 deletions
diff --git a/lib/ohai/mixin/oci_metadata.rb b/lib/ohai/mixin/oci_metadata.rb index b04243a3..c65c5754 100644 --- a/lib/ohai/mixin/oci_metadata.rb +++ b/lib/ohai/mixin/oci_metadata.rb @@ -18,6 +18,9 @@ require "net/http" unless defined?(Net::HTTP) +require_relative "../mixin/json_helper" +include Ohai::Mixin::JsonHelper + module Ohai module Mixin module OCIMetadata @@ -38,27 +41,15 @@ module Ohai ) end - # parse JSON data from a String to a Hash - # - # @param [String] response_body json as string to parse - # - # @return [Hash] - def parse_json(response_body) - data = String(response_body) - parser = FFI_Yajl::Parser.new - parser.parse(data) - rescue FFI_Yajl::ParseError - logger.warn("Mixin OciMetadata: Metadata response is NOT valid JSON") - nil - end - # Fetch metadata from api def fetch_metadata(metadata = "instance") response = http_get("#{OCI_METADATA_URL}/#{metadata}") - return nil unless response.code == "200" - if response.code == "200" - parse_json(response.body) + json_data = parse_json(response.body) + if json_data.nil? + logger.warn("Mixin OciMetadata: Metadata response is NOT valid JSON") + end + json_data else logger.warn("Mixin OciMetadata: Received response code #{response.code} requesting metadata") nil |