diff --git a/go.mod b/go.mod index 56019061..5b782856 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index c5490703..3c76b576 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/provider/common/edgegw/edgegw.go b/internal/provider/common/edgegw/edgegw.go index 421b9e85..4deb7e06 100644 --- a/internal/provider/common/edgegw/edgegw.go +++ b/internal/provider/common/edgegw/edgegw.go @@ -31,7 +31,7 @@ type BaseEdgeGW struct { type EdgeGateway struct { Client *client.CloudAvenue - // *sdkv1.EdgeGw + *sdkv1.EdgeClient *govcd.NsxtEdgeGateway } diff --git a/internal/provider/common/org/edgegw.go b/internal/provider/common/org/edgegw.go index f6c1e4d2..a35ee28c 100644 --- a/internal/provider/common/org/edgegw.go +++ b/internal/provider/common/org/edgegw.go @@ -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" ) @@ -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 } diff --git a/internal/provider/edgegw/edgegateway_datasource.go b/internal/provider/edgegw/edgegateway_datasource.go index 78c7c657..60e5b8ce 100644 --- a/internal/provider/edgegw/edgegateway_datasource.go +++ b/internal/provider/edgegw/edgegateway_datasource.go @@ -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 diff --git a/internal/provider/edgegw/edgegateway_resource.go b/internal/provider/edgegw/edgegateway_resource.go index f88619ed..7a672145 100644 --- a/internal/provider/edgegw/edgegateway_resource.go +++ b/internal/provider/edgegw/edgegateway_resource.go @@ -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" @@ -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 { @@ -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 } @@ -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 @@ -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", @@ -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) @@ -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 } diff --git a/internal/provider/edgegw/firewall_resource.go b/internal/provider/edgegw/firewall_resource.go index 68c93049..dbf00397 100644 --- a/internal/provider/edgegw/firewall_resource.go +++ b/internal/provider/edgegw/firewall_resource.go @@ -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()) diff --git a/internal/provider/network/routed_resource.go b/internal/provider/network/routed_resource.go index 28dc9058..36f403b1 100644 --- a/internal/provider/network/routed_resource.go +++ b/internal/provider/network/routed_resource.go @@ -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) @@ -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()) @@ -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())