Skip to content

Commit

Permalink
[Network] Doc deprecation message to router_route_v2 and refactoring (#…
Browse files Browse the repository at this point in the history
…2669)

[Network] Doc deprecation message to router_route_v2 and refactoring

Summary of the Pull Request
Resource not presented in docportal, it means that it is deprecated.
But still works, so refactored and added test case with instance recreation.
PR Checklist

 Refers to: #2652
 Tests added/passed.
 Documentation updated.
 Schema updated.
 Release notes added.

Acceptance Steps Performed
=== RUN   TestAccNetworkingV2RouterRoute_basic
=== PAUSE TestAccNetworkingV2RouterRoute_basic
=== CONT  TestAccNetworkingV2RouterRoute_basic
--- PASS: TestAccNetworkingV2RouterRoute_basic (233.55s)
PASS

=== RUN   TestAccNetworkingV2RouterRoute_ecs
=== PAUSE TestAccNetworkingV2RouterRoute_ecs
=== CONT  TestAccNetworkingV2RouterRoute_ecs
--- PASS: TestAccNetworkingV2RouterRoute_ecs (340.75s)
PASS

Debugger finished with the exit code 0

Reviewed-by: Artem Lifshits
Reviewed-by: Muneeb H. Jan <muneebhafeezjan@gmail.com>
  • Loading branch information
anton-sidelnikov authored Oct 1, 2024
1 parent e77eca0 commit 6880166
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 59 deletions.
1 change: 1 addition & 0 deletions docs/resources/networking_router_route_v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ description: |-
Up-to-date reference of API arguments for VPC router route you can get at
[documentation portal](https://docs.otc.t-systems.com/virtual-private-cloud/api-ref/native_openstack_neutron_apis_v2.0/router)

**DEPRECATED**
# opentelekomcloud_networking_router_route_v2

Creates a routing entry on a OpenTelekomCloud V2 router.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/common/quotas"
Expand Down Expand Up @@ -73,6 +74,40 @@ func TestAccNetworkingV2RouterRoute_basic(t *testing.T) {
})
}

func TestAccNetworkingV2RouterRoute_ecs(t *testing.T) {
resourceName1 := "opentelekomcloud_networking_router_route_v2.router_route_1"
resourceName2 := "opentelekomcloud_networking_router_route_v2.router_route_2"
name := fmt.Sprintf("router_acc_route%s", acctest.RandString(10))
t.Parallel()
qts := []*quotas.ExpectedQuota{
{Q: quotas.Router, Count: 1},
{Q: quotas.Network, Count: 1},
{Q: quotas.Subnet, Count: 1},
}
quotas.BookMany(t, qts)

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccNetworkingV2RouterRouteEcs(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckNetworkingV2RouterRouteExists(resourceName1),
testAccCheckNetworkingV2RouterRouteExists(resourceName2),
),
},
{
Config: testAccNetworkingV2RouterRouteEcsUpdate(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckNetworkingV2RouterRouteExists(resourceName1),
testAccCheckNetworkingV2RouterRouteExists(resourceName2),
),
},
},
})
}

