Skip to content

Commit 2745f81

Browse files
committed
Added support for Windows Platform for oci metadata capturing.
Modernized Ruby syntax to pass cookstyle --chefstyle. Signed-off-by: Michael Butler <[email protected]>
1 parent 24f018d commit 2745f81

File tree

4 files changed

+168
-168
lines changed

4 files changed

+168
-168
lines changed

lib/ohai/mixin/oci_metadata.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616
# See the License for the specific language governing permissions and
1717
# limitations under the License.
1818

19-
require 'net/http' unless defined?(Net::HTTP)
19+
require "net/http" unless defined?(Net::HTTP)
2020

21-
require_relative '../mixin/json_helper'
21+
require_relative "../mixin/json_helper"
2222
include Ohai::Mixin::JsonHelper
2323

2424
module Ohai
2525
module Mixin
2626
module OCIMetadata
27-
OCI_METADATA_ADDR = '169.254.169.254'
28-
OCI_METADATA_URL = '/opc/v2'
29-
CHASSIS_ASSET_TAG_FILE = '/sys/devices/virtual/dmi/id/chassis_asset_tag'
27+
OCI_METADATA_ADDR = "169.254.169.254"
28+
OCI_METADATA_URL = "/opc/v2"
29+
CHASSIS_ASSET_TAG_FILE = "/sys/devices/virtual/dmi/id/chassis_asset_tag"
3030

3131
# Get the chassis asset tag from DMI information
3232
# On Linux: reads from sysfs
@@ -51,11 +51,11 @@ def get_chassis_asset_tag_linux
5151

5252
# Read chassis asset tag from Windows WMI
5353
def get_chassis_asset_tag_windows
54-
require 'wmi-lite/wmi' unless defined?(WmiLite::Wmi)
54+
require "wmi-lite/wmi" unless defined?(WmiLite::Wmi)
5555

5656
wmi = WmiLite::Wmi.new
57-
enclosure = wmi.first_of('Win32_SystemEnclosure')
58-
enclosure&.[]('SMBIOSAssetTag')
57+
enclosure = wmi.first_of("Win32_SystemEnclosure")
58+
enclosure&.[]("SMBIOSAssetTag")
5959
rescue => e
6060
logger.debug("Mixin OciMetadata: Failed to read chassis asset tag from WMI: #{e}")
6161
nil
@@ -68,19 +68,19 @@ def http_get(uri)
6868
conn.get(
6969
uri,
7070
{
71-
'Authorization' => 'Bearer Oracle',
72-
'User-Agent' => "chef-ohai/#{Ohai::VERSION}",
71+
"Authorization" => "Bearer Oracle",
72+
"User-Agent" => "chef-ohai/#{Ohai::VERSION}",
7373
}
7474
)
7575
end
7676

7777
# Fetch metadata from api
78-
def fetch_metadata(metadata = 'instance')
78+
def fetch_metadata(metadata = "instance")
7979
response = http_get("#{OCI_METADATA_URL}/#{metadata}")
80-
if response.code == '200'
80+
if response.code == "200"
8181
json_data = parse_json(response.body)
8282
if json_data.nil?
83-
logger.warn('Mixin OciMetadata: Metadata response is NOT valid JSON')
83+
logger.warn("Mixin OciMetadata: Metadata response is NOT valid JSON")
8484
end
8585
json_data
8686
else

lib/ohai/plugins/oci.rb

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,25 @@
1818
#
1919

2020
Ohai.plugin(:Oci) do
21-
require_relative '../mixin/oci_metadata'
22-
require_relative '../mixin/http_helper'
21+
require_relative "../mixin/oci_metadata"
22+
require_relative "../mixin/http_helper"
2323

2424
include Ohai::Mixin::OCIMetadata
2525
include Ohai::Mixin::HttpHelper
2626

27-
provides 'oci'
27+
provides "oci"
2828

