From d15f9131aeda13835f962806b6f809ff0e78d481 Mon Sep 17 00:00:00 2001 From: Qiyue Yao Date: Mon, 11 Dec 2023 10:03:58 -0800 Subject: [PATCH] Add Gateway DNS forwarder datasource This solution adds a data source for Gateway DNS forwarder, to get DNS forwarders IP if it is created in a different project. Fixes #821. Signed-off-by: Qiyue Yao --- ...ource_nsxt_policy_gateway_dns_forwarder.go | 40 +++++++++++++++++++ nsxt/provider.go | 1 + ..._nsxt_policy_gateway_dns_forwarder_test.go | 9 ++++- ...policy_gateway_dns_forwarder.html.markdown | 36 +++++++++++++++++ 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 nsxt/data_source_nsxt_policy_gateway_dns_forwarder.go create mode 100644 website/docs/d/policy_gateway_dns_forwarder.html.markdown diff --git a/nsxt/data_source_nsxt_policy_gateway_dns_forwarder.go b/nsxt/data_source_nsxt_policy_gateway_dns_forwarder.go new file mode 100644 index 000000000..ed95c8889 --- /dev/null +++ b/nsxt/data_source_nsxt_policy_gateway_dns_forwarder.go @@ -0,0 +1,40 @@ +/* Copyright © 2023 VMware, Inc. All Rights Reserved. + SPDX-License-Identifier: MPL-2.0 */ + +package nsxt + +import ( + "fmt" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceNsxtPolicyGatewayDNSForwarder() *schema.Resource { + return &schema.Resource{ + Read: dataSourceNsxtPolicyGatewayDNSForwarderRead, + + Schema: map[string]*schema.Schema{ + "id": getDataSourceIDSchema(), + "display_name": getDataSourceDisplayNameSchema(), + "description": getDataSourceDescriptionSchema(), + "path": getPathSchema(), + "gateway_path": getPolicyPathSchema(false, false, "Gateway path"), + "context": getContextSchema(), + }, + } +} + +func dataSourceNsxtPolicyGatewayDNSForwarderRead(d *schema.ResourceData, m interface{}) error { + connector := getPolicyConnector(m) + + gwPath := d.Get("gateway_path").(string) + query := make(map[string]string) + if len(gwPath) > 0 { + query["parent_path"] = fmt.Sprintf("%s*", gwPath) + } + _, err := policyDataSourceResourceReadWithValidation(d, connector, getSessionContext(d, m), "PolicyDnsForwarder", query, false) + if err != nil { + return err + } + return nil +} diff --git a/nsxt/provider.go b/nsxt/provider.go index a9e32d188..7d20e176a 100644 --- a/nsxt/provider.go +++ b/nsxt/provider.go @@ -283,6 +283,7 @@ func Provider() *schema.Provider { "nsxt_policy_l2_vpn_service": dataSourceNsxtPolicyL2VpnService(), "nsxt_policy_segment": dataSourceNsxtPolicySegment(), "nsxt_policy_project": dataSourceNsxtPolicyProject(), + "nsxt_policy_gateway_dns_forwarder": dataSourceNsxtPolicyGatewayDNSForwarder(), "nsxt_policy_gateway_prefix_list": dataSourceNsxtPolicyGatewayPrefixList(), "nsxt_policy_gateway_route_map": dataSourceNsxtPolicyGatewayRouteMap(), "nsxt_policy_uplink_host_switch_profile": dataSourceNsxtUplinkHostSwitchProfile(), diff --git a/nsxt/resource_nsxt_policy_gateway_dns_forwarder_test.go b/nsxt/resource_nsxt_policy_gateway_dns_forwarder_test.go index 50474dfbc..9b36b44e4 100644 --- a/nsxt/resource_nsxt_policy_gateway_dns_forwarder_test.go +++ b/nsxt/resource_nsxt_policy_gateway_dns_forwarder_test.go @@ -261,7 +261,14 @@ resource "nsxt_policy_gateway_dns_forwarder" "test" { tag = "tag1" } } -`, context, attrMap["display_name"], attrMap["description"], whyDoesGoNeedToBeSoComplicated[isT0], attrMap["listener_ip"], attrMap["enabled"], attrMap["log_level"], attrMap["cache_size"]) + +data "nsxt_policy_gateway_dns_forwarder" "test" { + display_name = "%s" + + gateway_path = nsxt_policy_tier%d_gateway.test.path + depends_on = [nsxt_policy_gateway_dns_forwarder.test] +} +`, context, attrMap["display_name"], attrMap["description"], whyDoesGoNeedToBeSoComplicated[isT0], attrMap["listener_ip"], attrMap["enabled"], attrMap["log_level"], attrMap["cache_size"], attrMap["display_name"], whyDoesGoNeedToBeSoComplicated[isT0]) } func testAccNsxtPolicyGatewayDNSForwarderMinimalistic(isT0, withContext bool) string { diff --git a/website/docs/d/policy_gateway_dns_forwarder.html.markdown b/website/docs/d/policy_gateway_dns_forwarder.html.markdown new file mode 100644 index 000000000..29618c3db --- /dev/null +++ b/website/docs/d/policy_gateway_dns_forwarder.html.markdown @@ -0,0 +1,36 @@ +--- +subcategory: "DNS" +layout: "nsxt" +page_title: "NSXT: policy_gateway_dns_forwarder" +description: A policy gateway DNS forwarder data source. +--- + +# nsxt_policy_gateway_dns_forwarder + +This data source provides information about policy gateways DNS forwarder. + +This data source is applicable to NSX Policy Manager, NSX Global Manager and VMC. + +## Example Usage + +```hcl +data "nsxt_policy_gateway_dns_forwarder" "my_dns_forwarder" { + display_name = "dns-forwarder1" + gateway_path = data.nsxt_policy_tier1_gateway.path +} +``` + +## Argument Reference + +* `gateway_path` - (Required) Path for the gateway. +* `id` - (Optional) The ID of gateway DNS forwarder to retrieve. +* `display_name` - (Optional) The Display Name of the gateway DNS forwarder to retrieve. +* `context` - (Optional) The context which the object belongs to + * `project_id` - (Required) The ID of the project which the object belongs to + +## Attributes Reference + +In addition to arguments listed above, the following attributes are exported: + +* `description` - The description of the resource. +* `path` - The NSX path of the policy resource.