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

fix(vpc): dns update fix on resolver system to delegated #5937

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
39 changes: 38 additions & 1 deletion ibm/service/vpc/resource_ibm_is_vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,8 @@ func vpcUpdate(d *schema.ResourceData, meta interface{}, id, name string, hasCha
isDnsResolverVPCCrnNull := false
isDnsResolverManualServerChange := false
isDnsResolverManualServerEtag := ""
deleteBinding := false
deleteDnsBindings := &vpcv1.DeleteVPCDnsResolutionBindingOptions{}
var dnsPatch *vpcv1.VpcdnsPatch
if d.HasChange(isVPCDns) {
dnsPatch = &vpcv1.VpcdnsPatch{}
Expand Down Expand Up @@ -1336,10 +1338,38 @@ func vpcUpdate(d *schema.ResourceData, meta interface{}, id, name string, hasCha

}
if d.HasChange("dns.0.resolver.0.type") {
_, newResolverType := d.GetChange("dns.0.resolver.0.type")
oldResolverType, newResolverType := d.GetChange("dns.0.resolver.0.type")
if newResolverType != nil && newResolverType.(string) != "" {
ResolverModel.Type = core.StringPtr(newResolverType.(string))
}
if oldResolverType != nil && newResolverType != nil && oldResolverType.(string) != "" && newResolverType.(string) != "" {
if oldResolverType.(string) == "system" && newResolverType.(string) == "delegated" {
vpcId := d.Get("dns.0.resolver.0.vpc_id").(string)
createDnsBindings := &vpcv1.CreateVPCDnsResolutionBindingOptions{
VPCID: core.StringPtr(d.Id()),
VPC: &vpcv1.VPCIdentity{
ID: &vpcId,
},
}
if bindingNameOk, ok := d.GetOk("dns.0.resolver.0.dns_binding_name"); ok {
bindingName := bindingNameOk.(string)
createDnsBindings.Name = &bindingName
}
_, response, err := sess.CreateVPCDnsResolutionBinding(createDnsBindings)
if err != nil {
log.Printf("[DEBUG] CreateVPCDnsResolutionBindingWithContext failed %s\n%s", err, response)
return fmt.Errorf("[ERROR] CreateVPCDnsResolutionBinding failed in vpc update resource %s\n%s", err, response)
}
}
if oldResolverType.(string) == "delegated" && newResolverType.(string) == "system" && d.HasChange("dns_binding_name") && d.Get("dns_binding_name").(string) == "" {
dnsid := d.Get("dns.0.resolver.0.dns_binding_id").(string)
deleteBinding = true
deleteDnsBindings = &vpcv1.DeleteVPCDnsResolutionBindingOptions{
VPCID: core.StringPtr(d.Id()),
ID: &dnsid,
}
}
}
}
if d.HasChange("dns.0.resolver.0.vpc_id") {
_, newResolverVpc := d.GetChange("dns.0.resolver.0.vpc_id")
Expand Down Expand Up @@ -1421,6 +1451,13 @@ func vpcUpdate(d *schema.ResourceData, meta interface{}, id, name string, hasCha
return fmt.Errorf("[ERROR] Error Updating VPC : %s\n%s", err, response)
}
}
if deleteBinding && *deleteDnsBindings.VPCID != "" {
_, response, err := sess.DeleteVPCDnsResolutionBinding(deleteDnsBindings)
if err != nil {
log.Printf("[DEBUG] DeleteVPCDnsResolutionBindingWithContext failed %s\n%s", err, response)
return fmt.Errorf("[ERROR] DeleteVPCDnsResolutionBinding failed in vpc update resource %s\n%s", err, response)
}
}
if isDnsResolverVPCCrnNull || isDnsResolverVPCIDNull {

dnsList := make([]map[string]interface{}, 0)
Expand Down
168 changes: 168 additions & 0 deletions ibm/service/vpc/resource_ibm_is_vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -962,3 +962,171 @@ func testAccCheckIBMISVPCNoSgAclRulesConfig(vpcname string) string {
`, vpcname)

}

// vpc dns fix
func TestAccIBMISVPC_ResolverTypeTransition(t *testing.T) {
var vpc string
name := fmt.Sprintf("tf-vpc-resolver-%d", acctest.RandIntRange(10, 100))
bindingName := fmt.Sprintf("tf-binding-%d", acctest.RandIntRange(10, 100))

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
CheckDestroy: testAccCheckIBMISVPCDestroy,
Steps: []resource.TestStep{
// Initial setup with system resolver
{
Config: testAccCheckIBMISVPCResolverConfig(name, "system", "", ""),
Check: resource.ComposeTestCheckFunc(
testAccCheckIBMISVPCExists("ibm_is_vpc.hub_false_delegated", vpc),
resource.TestCheckResourceAttr(
"ibm_is_vpc.hub_false_delegated", "name", fmt.Sprintf("%s-vpc-hub-false-delssss", name)),
resource.TestCheckResourceAttr(
"ibm_is_vpc.hub_false_delegated", "dns.0.enable_hub", "false"),
resource.TestCheckResourceAttr(
"ibm_is_vpc.hub_false_delegated", "dns.0.resolver.0.type", "system"),
resource.TestCheckResourceAttr(
"ibm_is_vpc.hub_false_delegated", "dns.0.resolver.0.vpc_id", "null"),
resource.TestCheckResourceAttr(
"ibm_is_vpc.hub_false_delegated", "dns.0.resolver.0.dns_binding_name", ""),
),
},
// Change to delegated resolver
{
Config: testAccCheckIBMISVPCResolverConfig(name, "delegated", "ibm_is_vpc.hub_true.id", bindingName),
Check: resource.ComposeTestCheckFunc(
testAccCheckIBMISVPCExists("ibm_is_vpc.hub_false_delegated", vpc),
resource.TestCheckResourceAttr(
"ibm_is_vpc.hub_false_delegated", "dns.0.resolver.0.type", "delegated"),
resource.TestCheckResourceAttrSet(
"ibm_is_vpc.hub_false_delegated", "dns.0.resolver.0.vpc_id"),
resource.TestCheckResourceAttr(
"ibm_is_vpc.hub_false_delegated", "dns.0.resolver.0.dns_binding_name", bindingName),
),
},
// Change back to system resolver
{
Config: testAccCheckIBMISVPCResolverConfig(name, "system", "", ""),
Check: resource.ComposeTestCheckFunc(
testAccCheckIBMISVPCExists("ibm_is_vpc.hub_false_delegated", vpc),
resource.TestCheckResourceAttr(
"ibm_is_vpc.hub_false_delegated", "dns.0.resolver.0.type", "system"),
resource.TestCheckResourceAttr(
"ibm_is_vpc.hub_false_delegated", "dns.0.resolver.0.vpc_id", "null"),
resource.TestCheckResourceAttr(
"ibm_is_vpc.hub_false_delegated", "dns.0.resolver.0.dns_binding_name", ""),
),
},
},
})
}

func TestAccIBMISVPC_CustomResolverWithHubVPC(t *testing.T) {
var vpc string
name := fmt.Sprintf("tf-vpc-hub-%d", acctest.RandIntRange(10, 100))

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
CheckDestroy: testAccCheckIBMISVPCDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMISVPCCustomResolverConfig(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckIBMISVPCExists("ibm_is_vpc.hub_true", vpc),
resource.TestCheckResourceAttr(
"ibm_is_vpc.hub_true", "name", fmt.Sprintf("%s-vpc-hub-true", name)),
resource.TestCheckResourceAttr(
"ibm_is_vpc.hub_true", "dns.0.enable_hub", "true"),
testAccCheckIBMISVPCExists("ibm_is_subnet.hub_true_sub1", vpc),
testAccCheckIBMISVPCExists("ibm_is_subnet.hub_true_sub2", vpc),
resource.TestCheckResourceAttr(
"ibm_dns_custom_resolver.test_hub_true", "high_availability", "true"),
resource.TestCheckResourceAttr(
"ibm_dns_custom_resolver.test_hub_true", "enabled", "true"),
resource.TestCheckResourceAttr(
"ibm_dns_custom_resolver.test_hub_true", "locations.#", "2"),
),
},
},
})
}

// Helper function to generate config for resolver type transitions
func testAccCheckIBMISVPCResolverConfig(name, resolverType, vpcID, bindingName string) string {
return fmt.Sprintf(`
resource "ibm_is_vpc" "hub_true" {
name = "%[1]s-vpc-hub-true"
dns {
enable_hub = true
}
}

resource "ibm_is_vpc" "hub_false_delegated" {
depends_on = [ibm_is_vpc.hub_true]
name = "%[1]s-vpc-hub-false-delssss"
dns {
enable_hub = false
resolver {
type = "%[2]s"
vpc_id = %[3]s
dns_binding_name = "%[4]s"
}
}
}
`, name, resolverType, vpcID, bindingName)
}

// Helper function to generate config for custom resolver with hub VPC
func testAccCheckIBMISVPCCustomResolverConfig(name string) string {
return fmt.Sprintf(`
data "ibm_resource_group" "rg" {
is_default = true
}

resource "ibm_is_vpc" "hub_true" {
name = "%[1]s-vpc-hub-true"
dns {
enable_hub = true
}
}

resource "ibm_is_subnet" "hub_true_sub1" {
name = "hub-true-subnet1"
vpc = ibm_is_vpc.hub_true.id
zone = "${var.region}-2"
total_ipv4_address_count = 16
}

resource "ibm_is_subnet" "hub_true_sub2" {
name = "hub-true-subnet2"
vpc = ibm_is_vpc.hub_true.id
zone = "${var.region}-2"
total_ipv4_address_count = 16
}

resource "ibm_resource_instance" "dns-cr-instance" {
name = "dns-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_hub_true" {
name = "test-hub-true-customresolver"
instance_id = ibm_resource_instance.dns-cr-instance.guid
description = "new test CR - TF"
high_availability = true
enabled = true
locations {
subnet_crn = ibm_is_subnet.hub_true_sub1.crn
enabled = true
}
locations {
subnet_crn = ibm_is_subnet.hub_true_sub2.crn
enabled = true
}
}
`, name)
}
Loading