2929
collect_data do
30-
oci_metadata_from_hints = hint?('oci')
30+
oci_metadata_from_hints = hint?("oci")
3131
if oci_metadata_from_hints
32-
logger.trace('Plugin OCI: oci hint is present. Parsing any hint data.')
32+
logger.trace("Plugin OCI: oci hint is present. Parsing any hint data.")
3333
oci Mash.new
3434
oci_metadata_from_hints.each { |k, v| oci[k] = v }
35-
oci['metadata'] = parse_metadata
35+
oci["metadata"] = parse_metadata
3636
elsif oci_chassis_asset_tag?
37-
logger.trace('Plugin oci: No hints present, but system appears to be on oci.')
37+
logger.trace("Plugin oci: No hints present, but system appears to be on oci.")
3838
oci Mash.new
39-
oci['metadata'] = parse_metadata
39+
oci["metadata"] = parse_metadata
4040
else
4141
logger.trace("Plugin oci: No hints present and doesn't appear to be on oci.")
4242
false
@@ -48,7 +48,7 @@ def oci_chassis_asset_tag?
4848
return false if asset_tag.nil? || asset_tag.empty?
4949

5050
if /OracleCloud.com/.match?(asset_tag)
51-
logger.trace('Plugin oci: Found OracleCloud.com chassis_asset_tag used by oci.')
51+
logger.trace("Plugin oci: Found OracleCloud.com chassis_asset_tag used by oci.")
5252
true
5353
else
5454
false
@@ -58,32 +58,32 @@ def oci_chassis_asset_tag?
5858
def parse_metadata
5959
return unless can_socket_connect?(Ohai::Mixin::OCIMetadata::OCI_METADATA_ADDR, 80)
6060

61-
instance_data = fetch_metadata('instance')
61+
instance_data = fetch_metadata("instance")
6262
return if instance_data.nil?
6363

6464
metadata = Mash.new
65-
metadata['compute'] = Mash.new
65+
metadata["compute"] = Mash.new
6666

6767
instance_data.each do |k, v|
68-
metadata['compute'][k] = v
68+
metadata["compute"][k] = v
6969
end
7070

71-
vnics_data = fetch_metadata('vnics')
71+
vnics_data = fetch_metadata("vnics")
7272

7373
unless vnics_data.nil?
74-
metadata['network'] = Mash.new
75-
metadata['network']['interface'] = []
74+
metadata["network"] = Mash.new
75+
metadata["network"]["interface"] = []
7676
vnics_data.each do |v|
77-
metadata['network']['interface'].append(v)
77+
metadata["network"]["interface"].append(v)
7878
end
7979
end
8080

81-
volume_attachments_data = fetch_metadata('volumeAttachments')
81+
volume_attachments_data = fetch_metadata("volumeAttachments")
8282

8383
unless volume_attachments_data.nil?
84-
metadata['volumes'] = Mash.new
84+
metadata["volumes"] = Mash.new
8585
volume_attachments_data.each do |k, v|
86-
metadata['volumes'][k] = v
86+
metadata["volumes"][k] = v
8787
end
8888
end
8989

spec/unit/mixin/oci_metadata_spec.rb

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
# limitations under the License.
1818
#
1919

20-
require 'spec_helper'
21-
require 'ohai/mixin/oci_metadata'
20+
require "spec_helper"
21+
require "ohai/mixin/oci_metadata"
2222

2323
describe Ohai::Mixin::OCIMetadata do
2424
let(:mixin) do
@@ -27,84 +27,84 @@
2727
end
2828

2929
before do
30-
logger = instance_double('Mixlib::Log::Child', trace: nil, debug: nil, warn: nil)
30+
logger = instance_double("Mixlib::Log::Child", trace: nil, debug: nil, warn: nil)
3131
allow(mixin).to receive(:logger).and_return(logger)
3232
end
3333

