Skip to content

Commit

Permalink
Merge pull request #8859 from carinadigital/arm-vm-fix
Browse files Browse the repository at this point in the history
provider/azurerm: Terraform Plan errors when created resources disappear
  • Loading branch information
stack72 authored Oct 6, 2016
2 parents 4344a9e + e5219ba commit 97ad032
Show file tree
Hide file tree
Showing 38 changed files with 986 additions and 77 deletions.
8 changes: 4 additions & 4 deletions builtin/providers/azurerm/resource_arm_availability_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ func resourceArmAvailabilitySetRead(d *schema.ResourceData, meta interface{}) er

resp, err := availSetClient.Get(resGroup, name)
if err != nil {
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on Azure Availability Set %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}

availSet := *resp.Properties
d.Set("platform_update_domain_count", availSet.PlatformUpdateDomainCount)
Expand Down
51 changes: 51 additions & 0 deletions builtin/providers/azurerm/resource_arm_availability_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,32 @@ func TestAccAzureRMAvailabilitySet_basic(t *testing.T) {
})
}

func TestAccAzureRMAvailabilitySet_disappears(t *testing.T) {

ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMVAvailabilitySet_basic, ri, ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMAvailabilitySetDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAvailabilitySetExists("azurerm_availability_set.test"),
resource.TestCheckResourceAttr(
"azurerm_availability_set.test", "platform_update_domain_count", "5"),
resource.TestCheckResourceAttr(
"azurerm_availability_set.test", "platform_fault_domain_count", "3"),
testCheckAzureRMAvailabilitySetDisappears("azurerm_availability_set.test"),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAzureRMAvailabilitySet_withTags(t *testing.T) {

ri := acctest.RandInt()
Expand Down Expand Up @@ -125,6 +151,31 @@ func testCheckAzureRMAvailabilitySetExists(name string) resource.TestCheckFunc {
}
}

func testCheckAzureRMAvailabilitySetDisappears(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Ensure we have enough information in state to look up in API
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}

availSetName := rs.Primary.Attributes["name"]
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
if !hasResourceGroup {
return fmt.Errorf("Bad: no resource group found in state for availability set: %s", availSetName)
}

conn := testAccProvider.Meta().(*ArmClient).availSetClient

_, err := conn.Delete(resourceGroup, availSetName)
if err != nil {
return fmt.Errorf("Bad: Delete on availSetClient: %s", err)
}

return nil
}
}

func testCheckAzureRMAvailabilitySetDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).availSetClient

Expand Down
8 changes: 4 additions & 4 deletions builtin/providers/azurerm/resource_arm_cdn_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,12 @@ func resourceArmCdnEndpointRead(d *schema.ResourceData, meta interface{}) error
log.Printf("[INFO] Trying to find the AzureRM CDN Endpoint %s (Profile: %s, RG: %s)", name, profileName, resGroup)
resp, err := cdnEndpointsClient.Get(name, profileName, resGroup)
if err != nil {
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on Azure CDN Endpoint %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}

d.Set("name", resp.Name)
d.Set("host_name", resp.Properties.HostName)
Expand Down
47 changes: 47 additions & 0 deletions builtin/providers/azurerm/resource_arm_cdn_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@ func TestAccAzureRMCdnEndpoint_basic(t *testing.T) {
})
}

func TestAccAzureRMCdnEndpoint_disappears(t *testing.T) {
ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMCdnEndpoint_basic, ri, ri, ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMCdnEndpointDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMCdnEndpointExists("azurerm_cdn_endpoint.test"),
testCheckAzureRMCdnEndpointDisappears("azurerm_cdn_endpoint.test"),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAzureRMCdnEndpoint_withTags(t *testing.T) {
ri := acctest.RandInt()
preConfig := fmt.Sprintf(testAccAzureRMCdnEndpoint_withTags, ri, ri, ri)
Expand Down Expand Up @@ -96,6 +117,32 @@ func testCheckAzureRMCdnEndpointExists(name string) resource.TestCheckFunc {
}
}

func testCheckAzureRMCdnEndpointDisappears(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Ensure we have enough information in state to look up in API
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}

name := rs.Primary.Attributes["name"]
profileName := rs.Primary.Attributes["profile_name"]
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
if !hasResourceGroup {
return fmt.Errorf("Bad: no resource group found in state for cdn endpoint: %s", name)
}

conn := testAccProvider.Meta().(*ArmClient).cdnEndpointsClient

_, err := conn.DeleteIfExists(name, profileName, resourceGroup, make(chan struct{}))
if err != nil {
return fmt.Errorf("Bad: Delete on cdnEndpointsClient: %s", err)
}

return nil
}
}

func testCheckAzureRMCdnEndpointDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).cdnEndpointsClient

