Skip to content

Commit

Permalink
chore: move edgeGW to SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
azrod committed Nov 26, 2024
1 parent 322f76f commit 7b2dc9f
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 56 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.35.0
github.com/iancoleman/strcase v0.3.0
github.com/orange-cloudavenue/cloudavenue-sdk-go v0.12.7
github.com/orange-cloudavenue/cloudavenue-sdk-go v0.13.0
github.com/orange-cloudavenue/common-go/utils v0.0.0-20240119163616-66b473d92339
github.com/rs/zerolog v1.33.0
github.com/thanhpk/randstr v1.0.6
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
github.com/orange-cloudavenue/cloudavenue-sdk-go v0.12.7 h1:dFjSUbrE9mXjfN96zP6NLNHi7wCrcwx4fZkm/ZbaC+8=
github.com/orange-cloudavenue/cloudavenue-sdk-go v0.12.7/go.mod h1:CTQO1VIVFvImvmKoR4ntL3HxBi5REm6ssLzUj3kv1sc=
github.com/orange-cloudavenue/cloudavenue-sdk-go v0.13.0 h1:qqmOfyTj3LVZ2f059EhfevW1mdxo4mgFh8KLt6JzuAY=
github.com/orange-cloudavenue/cloudavenue-sdk-go v0.13.0/go.mod h1:CTQO1VIVFvImvmKoR4ntL3HxBi5REm6ssLzUj3kv1sc=
github.com/orange-cloudavenue/common-go/utils v0.0.0-20240119163616-66b473d92339 h1:DEKcWLGbEhu/I6kn9NAXhVCFrbPhR+Ef7oLmpLVnnPM=
github.com/orange-cloudavenue/common-go/utils v0.0.0-20240119163616-66b473d92339/go.mod h1:11JAFfGWVmhoT4AAORKsIC5M6nI+uDGSEOScMzavgPA=
github.com/peterhellberg/link v1.2.0 h1:UA5pg3Gp/E0F2WdX7GERiNrPQrM1K6CVJUUWfHa4t6c=
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/common/edgegw/edgegw.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type BaseEdgeGW struct {

type EdgeGateway struct {
Client *client.CloudAvenue
// *sdkv1.EdgeGw
*sdkv1.EdgeClient
*govcd.NsxtEdgeGateway
}

Expand Down
28 changes: 15 additions & 13 deletions internal/provider/common/org/edgegw.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org

import (
"github.com/vmware/go-vcloud-director/v2/govcd"

"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/common/edgegw"
)

Expand All @@ -14,22 +12,26 @@ func (o *Org) GetEdgeGateway(egw edgegw.BaseEdgeGW) (edgegw.EdgeGateway, error)
return edgegw.EdgeGateway{}, edgegw.ErrEdgeGatewayIDOrNameIsEmpty
}

var (
govcdValues *govcd.NsxtEdgeGateway
err error
)