34-
describe '#http_get' do
35-
it 'gets the passed URI' do
36-
http_mock = double('http')
34+
describe "#http_get" do
35+
it "gets the passed URI" do
36+
http_mock = double("http")
3737
allow(http_mock).to receive(:read_timeout=)
3838
allow(Net::HTTP).to receive(:start).with(Ohai::Mixin::OCIMetadata::OCI_METADATA_ADDR).and_return(http_mock)
3939

4040
expect(http_mock).to receive(:get).with(Ohai::Mixin::OCIMetadata::OCI_METADATA_ADDR,
41-
{ 'Authorization' => 'Bearer Oracle',
42-
'User-Agent' => "chef-ohai/#{Ohai::VERSION}" })
41+
{ "Authorization" => "Bearer Oracle",
42+
"User-Agent" => "chef-ohai/#{Ohai::VERSION}" })
4343
mixin.http_get(Ohai::Mixin::OCIMetadata::OCI_METADATA_ADDR)
4444
end
4545
end
4646

47-
describe '#fetch_metadata' do
48-
it 'returns an empty hash given a non-200 response' do
49-
http_mock = double('http', { code: '404' })
47+
describe "#fetch_metadata" do
48+
it "returns an empty hash given a non-200 response" do
49+
http_mock = double("http", { code: "404" })
5050
allow(mixin).to receive(:http_get).and_return(http_mock)
5151

5252
expect(mixin.logger).to receive(:warn)
5353
vals = mixin.fetch_metadata
5454
expect(vals).to eq(nil)
5555
end
5656

57-
it 'returns a populated hash given valid JSON response' do
58-
http_mock = double('http', { code: '200', body: '{ "foo": "bar"}' })
57+
it "returns a populated hash given valid JSON response" do
58+
http_mock = double("http", { code: "200", body: '{ "foo": "bar"}' })
5959
allow(mixin).to receive(:http_get).and_return(http_mock)
6060

6161
expect(mixin.logger).not_to receive(:warn)
6262
vals = mixin.fetch_metadata
63-
expect(vals).to eq({ 'foo' => 'bar' })
63+
expect(vals).to eq({ "foo" => "bar" })
6464
end
6565
end
6666

67-
describe '#chassis_asset_tag' do
68-
context 'on Windows platform' do
67+
describe "#chassis_asset_tag" do
68+
context "on Windows platform" do
6969
before do
70-
stub_const('RUBY_PLATFORM', 'mswin')
70+
stub_const("RUBY_PLATFORM", "mswin")
7171
end
7272

73-
it 'calls get_chassis_asset_tag_windows' do
74-
expect(mixin).to receive(:get_chassis_asset_tag_windows).and_return('test-asset-tag')
75-
expect(mixin.chassis_asset_tag).to eq('test-asset-tag')
73+
it "calls get_chassis_asset_tag_windows" do
74+
expect(mixin).to receive(:get_chassis_asset_tag_windows).and_return("test-asset-tag")
75+
expect(mixin.chassis_asset_tag).to eq("test-asset-tag")
7676
end
7777
end
7878

79-
context 'on non-Windows platform' do
79+
context "on non-Windows platform" do
8080
before do
81-
stub_const('RUBY_PLATFORM', 'linux')
81+
stub_const("RUBY_PLATFORM", "linux")
8282
end
8383

84-
it 'calls get_chassis_asset_tag_linux' do
85-
expect(mixin).to receive(:get_chassis_asset_tag_linux).and_return('test-asset-tag')
86-
expect(mixin.chassis_asset_tag).to eq('test-asset-tag')
84+
it "calls get_chassis_asset_tag_linux" do
85+
expect(mixin).to receive(:get_chassis_asset_tag_linux).and_return("test-asset-tag")
86+
expect(mixin.chassis_asset_tag).to eq("test-asset-tag")
8787
end
8888
end
8989
end
9090

91-
describe '#get_chassis_asset_tag_linux' do
91+
describe "#get_chassis_asset_tag_linux" do
9292
let(:chassis_file) { Ohai::Mixin::OCIMetadata::CHASSIS_ASSET_TAG_FILE }
9393

