diff --git a/ibm/service/dnsservices/data_source_ibm_private_dns_custom_resolver_forwarding_rules_test.go b/ibm/service/dnsservices/data_source_ibm_private_dns_custom_resolver_forwarding_rules_test.go index 1b8a6f9cf1..9cf6eec66a 100644 --- a/ibm/service/dnsservices/data_source_ibm_private_dns_custom_resolver_forwarding_rules_test.go +++ b/ibm/service/dnsservices/data_source_ibm_private_dns_custom_resolver_forwarding_rules_test.go @@ -9,6 +9,7 @@ import ( acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) @@ -17,12 +18,14 @@ func TestAccIBMPrivateDNSCustomResolverForwardingRulesDataSource_basic(t *testin forwardingRuleType := "zone" forwardingRuleMatch := "test.example.com" node := "data.ibm_dns_custom_resolver_forwarding_rules.test-fr" + vpcname := fmt.Sprintf("d-fr-vpc-%d", acctest.RandIntRange(10, 100)) + subnetname := fmt.Sprintf("d-fr-subnet-name-%d", acctest.RandIntRange(10, 100)) resource.Test(t, resource.TestCase{ PreCheck: func() { acc.TestAccPreCheck(t) }, Providers: acc.TestAccProviders, Steps: []resource.TestStep{ { - Config: testAccCheckIbmDnsCrForwardingRulesDataSourceConfig(forwardingRuleDescription, forwardingRuleType, forwardingRuleMatch), + Config: testAccCheckIbmDnsCrForwardingRulesDataSourceConfig(vpcname, subnetname, acc.ISZoneName, acc.ISCIDR, forwardingRuleDescription, forwardingRuleType, forwardingRuleMatch), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet(node, "rules.0.description"), resource.TestCheckResourceAttrSet(node, "rules.0.type"), @@ -33,32 +36,53 @@ func TestAccIBMPrivateDNSCustomResolverForwardingRulesDataSource_basic(t *testin }) } -func testAccCheckIbmDnsCrForwardingRulesDataSourceConfig(forwardingRuleDescription string, forwardingRuleType string, forwardingRuleMatch string) string { +func testAccCheckIbmDnsCrForwardingRulesDataSourceConfig(vpcname, subnetname, zone, cidr, forwardingRuleDescription, forwardingRuleType string, forwardingRuleMatch string) string { return fmt.Sprintf(` - resource "ibm_dns_custom_resolver" "test" { - name = "CustomResolverFW" - instance_id = "c9e23743-b039-4f33-ba8a-c3bf35e9b450" - description = "FW rules" - high_availability = false - enabled = true - locations { - subnet_crn = "crn:v1:bluemix:public:is:us-south-1:a/bcf1865e99742d38d2d5fc3fb80a5496::subnet:0717-4f53a236-cd7a-4688-9347-066bb5058a5c" - enabled = true - } - } - resource "ibm_dns_custom_resolver_forwarding_rule" "dns_custom_resolver_forwarding_rule" { - instance_id = "c9e23743-b039-4f33-ba8a-c3bf35e9b450" - resolver_id = ibm_dns_custom_resolver.test.custom_resolver_id - description = "%s" - type = "%s" - match = "%s" - forward_to = ["168.20.22.122"] - } - - data "ibm_dns_custom_resolver_forwarding_rules" "test-fr" { - depends_on = [ibm_dns_custom_resolver.test] - instance_id = ibm_dns_custom_resolver.test.instance_id - resolver_id = ibm_dns_custom_resolver.test.custom_resolver_id + + data "ibm_resource_group" "rg" { + is_default = true + } + resource "ibm_is_vpc" "test-pdns-cr-vpc" { + name = "%s" + resource_group = data.ibm_resource_group.rg.id + } + resource "ibm_is_subnet" "test-pdns-cr-subnet1" { + name = "%s" + vpc = ibm_is_vpc.test-pdns-cr-vpc.id + zone = "%s" + ipv4_cidr_block = "%s" + resource_group = data.ibm_resource_group.rg.id + } + resource "ibm_resource_instance" "test-pdns-cr-instance" { + name = "test-pdns-cr-instance" + resource_group_id = data.ibm_resource_group.rg.id + location = "global" + service = "dns-svcs" + plan = "standard-dns" + } + resource "ibm_dns_custom_resolver" "test" { + name = "testpdnscustomresolver" + instance_id = ibm_resource_instance.test-pdns-cr-instance.guid + description = "new test CR - TF" + high_availability = false + enabled = true + locations { + subnet_crn = ibm_is_subnet.test-pdns-cr-subnet1.crn + enabled = true } - `, forwardingRuleDescription, forwardingRuleType, forwardingRuleMatch) + } + resource "ibm_dns_custom_resolver_forwarding_rule" "dns_custom_resolver_forwarding_rule" { + instance_id = ibm_resource_instance.test-pdns-cr-instance.guid + resolver_id = ibm_dns_custom_resolver.test.custom_resolver_id + description = "Test Fw Rule" + type = "%s" + match = "%s" + forward_to = ["168.20.22.122"] + } + data "ibm_dns_custom_resolver_forwarding_rules" "test-fr" { + depends_on = [ibm_dns_custom_resolver.test] + instance_id = ibm_dns_custom_resolver.test.instance_id + resolver_id = ibm_dns_custom_resolver.test.custom_resolver_id + } + `, vpcname, subnetname, zone, cidr, forwardingRuleType, forwardingRuleMatch) } diff --git a/ibm/service/dnsservices/data_source_ibm_private_dns_custom_resolver_test.go b/ibm/service/dnsservices/data_source_ibm_private_dns_custom_resolver_test.go index a16aacc218..e6691e9056 100644 --- a/ibm/service/dnsservices/data_source_ibm_private_dns_custom_resolver_test.go +++ b/ibm/service/dnsservices/data_source_ibm_private_dns_custom_resolver_test.go @@ -17,12 +17,14 @@ func TestAccIBMPrivateDNSCustomResolverDataSource_basic(t *testing.T) { node := "data.ibm_dns_custom_resolvers.test-cr" crname := fmt.Sprintf("tf-pdns-custom-resolver-%d", acctest.RandIntRange(100, 200)) crdescription := fmt.Sprintf("tf-pdns-custom-resolver-tf-test%d", acctest.RandIntRange(100, 200)) + vpcname := fmt.Sprintf("d-cr-vpc-%d", acctest.RandIntRange(10, 100)) + subnetname := fmt.Sprintf("d-cr-loc-subnet-name-%d", acctest.RandIntRange(10, 100)) resource.Test(t, resource.TestCase{ PreCheck: func() { acc.TestAccPreCheck(t) }, Providers: acc.TestAccProviders, Steps: []resource.TestStep{ { - Config: testAccCheckIBMPrivateDNSCustomResolverDataSourceConfig(crname, crdescription), + Config: testAccCheckIBMPrivateDNSCustomResolverDataSourceConfig(vpcname, subnetname, acc.ISZoneName, acc.ISCIDR, crname, crdescription), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet(node, "custom_resolvers.0.name"), resource.TestCheckResourceAttrSet(node, "custom_resolvers.0.description"), @@ -35,20 +37,42 @@ func TestAccIBMPrivateDNSCustomResolverDataSource_basic(t *testing.T) { }) } -func testAccCheckIBMPrivateDNSCustomResolverDataSourceConfig(crname, crdescription string) string { +func testAccCheckIBMPrivateDNSCustomResolverDataSourceConfig(vpcname, subnetname, zone, cidr, crname, crdescription string) string { return fmt.Sprintf(` + data "ibm_resource_group" "rg" { + is_default = true + } + resource "ibm_is_vpc" "test-pdns-cr-vpc" { + name = "%s" + resource_group = data.ibm_resource_group.rg.id + } + resource "ibm_is_subnet" "test-pdns-cr-subnet1" { + name = "%s" + vpc = ibm_is_vpc.test-pdns-cr-vpc.id + zone = "%s" + ipv4_cidr_block = "%s" + resource_group = data.ibm_resource_group.rg.id + } + resource "ibm_resource_instance" "test-pdns-cr-instance" { + name = "test-pdns-cr-instance" + resource_group_id = data.ibm_resource_group.rg.id + location = "global" + service = "dns-svcs" + plan = "standard-dns" + } resource "ibm_dns_custom_resolver" "test" { name = "%s" - instance_id = "c9e23743-b039-4f33-ba8a-c3bf35e9b450" - description = "%s" + instance_id = ibm_resource_instance.test-pdns-cr-instance.guid + description = "%s" high_availability = false - locations{ - subnet_crn = "crn:v1:bluemix:public:is:us-south-1:a/bcf1865e99742d38d2d5fc3fb80a5496::subnet:0717-4f53a236-cd7a-4688-9347-066bb5058a5c" + enabled = true + locations { + subnet_crn = ibm_is_subnet.test-pdns-cr-subnet1.crn enabled = true } } data "ibm_dns_custom_resolvers" "test-cr" { depends_on = [ibm_dns_custom_resolver.test] instance_id = ibm_dns_custom_resolver.test.instance_id - }`, crname, crdescription) + }`, vpcname, subnetname, acc.ISZoneName, acc.ISCIDR, crname, crdescription) } diff --git a/ibm/service/dnsservices/resource_ibm_private_dns_custom_resolver.go b/ibm/service/dnsservices/resource_ibm_private_dns_custom_resolver.go index 23357622b9..8ba4a596b3 100644 --- a/ibm/service/dnsservices/resource_ibm_private_dns_custom_resolver.go +++ b/ibm/service/dnsservices/resource_ibm_private_dns_custom_resolver.go @@ -133,7 +133,42 @@ func ResourceIBMPrivateDNSCustomResolver() *schema.Resource { }, }, }, - + pdnsCRForwardRules: { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + pdnsCRFRRuleID: { + Type: schema.TypeString, + Computed: true, + Description: "Identifier of the forwarding rule.", + }, + pdnsCRFRDesctiption: { + Type: schema.TypeString, + Computed: true, + Description: "Descriptive text of the forwarding rule.", + }, + pdnsCRFRType: { + Type: schema.TypeString, + Computed: true, + Description: "Type of the forwarding rule.", + }, + pdnsCRFRMatch: { + Type: schema.TypeString, + Computed: true, + Description: "The matching zone or hostname.", + }, + pdnsCRFRForwardTo: { + Type: schema.TypeList, + Computed: true, + Description: "The upstream DNS servers will be forwarded to.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, pdnsCRCreatedOn: { Type: schema.TypeString, Computed: true, @@ -226,6 +261,23 @@ func resouceIBMPrivateDNSCustomResolverRead(context context.Context, d *schema.R } return diag.FromErr(fmt.Errorf("[ERROR] Error reading the custom resolver %s:%s", err, response)) } + fwopt := sess.NewListForwardingRulesOptions(crn, customResolverID) + + fwresult, fwresp, fwerr := sess.ListForwardingRulesWithContext(context, fwopt) + if fwerr != nil || fwresult == nil { + return diag.FromErr(fmt.Errorf("[ERROR] Error listing the forwarding rules %s:%s", fwerr, fwresp)) + } + + forwardRules := make([]interface{}, 0) + for _, instance := range fwresult.ForwardingRules { + forwardRule := map[string]interface{}{} + forwardRule[pdnsCRFRRuleID] = *instance.ID + forwardRule[pdnsCRFRDesctiption] = *instance.Description + forwardRule[pdnsCRFRType] = *instance.Type + forwardRule[pdnsCRFRMatch] = *instance.Match + forwardRule[pdnsCRFRForwardTo] = instance.ForwardTo + forwardRules = append(forwardRules, forwardRule) + } d.Set(pdnsInstanceID, crn) d.Set(pdnsCRId, *result.ID) d.Set(pdnsCRName, *result.Name) @@ -233,7 +285,7 @@ func resouceIBMPrivateDNSCustomResolverRead(context context.Context, d *schema.R d.Set(pdnsCRHealth, *result.Health) d.Set(pdnsCREnabled, *result.Enabled) d.Set(pdnsCustomResolverLocations, flattenPdnsCRLocations(result.Locations)) - + d.Set(pdnsCRForwardRules, forwardRules) return nil } diff --git a/ibm/service/dnsservices/resource_ibm_private_dns_custom_resolver_forwarding_rule.go b/ibm/service/dnsservices/resource_ibm_private_dns_custom_resolver_forwarding_rule.go index 1f28e762e5..2f2d6664cf 100644 --- a/ibm/service/dnsservices/resource_ibm_private_dns_custom_resolver_forwarding_rule.go +++ b/ibm/service/dnsservices/resource_ibm_private_dns_custom_resolver_forwarding_rule.go @@ -6,6 +6,7 @@ package dnsservices import ( "context" "fmt" + "strings" "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" "github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex" @@ -59,6 +60,7 @@ func ResourceIBMPrivateDNSForwardingRule() *schema.Resource { }, pdnsCRFRMatch: { Type: schema.TypeString, + Computed: true, Optional: true, Description: "The matching zone or hostname.", }, @@ -85,7 +87,7 @@ func ResourceIBMPrivateDNSForwardingRuleValidator() *validate.ResourceValidator ValidateFunctionIdentifier: validate.ValidateAllowedStringValue, Type: validate.TypeString, Optional: true, - AllowedValues: "hostname, zone", + AllowedValues: "hostname, zone, Default", }, ) @@ -160,24 +162,27 @@ func resourceIbmDnsCrForwardingRuleUpdate(context context.Context, d *schema.Res if err != nil { return diag.FromErr(err) } - opt := dnsSvcsClient.NewUpdateForwardingRuleOptions(instanceID, resolverID, ruleID) + if d.HasChange(pdnsCRFRDesctiption) || d.HasChange(pdnsCRFRMatch) || d.HasChange(pdnsCRFRForwardTo) { - if des, ok := d.GetOk(pdnsCRFRDesctiption); ok { - frdes := des.(string) - opt.SetDescription(frdes) - } - if ma, ok := d.GetOk(pdnsCRFRMatch); ok { - frmatch := ma.(string) - opt.SetMatch(frmatch) + frdesc := des.(string) + opt.SetDescription(frdesc) } if _, ok := d.GetOk(pdnsCRFRForwardTo); ok { opt.SetForwardTo(flex.ExpandStringList(d.Get(pdnsCRFRForwardTo).([]interface{}))) } - + if ty, ok := d.GetOk(pdnsCRFRType); ok { + crtype := ty.(string) + if strings.ToLower(crtype) == "Default" { + if match, ok := d.GetOk(pdnsCRFRMatch); ok { + frmatch := match.(string) + opt.SetMatch(frmatch) + } + } + } result, resp, err := dnsSvcsClient.UpdateForwardingRuleWithContext(context, opt) if err != nil || result == nil { return diag.FromErr(fmt.Errorf("[ERROR] Error updating the forwarding rule %s:%s", err, resp)) diff --git a/ibm/service/dnsservices/resource_ibm_private_dns_custom_resolver_forwarding_rule_test.go b/ibm/service/dnsservices/resource_ibm_private_dns_custom_resolver_forwarding_rule_test.go index ab7eb9f708..e34ab97b2d 100644 --- a/ibm/service/dnsservices/resource_ibm_private_dns_custom_resolver_forwarding_rule_test.go +++ b/ibm/service/dnsservices/resource_ibm_private_dns_custom_resolver_forwarding_rule_test.go @@ -9,19 +9,22 @@ import ( acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) func TestAccIBMPrivateDNSCustomResolverForwardingRule_basic(t *testing.T) { typeVar := "zone" match := "test.example.com" + vpcname := fmt.Sprintf("fr-vpc-%d", acctest.RandIntRange(10, 100)) + subnetname := fmt.Sprintf("fr-subnet-name-%d", acctest.RandIntRange(10, 100)) resource.Test(t, resource.TestCase{ PreCheck: func() { acc.TestAccPreCheck(t) }, Providers: acc.TestAccProviders, Steps: []resource.TestStep{ { - Config: testAccCheckIbmDnsCrForwardingRuleConfig(typeVar, match), + Config: testAccCheckIbmDnsCrForwardingRuleConfig(vpcname, subnetname, acc.ISZoneName, acc.ISCIDR, typeVar, match), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("ibm_dns_custom_resolver_forwarding_rule.dns_custom_resolver_forwarding_rule", "type", typeVar), resource.TestCheckResourceAttr("ibm_dns_custom_resolver_forwarding_rule.dns_custom_resolver_forwarding_rule", "match", match), @@ -31,31 +34,47 @@ func TestAccIBMPrivateDNSCustomResolverForwardingRule_basic(t *testing.T) { }) } -func testAccCheckIbmDnsCrForwardingRuleConfig(typeVar, match string) string { +func testAccCheckIbmDnsCrForwardingRuleConfig(vpcname, subnetname, zone, cidr, typeVar, match string) string { return fmt.Sprintf(` - + data "ibm_resource_group" "rg" { + is_default = true + } + resource "ibm_is_vpc" "test-pdns-cr-vpc" { + name = "%s" + resource_group = data.ibm_resource_group.rg.id + } + resource "ibm_is_subnet" "test-pdns-cr-subnet1" { + name = "%s" + vpc = ibm_is_vpc.test-pdns-cr-vpc.id + zone = "%s" + ipv4_cidr_block = "%s" + resource_group = data.ibm_resource_group.rg.id + } + resource "ibm_resource_instance" "test-pdns-cr-instance" { + name = "test-pdns-cr-instance" + resource_group_id = data.ibm_resource_group.rg.id + location = "global" + service = "dns-svcs" + plan = "standard-dns" + } resource "ibm_dns_custom_resolver" "test" { - name = "testpdnscustomresolver" - instance_id = "c9e23743-b039-4f33-ba8a-c3bf35e9b450" - description = "new test CR Locations - TF" - high_availability = true - enabled = true - locations{ - subnet_crn = "crn:v1:bluemix:public:is:us-south-3:a/bcf1865e99742d38d2d5fc3fb80a5496::subnet:0737-0d198509-3221-4162-b2d8-4a9326d3d7ad" - enabled = false - } + name = "testpdnscustomresolver" + instance_id = ibm_resource_instance.test-pdns-cr-instance.guid + description = "new test CR - TF" + high_availability = false + enabled = true locations { - subnet_crn = "crn:v1:bluemix:public:is:us-south-2:a/bcf1865e99742d38d2d5fc3fb80a5496::subnet:0727-f17967f2-2bbe-427c-bcf6-22f8c2395285" - enabled = true + subnet_crn = ibm_is_subnet.test-pdns-cr-subnet1.crn + enabled = true } } resource "ibm_dns_custom_resolver_forwarding_rule" "dns_custom_resolver_forwarding_rule" { - instance_id = "c9e23743-b039-4f33-ba8a-c3bf35e9b450" + instance_id = ibm_resource_instance.test-pdns-cr-instance.guid resolver_id = ibm_dns_custom_resolver.test.custom_resolver_id description = "Test Fw Rule" type = "%s" match = "%s" forward_to = ["168.20.22.122"] } - `, typeVar, match) + `, vpcname, subnetname, zone, cidr, typeVar, match) } diff --git a/ibm/service/dnsservices/resource_ibm_private_dns_custom_resolver_location_test.go b/ibm/service/dnsservices/resource_ibm_private_dns_custom_resolver_location_test.go index d5e6b1a5b6..f596684962 100644 --- a/ibm/service/dnsservices/resource_ibm_private_dns_custom_resolver_location_test.go +++ b/ibm/service/dnsservices/resource_ibm_private_dns_custom_resolver_location_test.go @@ -14,23 +14,23 @@ import ( ) func TestAccIBMPrivateDNSCustomResolverLocations_basic(t *testing.T) { + + vpcname := fmt.Sprintf("cr-loc-vpc-%d", acctest.RandIntRange(10, 100)) + subnetname := fmt.Sprintf("cr-loc-subnet-name-%d", acctest.RandIntRange(10, 100)) name := fmt.Sprintf("testpdnscustomresolver%s", acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)) description := "new test CR Locations - TF" - subnet_crn := "crn:v1:bluemix:public:is:us-south-3:a/bcf1865e99742d38d2d5fc3fb80a5496::subnet:0737-0d198509-3221-4162-b2d8-4a9326d3d7ad" - subnet_crn_new := "crn:v1:bluemix:public:is:us-south-2:a/bcf1865e99742d38d2d5fc3fb80a5496::subnet:0727-f17967f2-2bbe-427c-bcf6-22f8c2395285" + resource.Test(t, resource.TestCase{ PreCheck: func() { acc.TestAccPreCheck(t) }, Providers: acc.TestAccProviders, Steps: []resource.TestStep{ { - Config: testAccCheckIBMPrivateDNSCRLocationsBasic(name, description, subnet_crn, subnet_crn_new), + Config: testAccCheckIBMPrivateDNSCRLocationsBasic(vpcname, subnetname, acc.ISZoneName, acc.ISCIDR, name, description), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("ibm_dns_custom_resolver_location.test1", "subnet_crn", subnet_crn), resource.TestCheckResourceAttr("ibm_dns_custom_resolver_location.test1", "enabled", "true"), - resource.TestCheckResourceAttr("ibm_dns_custom_resolver_location.test2", "enabled", "false"), resource.TestCheckResourceAttr("ibm_dns_custom_resolver_location.test1", "cr_enabled", "false"), + resource.TestCheckResourceAttr("ibm_dns_custom_resolver_location.test2", "enabled", "false"), resource.TestCheckResourceAttr("ibm_dns_custom_resolver_location.test2", "cr_enabled", "false"), - resource.TestCheckResourceAttr("ibm_dns_custom_resolver_location.test2", "subnet_crn", subnet_crn_new), ), }, }, @@ -41,21 +41,20 @@ func TestAccIBMPrivateDNSCustomResolverLocations_Import(t *testing.T) { name := fmt.Sprintf("testpdnscustomresolver%s", acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)) description := "new test CR Locations - TF" - subnet_crn := "crn:v1:bluemix:public:is:us-south-3:a/bcf1865e99742d38d2d5fc3fb80a5496::subnet:0737-0d198509-3221-4162-b2d8-4a9326d3d7ad" - subnet_crn_new := "crn:v1:bluemix:public:is:us-south-2:a/bcf1865e99742d38d2d5fc3fb80a5496::subnet:0727-f17967f2-2bbe-427c-bcf6-22f8c2395285" + vpcname := fmt.Sprintf("cr-loc-vpc-%d", acctest.RandIntRange(10, 100)) + subnetname := fmt.Sprintf("cr-loc-subnet-name-%d", acctest.RandIntRange(10, 100)) + resource.Test(t, resource.TestCase{ PreCheck: func() { acc.TestAccPreCheck(t) }, Providers: acc.TestAccProviders, Steps: []resource.TestStep{ { - Config: testAccCheckIBMPrivateDNSCRLocationsBasic(name, description, subnet_crn, subnet_crn_new), + Config: testAccCheckIBMPrivateDNSCRLocationsBasic(vpcname, subnetname, acc.ISZoneName, acc.ISCIDR, name, description), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("ibm_dns_custom_resolver_location.test1", "subnet_crn", subnet_crn), resource.TestCheckResourceAttr("ibm_dns_custom_resolver_location.test1", "enabled", "true"), - resource.TestCheckResourceAttr("ibm_dns_custom_resolver_location.test2", "enabled", "false"), resource.TestCheckResourceAttr("ibm_dns_custom_resolver_location.test1", "cr_enabled", "false"), + resource.TestCheckResourceAttr("ibm_dns_custom_resolver_location.test2", "enabled", "false"), resource.TestCheckResourceAttr("ibm_dns_custom_resolver_location.test2", "cr_enabled", "false"), - resource.TestCheckResourceAttr("ibm_dns_custom_resolver_location.test2", "subnet_crn", subnet_crn_new), ), }, { @@ -73,28 +72,50 @@ func TestAccIBMPrivateDNSCustomResolverLocations_Import(t *testing.T) { }) } -func testAccCheckIBMPrivateDNSCRLocationsBasic(name, description, subnet_crn, subnet_crn_new string) string { +func testAccCheckIBMPrivateDNSCRLocationsBasic(vpcname, subnetname, zone, cidr, name, description string) string { return fmt.Sprintf(` - resource "ibm_dns_custom_resolver" "test" { - name = "%s" - instance_id = "c9e23743-b039-4f33-ba8a-c3bf35e9b450" - description = "%s" - high_availability = false - enabled = false - } - resource "ibm_dns_custom_resolver_location" "test1" { - instance_id = "c9e23743-b039-4f33-ba8a-c3bf35e9b450" - resolver_id = ibm_dns_custom_resolver.test.custom_resolver_id - subnet_crn = "%s" - enabled = true - cr_enabled = false - } - resource "ibm_dns_custom_resolver_location" "test2" { - instance_id = "c9e23743-b039-4f33-ba8a-c3bf35e9b450" - resolver_id = ibm_dns_custom_resolver.test.custom_resolver_id - subnet_crn = "%s" - enabled = false - cr_enabled = false - } - `, name, description, subnet_crn, subnet_crn_new) + data "ibm_resource_group" "rg" { + is_default = true + } + resource "ibm_is_vpc" "test-pdns-cr-vpc" { + name = "%s" + resource_group = data.ibm_resource_group.rg.id + } + resource "ibm_is_subnet" "test-pdns-cr-subnet1" { + name = "%s" + vpc = ibm_is_vpc.test-pdns-cr-vpc.id + zone = "%s" + ipv4_cidr_block = "%s" + resource_group = data.ibm_resource_group.rg.id + } + resource "ibm_resource_instance" "test-pdns-cr-instance" { + name = "test-pdns-cr-instance" + resource_group_id = data.ibm_resource_group.rg.id + location = "global" + service = "dns-svcs" + plan = "standard-dns" + } + resource "ibm_dns_custom_resolver" "test" { + name = "%s" + instance_id = ibm_resource_instance.test-pdns-cr-instance.guid + description = "%s" + high_availability = false + enabled = false + } + resource "ibm_dns_custom_resolver_location" "test1" { + instance_id = ibm_resource_instance.test-pdns-cr-instance.guid + resolver_id = ibm_dns_custom_resolver.test.custom_resolver_id + subnet_crn = ibm_is_subnet.test-pdns-cr-subnet1.crn + enabled = true + cr_enabled = false + } + resource "ibm_dns_custom_resolver_location" "test2" { + instance_id = ibm_resource_instance.test-pdns-cr-instance.guid + resolver_id = ibm_dns_custom_resolver.test.custom_resolver_id + subnet_crn = ibm_is_subnet.test-pdns-cr-subnet1.crn + enabled = false + cr_enabled = false + } + `, vpcname, subnetname, zone, cidr, name, description) + } diff --git a/ibm/service/dnsservices/resource_ibm_private_dns_custom_resolver_test.go b/ibm/service/dnsservices/resource_ibm_private_dns_custom_resolver_test.go index a97ae1ccba..4fb4d5e6d9 100644 --- a/ibm/service/dnsservices/resource_ibm_private_dns_custom_resolver_test.go +++ b/ibm/service/dnsservices/resource_ibm_private_dns_custom_resolver_test.go @@ -18,16 +18,17 @@ import ( func TestAccIBMPrivateDNSCustomResolver_basic(t *testing.T) { var resultprivatedns string + vpcname := fmt.Sprintf("cr-vpc-%d", acctest.RandIntRange(10, 100)) + subnetname := fmt.Sprintf("cr-subnet-name-%d", acctest.RandIntRange(10, 100)) name := fmt.Sprintf("testpdnscustomresolver%s", acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)) description := "new test CR - TF" resource.Test(t, resource.TestCase{ - PreCheck: func() { acc.TestAccPreCheck(t) }, - Providers: acc.TestAccProviders, - CheckDestroy: testAccCheckIBMPrivateDNSCustomResolverDestroy, + PreCheck: func() { acc.TestAccPreCheck(t) }, + Providers: acc.TestAccProviders, Steps: []resource.TestStep{ { - Config: testAccCheckIBMPrivateDNSCustomResolverBasic(name, description), + Config: testAccCheckIBMPrivateDNSCustomResolverBasic(vpcname, subnetname, acc.ISZoneName, acc.ISCIDR, name, description), Check: resource.ComposeTestCheckFunc( testAccCheckIBMPrivateDNSCustomResolverExists("ibm_dns_custom_resolver.test", resultprivatedns), resource.TestCheckResourceAttr("ibm_dns_custom_resolver.test", "name", name), @@ -40,15 +41,16 @@ func TestAccIBMPrivateDNSCustomResolver_basic(t *testing.T) { func TestAccIBMPrivateDNSCustomResolverImport(t *testing.T) { var resultprivatedns string + vpcname := fmt.Sprintf("cr-vpc-%d", acctest.RandIntRange(10, 100)) + subnetname := fmt.Sprintf("cr-subnet-name-%d", acctest.RandIntRange(10, 100)) name := fmt.Sprintf("testpdnscustomresolver%s", acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)) description := "new test CR - TF" resource.Test(t, resource.TestCase{ - PreCheck: func() { acc.TestAccPreCheck(t) }, - Providers: acc.TestAccProviders, - CheckDestroy: testAccCheckIBMPrivateDNSCustomResolverDestroy, + PreCheck: func() { acc.TestAccPreCheck(t) }, + Providers: acc.TestAccProviders, Steps: []resource.TestStep{ { - Config: testAccCheckIBMPrivateDNSCustomResolverBasic(name, description), + Config: testAccCheckIBMPrivateDNSCustomResolverBasic(vpcname, subnetname, acc.ISZoneName, acc.ISCIDR, name, description), Check: resource.ComposeTestCheckFunc( testAccCheckIBMPrivateDNSCustomResolverExists("ibm_dns_custom_resolver.test", resultprivatedns), resource.TestCheckResourceAttr("ibm_dns_custom_resolver.test", "name", name), @@ -66,50 +68,41 @@ func TestAccIBMPrivateDNSCustomResolverImport(t *testing.T) { }) } -func testAccCheckIBMPrivateDNSCustomResolverBasic(name, description string) string { +func testAccCheckIBMPrivateDNSCustomResolverBasic(vpcname, subnetname, zone, cidr, name, description string) string { return fmt.Sprintf(` - resource "ibm_dns_custom_resolver" "test" { + data "ibm_resource_group" "rg" { + is_default = true + } + resource "ibm_is_vpc" "test-pdns-cr-vpc" { name = "%s" - instance_id = "c9e23743-b039-4f33-ba8a-c3bf35e9b450" - description = "%s" - high_availability = false - enabled = true - locations { - subnet_crn = "crn:v1:bluemix:public:is:us-south-3:a/bcf1865e99742d38d2d5fc3fb80a5496::subnet:0737-0d198509-3221-4162-b2d8-4a9326d3d7ad" - enabled = false - } - locations { - subnet_crn = "crn:v1:bluemix:public:is:us-south-2:a/bcf1865e99742d38d2d5fc3fb80a5496::subnet:0727-f17967f2-2bbe-427c-bcf6-22f8c2395285" - enabled = true - } + resource_group = data.ibm_resource_group.rg.id } - `, name, description) -} - -func testAccCheckIBMPrivateDNSCustomResolverDestroy(s *terraform.State) error { - for _, rs := range s.RootModule().Resources { - if rs.Type != "ibm_dns_custom_resolver" { - continue - } - pdnsClient, err := acc.TestAccProvider.Meta().(conns.ClientSession).PrivateDNSClientSession() - if err != nil { - return err - } - parts := rs.Primary.ID - partslist := strings.Split(parts, ":") - customResolverID := partslist[0] - crn := partslist[1] - - getCustomResolverOptions := pdnsClient.NewDeleteCustomResolverOptions(crn, customResolverID) - res, err := pdnsClient.DeleteCustomResolver(getCustomResolverOptions) - if err != nil { - if res != nil && res.StatusCode == 404 { - return nil - } - return fmt.Errorf("testAccCheckIBMPrivateDNSCustomResolverDestroy: Error checking if instance (%s) has been destroyed: %s", rs.Primary.ID, err) + resource "ibm_is_subnet" "test-pdns-cr-subnet1" { + name = "%s" + vpc = ibm_is_vpc.test-pdns-cr-vpc.id + zone = "%s" + ipv4_cidr_block = "%s" + resource_group = data.ibm_resource_group.rg.id + } + resource "ibm_resource_instance" "test-pdns-cr-instance" { + name = "test-pdns-cr-instance" + resource_group_id = data.ibm_resource_group.rg.id + location = "global" + service = "dns-svcs" + plan = "standard-dns" + } + resource "ibm_dns_custom_resolver" "test" { + name = "%s" + instance_id = ibm_resource_instance.test-pdns-cr-instance.guid + description = "%s" + high_availability = false + enabled = true + locations { + subnet_crn = ibm_is_subnet.test-pdns-cr-subnet1.crn + enabled = true } } - return nil + `, vpcname, subnetname, zone, cidr, name, description) } func testAccCheckIBMPrivateDNSCustomResolverExists(n string, result string) resource.TestCheckFunc { diff --git a/website/docs/r/dns_custom_resolver.html.markdown b/website/docs/r/dns_custom_resolver.html.markdown index 155ab555af..34870d1405 100644 --- a/website/docs/r/dns_custom_resolver.html.markdown +++ b/website/docs/r/dns_custom_resolver.html.markdown @@ -84,6 +84,13 @@ In addition to all argument reference list, you can access the following attribu - `enabled`- (Bool) Whether the location is enabled. - `location_id`- (String) The location ID. + Nested scheme for `rules`: + - `rule_id` - (String) The rule ID is unique identifier of the custom resolver forwarding rule. + - `description`- (String) Descriptive text of the forwarding rule. + - `type` - (String) Type of the forwarding rule.Constraints: Allowable values are: `zone`, `hostname`. + - `match` - (String) The matching zone or hostname. + - `forward_to` - (List) The upstream DNS servers will be forwarded to. + ## Import The `ibm_dns_custom_resolver` can be imported by using private DNS instance ID, Custom Resolver ID. The `id` property can be formed from `custom_resolver_id` and `instance_id` in the following format: diff --git a/website/docs/r/dns_custom_resolver_forwarding_rule.html.markdown b/website/docs/r/dns_custom_resolver_forwarding_rule.html.markdown index 0ef0f7a33e..d4ff90b393 100644 --- a/website/docs/r/dns_custom_resolver_forwarding_rule.html.markdown +++ b/website/docs/r/dns_custom_resolver_forwarding_rule.html.markdown @@ -74,7 +74,7 @@ Review the argument reference that you can specify for your resource. * `resolver_id` - (Required, String) The unique identifier of a custom resolver. * `description` - (Optional, String) Descriptive text of the forwarding rule. * `type` - (Optional, String) Type of the forwarding rule. - * Constraints: Allowable values are: `zone`, `hostname`. + * Constraints: Allowable values are: `zone`, `hostname`,`Default`. * `match` - (Optional, String) The matching zone or hostname. * `forward_to` - (Optional, List) The upstream DNS servers will be forwarded to.