if egw.GetID() != "" {
govcdValues, err = o.GetNsxtEdgeGatewayById(egw.GetID())
} else {
govcdValues, err = o.GetNsxtEdgeGatewayByName(egw.GetName())
edge, err := o.c.CAVSDK.V1.EdgeGateway.Get(egw.GetIDOrName())
if err != nil {
return edgegw.EdgeGateway{}, err
}

vmwareEdgeGateway, err := edge.GetVmwareEdgeGateway()
if err != nil {
return edgegw.EdgeGateway{}, err
}

return edgegw.EdgeGateway{
Client: o.c,
NsxtEdgeGateway: govcdValues,
// Client is the CloudAvenue client.
Client: o.c,

// EdgeClient is the EdgeGateway client.
EdgeClient: edge,

// NsxtEdgeGateway is the NSX-T edge gateway.
//
// Deprecated: Use EdgeClient instead.
NsxtEdgeGateway: vmwareEdgeGateway,
}, err
}
2 changes: 1 addition & 1 deletion internal/provider/edgegw/edgegateway_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (d *edgeGatewayDataSource) Read(ctx context.Context, req datasource.ReadReq
data := config.Copy()

// Read data from the API
edgegw, err := d.client.CAVSDK.V1.EdgeGateway.GetByName(config.Name.Get())
edgegw, err := d.client.CAVSDK.V1.EdgeGateway.Get(config.Name.Get())
if err != nil {
resp.Diagnostics.AddError("Error retrieving edge gateway", err.Error())
return
Expand Down
45 changes: 16 additions & 29 deletions internal/provider/edgegw/edgegateway_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

"golang.org/x/exp/slices"

"github.com/vmware/go-vcloud-director/v2/govcd"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"

Expand Down Expand Up @@ -274,7 +276,7 @@ func (r *edgeGatewayResource) Create(ctx context.Context, req resource.CreateReq
return
}

var edgegwNew v1.EdgeGw
var edgegwNew v1.EdgeGatewayType

// Find the new edge gateway in the list of all edge gateways and set the ID. New edge gateway is in the list refreshed but not in the old list.
for _, edgegw := range *edgegwsRefreshed {
Expand All @@ -292,7 +294,7 @@ func (r *edgeGatewayResource) Create(ctx context.Context, req resource.CreateReq
}
}

if edgegwNew == (v1.EdgeGw{}) {
if edgegwNew == (v1.EdgeGatewayType{}) {
resp.Diagnostics.AddError("Error retrieving new edge gateway", "New edge gateway not found")
return
}
Expand Down Expand Up @@ -392,7 +394,7 @@ func (r *edgeGatewayResource) Update(ctx context.Context, req resource.UpdateReq
ctx, cancel = context.WithTimeout(ctx, updateTimeout)
defer cancel()

edgegw, err := r.client.CAVSDK.V1.EdgeGateway.GetByID(common.ExtractUUID(plan.ID.Get()))
edgegw, err := r.client.CAVSDK.V1.EdgeGateway.Get(common.ExtractUUID(plan.ID.Get()))
if err != nil {
resp.Diagnostics.AddError("Error retrieving edge gateway", err.Error())
return
Expand Down Expand Up @@ -435,7 +437,7 @@ func (r *edgeGatewayResource) Delete(ctx context.Context, req resource.DeleteReq
cloudavenue.Lock(ctx)
defer cloudavenue.Unlock(ctx)

deleteTimeout, errTO := state.Timeouts.Update(ctx, 8*time.Minute)
deleteTimeout, errTO := state.Timeouts.Delete(ctx, 8*time.Minute)
if errTO != nil {
resp.Diagnostics.AddError(
"Error creating timeout",
Expand All @@ -444,7 +446,7 @@ func (r *edgeGatewayResource) Delete(ctx context.Context, req resource.DeleteReq
return
}

edgegw, err := r.client.CAVSDK.V1.EdgeGateway.GetByID(common.ExtractUUID(state.ID.Get()))
edgegw, err := r.client.CAVSDK.V1.EdgeGateway.Get(common.ExtractUUID(state.ID.Get()))
if err != nil {
if commoncloudavenue.IsNotFound(err) {
resp.State.RemoveResource(ctx)
Expand Down Expand Up @@ -477,32 +479,17 @@ func (r *edgeGatewayResource) ImportState(ctx context.Context, req resource.Impo
func (r *edgeGatewayResource) read(_ context.Context, planOrState *edgeGatewayResourceModel) (stateRefreshed *edgeGatewayResourceModel, found bool, diags diag.Diagnostics) {
stateRefreshed = planOrState.Copy()

var (
edgegw *v1.EdgeGw
err error
)
nameOrID := planOrState.ID.Get()
if nameOrID == "" {
nameOrID = planOrState.Name.Get()
}

switch {
case planOrState.ID.IsKnown():
edgegw, err = r.client.CAVSDK.V1.EdgeGateway.GetByID(common.ExtractUUID(planOrState.ID.Get()))
if err != nil {
if commoncloudavenue.IsNotFound(err) {
return nil, false, nil
}
diags.AddError("Error retrieving edge gateway", err.Error())
return nil, true, diags
}
case planOrState.Name.IsKnown():
edgegw, err = r.client.CAVSDK.V1.EdgeGateway.GetByName(planOrState.Name.Get())
if err != nil {
if commoncloudavenue.IsNotFound(err) {
return nil, false, nil
}
diags.AddError("Error retrieving edge gateway", err.Error())
return nil, true, diags
edgegw, err := r.client.CAVSDK.V1.EdgeGateway.Get(nameOrID)
if err != nil {
if commoncloudavenue.IsNotFound(err) || govcd.IsNotFound(err) {
return nil, false, nil
}
default:
diags.AddError("Error retrieving edge gateway", "Either name or ID must be set")
diags.AddError("Error retrieving edge gateway", err.Error())
return nil, true, diags
}

Expand Down
10 changes: 7 additions & 3 deletions internal/provider/edgegw/firewall_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,13 @@ func (r *firewallResource) Delete(ctx context.Context, req resource.DeleteReques
return
}

mutex.GlobalMutex.KvLock(ctx, vdcOrVDCGroup.GetID())
defer mutex.GlobalMutex.KvUnlock(ctx, vdcOrVDCGroup.GetID())

if vdcOrVDCGroup.IsVDCGroup() {
mutex.GlobalMutex.KvLock(ctx, vdcOrVDCGroup.GetID())
defer mutex.GlobalMutex.KvUnlock(ctx, vdcOrVDCGroup.GetID())
} else {
mutex.GlobalMutex.KvLock(ctx, r.edgegw.GetID())
defer mutex.GlobalMutex.KvUnlock(ctx, r.edgegw.GetID())
}
fwRules, err := r.edgegw.GetNsxtFirewall()
if err != nil {
resp.Diagnostics.AddError("Error retrieving Edge Gateway Firewall", err.Error())
Expand Down
27 changes: 21 additions & 6 deletions internal/provider/network/routed_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,13 @@ func (r *networkRoutedResource) Create(ctx context.Context, req resource.CreateR
return
}

mutex.GlobalMutex.KvLock(ctx, vdcOrVDCGroup.GetID())
defer mutex.GlobalMutex.KvUnlock(ctx, vdcOrVDCGroup.GetID())
if vdcOrVDCGroup.IsVDCGroup() {
mutex.GlobalMutex.KvLock(ctx, vdcOrVDCGroup.GetID())
defer mutex.GlobalMutex.KvUnlock(ctx, vdcOrVDCGroup.GetID())
} else {
mutex.GlobalMutex.KvLock(ctx, r.edgegw.GetID())
defer mutex.GlobalMutex.KvUnlock(ctx, r.edgegw.GetID())
}

// Set Network
orgVDCNetworkConfig, diag := r.setNetworkAPIObject(ctx, plan, vdcOrVDCGroup)
Expand Down Expand Up @@ -209,8 +214,13 @@ func (r *networkRoutedResource) Update(ctx context.Context, req resource.UpdateR
return
}

mutex.GlobalMutex.KvLock(ctx, vdcOrVDCGroup.GetID())
defer mutex.GlobalMutex.KvUnlock(ctx, vdcOrVDCGroup.GetID())
if vdcOrVDCGroup.IsVDCGroup() {
mutex.GlobalMutex.KvLock(ctx, vdcOrVDCGroup.GetID())
defer mutex.GlobalMutex.KvUnlock(ctx, vdcOrVDCGroup.GetID())
} else {
mutex.GlobalMutex.KvLock(ctx, r.edgegw.GetID())
defer mutex.GlobalMutex.KvUnlock(ctx, r.edgegw.GetID())
}

// Get current network
orgNetwork, err := r.org.GetOpenApiOrgVdcNetworkById(plan.ID.Get())
Expand Down Expand Up @@ -275,8 +285,13 @@ func (r *networkRoutedResource) Delete(ctx context.Context, req resource.DeleteR
return
}

mutex.GlobalMutex.KvLock(ctx, vdcOrVDCGroup.GetID())
defer mutex.GlobalMutex.KvUnlock(ctx, vdcOrVDCGroup.GetID())
if vdcOrVDCGroup.IsVDCGroup() {
mutex.GlobalMutex.KvLock(ctx, vdcOrVDCGroup.GetID())
defer mutex.GlobalMutex.KvUnlock(ctx, vdcOrVDCGroup.GetID())
} else {
mutex.GlobalMutex.KvLock(ctx, r.edgegw.GetID())
defer mutex.GlobalMutex.KvUnlock(ctx, r.edgegw.GetID())
}

// Get current network
orgNetwork, err := r.org.GetOpenApiOrgVdcNetworkById(state.ID.Get())
Expand Down

0 comments on commit 7b2dc9f

Please sign in to comment.