-
Notifications
You must be signed in to change notification settings - Fork 456
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The DNS hosts feature allows DNS records to be created that point to a given IP address. This is useful for aliasing as well as setting up round-robin DNS (by creating multiple hosts that point to the same IP address). This creates a new datasource called libvirt_network_dns_hosts_template, which can be used to populate the dns_host attribute in libvirt_network resources. The use of a datasource is required in order to dynamically generate dns_host entries (since 'count' is the only looping construct in Terraform and 'count' is limited to datasources and resources). The list of hostnames has also been replaced by a single hostname with the option of declaring multiple dns_host blocks with the same IP address. I originally tried to externally generate a variable that would host the list of maps needed for the dns_host attribute, but Terraform explicitly disallows lists of maps greater than length one. Instead, I decided to follow the pattern established by template_file and create a resource that can be rendered. The hostnames attribute had to be replaced with hostname because the underlying schema for the 'rendered' attribute is a TypeMap. In Terraform, this is not a generic map, but is instead limited to map[string]string. To accommodate this, multiple dns_host blocks can be defined with the same IP address, one for each hostname.
- Loading branch information
Showing
6 changed files
with
304 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package libvirt | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"strconv" | ||
|
||
"github.com/hashicorp/terraform/helper/hashcode" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
// a libvirt network DNS host template datasource | ||
// | ||
// Datasource example: | ||
// | ||
// data "libvirt_network_dns_host_template" "k8smasters" { | ||
// count = "${var.master_count}" | ||
// ip = "${var.master_ips[count.index]}" | ||
// hostname = "master-${count.index}" | ||
// } | ||
// | ||
// resource "libvirt_network" "k8snet" { | ||
// ... | ||
// dns = [{ | ||
// hosts = [ "${flatten(data.libvirt_network_dns_host_template.k8smasters.*.rendered)}" ] | ||
// }] | ||
// ... | ||
// } | ||
// | ||
func datasourceLibvirtNetworkDNSHostTemplate() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: resourceLibvirtNetworkDNSHostRead, | ||
Schema: map[string]*schema.Schema{ | ||
"ip": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"hostname": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"rendered": { | ||
Type: schema.TypeMap, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceLibvirtNetworkDNSHostRead(d *schema.ResourceData, meta interface{}) error { | ||
dnsHost := map[string]interface{}{} | ||
if address, ok := d.GetOk("ip"); ok { | ||
ip := net.ParseIP(address.(string)) | ||
if ip == nil { | ||
return fmt.Errorf("Could not parse address '%s'", address) | ||
} | ||
dnsHost["ip"] = ip.String() | ||
} | ||
if hostname, ok := d.GetOk("hostname"); ok { | ||
dnsHost["hostname"] = hostname.(string) | ||
} | ||
d.Set("rendered", dnsHost) | ||
d.SetId(strconv.Itoa(hashcode.String(fmt.Sprintf("%v", dnsHost)))) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package libvirt | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccLibvirtNetworkDataSource_DNSHostTemplate(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckLibvirtNetworkDestroy, | ||
Steps: []resource.TestStep{ | ||
|
||
{ | ||
Config: `data "libvirt_network_dns_host_template" "bootstrap" { | ||
count = 2 | ||
ip = "1.1.1.${count.index}" | ||
hostname = "myhost${count.index}" | ||
}`, | ||
Check: resource.ComposeTestCheckFunc( | ||
checkDNSHostTemplate("data.libvirt_network_dns_host_template.bootstrap.0", "ip", "1.1.1.0"), | ||
checkDNSHostTemplate("data.libvirt_network_dns_host_template.bootstrap.0", "hostname", "myhost0"), | ||
checkDNSHostTemplate("data.libvirt_network_dns_host_template.bootstrap.1", "ip", "1.1.1.1"), | ||
checkDNSHostTemplate("data.libvirt_network_dns_host_template.bootstrap.1", "hostname", "myhost1"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func checkDNSHostTemplate(id, name, value string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[id] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", id) | ||
} | ||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No ID is set") | ||
} | ||
|
||
v := rs.Primary.Attributes[name] | ||
if v != value { | ||
return fmt.Errorf( | ||
"Value for %s is %s, not %s", name, v, value) | ||
} | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters