Skip to content

Commit c104ce6

Browse files
pmcatomineystack72
authored andcommitted
provider/azurerm: check if lb sub resources exist when reading (hashicorp#11553)
This fixes detection when a sub resource is deleted via the API or Portal
1 parent a4687c5 commit c104ce6

10 files changed

Lines changed: 354 additions & 100 deletions

builtin/providers/azurerm/resource_arm_loadbalancer_backend_address_pool.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -140,31 +140,31 @@ func resourceArmLoadBalancerBackendAddressPoolRead(d *schema.ResourceData, meta
140140
return nil
141141
}
142142

143-
configs := *loadBalancer.LoadBalancerPropertiesFormat.BackendAddressPools
144-
for _, config := range configs {
145-
if *config.Name == d.Get("name").(string) {
146-
d.Set("name", config.Name)
147-
148-
if config.BackendAddressPoolPropertiesFormat.BackendIPConfigurations != nil {
149-
backend_ip_configurations := make([]string, 0, len(*config.BackendAddressPoolPropertiesFormat.BackendIPConfigurations))
150-
for _, backendConfig := range *config.BackendAddressPoolPropertiesFormat.BackendIPConfigurations {
151-
backend_ip_configurations = append(backend_ip_configurations, *backendConfig.ID)
152-
}
153-
154-
d.Set("backend_ip_configurations", backend_ip_configurations)
155-
}
156-
157-
if config.BackendAddressPoolPropertiesFormat.LoadBalancingRules != nil {
158-
load_balancing_rules := make([]string, 0, len(*config.BackendAddressPoolPropertiesFormat.LoadBalancingRules))
159-
for _, rule := range *config.BackendAddressPoolPropertiesFormat.LoadBalancingRules {
160-
load_balancing_rules = append(load_balancing_rules, *rule.ID)
161-
}
162-
163-
d.Set("backend_ip_configurations", load_balancing_rules)
164-
}
165-
166-
break
143+
config, _, exists := findLoadBalancerBackEndAddressPoolByName(loadBalancer, d.Get("name").(string))
144+
if !exists {
145+
d.SetId("")
146+
log.Printf("[INFO] LoadBalancer Backend Address Pool %q not found. Removing from state", d.Get("name").(string))
147+
return nil
148+
}
149+
150+
d.Set("name", config.Name)
151+
152+
if config.BackendAddressPoolPropertiesFormat.BackendIPConfigurations != nil {
153+
backend_ip_configurations := make([]string, 0, len(*config.BackendAddressPoolPropertiesFormat.BackendIPConfigurations))
154+
for _, backendConfig := range *config.BackendAddressPoolPropertiesFormat.BackendIPConfigurations {
155+
backend_ip_configurations = append(backend_ip_configurations, *backendConfig.ID)
167156
}
157+
158+
d.Set("backend_ip_configurations", backend_ip_configurations)
159+
}
160+
161+
if config.BackendAddressPoolPropertiesFormat.LoadBalancingRules != nil {
162+
load_balancing_rules := make([]string, 0, len(*config.BackendAddressPoolPropertiesFormat.LoadBalancingRules))
163+
for _, rule := range *config.BackendAddressPoolPropertiesFormat.LoadBalancingRules {
164+
load_balancing_rules = append(load_balancing_rules, *rule.ID)
165+
}
166+
167+
d.Set("backend_ip_configurations", load_balancing_rules)
168168
}
169169

170170
return nil

builtin/providers/azurerm/resource_arm_loadbalancer_backend_address_pool_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,29 @@ func TestAccAzureRMLoadBalancerBackEndAddressPool_reapply(t *testing.T) {
9494
})
9595
}
9696

97+
func TestAccAzureRMLoadBalancerBackEndAddressPool_disappears(t *testing.T) {
98+
var lb network.LoadBalancer
99+
ri := acctest.RandInt()
100+
addressPoolName := fmt.Sprintf("%d-address-pool", ri)
101+
102+
resource.Test(t, resource.TestCase{
103+
PreCheck: func() { testAccPreCheck(t) },
104+
Providers: testAccProviders,
105+
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
106+
Steps: []resource.TestStep{
107+
{
108+
Config: testAccAzureRMLoadBalancerBackEndAddressPool_basic(ri, addressPoolName),
109+
Check: resource.ComposeTestCheckFunc(
110+
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
111+
testCheckAzureRMLoadBalancerBackEndAddressPoolExists(addressPoolName, &lb),
112+
testCheckAzureRMLoadBalancerBackEndAddressPoolDisappears(addressPoolName, &lb),
113+
),
114+
ExpectNonEmptyPlan: true,
115+
},
116+
},
117+
})
118+
}
119+
97120
func testCheckAzureRMLoadBalancerBackEndAddressPoolExists(addressPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
98121
return func(s *terraform.State) error {
99122
_, _, exists := findLoadBalancerBackEndAddressPoolByName(lb, addressPoolName)
@@ -116,6 +139,34 @@ func testCheckAzureRMLoadBalancerBackEndAddressPoolNotExists(addressPoolName str
116139
}
117140
}
118141

142+
func testCheckAzureRMLoadBalancerBackEndAddressPoolDisappears(addressPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
143+
return func(s *terraform.State) error {
144+
conn := testAccProvider.Meta().(*ArmClient).loadBalancerClient
145+
146+
_, i, exists := findLoadBalancerBackEndAddressPoolByName(lb, addressPoolName)
147+
if !exists {
148+
return fmt.Errorf("A BackEnd Address Pool with name %q cannot be found.", addressPoolName)
149+
}
150+
151+
currentPools := *lb.LoadBalancerPropertiesFormat.BackendAddressPools
152+
pools := append(currentPools[:i], currentPools[i+1:]...)
153+
lb.LoadBalancerPropertiesFormat.BackendAddressPools = &pools
154+
155+
id, err := parseAzureResourceID(*lb.ID)
156+
if err != nil {
157+
return err
158+
}
159+
160+
_, err = conn.CreateOrUpdate(id.ResourceGroup, *lb.Name, *lb, make(chan struct{}))
161+
if err != nil {
162+
return fmt.Errorf("Error Creating/Updating LoadBalancer %s", err)
163+
}
164+
165+
_, err = conn.Get(id.ResourceGroup, *lb.Name, "")
166+
return err
167+
}
168+
}
169+
119170
func testAccAzureRMLoadBalancerBackEndAddressPool_basic(rInt int, addressPoolName string) string {
120171
return fmt.Sprintf(`
121172
resource "azurerm_resource_group" "test" {

builtin/providers/azurerm/resource_arm_loadbalancer_nat_pool.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -163,22 +163,21 @@ func resourceArmLoadBalancerNatPoolRead(d *schema.ResourceData, meta interface{}
163163
return nil
164164
}
165165

166-
configs := *loadBalancer.LoadBalancerPropertiesFormat.InboundNatPools
167-
for _, config := range configs {
168-
if *config.Name == d.Get("name").(string) {
169-
d.Set("name", config.Name)
170-
171-
d.Set("protocol", config.InboundNatPoolPropertiesFormat.Protocol)
172-
d.Set("frontend_port_start", config.InboundNatPoolPropertiesFormat.FrontendPortRangeStart)
173-
d.Set("frontend_port_end", config.InboundNatPoolPropertiesFormat.FrontendPortRangeEnd)
174-
d.Set("backend_port", config.InboundNatPoolPropertiesFormat.BackendPort)
166+
config, _, exists := findLoadBalancerNatPoolByName(loadBalancer, d.Get("name").(string))
167+
if !exists {
168+
d.SetId("")
169+
log.Printf("[INFO] LoadBalancer Nat Pool %q not found. Removing from state", d.Get("name").(string))
170+
return nil
171+
}
175172

176-
if config.InboundNatPoolPropertiesFormat.FrontendIPConfiguration != nil {
177-
d.Set("frontend_ip_configuration_id", config.InboundNatPoolPropertiesFormat.FrontendIPConfiguration.ID)
178-
}
173+
d.Set("name", config.Name)
174+
d.Set("protocol", config.InboundNatPoolPropertiesFormat.Protocol)
175+
d.Set("frontend_port_start", config.InboundNatPoolPropertiesFormat.FrontendPortRangeStart)
176+
d.Set("frontend_port_end", config.InboundNatPoolPropertiesFormat.FrontendPortRangeEnd)
177+
d.Set("backend_port", config.InboundNatPoolPropertiesFormat.BackendPort)
179178

180-
break
181-
}
179+
if config.InboundNatPoolPropertiesFormat.FrontendIPConfiguration != nil {
180+
d.Set("frontend_ip_configuration_id", config.InboundNatPoolPropertiesFormat.FrontendIPConfiguration.ID)
182181
}
183182

184183
return nil

builtin/providers/azurerm/resource_arm_loadbalancer_nat_pool_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,29 @@ func TestAccAzureRMLoadBalancerNatPool_reapply(t *testing.T) {
134134
})
135135
}
136136

137+
func TestAccAzureRMLoadBalancerNatPool_disappears(t *testing.T) {
138+
var lb network.LoadBalancer
139+
ri := acctest.RandInt()
140+
natPoolName := fmt.Sprintf("NatPool-%d", ri)
141+
142+
resource.Test(t, resource.TestCase{
143+
PreCheck: func() { testAccPreCheck(t) },
144+
Providers: testAccProviders,
145+
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
146+
Steps: []resource.TestStep{
147+
{
148+
Config: testAccAzureRMLoadBalancerNatPool_basic(ri, natPoolName),
149+
Check: resource.ComposeTestCheckFunc(
150+
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
151+
testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb),
152+
testCheckAzureRMLoadBalancerNatPoolDisappears(natPoolName, &lb),
153+
),
154+
ExpectNonEmptyPlan: true,
155+
},
156+
},
157+
})
158+
}
159+
137160
func testCheckAzureRMLoadBalancerNatPoolExists(natPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
138161
return func(s *terraform.State) error {
139162
_, _, exists := findLoadBalancerNatPoolByName(lb, natPoolName)
@@ -156,6 +179,34 @@ func testCheckAzureRMLoadBalancerNatPoolNotExists(natPoolName string, lb *networ
156179
}
157180
}
158181

182+
func testCheckAzureRMLoadBalancerNatPoolDisappears(natPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
183+
return func(s *terraform.State) error {
184+
conn := testAccProvider.Meta().(*ArmClient).loadBalancerClient
185+
186+
_, i, exists := findLoadBalancerNatPoolByName(lb, natPoolName)
187+
if !exists {
188+
return fmt.Errorf("A Nat Pool with name %q cannot be found.", natPoolName)
189+
}
190+
191+
currentPools := *lb.LoadBalancerPropertiesFormat.InboundNatPools
192+
pools := append(currentPools[:i], currentPools[i+1:]...)
193+
lb.LoadBalancerPropertiesFormat.InboundNatPools = &pools
194+
195+
id, err := parseAzureResourceID(*lb.ID)
196+
if err != nil {
197+
return err
198+
}
199+
200+
_, err = conn.CreateOrUpdate(id.ResourceGroup, *lb.Name, *lb, make(chan struct{}))
201+
if err != nil {
202+
return fmt.Errorf("Error Creating/Updating LoadBalancer %s", err)
203+
}
204+
205+
_, err = conn.Get(id.ResourceGroup, *lb.Name, "")
206+
return err
207+
}
208+
}
209+
159210
func testAccAzureRMLoadBalancerNatPool_basic(rInt int, natPoolName string) string {
160211
return fmt.Sprintf(`
161212
resource "azurerm_resource_group" "test" {

builtin/providers/azurerm/resource_arm_loadbalancer_nat_rule.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -163,25 +163,24 @@ func resourceArmLoadBalancerNatRuleRead(d *schema.ResourceData, meta interface{}
163163
return nil
164164
}
165165

166-
configs := *loadBalancer.LoadBalancerPropertiesFormat.InboundNatRules
167-
for _, config := range configs {
168-
if *config.Name == d.Get("name").(string) {
169-
d.Set("name", config.Name)
170-
171-
d.Set("protocol", config.InboundNatRulePropertiesFormat.Protocol)
172-
d.Set("frontend_port", config.InboundNatRulePropertiesFormat.FrontendPort)
173-
d.Set("backend_port", config.InboundNatRulePropertiesFormat.BackendPort)
166+
config, _, exists := findLoadBalancerNatRuleByName(loadBalancer, d.Get("name").(string))
167+
if !exists {
168+
d.SetId("")
169+
log.Printf("[INFO] LoadBalancer Nat Rule %q not found. Removing from state", d.Get("name").(string))
170+
return nil
171+
}
174172

175-
if config.InboundNatRulePropertiesFormat.FrontendIPConfiguration != nil {
176-
d.Set("frontend_ip_configuration_id", config.InboundNatRulePropertiesFormat.FrontendIPConfiguration.ID)
177-
}
173+
d.Set("name", config.Name)
174+
d.Set("protocol", config.InboundNatRulePropertiesFormat.Protocol)
175+
d.Set("frontend_port", config.InboundNatRulePropertiesFormat.FrontendPort)
176+
d.Set("backend_port", config.InboundNatRulePropertiesFormat.BackendPort)
178177

179-
if config.InboundNatRulePropertiesFormat.BackendIPConfiguration != nil {
180-
d.Set("backend_ip_configuration_id", config.InboundNatRulePropertiesFormat.BackendIPConfiguration.ID)
181-
}
178+
if config.InboundNatRulePropertiesFormat.FrontendIPConfiguration != nil {
179+
d.Set("frontend_ip_configuration_id", config.InboundNatRulePropertiesFormat.FrontendIPConfiguration.ID)
180+
}
182181

183-
break
184-
}
182+
if config.InboundNatRulePropertiesFormat.BackendIPConfiguration != nil {
183+
d.Set("backend_ip_configuration_id", config.InboundNatRulePropertiesFormat.BackendIPConfiguration.ID)
185184
}
186185

187186
return nil

builtin/providers/azurerm/resource_arm_loadbalancer_nat_rule_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,29 @@ func TestAccAzureRMLoadBalancerNatRule_reapply(t *testing.T) {
136136
})
137137
}
138138

139+
func TestAccAzureRMLoadBalancerNatRule_disappears(t *testing.T) {
140+
var lb network.LoadBalancer
141+
ri := acctest.RandInt()
142+
natRuleName := fmt.Sprintf("NatRule-%d", ri)
143+
144+
resource.Test(t, resource.TestCase{
145+
PreCheck: func() { testAccPreCheck(t) },
146+
Providers: testAccProviders,
147+
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
148+
Steps: []resource.TestStep{
149+
{
150+
Config: testAccAzureRMLoadBalancerNatRule_basic(ri, natRuleName),
151+
Check: resource.ComposeTestCheckFunc(
152+
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
153+
testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb),
154+
testCheckAzureRMLoadBalancerNatRuleDisappears(natRuleName, &lb),
155+
),
156+
ExpectNonEmptyPlan: true,
157+
},
158+
},
159+
})
160+
}
161+
139162
func testCheckAzureRMLoadBalancerNatRuleExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
140163
return func(s *terraform.State) error {
141164
_, _, exists := findLoadBalancerNatRuleByName(lb, natRuleName)
@@ -158,6 +181,34 @@ func testCheckAzureRMLoadBalancerNatRuleNotExists(natRuleName string, lb *networ
158181
}
159182
}
160183

184+
func testCheckAzureRMLoadBalancerNatRuleDisappears(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
185+
return func(s *terraform.State) error {
186+
conn := testAccProvider.Meta().(*ArmClient).loadBalancerClient
187+
188+
_, i, exists := findLoadBalancerNatRuleByName(lb, natRuleName)
189+
if !exists {
190+
return fmt.Errorf("A Nat Rule with name %q cannot be found.", natRuleName)
191+
}
192+
193+
currentRules := *lb.LoadBalancerPropertiesFormat.InboundNatRules
194+
rules := append(currentRules[:i], currentRules[i+1:]...)
195+
lb.LoadBalancerPropertiesFormat.InboundNatRules = &rules
196+
197+
id, err := parseAzureResourceID(*lb.ID)
198+
if err != nil {
199+
return err
200+
}
201+
202+
_, err = conn.CreateOrUpdate(id.ResourceGroup, *lb.Name, *lb, make(chan struct{}))
203+
if err != nil {
204+
return fmt.Errorf("Error Creating/Updating LoadBalancer %s", err)
205+
}
206+
207+
_, err = conn.Get(id.ResourceGroup, *lb.Name, "")
208+
return err
209+
}
210+
}
211+
161212
func testAccAzureRMLoadBalancerNatRule_basic(rInt int, natRuleName string) string {
162213
return fmt.Sprintf(`
163214
resource "azurerm_resource_group" "test" {

builtin/providers/azurerm/resource_arm_loadbalancer_probe.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -168,21 +168,20 @@ func resourceArmLoadBalancerProbeRead(d *schema.ResourceData, meta interface{})
168168
return nil
169169
}
170170

171-
configs := *loadBalancer.LoadBalancerPropertiesFormat.Probes
172-
for _, config := range configs {
173-
if *config.Name == d.Get("name").(string) {
174-
d.Set("name", config.Name)
175-
176-
d.Set("protocol", config.ProbePropertiesFormat.Protocol)
177-
d.Set("interval_in_seconds", config.ProbePropertiesFormat.IntervalInSeconds)
178-
d.Set("number_of_probes", config.ProbePropertiesFormat.NumberOfProbes)
179-
d.Set("port", config.ProbePropertiesFormat.Port)
180-
d.Set("request_path", config.ProbePropertiesFormat.RequestPath)
181-
182-
break
183-
}
171+
config, _, exists := findLoadBalancerProbeByName(loadBalancer, d.Get("name").(string))
172+
if !exists {
173+
d.SetId("")
174+
log.Printf("[INFO] LoadBalancer Probe %q not found. Removing from state", d.Get("name").(string))
175+
return nil
184176
}
185177

178+
d.Set("name", config.Name)
179+
d.Set("protocol", config.ProbePropertiesFormat.Protocol)
180+
d.Set("interval_in_seconds", config.ProbePropertiesFormat.IntervalInSeconds)
181+
d.Set("number_of_probes", config.ProbePropertiesFormat.NumberOfProbes)
182+
d.Set("port", config.ProbePropertiesFormat.Port)
183+
d.Set("request_path", config.ProbePropertiesFormat.RequestPath)
184+
186185
return nil
187186
}
188187

0 commit comments

Comments
 (0)