Skip to content

Commit

Permalink
[VPC]: opentelekomcloud_networking_router_route_v2 refactor & depre…
Browse files Browse the repository at this point in the history
…cation message (#2328)

[VPC]: `opentelekomcloud_networking_router_route_v2` refactor & deprecation message

Summary of the Pull Request
Small refactoring for opentelekomcloud_networking_router_route_v2 and deprecation message added.
PR Checklist

 Refers to: #2322
 Tests added/passed.
 Release notes added.

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

Process finished with the exit code 0

Reviewed-by: Anton Sidelnikov
Reviewed-by: Aloento
  • Loading branch information
artem-lifshits authored Oct 4, 2023
1 parent a08b6d7 commit b2ac85f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"

"github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v2/extensions/layer3/routers"

"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg"
Expand All @@ -18,9 +16,10 @@ import (

func ResourceNetworkingRouterRouteV2() *schema.Resource {
return &schema.Resource{
CreateContext: resourceNetworkingRouterRouteV2Create,
ReadContext: resourceNetworkingRouterRouteV2Read,
DeleteContext: resourceNetworkingRouterRouteV2Delete,
CreateContext: resourceNetworkingRouterRouteV2Create,
ReadContext: resourceNetworkingRouterRouteV2Read,
DeleteContext: resourceNetworkingRouterRouteV2Delete,
DeprecationMessage: "use opentelekomcloud_vpc_route_v2 resource instead",

Schema: map[string]*schema.Schema{
"region": {
Expand Down Expand Up @@ -49,33 +48,29 @@ func ResourceNetworkingRouterRouteV2() *schema.Resource {
}

func resourceNetworkingRouterRouteV2Create(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
routerId := d.Get("router_id").(string)
osMutexKV.Lock(routerId)
defer osMutexKV.Unlock(routerId)

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

config := meta.(*cfg.Config)
networkingClient, err := config.NetworkingV2Client(config.GetRegion(d))
if err != nil {
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)

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

n, err := routers.Get(networkingClient, routerId).Extract()
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
d.SetId("")
return nil
}

return fmterr.Errorf("error retrieving OpenTelekomCloud Neutron Router: %s", err)
}

var updateOpts routers.UpdateOpts
var routeExists bool = false
var routeExists bool

var rts []routers.Route = n.Routes
rts := n.Routes
for _, r := range rts {
if r.DestinationCIDR == destCidr && r.NextHop == nextHop {
routeExists = true
Expand Down Expand Up @@ -108,28 +103,23 @@ func resourceNetworkingRouterRouteV2Create(ctx context.Context, d *schema.Resour
}

func resourceNetworkingRouterRouteV2Read(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
routerId := d.Get("router_id").(string)

config := meta.(*cfg.Config)
networkingClient, err := config.NetworkingV2Client(config.GetRegion(d))
if err != nil {
return fmterr.Errorf("error creating OpenTelekomCloud networking client: %s", err)
}

routerId := d.Get("router_id").(string)

n, err := routers.Get(networkingClient, routerId).Extract()
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
d.SetId("")
return nil
}

return fmterr.Errorf("error retrieving OpenTelekomCloud Neutron Router: %s", err)
}

log.Printf("[DEBUG] Retrieved Router %s: %+v", routerId, n)

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

mErr := multierror.Append(
d.Set("next_hop", ""),
Expand All @@ -155,32 +145,28 @@ func resourceNetworkingRouterRouteV2Read(_ context.Context, d *schema.ResourceDa
}

func resourceNetworkingRouterRouteV2Delete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
routerId := d.Get("router_id").(string)
osMutexKV.Lock(routerId)
defer osMutexKV.Unlock(routerId)

config := meta.(*cfg.Config)

networkingClient, err := config.NetworkingV2Client(config.GetRegion(d))
if err != nil {
return fmterr.Errorf("error creating OpenTelekomCloud networking client: %s", err)
}

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

n, err := routers.Get(networkingClient, routerId).Extract()
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
return nil
}

return fmterr.Errorf("error retrieving OpenTelekomCloud Neutron Router: %s", err)
}

var updateOpts routers.UpdateOpts

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

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

for _, r := range oldRts {
Expand Down
9 changes: 9 additions & 0 deletions releasenotes/notes/router_route-803566ca6c2e8565.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
deprecations:
- |
**[VPC]** Added deprecation warning for ``resource/opentelekomcloud_networking_router_route_v2``
(`#2328 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/2328>`_)
other:
- |
**[VPC]** Refactoring of ``resource/opentelekomcloud_networking_router_route_v2``
(`#2328 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/2328>`_)

0 comments on commit b2ac85f

Please sign in to comment.