func testAccCheckNetworkingV2RouterRouteEmpty(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -353,3 +388,154 @@ resource "opentelekomcloud_networking_router_interface_v2" "int_2" {
port_id = opentelekomcloud_networking_port_v2.port_2.id
}
`

func testAccNetworkingV2RouterRouteEcs(name string) string {
return fmt.Sprintf(`
%[3]s
data "opentelekomcloud_images_image_v2" "other_image" {
name = "Standard_Debian_12_amd64_bios_latest"
most_recent = true
}
resource "opentelekomcloud_networking_router_v2" "router_1" {
name = "%[1]s_router"
admin_state_up = "true"
}
resource "opentelekomcloud_networking_network_v2" "network_1" {
name = "%[1]s_network"
admin_state_up = "true"
}
resource "opentelekomcloud_networking_subnet_v2" "subnet_1" {
cidr = "192.168.199.0/24"
ip_version = 4
network_id = opentelekomcloud_networking_network_v2.network_1.id
}
resource "opentelekomcloud_networking_port_v2" "port_1" {
name = "%[1]s_port"
network_id = opentelekomcloud_networking_network_v2.network_1.id
fixed_ip {
subnet_id = opentelekomcloud_networking_subnet_v2.subnet_1.id
}
}
resource "opentelekomcloud_networking_port_v2" "instance_port_1" {
name = "%[1]s_port"
network_id = opentelekomcloud_networking_network_v2.network_1.id
fixed_ip {
subnet_id = opentelekomcloud_networking_subnet_v2.subnet_1.id
}
}
resource "opentelekomcloud_compute_instance_v2" "instance_1" {
name = "%[1]s_instance"
security_groups = ["default"]
availability_zone = "%[2]s"
image_id = data.opentelekomcloud_images_image_v2.latest_image.id
network {
port = opentelekomcloud_networking_port_v2.instance_port_1.id
}
}
resource "opentelekomcloud_networking_router_interface_v2" "int_1" {
router_id = opentelekomcloud_networking_router_v2.router_1.id
port_id = opentelekomcloud_networking_port_v2.port_1.id
}
resource "opentelekomcloud_networking_router_route_v2" "router_route_1" {
destination_cidr = "192.168.254.254/32"
next_hop = opentelekomcloud_compute_instance_v2.instance_1.network[0].fixed_ip_v4
depends_on = ["opentelekomcloud_networking_router_interface_v2.int_1"]
router_id = opentelekomcloud_networking_router_v2.router_1.id
}
resource "opentelekomcloud_networking_router_route_v2" "router_route_2" {
destination_cidr = "10.0.1.0/24"
next_hop = "192.168.199.250"
depends_on = ["opentelekomcloud_networking_router_interface_v2.int_1"]
router_id = opentelekomcloud_networking_router_v2.router_1.id
}
`, name, env.OS_AVAILABILITY_ZONE, common.DataSourceImage)
}

func testAccNetworkingV2RouterRouteEcsUpdate(name string) string {
return fmt.Sprintf(`
%[3]s
data "opentelekomcloud_images_image_v2" "other_image" {
name = "Standard_Debian_12_amd64_bios_latest"
most_recent = true
}
resource "opentelekomcloud_networking_router_v2" "router_1" {
name = "%[1]s_router"
admin_state_up = "true"
}
resource "opentelekomcloud_networking_network_v2" "network_1" {
name = "%[1]s_network"
admin_state_up = "true"
}
resource "opentelekomcloud_networking_subnet_v2" "subnet_1" {
cidr = "192.168.199.0/24"
ip_version = 4
network_id = opentelekomcloud_networking_network_v2.network_1.id
}
resource "opentelekomcloud_networking_port_v2" "port_1" {
name = "%[1]s_port"
network_id = opentelekomcloud_networking_network_v2.network_1.id
fixed_ip {
subnet_id = opentelekomcloud_networking_subnet_v2.subnet_1.id
}
}
resource "opentelekomcloud_networking_port_v2" "instance_port_1" {
name = "%[1]s_port"
network_id = opentelekomcloud_networking_network_v2.network_1.id
fixed_ip {
subnet_id = opentelekomcloud_networking_subnet_v2.subnet_1.id
}
}
resource "opentelekomcloud_compute_instance_v2" "instance_1" {
name = "%[1]s_instance"
security_groups = ["default"]
availability_zone = "%[2]s"
image_id = data.opentelekomcloud_images_image_v2.other_image.id
network {
port = opentelekomcloud_networking_port_v2.instance_port_1.id
}
}
resource "opentelekomcloud_networking_router_interface_v2" "int_1" {
router_id = opentelekomcloud_networking_router_v2.router_1.id
port_id = opentelekomcloud_networking_port_v2.port_1.id
}
resource "opentelekomcloud_networking_router_route_v2" "router_route_1" {
destination_cidr = "192.168.254.254/32"
next_hop = opentelekomcloud_compute_instance_v2.instance_1.network[0].fixed_ip_v4
depends_on = ["opentelekomcloud_networking_router_interface_v2.int_1"]
router_id = opentelekomcloud_networking_router_v2.router_1.id
}
resource "opentelekomcloud_networking_router_route_v2" "router_route_2" {
destination_cidr = "10.0.1.0/24"
next_hop = "192.168.199.250"
depends_on = ["opentelekomcloud_networking_router_interface_v2.int_1"]
router_id = opentelekomcloud_networking_router_v2.router_1.id
}
`, name, env.OS_AVAILABILITY_ZONE, common.DataSourceImage)
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,50 +54,46 @@ func resourceNetworkingRouterRouteV2Create(ctx context.Context, d *schema.Resour
return fmterr.Errorf("error creating OpenTelekomCloud networking client: %s", err)
}

updateOpts := routers.UpdateOpts{}
routerID := d.Get("router_id").(string)
osMutexKV.Lock(routerID)
defer osMutexKV.Unlock(routerID)

routerId := d.Get("router_id").(string)
osMutexKV.Lock(routerId)
defer osMutexKV.Unlock(routerId)

destCidr := d.Get("destination_cidr").(string)
nextHop := d.Get("next_hop").(string)

n, err := routers.Get(networkingClient, routerId).Extract()
n, err := routers.Get(networkingClient, routerID).Extract()
if err != nil {
return fmterr.Errorf("error retrieving OpenTelekomCloud Neutron Router: %s", err)
}

var routeExists bool
routes := n.Routes
dstCIDR := d.Get("destination_cidr").(string)
nextHop := d.Get("next_hop").(string)
exists := false

rts := n.Routes
for _, r := range rts {
if r.DestinationCIDR == destCidr && r.NextHop == nextHop {
routeExists = true
for _, route := range routes {
if route.DestinationCIDR == dstCIDR && route.NextHop == nextHop {
exists = true
break
}
}

if !routeExists {
if destCidr != "" && nextHop != "" {
r := routers.Route{DestinationCIDR: destCidr, NextHop: nextHop}
log.Printf(
"[INFO] Adding route %s", r)
rts = append(rts, r)
}

updateOpts.Routes = rts

log.Printf("[DEBUG] Updating Router %s with options: %+v", routerId, updateOpts)
if exists {
log.Printf("[DEBUG] OpenTelekomCloud Neutron Router %s already has route to %s via %s", routerID, dstCIDR, nextHop)
return resourceNetworkingRouterRouteV2Read(ctx, d, meta)
}

_, err = routers.Update(networkingClient, routerId, updateOpts).Extract()
if err != nil {
return fmterr.Errorf("error updating OpenTelekomCloud Neutron Router: %s", err)
}
d.SetId(fmt.Sprintf("%s-route-%s-%s", routerId, destCidr, nextHop))
} else {
log.Printf("[DEBUG] Router %s has route already", routerId)
routes = append(routes, routers.Route{
DestinationCIDR: dstCIDR,
NextHop: nextHop,
})
updateOpts := routers.UpdateOpts{
Routes: routes,
}
log.Printf("[DEBUG] OpenTelekomCloud Neutron Router %s update options: %#v", routerID, updateOpts)
_, err = routers.Update(networkingClient, routerID, updateOpts).Extract()
if err != nil {
return diag.Errorf("Error updating OpenTelekomCloud Neutron Router: %s", err)
}

d.SetId(fmt.Sprintf("%s-route-%s-%s", routerID, dstCIDR, nextHop))

return resourceNetworkingRouterRouteV2Read(ctx, d, meta)
}
Expand All @@ -122,8 +118,6 @@ func resourceNetworkingRouterRouteV2Read(_ context.Context, d *schema.ResourceDa
nextHop := d.Get("next_hop").(string)

mErr := multierror.Append(
d.Set("next_hop", ""),
d.Set("destination_cidr", ""),
d.Set("region", config.GetRegion(d)),
)

Expand Down Expand Up @@ -152,43 +146,38 @@ func resourceNetworkingRouterRouteV2Delete(_ context.Context, d *schema.Resource
return fmterr.Errorf("error creating OpenTelekomCloud networking client: %s", err)
}

routerId := d.Get("router_id").(string)
osMutexKV.Lock(routerId)
defer osMutexKV.Unlock(routerId)
routerID := d.Get("router_id").(string)
osMutexKV.Lock(routerID)
defer osMutexKV.Unlock(routerID)

n, err := routers.Get(networkingClient, routerId).Extract()
n, err := routers.Get(networkingClient, routerID).Extract()
if err != nil {
return fmterr.Errorf("error retrieving OpenTelekomCloud Neutron Router: %s", err)
}

var updateOpts routers.UpdateOpts

destCidr := d.Get("destination_cidr").(string)
dstCIDR := d.Get("destination_cidr").(string)
nextHop := d.Get("next_hop").(string)

oldRts := n.Routes
var newRts []routers.Route
oldRoutes := n.Routes
var newRoute []routers.Route

for _, r := range oldRts {
if r.DestinationCIDR != destCidr || r.NextHop != nextHop {
newRts = append(newRts, r)
for _, route := range oldRoutes {
if route.DestinationCIDR != dstCIDR || route.NextHop != nextHop {
newRoute = append(newRoute, route)
}
}

if len(oldRts) != len(newRts) {
r := routers.Route{DestinationCIDR: destCidr, NextHop: nextHop}
log.Printf(
"[INFO] Deleting route %s", r)
updateOpts.Routes = newRts

log.Printf("[DEBUG] Updating Router %s with options: %+v", routerId, updateOpts)
if len(oldRoutes) == len(newRoute) {
return diag.Errorf("Can't find route to %s via %s on OpenTelekomCloud Neutron Router %s", dstCIDR, nextHop, routerID)
}

_, err = routers.Update(networkingClient, routerId, updateOpts).Extract()
if err != nil {
return fmterr.Errorf("error updating OpenTelekomCloud Neutron Router: %s", err)
}
} else {
return fmterr.Errorf("route did not exist already")
log.Printf("[DEBUG] Deleting OpenTelekomCloud Neutron Router %s route to %s via %s", routerID, dstCIDR, nextHop)
updateOpts := routers.UpdateOpts{
Routes: newRoute,
}
_, err = routers.Update(networkingClient, routerID, updateOpts).Extract()
if err != nil {
return diag.Errorf("Error updating OpenTelekomCloud Neutron Router: %s", err)
}

return nil
Expand Down
6 changes: 6 additions & 0 deletions releasenotes/notes/network-route-v2-ref-9679902686614fd0.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
other:
- |
**[NETWORK]** Refactoring of ``resource/opentelekomcloud_networking_router_route_v2`` (`#2669 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/2669>`_)
- |
**[NETWORK]** Deprecation message in documentation for ``resource/opentelekomcloud_networking_router_route_v2`` (`#2669 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/2669>`_)

0 comments on commit 6880166

Please sign in to comment.