Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6.2.4 NSX name fix #209

Merged
merged 6 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions acc-testcases/resources/load_balancer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
vars:
name: "tf_loadbalancer_%rand_int"
acc:
- config: |
name = "$(name)"
description = "Loadbalancer created using tf"
enabled = true
group_access {
all = true
}
config {
admin_state = true
size = "SMALL"
log_level = "INFO"
tier1_gateways = "/infra/tier-1s/d3561ba7-01c9-4fa7-a7c5-bac401fd8f75"
}
validations:
json.loadBalancer.config.tier1: "/infra/tier-1s/d3561ba7-01c9-4fa7-a7c5-bac401fd8f75"
8 changes: 7 additions & 1 deletion internal/acceptance_test/data_source_edge_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
package acceptancetest

import (
"context"
"testing"

api_client "github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/client"
"github.com/HewlettPackard/hpegl-vmaas-terraform-resources/internal/cmp"
"github.com/HewlettPackard/hpegl-vmaas-terraform-resources/pkg/atf"
)

Expand All @@ -30,8 +32,12 @@ func TestAccDataSourceNetworkEdgeCluster(t *testing.T) {
if len(ServerResp.NetworkServices) == 0 {
return nil, err
}
nsxType, err := cmp.GetNsxTypeFromCMP(context.Background(), iClient.Client)
if err != nil {
return nil, err
}
for i, n := range ServerResp.NetworkServices {
if n.TypeName == "NSX-T" {
if n.TypeName == nsxType {
serverID = ServerResp.NetworkServices[i].ID

break
Expand Down
7 changes: 4 additions & 3 deletions internal/cmp/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import "time"

const (
vmware = "vmware"
nsxtSegment = "NSX-T Segment"
nsx = "NSX"
nsxt = "NSX-T"
nsxSegment = "Segment"
errExactMatch = "error, could not find the %s with the specified name. Please verify the name and try again"
successErr = "got success = 'false while %s"
// query params keys
Expand All @@ -20,8 +21,8 @@ const (
// retry related constants
maxTimeout = time.Hour * 2
// router consts
tier0GatewayType = "NSX-T Tier-0 Gateway"
tier1GatewayType = "NSX-T Tier-1 Gateway"
tier0GatewayType = "Tier-0 Gateway"
tier1GatewayType = "Tier-1 Gateway"
routerFirewallExternalPolicy = "GatewayPolicy"
syncedTypeValue = "Synced"

Expand Down
7 changes: 5 additions & 2 deletions internal/cmp/data_source_lb_pool_member_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ func (n *poolMemberGroupds) Read(ctx context.Context, d *utils.Data, meta interf
if err := d.Error(); err != nil {
return err
}

nsxType, err := GetNsxTypeFromCMP(ctx, n.rClient.Client)
if err != nil {
return err
}
mahesh-hpe marked this conversation as resolved.
Show resolved Hide resolved
setMeta(meta, n.rClient.Client)
// Get network server ID for nsx-t
serverResp, err := n.rClient.GetNetworkServices(ctx, nil)
Expand All @@ -44,7 +47,7 @@ func (n *poolMemberGroupds) Read(ctx context.Context, d *utils.Data, meta interf

var serverID int
for i, n := range serverResp.NetworkServices {
if n.TypeName == nsxt {
if n.TypeName == nsxType {
serverID = serverResp.NetworkServices[i].ID

break
Expand Down
6 changes: 5 additions & 1 deletion internal/cmp/dhcp_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ func (dhcp *dhcpServer) Delete(ctx context.Context, d *utils.Data, meta interfac
func (dhcp *dhcpServer) dhcpServerAlignRequest(ctx context.Context, meta interface{},
createReq *models.CreateNetworkDhcpServerRequest) error {
// Get network service ID
nsxType, err := GetNsxTypeFromCMP(ctx, dhcp.rClient.Client)
if err != nil {
return err
mahesh-hpe marked this conversation as resolved.
Show resolved Hide resolved
}
setMeta(meta, dhcp.rClient.Client)
nsRetry := utils.CustomRetry{}
nsRetry.RetryParallel(ctx, meta, func(ctx context.Context) (interface{}, error) {
Expand All @@ -136,7 +140,7 @@ func (dhcp *dhcpServer) dhcpServerAlignRequest(ctx context.Context, meta interfa
networkService := nsResp.(models.GetNetworkServicesResp)

for i, n := range networkService.NetworkServices {
if n.TypeName == nsxt {
if n.TypeName == nsxType {
createReq.NetworkDhcpServer.ID = networkService.NetworkServices[i].ID

break
Expand Down
7 changes: 5 additions & 2 deletions internal/cmp/dhcp_server_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ func (n *dhcpServerds) Read(ctx context.Context, d *utils.Data, meta interface{}
if err := d.Error(); err != nil {
return err
}

nsxType, err := GetNsxTypeFromCMP(ctx, n.rClient.Client)
if err != nil {
return err
}
setMeta(meta, n.rClient.Client)
// Get network server ID for nsx-t
serverResp, err := n.rClient.GetNetworkServices(ctx, nil)
Expand All @@ -44,7 +47,7 @@ func (n *dhcpServerds) Read(ctx context.Context, d *utils.Data, meta interface{}

var serverID int
for i, n := range serverResp.NetworkServices {
if n.TypeName == nsxt {
if n.TypeName == nsxType {
serverID = serverResp.NetworkServices[i].ID

break
Expand Down
6 changes: 5 additions & 1 deletion internal/cmp/edge_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func newEdgeCluster(tClient *client.RouterAPIService) *edgeCluster {
}

func (r *edgeCluster) Read(ctx context.Context, d *utils.Data, meta interface{}) error {
nsxType, err := GetNsxTypeFromCMP(ctx, r.tClient.Client)
if err != nil {
return err
}
setMeta(meta, r.tClient.Client)
log.Printf("[INFO] Get Edge Cluster")
var tfEdgeCluster models.NetworkEdgeClusters
Expand All @@ -39,7 +43,7 @@ func (r *edgeCluster) Read(ctx context.Context, d *utils.Data, meta interface{})

var serverID int
for i, n := range serverResp.NetworkServices {
if n.TypeName == nsxt {
if n.TypeName == nsxType {
serverID = serverResp.NetworkServices[i].ID

break
Expand Down
51 changes: 51 additions & 0 deletions internal/cmp/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
package cmp

import (
"context"
"log"
"strconv"
"strings"

"github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/client"
"github.com/HewlettPackard/hpegl-vmaas-terraform-resources/pkg/auth"
Expand All @@ -15,3 +18,51 @@ func setMeta(meta interface{}, apiClient client.APIClientHandler) {
log.Printf("[ERROR] error while setting meta information for cmp-sdk, error: %v", err)
}
}

func ParseVersion(version string) (int, error) {
if version == "" {
return 0, nil
}

versionSplit := strings.Split(version, ".")

mul := 10000
sum := 0
for _, v := range versionSplit {
vInt, err := strconv.Atoi(v)
if err != nil {
return 0, err
}
sum += mul * vInt
mul /= 100
}

return sum, nil
}

func GetCmpVersion(ctx context.Context, apiClient client.APIClientHandler) (int, error) {
c := client.CmpStatus{
Client: apiClient,
}

cmpVersion, err := c.GetCmpVersion(ctx)
if err != nil {
return 0, err
}

return ParseVersion(cmpVersion.Appliance.BuildVersion)
}

func GetNsxTypeFromCMP(ctx context.Context, apiClient client.APIClientHandler) (string, error) {
cmpVersion, err := GetCmpVersion(ctx, apiClient)
if err != nil {
return "", err
}
if v, _ := ParseVersion("6.2.4"); v <= cmpVersion {
// from 6.2.4 onwards the display name of NSX-T has been change to NSX

return nsx, nil
}

return nsxt, nil
}
12 changes: 8 additions & 4 deletions internal/cmp/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ func (lb *loadBalancer) Update(ctx context.Context, d *utils.Data, meta interfac

func (lb *loadBalancer) loadBalancerAlignRequest(ctx context.Context, meta interface{},
createReq *models.CreateLoadBalancerRequest) error {
nsxType, err := GetNsxTypeFromCMP(ctx, lb.rClient.Client)
if err != nil {
return err
}
mahesh-hpe marked this conversation as resolved.
Show resolved Hide resolved
allTypes, _ := lb.lbClient.GetLoadBalancerTypes(ctx, map[string]string{
nameKey: nsxt,
nameKey: nsxType,
})

// Get network service ID
Expand All @@ -80,15 +84,15 @@ func (lb *loadBalancer) loadBalancerAlignRequest(ctx context.Context, meta inter
networkService := nsResp.(models.GetNetworkServicesResp)

for i, n := range networkService.NetworkServices {
if n.TypeName == nsxt {
if n.TypeName == nsxType {
createReq.NetworkLoadBalancer.NetworkServerID = networkService.NetworkServices[i].ID

break
}
}
for i, n := range allTypes.LoadBalancerTypes {
if n.Name == nsxt {
createReq.NetworkLoadBalancer.Type = allTypes.LoadBalancerTypes[i].Name
if n.Name == nsxType {
createReq.NetworkLoadBalancer.Type = allTypes.LoadBalancerTypes[i].Code

break
}
Expand Down
8 changes: 6 additions & 2 deletions internal/cmp/resource_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func (r *resNetwork) Read(ctx context.Context, d *utils.Data, meta interface{})
}

func (r *resNetwork) Create(ctx context.Context, d *utils.Data, meta interface{}) error {
nsxType, err := GetNsxTypeFromCMP(ctx, r.rClient.Client)
if err != nil {
return err
}
setMeta(meta, r.rClient.Client)
var createReq models.CreateNetwork
if err := tftags.Get(d, &createReq); err != nil {
Expand All @@ -56,7 +60,7 @@ func (r *resNetwork) Create(ctx context.Context, d *utils.Data, meta interface{}
typeRetry := utils.CustomRetry{}
typeRetry.RetryParallel(ctx, meta, func(ctx context.Context) (interface{}, error) {
return r.nClient.GetNetworkType(ctx, map[string]string{
nameKey: nsxtSegment,
nameKey: fmt.Sprintf("%s %s", nsxType, nsxSegment),
})
})
// Get network server ID for nsx-t
Expand Down Expand Up @@ -86,7 +90,7 @@ func (r *resNetwork) Create(ctx context.Context, d *utils.Data, meta interface{}
return fmt.Errorf(errExactMatch, "network server")
}
for i, n := range networkService.NetworkServices {
if n.TypeName == nsxt {
if n.TypeName == nsxType {
createReq.NetworkServer.ID = networkService.NetworkServices[i].ID

break
Expand Down
10 changes: 7 additions & 3 deletions internal/cmp/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func (r *router) Delete(ctx context.Context, d *utils.Data, meta interface{}) er
}

func (r *router) routerAlignRouterRequest(ctx context.Context, meta interface{}, routerReq *models.CreateRouterRequest) error {
nsxType, err := GetNsxTypeFromCMP(ctx, r.rClient.Client)
if err != nil {
return err
}
queryParam := make(map[string]string)
// Check whether teir0 or tier1 and assign properties to proper child, so json can marshal properly
tier0Config := routerReq.NetworkRouter.Config.CreateRouterTier0Config
Expand All @@ -134,14 +138,14 @@ func (r *router) routerAlignRouterRequest(ctx context.Context, meta interface{},
routerReq.NetworkRouter.Config.FailOver = routerReq.NetworkRouter.TfTier0Config.TfFailOver
routerReq.NetworkRouter.Config.EdgeCluster = routerReq.NetworkRouter.TfTier0Config.TfEdgeCluster
routerReq.NetworkRouter.EnableBGP = routerReq.NetworkRouter.TfTier0Config.TfBGP.TfEnableBgp
queryParam[nameKey] = tier0GatewayType
queryParam[nameKey] = fmt.Sprintf("%s %s", nsxType, tier0GatewayType)
} else {
routerReq.NetworkRouter.Config.CreateRouterTier0Config.RouteRedistributionTier1.RouteAdvertisement =
routerReq.NetworkRouter.TfTier1Config.TfRouteAdvertisement
routerReq.NetworkRouter.Config.EdgeCluster = routerReq.NetworkRouter.TfTier1Config.TfEdgeCluster
routerReq.NetworkRouter.Config.FailOver = routerReq.NetworkRouter.TfTier1Config.TfFailOver
routerReq.NetworkRouter.Config.Tier0Gateways = routerReq.NetworkRouter.TfTier1Config.TfTier0Gateways
queryParam[nameKey] = tier1GatewayType
queryParam[nameKey] = fmt.Sprintf("%s %s", nsxType, tier1GatewayType)
}
// Get Router type
rtRetry := utils.CustomRetry{}
Expand Down Expand Up @@ -174,7 +178,7 @@ func (r *router) routerAlignRouterRequest(ctx context.Context, meta interface{},
return fmt.Errorf(errExactMatch, "network-service")
}
for i, n := range networkService.NetworkServices {
if n.TypeName == nsxt {
if n.TypeName == nsxType {
routerReq.NetworkRouter.NetworkServer.ID = networkService.NetworkServices[i].ID
routerReq.NetworkRouter.NetworkServerID = networkService.NetworkServices[i].ID

Expand Down
6 changes: 5 additions & 1 deletion internal/cmp/transport_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func newTransportZone(tClient *client.RouterAPIService) *transportZone {
}

func (r *transportZone) Read(ctx context.Context, d *utils.Data, meta interface{}) error {
nsxType, err := GetNsxTypeFromCMP(ctx, r.tClient.Client)
if err != nil {
return err
}
setMeta(meta, r.tClient.Client)
var tfScope models.NetworkScope
if err := tftags.Get(d, &tfScope); err != nil {
Expand All @@ -37,7 +41,7 @@ func (r *transportZone) Read(ctx context.Context, d *utils.Data, meta interface{

var serverID int
for i, n := range serverResp.NetworkServices {
if n.TypeName == nsxt {
if n.TypeName == nsxType {
serverID = serverResp.NetworkServices[i].ID

break
Expand Down
Loading