94-
it 'returns asset tag when file exists and is readable' do
94+
it "returns asset tag when file exists and is readable" do
9595
allow(::File).to receive(:exist?).with(chassis_file).and_return(true)
9696
allow(::File).to receive(:read).with(chassis_file).and_return(" OracleCloud.com \n")
9797

98-
expect(mixin.get_chassis_asset_tag_linux).to eq('OracleCloud.com')
98+
expect(mixin.get_chassis_asset_tag_linux).to eq("OracleCloud.com")
9999
end
100100

101-
it 'returns nil when file does not exist' do
101+
it "returns nil when file does not exist" do
102102
allow(::File).to receive(:exist?).with(chassis_file).and_return(false)
103103

104104
expect(mixin.get_chassis_asset_tag_linux).to be_nil
105105
end
106106

107-
it 'returns nil when file read fails' do
107+
it "returns nil when file read fails" do
108108
allow(::File).to receive(:exist?).with(chassis_file).and_return(true)
109109
allow(::File).to receive(:read).with(chassis_file).and_raise(Errno::EACCES)
110110

@@ -113,36 +113,36 @@
113113
end
114114
end
115115

116-
describe '#get_chassis_asset_tag_windows' do
117-
let(:wmi_mock) { double('WmiLite::Wmi') }
118-
let(:enclosure_mock) { { 'SMBIOSAssetTag' => 'OracleCloud.com' } }
116+
describe "#get_chassis_asset_tag_windows" do
117+
let(:wmi_mock) { double("WmiLite::Wmi") }
118+
let(:enclosure_mock) { { "SMBIOSAssetTag" => "OracleCloud.com" } }
119119

120120
before do
121121
allow(mixin).to receive(:require)
122-
stub_const('WmiLite::Wmi', double(new: wmi_mock))
122+
stub_const("WmiLite::Wmi", double(new: wmi_mock))
123123
end
124124

125-
it 'returns asset tag from WMI when available' do
126-
allow(wmi_mock).to receive(:first_of).with('Win32_SystemEnclosure').and_return(enclosure_mock)
125+
it "returns asset tag from WMI when available" do
126+
allow(wmi_mock).to receive(:first_of).with("Win32_SystemEnclosure").and_return(enclosure_mock)
127127

128-
expect(mixin.get_chassis_asset_tag_windows).to eq('OracleCloud.com')
128+
expect(mixin.get_chassis_asset_tag_windows).to eq("OracleCloud.com")
129129
end
130130

131-
it 'returns nil when WMI query returns nil' do
132-
allow(wmi_mock).to receive(:first_of).with('Win32_SystemEnclosure').and_return(nil)
131+
it "returns nil when WMI query returns nil" do
132+
allow(wmi_mock).to receive(:first_of).with("Win32_SystemEnclosure").and_return(nil)
133133

134134
expect(mixin.get_chassis_asset_tag_windows).to be_nil
135135
end
136136

137-
it 'returns nil when WMI query fails' do
138-
allow(wmi_mock).to receive(:first_of).with('Win32_SystemEnclosure').and_raise(StandardError.new('WMI error'))
137+
it "returns nil when WMI query fails" do
138+
allow(wmi_mock).to receive(:first_of).with("Win32_SystemEnclosure").and_raise(StandardError.new("WMI error"))
139139

140140
expect(mixin.logger).to receive(:debug).with(/Failed to read chassis asset tag from WMI/)
141141
expect(mixin.get_chassis_asset_tag_windows).to be_nil
142142
end
143143

144-
it 'returns nil when SMBIOSAssetTag is not present' do
145-
allow(wmi_mock).to receive(:first_of).with('Win32_SystemEnclosure').and_return({})
144+
it "returns nil when SMBIOSAssetTag is not present" do
145+
allow(wmi_mock).to receive(:first_of).with("Win32_SystemEnclosure").and_return({})
146146

147147
expect(mixin.get_chassis_asset_tag_windows).to be_nil
148148
end

0 commit comments

Comments
 (0)