mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
provider/azurerm: support import of routes, fix route_table (#10389)
* provider/azurerm: support import of route TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMRoute_import -timeout 120m === RUN TestAccAzureRMRoute_importBasic --- PASS: TestAccAzureRMRoute_importBasic (166.99s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 167.066s * provider/azurerm: fix route_table not setting routes The resource wasn't actually setting the routes in the create/update method, this went unnoticed as it also didn't read the routes array back to state. Fixes #10316 TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMRouteTable -timeout 120m === RUN TestAccAzureRMRouteTable_basic --- PASS: TestAccAzureRMRouteTable_basic (122.96s) === RUN TestAccAzureRMRouteTable_disappears --- PASS: TestAccAzureRMRouteTable_disappears (121.12s) === RUN TestAccAzureRMRouteTable_withTags --- PASS: TestAccAzureRMRouteTable_withTags (136.01s) === RUN TestAccAzureRMRouteTable_multipleRoutes --- PASS: TestAccAzureRMRouteTable_multipleRoutes (155.44s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 535.612s * provider/azurerm: support import of route_table TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMRouteTable_import -timeout 120m === RUN TestAccAzureRMRouteTable_importBasic --- PASS: TestAccAzureRMRouteTable_importBasic (121.90s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 121.978s
This commit is contained in:
parent
5cda81a6d7
commit
3bb9312d19
33
builtin/providers/azurerm/import_arm_route_table_test.go
Normal file
33
builtin/providers/azurerm/import_arm_route_table_test.go
Normal file
@ -0,0 +1,33 @@
|
||||
package azurerm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/acctest"
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
)
|
||||
|
||||
func TestAccAzureRMRouteTable_importBasic(t *testing.T) {
|
||||
resourceName := "azurerm_route_table.test"
|
||||
|
||||
ri := acctest.RandInt()
|
||||
config := fmt.Sprintf(testAccAzureRMRouteTable_basic, ri, ri)
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testCheckAzureRMRouteTableDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: config,
|
||||
},
|
||||
|
||||
resource.TestStep{
|
||||
ResourceName: resourceName,
|
||||
ImportState: true,
|
||||
ImportStateVerify: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
33
builtin/providers/azurerm/import_arm_route_test.go
Normal file
33
builtin/providers/azurerm/import_arm_route_test.go
Normal file
@ -0,0 +1,33 @@
|
||||
package azurerm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/acctest"
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
)
|
||||
|
||||
func TestAccAzureRMRoute_importBasic(t *testing.T) {
|
||||
resourceName := "azurerm_route.test"
|
||||
|
||||
ri := acctest.RandInt()
|
||||
config := fmt.Sprintf(testAccAzureRMRoute_basic, ri, ri, ri)
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testCheckAzureRMRouteDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: config,
|
||||
},
|
||||
|
||||
resource.TestStep{
|
||||
ResourceName: resourceName,
|
||||
ImportState: true,
|
||||
ImportStateVerify: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
@ -3,6 +3,7 @@ package azurerm
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/Azure/azure-sdk-for-go/arm/network"
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
@ -14,6 +15,9 @@ func resourceArmRoute() *schema.Resource {
|
||||
Read: resourceArmRouteRead,
|
||||
Update: resourceArmRouteCreate,
|
||||
Delete: resourceArmRouteDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
State: schema.ImportStatePassthrough,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"name": {
|
||||
@ -43,6 +47,9 @@ func resourceArmRoute() *schema.Resource {
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ValidateFunc: validateRouteTableNextHopType,
|
||||
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
|
||||
return strings.ToLower(old) == strings.ToLower(new)
|
||||
},
|
||||
},
|
||||
|
||||
"next_hop_in_ip_address": {
|
||||
@ -120,6 +127,16 @@ func resourceArmRouteRead(d *schema.ResourceData, meta interface{}) error {
|
||||
return fmt.Errorf("Error making Read request on Azure Route %s: %s", routeName, err)
|
||||
}
|
||||
|
||||
d.Set("name", routeName)
|
||||
d.Set("resource_group_name", resGroup)
|
||||
d.Set("route_table_name", rtName)
|
||||
d.Set("address_prefix", resp.Properties.AddressPrefix)
|
||||
d.Set("next_hop_type", string(resp.Properties.NextHopType))
|
||||
|
||||
if resp.Properties.NextHopIPAddress != nil {
|
||||
d.Set("next_hop_in_ip_address", resp.Properties.NextHopIPAddress)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,9 @@ func resourceArmRouteTable() *schema.Resource {
|
||||
Read: resourceArmRouteTableRead,
|
||||
Update: resourceArmRouteTableCreate,
|
||||
Delete: resourceArmRouteTableDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
State: schema.ImportStatePassthrough,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"name": {
|
||||
@ -73,7 +76,6 @@ func resourceArmRouteTable() *schema.Resource {
|
||||
|
||||
"subnets": {
|
||||
Type: schema.TypeSet,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
Elem: &schema.Schema{Type: schema.TypeString},
|
||||
Set: schema.HashString,
|
||||
@ -102,15 +104,16 @@ func resourceArmRouteTableCreate(d *schema.ResourceData, meta interface{}) error
|
||||
}
|
||||
|
||||
if _, ok := d.GetOk("route"); ok {
|
||||
properties := network.RouteTablePropertiesFormat{}
|
||||
routes, routeErr := expandAzureRmRouteTableRoutes(d)
|
||||
if routeErr != nil {
|
||||
return fmt.Errorf("Error Building list of Route Table Routes: %s", routeErr)
|
||||
}
|
||||
if len(routes) > 0 {
|
||||
routeSet.Properties = &properties
|
||||
}
|
||||
|
||||
if len(routes) > 0 {
|
||||
routeSet.Properties = &network.RouteTablePropertiesFormat{
|
||||
Routes: &routes,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_, err := routeTablesClient.CreateOrUpdate(resGroup, name, routeSet, make(chan struct{}))
|
||||
@ -150,19 +153,22 @@ func resourceArmRouteTableRead(d *schema.ResourceData, meta interface{}) error {
|
||||
return fmt.Errorf("Error making Read request on Azure Route Table %s: %s", name, err)
|
||||
}
|
||||
|
||||
if resp.Properties.Subnets != nil {
|
||||
if len(*resp.Properties.Subnets) > 0 {
|
||||
subnets := make([]string, 0, len(*resp.Properties.Subnets))
|
||||
for _, subnet := range *resp.Properties.Subnets {
|
||||
id := subnet.ID
|
||||
subnets = append(subnets, *id)
|
||||
}
|
||||
d.Set("name", name)
|
||||
d.Set("resource_group_name", resGroup)
|
||||
d.Set("location", resp.Location)
|
||||
|
||||
if err := d.Set("subnets", subnets); err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.Properties.Routes != nil {
|
||||
d.Set("route", schema.NewSet(resourceArmRouteTableRouteHash, flattenAzureRmRouteTableRoutes(resp.Properties.Routes)))
|
||||
}
|
||||
|
||||
subnets := []string{}
|
||||
if resp.Properties.Subnets != nil {
|
||||
for _, subnet := range *resp.Properties.Subnets {
|
||||
id := subnet.ID
|
||||
subnets = append(subnets, *id)
|
||||
}
|
||||
}
|
||||
d.Set("subnets", subnets)
|
||||
|
||||
flattenAndSetTags(d, resp.Tags)
|
||||
|
||||
@ -215,12 +221,29 @@ func expandAzureRmRouteTableRoutes(d *schema.ResourceData) ([]network.Route, err
|
||||
return routes, nil
|
||||
}
|
||||
|
||||
func flattenAzureRmRouteTableRoutes(routes *[]network.Route) []interface{} {
|
||||
results := make([]interface{}, 0, len(*routes))
|
||||
|
||||
for _, route := range *routes {
|
||||
r := make(map[string]interface{})
|
||||
r["name"] = *route.Name
|
||||
r["address_prefix"] = *route.Properties.AddressPrefix
|
||||
r["next_hop_type"] = string(route.Properties.NextHopType)
|
||||
if route.Properties.NextHopIPAddress != nil {
|
||||
r["next_hop_in_ip_address"] = *route.Properties.NextHopIPAddress
|
||||
}
|
||||
results = append(results, r)
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
func resourceArmRouteTableRouteHash(v interface{}) int {
|
||||
var buf bytes.Buffer
|
||||
m := v.(map[string]interface{})
|
||||
buf.WriteString(fmt.Sprintf("%s-", m["name"].(string)))
|
||||
buf.WriteString(fmt.Sprintf("%s-", m["address_prefix"].(string)))
|
||||
buf.WriteString(fmt.Sprintf("%s-", m["next_hop_type"].(string)))
|
||||
buf.WriteString(fmt.Sprintf("%s-", strings.ToLower(m["next_hop_type"].(string))))
|
||||
|
||||
return hashcode.String(buf.String())
|
||||
}
|
||||
|
@ -262,8 +262,8 @@ resource "azurerm_route_table" "test" {
|
||||
|
||||
route {
|
||||
name = "route1"
|
||||
address_prefix = "*"
|
||||
next_hop_type = "internet"
|
||||
address_prefix = "10.1.0.0/16"
|
||||
next_hop_type = "vnetlocal"
|
||||
}
|
||||
}
|
||||
`
|
||||
@ -281,14 +281,14 @@ resource "azurerm_route_table" "test" {
|
||||
|
||||
route {
|
||||
name = "route1"
|
||||
address_prefix = "*"
|
||||
next_hop_type = "internet"
|
||||
address_prefix = "10.1.0.0/16"
|
||||
next_hop_type = "vnetlocal"
|
||||
}
|
||||
|
||||
route {
|
||||
name = "route2"
|
||||
address_prefix = "*"
|
||||
next_hop_type = "virtualappliance"
|
||||
address_prefix = "10.2.0.0/16"
|
||||
next_hop_type = "vnetlocal"
|
||||
}
|
||||
}
|
||||
`
|
||||
@ -306,8 +306,8 @@ resource "azurerm_route_table" "test" {
|
||||
|
||||
route {
|
||||
name = "route1"
|
||||
address_prefix = "*"
|
||||
next_hop_type = "internet"
|
||||
address_prefix = "10.1.0.0/16"
|
||||
next_hop_type = "vnetlocal"
|
||||
}
|
||||
|
||||
tags {
|
||||
@ -330,8 +330,8 @@ resource "azurerm_route_table" "test" {
|
||||
|
||||
route {
|
||||
name = "route1"
|
||||
address_prefix = "*"
|
||||
next_hop_type = "internet"
|
||||
address_prefix = "10.1.0.0/16"
|
||||
next_hop_type = "vnetlocal"
|
||||
}
|
||||
|
||||
tags {
|
||||
|
@ -59,3 +59,11 @@ The following arguments are supported:
|
||||
The following attributes are exported:
|
||||
|
||||
* `id` - The Route ID.
|
||||
|
||||
## Import
|
||||
|
||||
|
||||
Routes can be imported using the `resource id`, e.g.
|
||||
```
|
||||
terraform import azurerm_route.testRoute /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.Network/routeTables/mytable1/routes/myroute1
|
||||
```
|
||||
|
@ -25,8 +25,8 @@ resource "azurerm_route_table" "test" {
|
||||
|
||||
route {
|
||||
name = "route1"
|
||||
address_prefix = "*"
|
||||
next_hop_type = "internet"
|
||||
address_prefix = "10.1.0.0/16"
|
||||
next_hop_type = "vnetlocal"
|
||||
}
|
||||
|
||||
tags {
|
||||
@ -69,3 +69,11 @@ The following attributes are exported:
|
||||
|
||||
* `id` - The Route Table ID.
|
||||
* `subnets` - The collection of Subnets associated with this route table.
|
||||
|
||||
## Import
|
||||
|
||||
|
||||
Route Tables can be imported using the `resource id`, e.g.
|
||||
```
|
||||
terraform import azurerm_route_table.test /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.Network/routeTables/mytable1
|
||||
```
|
Loading…
Reference in New Issue
Block a user