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

Make 'option_value' for dnsmasq optional #960

Merged
merged 2 commits into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libvirt/data_source_libvirt_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func datasourceLibvirtNetworkDnsmasqOptionsTemplate() *schema.Resource {
},
"option_value": {
Type: schema.TypeString,
Required: true,
Optional: true,
},
"rendered": {
Type: schema.TypeMap,
Expand Down
9 changes: 8 additions & 1 deletion libvirt/data_source_libvirt_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func TestAccLibvirtNetworkDataSource_DNSDnsmasqTemplate(t *testing.T) {
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{

{
Config: `data "libvirt_network_dnsmasq_options_template" "options" {
count = 2
Expand All @@ -94,6 +93,14 @@ func TestAccLibvirtNetworkDataSource_DNSDnsmasqTemplate(t *testing.T) {
checkTemplate("data.libvirt_network_dnsmasq_options_template.options.1", "option_value", "/.apps.tt1.testing/1.1.1.2"),
),
},
{
Config: `data "libvirt_network_dnsmasq_options_template" "noval_options" {
option_name = "no-hosts"
}`,
Check: resource.ComposeTestCheckFunc(
checkTemplate("data.libvirt_network_dnsmasq_options_template.noval_options", "option_name", "no-hosts"),
),
},
},
})
}
10 changes: 8 additions & 2 deletions libvirt/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,18 @@ func getDNSMasqOptionFromResource(d *schema.ResourceData) ([]libvirtxml.NetworkD
dnsmasqOptionPrefix := "dnsmasq_options.0"
if dnsmasqOptionCount, ok := d.GetOk(dnsmasqOptionPrefix + ".options.#"); ok {
for i := 0; i < dnsmasqOptionCount.(int); i++ {
var optionString string
dnsmasqOptionsPrefix := fmt.Sprintf(dnsmasqOptionPrefix+".options.%d", i)

optionName := d.Get(dnsmasqOptionsPrefix + ".option_name").(string)
optionValue := d.Get(dnsmasqOptionsPrefix + ".option_value").(string)
optionValue, ok := d.GetOk(dnsmasqOptionsPrefix + ".option_value")
if ok {
optionString = optionName + "=" + optionValue.(string)
} else {
optionString = optionName
}
dnsmasqOption = append(dnsmasqOption, libvirtxml.NetworkDnsmasqOption{
Value: optionName + "=" + optionValue,
Value: optionString,
})
}
}
Expand Down
5 changes: 5 additions & 0 deletions libvirt/resource_libvirt_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,16 +653,21 @@ func TestAccLibvirtNetwork_DnsmasqOptions(t *testing.T) {
option_name = "address"
option_value = "/.apps.tt.testing/1.1.1.2"
}
options {
option_name = "no-hosts"
}
}
}`, randomNetworkResource, randomNetworkName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("libvirt_network."+randomNetworkResource, "dnsmasq_options.0.options.0.option_name", "server"),
resource.TestCheckResourceAttr("libvirt_network."+randomNetworkResource, "dnsmasq_options.0.options.0.option_value", "/tt.testing/1.1.1.1"),
resource.TestCheckResourceAttr("libvirt_network."+randomNetworkResource, "dnsmasq_options.0.options.1.option_name", "address"),
resource.TestCheckResourceAttr("libvirt_network."+randomNetworkResource, "dnsmasq_options.0.options.1.option_value", "/.apps.tt.testing/1.1.1.2"),
resource.TestCheckResourceAttr("libvirt_network."+randomNetworkResource, "dnsmasq_options.0.options.2.option_name", "no-hosts"),
testAccCheckDnsmasqOptions("libvirt_network."+randomNetworkResource, []libvirtxml.NetworkDnsmasqOption{
{Value: "server=/tt.testing/1.1.1.1"},
{Value: "address=/.apps.tt.testing/1.1.1.2"},
{Value: "no-hosts"},
}),
),
},
Expand Down
11 changes: 8 additions & 3 deletions website/docs/r/network.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,16 @@ resource "libvirt_network" "kube_network" {

# (Optional) Dnsmasq options configuration
dnsmasq_options {
# (Optional) one or more option entries. Both of
# "option_name" and "option_value" must be specified. The format is:
# (Optional) one or more option entries.
# "option_name" muast be specified while "option_value" is
# optional to also support value-less options. The format is:
# options {
# option_name = "server"
# option_value = "/base.domain/my.ip.address.1"
# }
# options {
# option_name = "no-hosts"
# }
# options {
# option_name = "address"
# ip = "/.api.base.domain/my.ip.address.2"
Expand Down Expand Up @@ -205,7 +209,8 @@ resource "libvirt_network" "k8snet" {
You need to provide a list of option name and value pairs.

* `options` - (Optional) a Dnsmasq option entry block. You can have one or more of these
blocks in your definition. You must specify both `option_name` and `option_value`.
blocks in your definition. You must specify `option_name` while `option_value` is
optional to support value-less options.

An example of setting Dnsmasq options (using Dnsmasq option templates) follows:

Expand Down