Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Hetzner server identifier from API #2

Open
vkameswaran opened this issue Apr 24, 2024 · 1 comment
Open

Get Hetzner server identifier from API #2

vkameswaran opened this issue Apr 24, 2024 · 1 comment

Comments

@vkameswaran
Copy link

When setting up a VM Host with Hetzner, we manually enter the IP address and Hetzner server ID. The https://robot.hetzner.com/doc/webservice/en.html#get-server endpoint of the Hetzner webservice provides a list of all servers, allowing us to find the Hetzner identifier using the IP address.

Copy link

To implement a feature for retrieving a Hetzner server identifier using an IP address, you can extend the Hosting::HetznerApis class. Here's a step-by-step guide based on the existing code structure:

  1. Add a New Method in HetznerApis Class:
    Implement a new method in /lib/hosting/hetzner_apis.rb to fetch the server list and filter by IP address to find the server identifier.

    def find_server_id_by_ip(ip_address)
      connection = Excon.new(@host.connection_string, user: @host.user, password: @host.password)
      response = connection.get(path: '/server', expects: 200)
      servers = JSON.parse(response.body)
    
      server = servers.find { |s| s['server']['server_ip'] == ip_address }
      server ? server['server']['server_id'] : nil
    end
  2. Usage Example:
    This method can be called with the IP address to get the corresponding server identifier. Ensure that the IP address passed to this method is the main IP of the server as listed in the Hetzner Robot API.

  3. Integration:
    Integrate this method where you need to retrieve the server identifier based on the IP address, ensuring that the Hetzner API's rate limits and authentication mechanisms are respected.

This approach leverages the existing infrastructure for making API calls within the HetznerApis class and follows the pattern used for other methods like get_main_ip4 and pull_ips. Remember to handle potential errors and exceptions, especially concerning API call limits and unauthorized access.

References

greptileai/ubicloud/lib/hosting/hetzner_apis.rb
greptileai/ubicloud/spec/lib/hosting/hetzner_apis_spec.rb

Ask Greptile

@greptileai greptileai deleted a comment from greptile-apps bot Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant