timohirt / terraform-provider-hetznerdns

Terraform provider for Hetzner DNS
Mozilla Public License 2.0
106 stars 21 forks source link

zone_id should not be required for creating a record #17

Closed c33s closed 4 years ago

c33s commented 4 years ago

comming from the powerdns provider where the id of a zone is simply its name, the current implemenation of the provider (and api?) is a little bit un-DRY

we have to write:

resource "hetznerdns_zone" "example_at" {
    name = "example.at"
    ttl = 3600
}
data "hetznerdns_zone" "example_at" {
    name = "example_at"
}
resource "hetznerdns_record" "example_at" {
    zone_id = "${hetznerdns_zone.example_at.id}"
    name = "@"
    value = "127.0.0.1"
    type = "A"
    ttl= 3600
}

instead of simply

resource "hetznerdns_zone" "example_at" {
    name = "example.at"
    ttl = 3600
}
resource "hetznerdns_record" "example_at" {
    zone = "example.at"
    name = "@"
    value = "127.0.0.1"
    type = "A"
    ttl= 3600
}

i assume this is because of hetzners api implemenation but maybe it would be more handy to abstract that and make the datasource access internal.

timohirt commented 4 years ago

You don't need the data source in the first example. It is never used. You only need the data source if the corresponding resource is created in another project.

resource "hetznerdns_zone" "example_at" {
    name = "example.at"
    ttl = 3600
}
resource "hetznerdns_record" "example_at" {
    zone_id = hetznerdns_zone.example_at.id
    name = "@"
    value = "127.0.0.1"
    type = "A"
    ttl= 3600
}

Above I copied and modified your code. Terraform uses the reference to hetznerdns_zone.example_at.id to determine the order in this resources are being created. Without the reference it might create the record before the zone, which would result in errors.

timohirt commented 4 years ago

Hey @c33s! Have you tried removing the data source? It is not used in the code you provided and I think you can just remove it. I'll close this issue. Please reopen if you still need the data source.

c33s commented 4 years ago

the ticket isn't about the datasource, yes i included the datasource as it was in the example in the docs but it's of course not required.

this ticket is about the requirement to access a zone over a id instead of it's name. a zones name is unique, so why not access it via its name. see the zone instead of the zone_id

resource "hetznerdns_record" "example_at" {
    zone_id = "${hetznerdns_zone.example_at.id}"
    name = "@"
    value = "127.0.0.1"
    type = "A"
    ttl= 3600
}
resource "hetznerdns_record" "example_at" {
    zone = "example.at"
    name = "@"
    value = "127.0.0.1"
    type = "A"
    ttl= 3600
}

internally the provider could fetch the zone ids over the datasource (and maybe cache them?) and let the developer access the zones by their real name. it's a general hetzner problem that there are sometimes unneeded ids instead of the real unique identifieres of a resource (like the name of a zone for dns or the fingerprint of an ssh key . for accessing those resources we have to make our way trough datasources even if we already have or know a unique id).