Skip to content

Commit

Permalink
Merge pull request #409 from terraform-providers/refactoring
Browse files Browse the repository at this point in the history
refactoring of `azurerm_network_interface` and `azurerm_availability_set`
  • Loading branch information
tombuildsstuff authored Oct 11, 2017
2 parents 8512d93 + 428cacb commit 679b689
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 62 deletions.
38 changes: 19 additions & 19 deletions azurerm/resource_arm_availability_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package azurerm

import (
"fmt"
"net/http"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func TestAccAzureRMAvailabilitySet_basic(t *testing.T) {
Expand Down Expand Up @@ -146,15 +146,14 @@ func testCheckAzureRMAvailabilitySetExists(name string) resource.TestCheckFunc {
return fmt.Errorf("Bad: no resource group found in state for availability set: %s", availSetName)
}

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

resp, err := conn.Get(resourceGroup, availSetName)
client := testAccProvider.Meta().(*ArmClient).availSetClient
resp, err := client.Get(resourceGroup, availSetName)
if err != nil {
return fmt.Errorf("Bad: Get on availSetClient: %+v", err)
}
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Bad: Availability Set %q (resource group: %q) does not exist", name, resourceGroup)
}

if resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("Bad: Availability Set %q (resource group: %q) does not exist", name, resourceGroup)
return fmt.Errorf("Bad: Get on availSetClient: %+v", err)
}

return nil
Expand All @@ -175,20 +174,19 @@ func testCheckAzureRMAvailabilitySetDisappears(name string) resource.TestCheckFu
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)
client := testAccProvider.Meta().(*ArmClient).availSetClient
resp, err := client.Delete(resourceGroup, availSetName)
if err != nil {
return fmt.Errorf("Bad: Delete on availSetClient: %+v", err)
if !utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Bad: Delete on availSetClient: %+v", err)
}
}

return nil
}
}

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

for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_availability_set" {
continue
Expand All @@ -197,15 +195,17 @@ func testCheckAzureRMAvailabilitySetDestroy(s *terraform.State) error {
name := rs.Primary.Attributes["name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]

resp, err := conn.Get(resourceGroup, name)
client := testAccProvider.Meta().(*ArmClient).availSetClient
resp, err := client.Get(resourceGroup, name)

if err != nil {
return nil
if utils.ResponseWasNotFound(resp.Response) {
return nil
}
return err
}

if resp.StatusCode != http.StatusNotFound {
return fmt.Errorf("Availability Set still exists:\n%#v", resp.AvailabilitySetProperties)
}
return fmt.Errorf("Availability Set still exists:\n%#v", resp.AvailabilitySetProperties)
}

return nil
Expand Down
36 changes: 26 additions & 10 deletions azurerm/resource_arm_network_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ func resourceArmNetworkInterface() *schema.Resource {
Computed: true,
},

"private_ip_addresses": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"tags": tagsSchema(),
},
}
Expand All @@ -159,7 +167,7 @@ func resourceArmNetworkInterface() *schema.Resource {
func resourceArmNetworkInterfaceCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).ifaceClient

log.Printf("[INFO] preparing arguments for Azure ARM Network Interface creation.")
log.Printf("[INFO] preparing arguments for AzureRM Network Interface creation.")

name := d.Get("name").(string)
location := d.Get("location").(string)
Expand Down Expand Up @@ -231,8 +239,8 @@ func resourceArmNetworkInterfaceCreateUpdate(d *schema.ResourceData, meta interf
Tags: expandTags(tags),
}

_, error := client.CreateOrUpdate(resGroup, name, iface, make(chan struct{}))
err := <-error
_, createErr := client.CreateOrUpdate(resGroup, name, iface, make(chan struct{}))
err := <-createErr
if err != nil {
return err
}
Expand All @@ -242,7 +250,7 @@ func resourceArmNetworkInterfaceCreateUpdate(d *schema.ResourceData, meta interf
return err
}
if read.ID == nil {
return fmt.Errorf("Cannot read NIC %s (resource group %s) ID", name, resGroup)
return fmt.Errorf("Cannot read NIC %q (resource group %q) ID", name, resGroup)
}

d.SetId(*read.ID)
Expand Down Expand Up @@ -278,15 +286,23 @@ func resourceArmNetworkInterfaceRead(d *schema.ResourceData, meta interface{}) e
}

