Skip to content

Commit

Permalink
fix: apply chefstyle
Browse files Browse the repository at this point in the history
Signed-off-by: Renato Covarrubias <[email protected]>
  • Loading branch information
rnt committed Jan 24, 2023
1 parent 2e2ac23 commit 7ae399f
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 159 deletions.
14 changes: 7 additions & 7 deletions lib/ohai/mixin/oci_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

require 'net/http' unless defined?(Net::HTTP)
require "net/http" unless defined?(Net::HTTP)

module Ohai
module Mixin
module OCIMetadata
OCI_METADATA_ADDR = '169.254.169.254'
OCI_METADATA_URL = '/opc/v2'
OCI_METADATA_ADDR = "169.254.169.254"
OCI_METADATA_URL = "/opc/v2"

# fetch the meta content with a timeout and the required header
def http_get(uri)
Expand All @@ -31,15 +31,15 @@ def http_get(uri)
conn.get(
uri,
{
'Authorization' => 'Bearer Oracle',
'User-Agent' => "chef-ohai/#{Ohai::VERSION}"
"Authorization" => "Bearer Oracle",
"User-Agent" => "chef-ohai/#{Ohai::VERSION}",
}
)
end

def fetch_metadata(metadata = 'instance')
def fetch_metadata(metadata = "instance")
response = http_get("#{OCI_METADATA_URL}/#{metadata}")
return nil unless response.code == '200'
return nil unless response.code == "200"

if json?(response.body)
data = String(response.body)
Expand Down
6 changes: 3 additions & 3 deletions lib/ohai/plugins/cloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,9 @@ def on_oci?

# Fill cloud hash with OCI values
def oci_values
oci['metadata']['network']['interface'].each { |vnic| @cloud_attr_obj.add_ipv4_addr(vnic['privateIp'], :private) }
@cloud_attr_obj.local_hostname = oci['metadata']['compute']['hostname']
@cloud_attr_obj.provider = 'oci'
oci["metadata"]["network"]["interface"].each { |vnic| @cloud_attr_obj.add_ipv4_addr(vnic["privateIp"], :private) }
@cloud_attr_obj.local_hostname = oci["metadata"]["compute"]["hostname"]
@cloud_attr_obj.provider = "oci"
end

collect_data do
Expand Down
42 changes: 21 additions & 21 deletions lib/ohai/plugins/oci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@
#

Ohai.plugin(:Oci) do
require_relative '../mixin/oci_metadata'
require_relative '../mixin/http_helper'
require_relative "../mixin/oci_metadata"
require_relative "../mixin/http_helper"

include Ohai::Mixin::OCIMetadata
include Ohai::Mixin::HttpHelper

provides 'oci'
provides "oci"

collect_data do
oci_metadata_from_hints = hint?('oci')
oci_metadata_from_hints = hint?("oci")
if oci_metadata_from_hints
logger.trace('Plugin OCI: oci hint is present. Parsing any hint data.')
logger.trace("Plugin OCI: oci hint is present. Parsing any hint data.")
oci Mash.new
oci_metadata_from_hints.each { |k, v| oci[k] = v }
oci['metadata'] = parse_metadata
oci["metadata"] = parse_metadata
elsif oci_chassis_asset_tag?
logger.trace('Plugin oci: No hints present, but system appears to be on oci.')
logger.trace("Plugin oci: No hints present, but system appears to be on oci.")
oci Mash.new
oci['metadata'] = parse_metadata
oci["metadata"] = parse_metadata
else
logger.trace("Plugin oci: No hints present and doesn't appear to be on oci.")
false
Expand All @@ -45,11 +45,11 @@

def oci_chassis_asset_tag?
has_oci_chassis_asset_tag = false
if file_exist?('/sys/devices/virtual/dmi/id/chassis_asset_tag')
file_open('/sys/devices/virtual/dmi/id/chassis_asset_tag').each do |line|
if file_exist?("/sys/devices/virtual/dmi/id/chassis_asset_tag")
file_open("/sys/devices/virtual/dmi/id/chassis_asset_tag").each do |line|
next unless /OracleCloud.com/.match?(line)

logger.trace('Plugin oci: Found OracleCloud.com chassis_asset_tag used by oci.')
logger.trace("Plugin oci: Found OracleCloud.com chassis_asset_tag used by oci.")
has_oci_chassis_asset_tag = true
break
end
Expand All @@ -60,32 +60,32 @@ def oci_chassis_asset_tag?
def parse_metadata
return nil unless can_socket_connect?(Ohai::Mixin::OCIMetadata::OCI_METADATA_ADDR, 80)

instance_data = fetch_metadata('instance')
instance_data = fetch_metadata("instance")
return nil if instance_data.nil?

metadata = Mash.new
metadata['compute'] = Mash.new
metadata["compute"] = Mash.new

instance_data.each do |k, v|
metadata['compute'][k] = v
metadata["compute"][k] = v
end

vnics_data = fetch_metadata('vnics')
vnics_data = fetch_metadata("vnics")

unless vnics_data.nil?
metadata['network'] = Mash.new
metadata['network']['interface'] = []
metadata["network"] = Mash.new
metadata["network"]["interface"] = []
vnics_data.each do |v|
metadata['network']['interface'].append(v)
metadata["network"]["interface"].append(v)
end
end

volume_attachments_data = fetch_metadata('volumeAttachments')
volume_attachments_data = fetch_metadata("volumeAttachments")

unless volume_attachments_data.nil?
metadata['volumes'] = Mash.new
metadata["volumes"] = Mash.new
volume_attachments_data.each do |k, v|
metadata['volumes'][k] = v
metadata["volumes"][k] = v
end
end

Expand Down
34 changes: 17 additions & 17 deletions spec/unit/mixin/oci_metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
# limitations under the License.
#

require 'spec_helper'
require 'ohai/mixin/oci_metadata'
require "spec_helper"
require "ohai/mixin/oci_metadata"

describe Ohai::Mixin::OCIMetadata do
let(:mixin) do
Expand All @@ -27,40 +27,40 @@
end

before do
logger = instance_double('Mixlib::Log::Child', trace: nil, debug: nil, warn: nil)
logger = instance_double("Mixlib::Log::Child", trace: nil, debug: nil, warn: nil)
allow(mixin).to receive(:logger).and_return(logger)
end

describe '#http_get' do
it 'gets the passed URI' do
http_mock = double('http')
describe "#http_get" do
it "gets the passed URI" do
http_mock = double("http")
allow(http_mock).to receive(:read_timeout=)
allow(Net::HTTP).to receive(:start).with('169.254.169.254').and_return(http_mock)
allow(Net::HTTP).to receive(:start).with("169.254.169.254").and_return(http_mock)

expect(http_mock).to receive(:get).with('169.254.169.254',
{ 'Authorization' => 'Bearer Oracle',
'User-Agent' => "chef-ohai/#{Ohai::VERSION}" })
mixin.http_get('169.254.169.254')
expect(http_mock).to receive(:get).with("169.254.169.254",
{ "Authorization" => "Bearer Oracle",
"User-Agent" => "chef-ohai/#{Ohai::VERSION}" })
mixin.http_get("169.254.169.254")
end
end

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

expect(mixin.logger).not_to receive(:warn)
vals = mixin.fetch_metadata
expect(vals).to eq(nil)
end

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

expect(mixin.logger).not_to receive(:warn)
vals = mixin.fetch_metadata
expect(vals).to eq({ 'foo' => 'bar' })
expect(vals).to eq({ "foo" => "bar" })
end
end
end
34 changes: 17 additions & 17 deletions spec/unit/plugins/cloud_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -512,41 +512,41 @@
end
end

describe 'with OCI mash' do
describe "with OCI mash" do
before do
@plugin[:oci] = Mash.new
@plugin[:oci][:metadata] = {
'compute' => {
'hostname' => 'my-hostname'
"compute" => {
"hostname" => "my-hostname",
},
"network" => {
"interface" => [
{ "vnicId" => "ocid1.vnic.oc1.phx.exampleuniqueID", "privateIp" => "10.0.3.6", "vlanTag" => 11,
"macAddr" => "00:00:00:00:00:01", "virtualRouterIp" => "10.0.3.1", "subnetCidrBlock" => "10.0.3.0/24",
"nicIndex" => 0 },
],
},
'network' => {
'interface' => [
{ 'vnicId' => 'ocid1.vnic.oc1.phx.exampleuniqueID', 'privateIp' => '10.0.3.6', 'vlanTag' => 11,
'macAddr' => '00:00:00:00:00:01', 'virtualRouterIp' => '10.0.3.1', 'subnetCidrBlock' => '10.0.3.0/24',
'nicIndex' => 0 }
]
}
}
end

it "doesn't populates cloud vm_name" do
@plugin.run
expect(@plugin[:cloud][:vm_name]).not_to eq('testtest')
expect(@plugin[:cloud][:vm_name]).not_to eq("testtest")
end

it 'populates cloud local_hostname' do
it "populates cloud local_hostname" do
@plugin.run
expect(@plugin[:cloud][:local_hostname]).to eq('my-hostname')
expect(@plugin[:cloud][:local_hostname]).to eq("my-hostname")
end

it 'populates cloud private ip' do
it "populates cloud private ip" do
@plugin.run
expect(@plugin[:cloud][:local_ipv4]).to eq(@plugin[:oci][:metadata][:network][:interface][0]['privateIp'])
expect(@plugin[:cloud][:local_ipv4]).to eq(@plugin[:oci][:metadata][:network][:interface][0]["privateIp"])
end

it 'populates cloud provider' do
it "populates cloud provider" do
@plugin.run
expect(@plugin[:cloud][:provider]).to eq('oci')
expect(@plugin[:cloud][:provider]).to eq("oci")
end
end
end
Loading

0 comments on commit 7ae399f

Please sign in to comment.