Expand Down
8 changes: 4 additions & 4 deletions builtin/providers/azurerm/resource_arm_cdn_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ func resourceArmCdnProfileRead(d *schema.ResourceData, meta interface{}) error {

resp, err := cdnProfilesClient.Get(name, resGroup)
if err != nil {
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on Azure CDN Profile %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}

if resp.Sku != nil {
d.Set("sku", string(resp.Sku.Name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ func resourceArmLocalNetworkGatewayRead(d *schema.ResourceData, meta interface{}

resp, err := lnetClient.Get(resGroup, name)
if err != nil {
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
return fmt.Errorf("Error reading the state of Azure ARM local network gateway '%s': %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}

d.Set("name", resp.Name)
d.Set("location", resp.Location)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ func TestAccAzureRMLocalNetworkGateway_basic(t *testing.T) {
})
}

func TestAccAzureRMLocalNetworkGateway_disappears(t *testing.T) {
name := "azurerm_local_network_gateway.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLocalNetworkGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMLocalNetworkGatewayConfig_basic,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLocalNetworkGatewayExists(name),
resource.TestCheckResourceAttr(name, "gateway_address", "127.0.0.1"),
resource.TestCheckResourceAttr(name, "address_space.0", "127.0.0.0/8"),
testCheckAzureRMLocalNetworkGatewayDisappears(name),
),
ExpectNonEmptyPlan: true,
},
},
})
}

// testCheckAzureRMLocalNetworkGatewayExists returns the resurce.TestCheckFunc
// which checks whether or not the expected local network gateway exists both
// in the schema, and on Azure.
Expand Down Expand Up @@ -63,6 +85,37 @@ func testCheckAzureRMLocalNetworkGatewayExists(name string) resource.TestCheckFu
}
}

func testCheckAzureRMLocalNetworkGatewayDisappears(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// first check within the schema for the local network gateway:
res, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Local network gateway '%s' not found.", name)
}

// then, extract the name and the resource group:
id, err := parseAzureResourceID(res.Primary.ID)
if err != nil {
return err
}
localNetName := id.Path["localNetworkGateways"]
resGrp := id.ResourceGroup

// and finally, check that it exists on Azure:
lnetClient := testAccProvider.Meta().(*ArmClient).localNetConnClient

resp, err := lnetClient.Delete(resGrp, localNetName, make(chan struct{}))
if err != nil {
if resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("Local network gateway '%s' (resource group '%s') does not exist on Azure.", localNetName, resGrp)
}
return fmt.Errorf("Error deleting the state of local network gateway '%s'.", localNetName)
}

return nil
}
}

func testCheckAzureRMLocalNetworkGatewayDestroy(s *terraform.State) error {
for _, res := range s.RootModule().Resources {
if res.Type != "azurerm_local_network_gateway" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,12 @@ func resourceArmNetworkInterfaceRead(d *schema.ResourceData, meta interface{}) e

resp, err := ifaceClient.Get(resGroup, name, "")
if err != nil {
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on Azure Network Interface %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}

iface := *resp.Properties

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ func TestAccAzureRMNetworkInterface_basic(t *testing.T) {
})
}

func TestAccAzureRMNetworkInterface_disappears(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMNetworkInterfaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMNetworkInterface_basic,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMNetworkInterfaceExists("azurerm_network_interface.test"),
testCheckAzureRMNetworkInterfaceDisappears("azurerm_network_interface.test"),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAzureRMNetworkInterface_enableIPForwarding(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -133,6 +151,31 @@ func testCheckAzureRMNetworkInterfaceExists(name string) resource.TestCheckFunc
}
}

func testCheckAzureRMNetworkInterfaceDisappears(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Ensure we have enough information in state to look up in API
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}

name := rs.Primary.Attributes["name"]
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
if !hasResourceGroup {
return fmt.Errorf("Bad: no resource group found in state for availability set: %s", name)
}

conn := testAccProvider.Meta().(*ArmClient).ifaceClient

_, err := conn.Delete(resourceGroup, name, make(chan struct{}))
if err != nil {
return fmt.Errorf("Bad: Delete on ifaceClient: %s", err)
}

return nil
}
}

func testCheckAzureRMNetworkInterfaceDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).ifaceClient

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,12 @@ func resourceArmNetworkSecurityGroupRead(d *schema.ResourceData, meta interface{

resp, err := secGroupClient.Get(resGroup, name, "")
if err != nil {
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on Azure Network Security Group %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}

if resp.Properties.SecurityRules != nil {
d.Set("security_rule", flattenNetworkSecurityRules(resp.Properties.SecurityRules))
Expand Down
Loading

0 comments on commit 97ad032

Please sign in to comment.