if iface.IPConfigurations != nil && len(*iface.IPConfigurations) > 0 {
var privateIPAddress *string
///TODO: Change this to a loop when https://github.com/Azure/azure-sdk-for-go/issues/259 is fixed
if (*iface.IPConfigurations)[0].InterfaceIPConfigurationPropertiesFormat != nil {
privateIPAddress = (*iface.IPConfigurations)[0].InterfaceIPConfigurationPropertiesFormat.PrivateIPAddress
}
configs := *iface.IPConfigurations

if *privateIPAddress != "" {
if configs[0].InterfaceIPConfigurationPropertiesFormat != nil {
privateIPAddress := configs[0].InterfaceIPConfigurationPropertiesFormat.PrivateIPAddress
d.Set("private_ip_address", *privateIPAddress)
}

addresses := make([]interface{}, 0)
for _, config := range configs {
if config.InterfaceIPConfigurationPropertiesFormat != nil {
addresses = append(addresses, *config.InterfaceIPConfigurationPropertiesFormat.PrivateIPAddress)
}
}

if err := d.Set("private_ip_addresses", addresses); err != nil {
return err
}
}

if iface.IPConfigurations != nil {
Expand Down
23 changes: 10 additions & 13 deletions azurerm/resource_arm_network_interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package azurerm

import (
"fmt"
"net/http"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
Expand Down Expand Up @@ -259,18 +258,17 @@ func testCheckAzureRMNetworkInterfaceExists(name string) resource.TestCheckFunc
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)
return fmt.Errorf("Bad: no resource group found in state for availability set: %q", name)
}

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

resp, err := conn.Get(resourceGroup, name, "")
client := testAccProvider.Meta().(*ArmClient).ifaceClient
resp, err := client.Get(resourceGroup, name, "")
if err != nil {
return fmt.Errorf("Bad: Get on ifaceClient: %+v", err)
}
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Bad: Network Interface %q (resource group: %q) does not exist", name, resourceGroup)
}

if resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("Bad: Network Interface %q (resource group: %q) does not exist", name, resourceGroup)
return fmt.Errorf("Bad: Get on ifaceClient: %+v", err)
}

return nil
Expand All @@ -288,7 +286,7 @@ func testCheckAzureRMNetworkInterfaceDisappears(name string) resource.TestCheckF
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)
return fmt.Errorf("Bad: no resource group found in state for availability set: %q", name)
}

conn := testAccProvider.Meta().(*ArmClient).ifaceClient
Expand All @@ -304,7 +302,7 @@ func testCheckAzureRMNetworkInterfaceDisappears(name string) resource.TestCheckF
}

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

for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_network_interface" {
Expand All @@ -314,8 +312,7 @@ func testCheckAzureRMNetworkInterfaceDestroy(s *terraform.State) error {
name := rs.Primary.Attributes["name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]

resp, err := conn.Get(resourceGroup, name, "")

resp, err := client.Get(resourceGroup, name, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return nil
Expand Down
17 changes: 8 additions & 9 deletions website/docs/r/availability_set.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_availability_set"
sidebar_current: "docs-azurerm-resource-virtualmachine-availability-set"
description: |-
Create an availability set for virtual machines.
Manages an availability set for virtual machines.
---

# azurerm\_availability\_set
# azurerm_availability_set

Create an availability set for virtual machines.
Manages an availability set for virtual machines.

## Example Usage

Expand All @@ -20,7 +21,7 @@ resource "azurerm_resource_group" "test" {
resource "azurerm_availability_set" "test" {
name = "acceptanceTestAvailabilitySet1"
location = "West US"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
tags {
Expand All @@ -33,11 +34,9 @@ resource "azurerm_availability_set" "test" {

The following arguments are supported:

* `name` - (Required) Specifies the name of the availability set. Changing this forces a
new resource to be created.
* `name` - (Required) Specifies the name of the availability set. Changing this forces a new resource to be created.

* `resource_group_name` - (Required) The name of the resource group in which to
create the availability set.
* `resource_group_name` - (Required) The name of the resource group in which to create the availability set. Changing this forces a new resource to be created.

* `location` - (Required) Specifies the supported Azure location where the resource exists. Changing this forces a new resource to be created.

Expand All @@ -53,7 +52,7 @@ The following arguments are supported:

The following attributes are exported:

* `id` - The virtual AvailabilitySet ID.
* `id` - The virtual Availability Set ID.


## Import
Expand Down
18 changes: 7 additions & 11 deletions website/docs/r/network_interface.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ description: |-
---

# azurerm\_network\_interface
# azurerm_network_interface

Manages a Network Interface located in a Virtual Network, usually attached to a Virtual Machine.

Expand All @@ -22,7 +22,7 @@ resource "azurerm_resource_group" "test" {
resource "azurerm_virtual_network" "test" {
name = "acceptanceTestVirtualNetwork1"
address_space = ["10.0.0.0/16"]
location = "West US"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
Expand All @@ -35,7 +35,7 @@ resource "azurerm_subnet" "test" {
resource "azurerm_network_interface" "test" {
name = "acceptanceTestNetworkInterface1"
location = "West US"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
ip_configuration {
Expand All @@ -54,17 +54,13 @@ resource "azurerm_network_interface" "test" {

The following arguments are supported:

* `name` - (Required) The name of the network interface. Changing this forces a
new resource to be created.
* `name` - (Required) The name of the network interface. Changing this forces a new resource to be created.

* `resource_group_name` - (Required) The name of the resource group in which to
create the network interface.
* `resource_group_name` - (Required) The name of the resource group in which to create the network interface. Changing this forces a new resource to be created.

* `location` - (Required) The location/region where the network interface is
created. Changing this forces a new resource to be created.
* `location` - (Required) The location/region where the network interface is created. Changing this forces a new resource to be created.

* `network_security_group_id` - (Optional) The ID of the Network Security Group to associate with
the network interface.
* `network_security_group_id` - (Optional) The ID of the Network Security Group to associate with the network interface.

* `internal_dns_name_label` - (Optional) Relative DNS name for this NIC used for internal communications between VMs in the same VNet

Expand Down

0 comments on commit 679b689

Please sign in to comment.