-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
473932b
commit bfd0b64
Showing
10 changed files
with
506 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module Facts | ||
module Linux | ||
class Gce | ||
FACT_NAME = 'gce' | ||
|
||
def call_the_resolver | ||
bios_vendor = Facter::Resolvers::Linux::DmiBios.resolve(:bios_vendor) | ||
|
||
fact_value = bios_vendor&.include?('Google') ? Facter::Resolvers::Gce.resolve(:metadata) : nil | ||
Facter::ResolvedFact.new(FACT_NAME, fact_value) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module Facts | ||
module Windows | ||
class Gce | ||
FACT_NAME = 'gce' | ||
|
||
def call_the_resolver | ||
virtualization = Facter::Resolvers::Virtualization.resolve(:virtual) | ||
|
||
fact_value = virtualization&.include?('gce') ? Facter::Resolvers::Gce.resolve(:metadata) : nil | ||
Facter::ResolvedFact.new(FACT_NAME, fact_value) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
module Facter | ||
module Resolvers | ||
class Gce < BaseResolver | ||
@semaphore = Mutex.new | ||
@fact_list ||= {} | ||
METADATA_URL = 'http://metadata.google.internal/computeMetadata/v1/?recursive=true&alt=json' | ||
HEADERS = { "Metadata-Flavor": 'Google', "Accept": 'application/json' }.freeze | ||
|
||
class << self | ||
private | ||
|
||
def post_resolve(fact_name) | ||
log.debug('reading Gce metadata') | ||
@fact_list.fetch(fact_name) { read_facts(fact_name) } | ||
end | ||
|
||
def read_facts(fact_name) | ||
@fact_list[:metadata] = query_for_metadata | ||
@fact_list[fact_name] | ||
end | ||
|
||
def query_for_metadata | ||
gce_data = extract_to_hash(Utils::Http.get_request(METADATA_URL, HEADERS)) | ||
parse_instance(gce_data) | ||
|
||
gce_data.empty? ? nil : gce_data | ||
end | ||
|
||
def extract_to_hash(metadata) | ||
JSON.parse(metadata) | ||
rescue JSON::ParserError => e | ||
log.debug("Trying to parse result but got: #{e.message}") | ||
{} | ||
end | ||
|
||
def parse_instance(gce_data) | ||
instance_data = gce_data['instance'] | ||
return if instance_data.nil? || instance_data.empty? | ||
|
||
%w[image machineType zone].each do |key| | ||
instance_data[key] = instance_data[key].split('/').last if instance_data[key] | ||
end | ||
|
||
network = instance_data.dig('networkInterfaces', 0, 'network') | ||
instance_data['networkInterfaces'][0]['network'] = network.split('/').last unless network.nil? | ||
|
||
gce_data['instance'] = instance_data | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
module Facter | ||
module Resolvers | ||
module Utils | ||
module Http | ||
class << self | ||
CONNECTION_TIMEOUT = 0.6 | ||
SESSION_TIMEOUT = 5 | ||
@log = Facter::Log.new(self) | ||
|
||
# Makes a GET http request and returns it's response. | ||
# | ||
# Params: | ||
# url: String which contains the address to which the request will be made | ||
# headers: Hash which contains the headers you need to add to your request. | ||
# Default headers is an empty hash | ||
# Example: { "Accept": 'application/json' } | ||
# timeouts: Hash that includes the values for the session and connection timeouts. | ||
# Example: { session: 2.4. connection: 5 } | ||
# | ||
# Return value: | ||
# is a string with the response body if the response code is 200. | ||
# If the response code is not 200, an empty string is returned. | ||
def get_request(url, headers = {}, timeouts = {}) | ||
make_request(url, headers, timeouts, 'GET') | ||
end | ||
|
||
private | ||
|
||
def make_request(url, headers, timeouts, request_type) | ||
require 'net/http' | ||
|
||
uri = URI.parse(url) | ||
http = http_obj(uri, timeouts) | ||
request = request_obj(headers, uri, request_type) | ||
|
||
# Make the request | ||
resp = http.request(request) | ||
response_code_valid?(resp.code.to_i) ? resp.body : '' | ||
rescue StandardError => e | ||
@log.debug("Trying to connect to #{url} but got: #{e.message}") | ||
'' | ||
end | ||
|
||
def http_obj(parsed_url, timeouts) | ||
http = Net::HTTP.new(parsed_url.host) | ||
http.read_timeout = timeouts[:session] || SESSION_TIMEOUT | ||
http.open_timeout = timeouts[:connection] || CONNECTION_TIMEOUT | ||
http | ||
end | ||
|
||
def request_obj(headers, parsed_url, request_type) | ||
return Net::HTTP::Get.new(parsed_url.request_uri, headers) if request_type == 'GET' | ||
|
||
raise StandardError("Unknown http request type: #{request_type}") | ||
end | ||
|
||
def response_code_valid?(http_code) | ||
@log.debug("Request failed with error code #{http_code}") unless http_code.equal?(200) | ||
http_code.equal?(200) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Facts::Linux::Gce do | ||
describe '#call_the_resolver' do | ||
subject(:fact) { Facts::Linux::Gce.new } | ||
|
||
before do | ||
allow(Facter::Resolvers::Gce).to receive(:resolve).with(:metadata).and_return(value) | ||
allow(Facter::Resolvers::Linux::DmiBios).to receive(:resolve).with(:bios_vendor).and_return(vendor) | ||
end | ||
|
||
context 'when hypervisor is Gce' do | ||
let(:vendor) { 'Google' } | ||
let(:value) do | ||
{ | ||
'oslogin' => { | ||
'authenticate' => { | ||
'sessions' => { | ||
} | ||
} | ||
}, | ||
'project' => { | ||
'numericProjectId' => 728_618_928_092, | ||
'projectId' => 'facter-performance-history' | ||
} | ||
} | ||
end | ||
|
||
it 'calls Facter::Resolvers::Linux::Gce' do | ||
fact.call_the_resolver | ||
expect(Facter::Resolvers::Gce).to have_received(:resolve).with(:metadata) | ||
end | ||
|
||
it 'calls Facter::Resolvers::Linux::DmiBios' do | ||
fact.call_the_resolver | ||
expect(Facter::Resolvers::Linux::DmiBios).to have_received(:resolve).with(:bios_vendor) | ||
end | ||
|
||
it 'returns gce fact' do | ||
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \ | ||
have_attributes(name: 'gce', value: value) | ||
end | ||
end | ||
|
||
context 'when hypervisor is not Gce' do | ||
let(:vendor) { 'unknown' } | ||
let(:value) { nil } | ||
|
||
it 'returns nil' do | ||
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \ | ||
have_attributes(name: 'gce', value: nil) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.