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

Is there any way to link server_types with locations? #384

Closed
DravenJohnson opened this issue Apr 18, 2024 · 3 comments
Closed

Is there any way to link server_types with locations? #384

DravenJohnson opened this issue Apr 18, 2024 · 3 comments
Labels
documentation Documentation Changes question Further information is requested

Comments

@DravenJohnson
Copy link

Bug Report

Current Behavior

We have an API wrapper, which create instance from the client.servers.create(). And it takes:

  • location (random selected from client.locations.get_all()) and
  • server_types (fixed value and get from client.server_types.get_by_name(server_type_name))

We were using cx21 to test but then we noticed the API will throw error if cx21 isn unavailable in certain locations, like "Hillsboro US"

Input Code

instance = client.servers.create(
          name="test-server"
          server_type=client.server_types.get_by_name('cx21')
          location=random.choice(client.locations.get_all())
)

Expected behavior/code
It works for some location like fsn1 because in this location, cx21 type exist. But for something in US, only cpx21 available.

I read through the doc i didn't see anywhere I can link (or get) the available server_types based on location.

Environment

Not Related

Possible Solution

add another function like client.sever_type.get_by_location()?

Additional context/Screenshots
N/A

@jooola jooola assigned jooola and unassigned jooola Apr 18, 2024
@jooola jooola added question Further information is requested documentation Documentation Changes labels Apr 22, 2024
@apricote
Copy link
Member

apricote commented Apr 22, 2024

Hey @DravenJohnson,

the available server types are available through the Datacenter API endpoint (Location <- Datacenter -> Server Types): https://hcloud-python.readthedocs.io/en/stable/api.clients.datacenters.html#hcloud.datacenters.domain.DatacenterServerTypes

https://docs.hetzner.cloud/#datacenters-get-all-datacenters

Some untested pseudo-code:

server_type = client.server_types.get_by_name('cx21')
datacenter = random.choice(
  [
    dc for dc in client.datacenters.get_all() if any(
      available_type.id == server_type.id for available_type in dc.server_types.available
    )
  ]
)
instance = client.servers.create(
          name="test-server"
          server_type=server_type
          datacenter=datacenter
)

By comparing the IDs of the server types, we can avoid additional API requests to get the name of each server type in the check. (available_type.id == server_type.id)

Please note, that it is possible, that a server type is temporarily unavailable in all locations/datacenters.

@DravenJohnson
Copy link
Author

Thanks, we noticed that.

So technically, when creating a new server, we just have to pick one of the following right?

I can use datacenter instead of location

Also datacenter include a datacenter.location.country which is the country code we currently using in location.country.

@jooola
Copy link
Member

jooola commented May 24, 2024

So technically, when creating a new server, we just have to pick one of the following right?

Yes.

Closing as completed, feel free to ask again if you think something is missing.

@jooola jooola closed this as completed May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Documentation Changes question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants