hetznercloud / terraform-provider-hcloud

Terraform Hetzner Cloud provider
https://registry.terraform.io/providers/hetznercloud/hcloud/latest
Mozilla Public License 2.0
518 stars 74 forks source link

[Feature]: Enhance Data Source: hcloud_server_type to return locations for the server type #520

Open gc-ss opened 2 years ago

gc-ss commented 2 years ago

What whould you like to see?

https://registry.terraform.io/providers/hetznercloud/hcloud/latest/docs/data-sources/server_type at the moment returns some specs for the server type but leaves out rich data as available from the API: https://docs.hetzner.cloud/#server-types-get-a-server-type

One benefit of the rich data is to ensure:

  1. The requested server_type exists at the location
  2. The requested server_type prices at the location is within budget (price_hourly, price_monthly) and estimate the cost of running a stack
gc-ss commented 2 years ago

Current workaround for 1:

def ensure_server_type_exists(token, server_type_name, server_location):
    server_type_name = server_type_name.lower()        
    response = httpx.get(f'https://api.hetzner.cloud/v1/server_types?name={server_type_name}', headers={"Authorization": f"Bearer {token}"})
    if response.status_code == HTTPStatus.OK.value:
        response = response.json()
        if response:
            _server_types = response.get('server_types', None)
            if _server_types:
                if len(_server_types) != 1:
                    raise ValueError(f"Expected to find unique server type {server_type_name}, instead found '{_server_types}'")
                else:        
                    _prices = _server_types[0].get('prices', None)
                    if _prices:
                        locations = {_price.get('location', None) for _price in _prices}
                        if server_location not in locations:
                            raise RuntimeError(f"Server type '{server_type_name}' unsupported at location '{server_location}'. Available at locations {locations}")
            else:
                raise ValueError(f"Did not find server type {server_type_name}")
                # TODO: ServerTypeUnknown exception
    else:
        raise RuntimeError(f"Failed to lookup server types")
github-actions[bot] commented 1 year ago

This issue has been marked as stale because it has not had recent activity. The bot will close the issue if no further action occurs.

apricote commented 1 year ago

Sounds like a good addition.

Right now the available server types are exposed in the data.hcloud_datacenter data source: https://registry.terraform.io/providers/hetznercloud/hcloud/latest/docs/data-sources/datacenter#available_server_type_ids

github-actions[bot] commented 10 months ago

This issue has been marked as stale because it has not had recent activity. The bot will close the issue if no further action occurs.