Skip to content
This repository has been archived by the owner on Jun 19, 2020. It is now read-only.

(FACT-2620) Add EC2 facts for linux #544

Merged
merged 1 commit into from
Jun 12, 2020
Merged

(FACT-2620) Add EC2 facts for linux #544

merged 1 commit into from
Jun 12, 2020

Conversation

oanatmaria
Copy link
Contributor

No description provided.

lib/resolvers/ec2.rb Show resolved Hide resolved
Comment on lines 52 to 58
http = Net::HTTP.new(parsed_url.host)

session_timeout = ENV['EC2_SESSION_TIMEOUT']
http.read_timeout = session_timeout ? session_timeout.to_i : EC2_SESSION_TIMEOUT
http.open_timeout = EC2_CONNECTION_TIMEOUT
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be moved in a method called config_http

Comment on lines 38 to 42
if !name.end_with?('/')
container[name] = get_data_from("#{url}#{name}").strip
else
child = {}
child[name] = query_for_metadata("#{url}#{name}", child)
child.reject! { |key, _info| key == name }
name = name.gsub(%r{/$}, '')
container[name] = child
Copy link
Contributor

@Filipovici-Andrei Filipovici-Andrei Jun 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be moved in a method called name_parsing or something better. Even name should be changed into something more suggestive, something like metadata_name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not move this code into another method as this method is a recursive one and i think is more easy to understand if the recursive call is made from inside it.

http = Net::HTTP.new(parsed_url.host)

session_timeout = ENV['EC2_SESSION_TIMEOUT']
http.read_timeout = session_timeout ? session_timeout.to_i : EC2_SESSION_TIMEOUT
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition can be moved where the constant is declared.EC2_SESSION_TIMEOUT = ENV['k'] || 5
Or, create a method that returns the timeout and will also handle the to_i part.

Copy link
Contributor

@BogdanIrimie BogdanIrimie Jun 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would go even further and extract all the logic for timeout in a different method e.g.

EC2_SESSION_TIMEOUT = ENV['EC2_SESSION_TIMEOUT'].to_i || 5
...
def configure_timout(http)
  http.read_timeout = EC2_SESSION_TIMEOUT
  http.open_timeout = EC2_CONNECTION_TIMEOUT
end

lib/resolvers/ec2.rb Show resolved Hide resolved
lib/resolvers/ec2.rb Outdated Show resolved Hide resolved
lib/resolvers/ec2.rb Outdated Show resolved Hide resolved
lib/resolvers/ec2.rb Outdated Show resolved Hide resolved
lib/resolvers/ec2.rb Outdated Show resolved Hide resolved
lib/resolvers/ec2.rb Outdated Show resolved Hide resolved
@oanatmaria oanatmaria force-pushed the FACT-2620 branch 3 times, most recently from 27ea257 to b9a90c7 Compare June 11, 2020 12:28
@oanatmaria oanatmaria force-pushed the FACT-2620 branch 2 times, most recently from 97f6eef to ec374b4 Compare June 11, 2020 12:49
@gimmyxd
Copy link
Contributor

gimmyxd commented Jun 11, 2020

I suggest using https://github.com/bblimke/webmock to stub external APIs and disable external calls instead of mocking NET::HTTP.
This way, if the HTTP library will be changed in the future, the tests will not need adjustments

@oanatmaria oanatmaria force-pushed the FACT-2620 branch 2 times, most recently from c8f231a to cf03a9b Compare June 12, 2020 11:19
Facter::Resolvers::Xen.resolve(:vm)
end

def check_other_facts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this method be split into two? I don't see any dependency between the two resolver calls.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

virtual == 'kvm' || virtual =~ /xen/
end

def retrieve_from_virt_what
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rename this to check_... to be consistent with the other methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

module Linux
class Ec2Userdata
FACT_NAME = 'ec2_userdata'
HYPERVISORS_HASH = { 'VMware' => 'vmware', 'VirtualBox' => 'virtualbox', 'Parallels' => 'parallels',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this hash be moved to a different, common location? I saw that this is already defined in two other resolvers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@oanatmaria oanatmaria merged commit 96d2eac into master Jun 12, 2020
@oanatmaria oanatmaria deleted the FACT-2620 branch June 12, 2020 13:31
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants