provider/azurerm: check if lb sub resources exist when reading (#11553)

This fixes detection when a sub resource is deleted via the API or Portal
This commit is contained in:
Peter McAtominey 2017-01-31 15:55:17 +00:00 committed by Paul Stack
parent a4687c5b36
commit c104ce6815
10 changed files with 345 additions and 91 deletions

View File

@ -140,9 +140,13 @@ func resourceArmLoadBalancerBackendAddressPoolRead(d *schema.ResourceData, meta
return nil
}
configs := *loadBalancer.LoadBalancerPropertiesFormat.BackendAddressPools
for _, config := range configs {
if *config.Name == d.Get("name").(string) {
config, _, exists := findLoadBalancerBackEndAddressPoolByName(loadBalancer, d.Get("name").(string))
if !exists {
d.SetId("")
log.Printf("[INFO] LoadBalancer Backend Address Pool %q not found. Removing from state", d.Get("name").(string))
return nil
}
d.Set("name", config.Name)
if config.BackendAddressPoolPropertiesFormat.BackendIPConfigurations != nil {
@ -163,10 +167,6 @@ func resourceArmLoadBalancerBackendAddressPoolRead(d *schema.ResourceData, meta
d.Set("backend_ip_configurations", load_balancing_rules)
}
break
}
}
return nil
}

View File

@ -94,6 +94,29 @@ func TestAccAzureRMLoadBalancerBackEndAddressPool_reapply(t *testing.T) {
})
}
func TestAccAzureRMLoadBalancerBackEndAddressPool_disappears(t *testing.T) {
var lb network.LoadBalancer
ri := acctest.RandInt()
addressPoolName := fmt.Sprintf("%d-address-pool", ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMLoadBalancerBackEndAddressPool_basic(ri, addressPoolName),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
testCheckAzureRMLoadBalancerBackEndAddressPoolExists(addressPoolName, &lb),
testCheckAzureRMLoadBalancerBackEndAddressPoolDisappears(addressPoolName, &lb),
),
ExpectNonEmptyPlan: true,
},
},
})
}
func testCheckAzureRMLoadBalancerBackEndAddressPoolExists(addressPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, _, exists := findLoadBalancerBackEndAddressPoolByName(lb, addressPoolName)
@ -116,6 +139,34 @@ func testCheckAzureRMLoadBalancerBackEndAddressPoolNotExists(addressPoolName str
}
}
func testCheckAzureRMLoadBalancerBackEndAddressPoolDisappears(addressPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).loadBalancerClient
_, i, exists := findLoadBalancerBackEndAddressPoolByName(lb, addressPoolName)
if !exists {
return fmt.Errorf("A BackEnd Address Pool with name %q cannot be found.", addressPoolName)
}
currentPools := *lb.LoadBalancerPropertiesFormat.BackendAddressPools
pools := append(currentPools[:i], currentPools[i+1:]...)
lb.LoadBalancerPropertiesFormat.BackendAddressPools = &pools
id, err := parseAzureResourceID(*lb.ID)
if err != nil {
return err
}
_, err = conn.CreateOrUpdate(id.ResourceGroup, *lb.Name, *lb, make(chan struct{}))
if err != nil {
return fmt.Errorf("Error Creating/Updating LoadBalancer %s", err)
}
_, err = conn.Get(id.ResourceGroup, *lb.Name, "")
return err
}
}
func testAccAzureRMLoadBalancerBackEndAddressPool_basic(rInt int, addressPoolName string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {

View File

@ -163,11 +163,14 @@ func resourceArmLoadBalancerNatPoolRead(d *schema.ResourceData, meta interface{}
return nil
}
configs := *loadBalancer.LoadBalancerPropertiesFormat.InboundNatPools
for _, config := range configs {
if *config.Name == d.Get("name").(string) {
d.Set("name", config.Name)
config, _, exists := findLoadBalancerNatPoolByName(loadBalancer, d.Get("name").(string))
if !exists {
d.SetId("")
log.Printf("[INFO] LoadBalancer Nat Pool %q not found. Removing from state", d.Get("name").(string))
return nil
}
d.Set("name", config.Name)
d.Set("protocol", config.InboundNatPoolPropertiesFormat.Protocol)
d.Set("frontend_port_start", config.InboundNatPoolPropertiesFormat.FrontendPortRangeStart)
d.Set("frontend_port_end", config.InboundNatPoolPropertiesFormat.FrontendPortRangeEnd)
@ -177,10 +180,6 @@ func resourceArmLoadBalancerNatPoolRead(d *schema.ResourceData, meta interface{}
d.Set("frontend_ip_configuration_id", config.InboundNatPoolPropertiesFormat.FrontendIPConfiguration.ID)
}
break
}
}
return nil
}

View File

@ -134,6 +134,29 @@ func TestAccAzureRMLoadBalancerNatPool_reapply(t *testing.T) {
})
}
func TestAccAzureRMLoadBalancerNatPool_disappears(t *testing.T) {
var lb network.LoadBalancer
ri := acctest.RandInt()
natPoolName := fmt.Sprintf("NatPool-%d", ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMLoadBalancerNatPool_basic(ri, natPoolName),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb),
testCheckAzureRMLoadBalancerNatPoolDisappears(natPoolName, &lb),
),
ExpectNonEmptyPlan: true,
},
},
})
}
func testCheckAzureRMLoadBalancerNatPoolExists(natPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, _, exists := findLoadBalancerNatPoolByName(lb, natPoolName)
@ -156,6 +179,34 @@ func testCheckAzureRMLoadBalancerNatPoolNotExists(natPoolName string, lb *networ
}
}
func testCheckAzureRMLoadBalancerNatPoolDisappears(natPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).loadBalancerClient
_, i, exists := findLoadBalancerNatPoolByName(lb, natPoolName)
if !exists {
return fmt.Errorf("A Nat Pool with name %q cannot be found.", natPoolName)
}
currentPools := *lb.LoadBalancerPropertiesFormat.InboundNatPools
pools := append(currentPools[:i], currentPools[i+1:]...)
lb.LoadBalancerPropertiesFormat.InboundNatPools = &pools
id, err := parseAzureResourceID(*lb.ID)
if err != nil {
return err
}
_, err = conn.CreateOrUpdate(id.ResourceGroup, *lb.Name, *lb, make(chan struct{}))
if err != nil {
return fmt.Errorf("Error Creating/Updating LoadBalancer %s", err)
}
_, err = conn.Get(id.ResourceGroup, *lb.Name, "")
return err
}
}
func testAccAzureRMLoadBalancerNatPool_basic(rInt int, natPoolName string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {

View File

@ -163,11 +163,14 @@ func resourceArmLoadBalancerNatRuleRead(d *schema.ResourceData, meta interface{}
return nil
}
configs := *loadBalancer.LoadBalancerPropertiesFormat.InboundNatRules
for _, config := range configs {
if *config.Name == d.Get("name").(string) {
d.Set("name", config.Name)
config, _, exists := findLoadBalancerNatRuleByName(loadBalancer, d.Get("name").(string))
if !exists {
d.SetId("")
log.Printf("[INFO] LoadBalancer Nat Rule %q not found. Removing from state", d.Get("name").(string))
return nil
}
d.Set("name", config.Name)
d.Set("protocol", config.InboundNatRulePropertiesFormat.Protocol)
d.Set("frontend_port", config.InboundNatRulePropertiesFormat.FrontendPort)
d.Set("backend_port", config.InboundNatRulePropertiesFormat.BackendPort)
@ -180,10 +183,6 @@ func resourceArmLoadBalancerNatRuleRead(d *schema.ResourceData, meta interface{}
d.Set("backend_ip_configuration_id", config.InboundNatRulePropertiesFormat.BackendIPConfiguration.ID)
}
break
}
}
return nil
}

View File

@ -136,6 +136,29 @@ func TestAccAzureRMLoadBalancerNatRule_reapply(t *testing.T) {
})
}
func TestAccAzureRMLoadBalancerNatRule_disappears(t *testing.T) {
var lb network.LoadBalancer
ri := acctest.RandInt()
natRuleName := fmt.Sprintf("NatRule-%d", ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMLoadBalancerNatRule_basic(ri, natRuleName),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb),
testCheckAzureRMLoadBalancerNatRuleDisappears(natRuleName, &lb),
),
ExpectNonEmptyPlan: true,
},
},
})
}
func testCheckAzureRMLoadBalancerNatRuleExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, _, exists := findLoadBalancerNatRuleByName(lb, natRuleName)
@ -158,6 +181,34 @@ func testCheckAzureRMLoadBalancerNatRuleNotExists(natRuleName string, lb *networ
}
}
func testCheckAzureRMLoadBalancerNatRuleDisappears(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).loadBalancerClient
_, i, exists := findLoadBalancerNatRuleByName(lb, natRuleName)
if !exists {
return fmt.Errorf("A Nat Rule with name %q cannot be found.", natRuleName)
}
currentRules := *lb.LoadBalancerPropertiesFormat.InboundNatRules
rules := append(currentRules[:i], currentRules[i+1:]...)
lb.LoadBalancerPropertiesFormat.InboundNatRules = &rules
id, err := parseAzureResourceID(*lb.ID)
if err != nil {
return err
}
_, err = conn.CreateOrUpdate(id.ResourceGroup, *lb.Name, *lb, make(chan struct{}))
if err != nil {
return fmt.Errorf("Error Creating/Updating LoadBalancer %s", err)
}
_, err = conn.Get(id.ResourceGroup, *lb.Name, "")
return err
}
}
func testAccAzureRMLoadBalancerNatRule_basic(rInt int, natRuleName string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {

View File

@ -168,21 +168,20 @@ func resourceArmLoadBalancerProbeRead(d *schema.ResourceData, meta interface{})
return nil
}
configs := *loadBalancer.LoadBalancerPropertiesFormat.Probes
for _, config := range configs {
if *config.Name == d.Get("name").(string) {
d.Set("name", config.Name)
config, _, exists := findLoadBalancerProbeByName(loadBalancer, d.Get("name").(string))
if !exists {
d.SetId("")
log.Printf("[INFO] LoadBalancer Probe %q not found. Removing from state", d.Get("name").(string))
return nil
}
d.Set("name", config.Name)
d.Set("protocol", config.ProbePropertiesFormat.Protocol)
d.Set("interval_in_seconds", config.ProbePropertiesFormat.IntervalInSeconds)
d.Set("number_of_probes", config.ProbePropertiesFormat.NumberOfProbes)
d.Set("port", config.ProbePropertiesFormat.Port)
d.Set("request_path", config.ProbePropertiesFormat.RequestPath)
break
}
}
return nil
}

View File

@ -164,6 +164,29 @@ func TestAccAzureRMLoadBalancerProbe_reapply(t *testing.T) {
})
}
func TestAccAzureRMLoadBalancerProbe_disappears(t *testing.T) {
var lb network.LoadBalancer
ri := acctest.RandInt()
probeName := fmt.Sprintf("probe-%d", ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMLoadBalancerProbe_basic(ri, probeName),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
testCheckAzureRMLoadBalancerProbeExists(probeName, &lb),
testCheckAzureRMLoadBalancerProbeDisappears(probeName, &lb),
),
ExpectNonEmptyPlan: true,
},
},
})
}
func testCheckAzureRMLoadBalancerProbeExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, _, exists := findLoadBalancerProbeByName(lb, natRuleName)
@ -186,6 +209,34 @@ func testCheckAzureRMLoadBalancerProbeNotExists(natRuleName string, lb *network.
}
}
func testCheckAzureRMLoadBalancerProbeDisappears(addressPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).loadBalancerClient
_, i, exists := findLoadBalancerProbeByName(lb, addressPoolName)
if !exists {
return fmt.Errorf("A Probe with name %q cannot be found.", addressPoolName)
}
currentProbes := *lb.LoadBalancerPropertiesFormat.Probes
probes := append(currentProbes[:i], currentProbes[i+1:]...)
lb.LoadBalancerPropertiesFormat.Probes = &probes
id, err := parseAzureResourceID(*lb.ID)
if err != nil {
return err
}
_, err = conn.CreateOrUpdate(id.ResourceGroup, *lb.Name, *lb, make(chan struct{}))
if err != nil {
return fmt.Errorf("Error Creating/Updating LoadBalancer %s", err)
}
_, err = conn.Get(id.ResourceGroup, *lb.Name, "")
return err
}
}
func testAccAzureRMLoadBalancerProbe_basic(rInt int, probeName string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {

View File

@ -190,9 +190,13 @@ func resourceArmLoadBalancerRuleRead(d *schema.ResourceData, meta interface{}) e
return nil
}
configs := *loadBalancer.LoadBalancerPropertiesFormat.LoadBalancingRules
for _, config := range configs {
if *config.Name == d.Get("name").(string) {
config, _, exists := findLoadBalancerRuleByName(loadBalancer, d.Get("name").(string))
if !exists {
d.SetId("")
log.Printf("[INFO] LoadBalancer Rule %q not found. Removing from state", d.Get("name").(string))
return nil
}
d.Set("name", config.Name)
d.Set("protocol", config.LoadBalancingRulePropertiesFormat.Protocol)
@ -222,8 +226,6 @@ func resourceArmLoadBalancerRuleRead(d *schema.ResourceData, meta interface{}) e
if config.LoadBalancingRulePropertiesFormat.LoadDistribution != "" {
d.Set("load_distribution", config.LoadBalancingRulePropertiesFormat.LoadDistribution)
}
}
}
return nil
}

View File

@ -231,6 +231,29 @@ func TestAccAzureRMLoadBalancerRule_reapply(t *testing.T) {
})
}
func TestAccAzureRMLoadBalancerRule_disappears(t *testing.T) {
var lb network.LoadBalancer
ri := acctest.RandInt()
lbRuleName := fmt.Sprintf("LbRule-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMLoadBalancerRule_basic(ri, lbRuleName),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
testCheckAzureRMLoadBalancerRuleExists(lbRuleName, &lb),
testCheckAzureRMLoadBalancerRuleDisappears(lbRuleName, &lb),
),
ExpectNonEmptyPlan: true,
},
},
})
}
func testCheckAzureRMLoadBalancerRuleExists(lbRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, _, exists := findLoadBalancerRuleByName(lb, lbRuleName)
@ -253,6 +276,34 @@ func testCheckAzureRMLoadBalancerRuleNotExists(lbRuleName string, lb *network.Lo
}
}
func testCheckAzureRMLoadBalancerRuleDisappears(ruleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).loadBalancerClient
_, i, exists := findLoadBalancerRuleByName(lb, ruleName)
if !exists {
return fmt.Errorf("A Rule with name %q cannot be found.", ruleName)
}
currentRules := *lb.LoadBalancerPropertiesFormat.LoadBalancingRules
rules := append(currentRules[:i], currentRules[i+1:]...)
lb.LoadBalancerPropertiesFormat.LoadBalancingRules = &rules
id, err := parseAzureResourceID(*lb.ID)
if err != nil {
return err
}
_, err = conn.CreateOrUpdate(id.ResourceGroup, *lb.Name, *lb, make(chan struct{}))
if err != nil {
return fmt.Errorf("Error Creating/Updating LoadBalancer %s", err)
}
_, err = conn.Get(id.ResourceGroup, *lb.Name, "")
return err
}
}
func testAccAzureRMLoadBalancerRule_basic(rInt int, lbRuleName string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {