From 501c7c4703870dbff8926e9622ef6f615358376b Mon Sep 17 00:00:00 2001 From: Mahesh N Date: Fri, 24 Nov 2023 14:07:10 +0530 Subject: [PATCH 1/6] initial commit --- .../data_source_edge_cluster_test.go | 12 ++++- internal/cmp/constants.go | 4 ++ .../cmp/data_source_lb_pool_member_group.go | 12 ++++- internal/cmp/dhcp_server.go | 11 ++++- internal/cmp/dhcp_server_datasource.go | 12 ++++- internal/cmp/edge_cluster.go | 11 ++++- internal/cmp/loadbalancer.go | 18 ++++++-- internal/cmp/resource_network.go | 15 ++++++- internal/cmp/router.go | 19 ++++++-- internal/cmp/transport_zone.go | 11 ++++- internal/utils/cmp_version.go | 44 +++++++++++++++++++ 11 files changed, 152 insertions(+), 17 deletions(-) create mode 100644 internal/utils/cmp_version.go diff --git a/internal/acceptance_test/data_source_edge_cluster_test.go b/internal/acceptance_test/data_source_edge_cluster_test.go index d861cebf..84981963 100644 --- a/internal/acceptance_test/data_source_edge_cluster_test.go +++ b/internal/acceptance_test/data_source_edge_cluster_test.go @@ -6,6 +6,7 @@ import ( "testing" api_client "github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/client" + "github.com/HewlettPackard/hpegl-vmaas-terraform-resources/internal/utils" "github.com/HewlettPackard/hpegl-vmaas-terraform-resources/pkg/atf" ) @@ -30,8 +31,17 @@ func TestAccDataSourceNetworkEdgeCluster(t *testing.T) { if len(ServerResp.NetworkServices) == 0 { return nil, err } + cmpVersion, err := utils.GetCmpVersion(iClient.Client) + if err != nil { + return nil, err + } + nsxVar := "NSX-T" + if v, _ := utils.ParseVersion("6.2.4"); v <= cmpVersion { + // from 6.2.4 onwards the display name of NSX-T has been change to NSX + nsxVar = "NSX" + } for i, n := range ServerResp.NetworkServices { - if n.TypeName == "NSX-T" { + if n.TypeName == nsxVar { serverID = ServerResp.NetworkServices[i].ID break diff --git a/internal/cmp/constants.go b/internal/cmp/constants.go index ab708104..63881adf 100644 --- a/internal/cmp/constants.go +++ b/internal/cmp/constants.go @@ -6,7 +6,9 @@ import "time" const ( vmware = "vmware" + nsxSegment = "NSX Segment" nsxtSegment = "NSX-T Segment" + nsx = "NSX" nsxt = "NSX-T" errExactMatch = "error, could not find the %s with the specified name. Please verify the name and try again" successErr = "got success = 'false while %s" @@ -20,7 +22,9 @@ const ( // retry related constants maxTimeout = time.Hour * 2 // router consts + nsxTier0GatewayType = "NSX Tier-0 Gateway" tier0GatewayType = "NSX-T Tier-0 Gateway" + nsxTier1GatewayType = "NSX Tier-1 Gateway" tier1GatewayType = "NSX-T Tier-1 Gateway" routerFirewallExternalPolicy = "GatewayPolicy" syncedTypeValue = "Synced" diff --git a/internal/cmp/data_source_lb_pool_member_group.go b/internal/cmp/data_source_lb_pool_member_group.go index d918da00..f0458c54 100644 --- a/internal/cmp/data_source_lb_pool_member_group.go +++ b/internal/cmp/data_source_lb_pool_member_group.go @@ -34,7 +34,15 @@ func (n *poolMemberGroupds) Read(ctx context.Context, d *utils.Data, meta interf if err := d.Error(); err != nil { return err } - + cmpVersion, err := utils.GetCmpVersion(n.rClient.Client) + if err != nil { + return err + } + nsxVar := nsxt + if v, _ := utils.ParseVersion("6.2.4"); v <= cmpVersion { + // from 6.2.4 onwards the display name of NSX-T has been change to NSX + nsxVar = nsx + } setMeta(meta, n.rClient.Client) // Get network server ID for nsx-t serverResp, err := n.rClient.GetNetworkServices(ctx, nil) @@ -44,7 +52,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 == nsxVar { serverID = serverResp.NetworkServices[i].ID break diff --git a/internal/cmp/dhcp_server.go b/internal/cmp/dhcp_server.go index 59fe3bb4..c4f0d0ab 100644 --- a/internal/cmp/dhcp_server.go +++ b/internal/cmp/dhcp_server.go @@ -122,6 +122,15 @@ 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 + cmpVersion, err := utils.GetCmpVersion(dhcp.rClient.Client) + if err != nil { + return err + } + nsxVar := nsxt + if v, _ := utils.ParseVersion("6.2.4"); v <= cmpVersion { + // from 6.2.4 onwards the display name of NSX-T has been change to NSX + nsxVar = nsx + } setMeta(meta, dhcp.rClient.Client) nsRetry := utils.CustomRetry{} nsRetry.RetryParallel(ctx, meta, func(ctx context.Context) (interface{}, error) { @@ -136,7 +145,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 == nsxVar { createReq.NetworkDhcpServer.ID = networkService.NetworkServices[i].ID break diff --git a/internal/cmp/dhcp_server_datasource.go b/internal/cmp/dhcp_server_datasource.go index 981a3742..8ad299b5 100644 --- a/internal/cmp/dhcp_server_datasource.go +++ b/internal/cmp/dhcp_server_datasource.go @@ -34,7 +34,15 @@ func (n *dhcpServerds) Read(ctx context.Context, d *utils.Data, meta interface{} if err := d.Error(); err != nil { return err } - + cmpVersion, err := utils.GetCmpVersion(n.rClient.Client) + if err != nil { + return err + } + nsxVar := nsxt + if v, _ := utils.ParseVersion("6.2.4"); v <= cmpVersion { + // from 6.2.4 onwards the display name of NSX-T has been change to NSX + nsxVar = nsx + } setMeta(meta, n.rClient.Client) // Get network server ID for nsx-t serverResp, err := n.rClient.GetNetworkServices(ctx, nil) @@ -44,7 +52,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 == nsxVar { serverID = serverResp.NetworkServices[i].ID break diff --git a/internal/cmp/edge_cluster.go b/internal/cmp/edge_cluster.go index ff64d26a..9341b192 100644 --- a/internal/cmp/edge_cluster.go +++ b/internal/cmp/edge_cluster.go @@ -24,6 +24,15 @@ func newEdgeCluster(tClient *client.RouterAPIService) *edgeCluster { } func (r *edgeCluster) Read(ctx context.Context, d *utils.Data, meta interface{}) error { + cmpVersion, err := utils.GetCmpVersion(r.tClient.Client) + if err != nil { + return err + } + nsxVar := nsxt + if v, _ := utils.ParseVersion("6.2.4"); v <= cmpVersion { + // from 6.2.4 onwards the display name of NSX-T has been change to NSX + nsxVar = nsx + } setMeta(meta, r.tClient.Client) log.Printf("[INFO] Get Edge Cluster") var tfEdgeCluster models.NetworkEdgeClusters @@ -39,7 +48,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 == nsxVar { serverID = serverResp.NetworkServices[i].ID break diff --git a/internal/cmp/loadbalancer.go b/internal/cmp/loadbalancer.go index 982e9d2d..9f1f1942 100644 --- a/internal/cmp/loadbalancer.go +++ b/internal/cmp/loadbalancer.go @@ -61,8 +61,18 @@ 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 { + cmpVersion, err := utils.GetCmpVersion(lb.rClient.Client) + if err != nil { + return err + } + nsxVar := nsxt + if v, _ := utils.ParseVersion("6.2.4"); v <= cmpVersion { + // from 6.2.4 onwards the display name of NSX-T has been change to NSX + nsxVar = nsx + + } allTypes, _ := lb.lbClient.GetLoadBalancerTypes(ctx, map[string]string{ - nameKey: nsxt, + nameKey: nsxVar, }) // Get network service ID @@ -80,15 +90,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 == nsxVar { 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 == nsxVar { + createReq.NetworkLoadBalancer.Type = allTypes.LoadBalancerTypes[i].Code break } diff --git a/internal/cmp/resource_network.go b/internal/cmp/resource_network.go index 74e90f4f..bb0c5f72 100644 --- a/internal/cmp/resource_network.go +++ b/internal/cmp/resource_network.go @@ -42,6 +42,17 @@ 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 { + cmpVersion, err := utils.GetCmpVersion(r.rClient.Client) + if err != nil { + return err + } + nsxVar := nsxt + nsxSegmentVar := nsxtSegment + if v, _ := utils.ParseVersion("6.2.4"); v <= cmpVersion { + // from 6.2.4 onwards the display name of NSX-T has been change to NSX + nsxVar = nsx + nsxSegmentVar = nsxSegment + } setMeta(meta, r.rClient.Client) var createReq models.CreateNetwork if err := tftags.Get(d, &createReq); err != nil { @@ -56,7 +67,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: nsxSegmentVar, }) }) // Get network server ID for nsx-t @@ -86,7 +97,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 == nsxVar { createReq.NetworkServer.ID = networkService.NetworkServices[i].ID break diff --git a/internal/cmp/router.go b/internal/cmp/router.go index b205af38..7bd321ac 100644 --- a/internal/cmp/router.go +++ b/internal/cmp/router.go @@ -111,6 +111,19 @@ 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 { + cmpVersion, err := utils.GetCmpVersion(r.rClient.Client) + if err != nil { + return err + } + nsxVar := nsxt + tier0GatewayTypeVar := tier0GatewayType + tier1GatewayTypeVar := tier1GatewayType + if v, _ := utils.ParseVersion("6.2.4"); v <= cmpVersion { + // from 6.2.4 onwards the display name of NSX-T has been change to NSX + nsxVar = nsx + tier0GatewayTypeVar = nsxTier0GatewayType + tier1GatewayTypeVar = nsxTier1GatewayType + } 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 @@ -134,14 +147,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] = tier0GatewayTypeVar } 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] = tier1GatewayTypeVar } // Get Router type rtRetry := utils.CustomRetry{} @@ -174,7 +187,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 == nsxVar { routerReq.NetworkRouter.NetworkServer.ID = networkService.NetworkServices[i].ID routerReq.NetworkRouter.NetworkServerID = networkService.NetworkServices[i].ID diff --git a/internal/cmp/transport_zone.go b/internal/cmp/transport_zone.go index 2ebd010d..e8aeaa20 100644 --- a/internal/cmp/transport_zone.go +++ b/internal/cmp/transport_zone.go @@ -23,6 +23,15 @@ func newTransportZone(tClient *client.RouterAPIService) *transportZone { } func (r *transportZone) Read(ctx context.Context, d *utils.Data, meta interface{}) error { + cmpVersion, err := utils.GetCmpVersion(r.tClient.Client) + if err != nil { + return err + } + nsxVar := nsxt + if v, _ := utils.ParseVersion("6.2.4"); v <= cmpVersion { + // from 6.2.4 onwards the display name of NSX-T has been change to NSX + nsxVar = nsx + } setMeta(meta, r.tClient.Client) var tfScope models.NetworkScope if err := tftags.Get(d, &tfScope); err != nil { @@ -37,7 +46,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 == nsxVar { serverID = serverResp.NetworkServices[i].ID break diff --git a/internal/utils/cmp_version.go b/internal/utils/cmp_version.go new file mode 100644 index 00000000..f3c59e88 --- /dev/null +++ b/internal/utils/cmp_version.go @@ -0,0 +1,44 @@ +// (C) Copyright 2023 Hewlett Packard Enterprise Development LP + +package utils + +import ( + "context" + "strconv" + "strings" + + "github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/client" +) + +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(apiClient client.APIClientHandler) (int, error) { + c := client.CmpStatus{ + Client: apiClient, + } + + cmpVersion, err := c.GetCmpVersion(context.Background()) + if err != nil { + return 0, err + } + return ParseVersion(cmpVersion.Appliance.BuildVersion) +} From 7415ca23baed54c3b99817b87ec1fe79f610540a Mon Sep 17 00:00:00 2001 From: Mahesh N Date: Fri, 24 Nov 2023 14:09:27 +0530 Subject: [PATCH 2/6] added create lb testcase --- acc-testcases/resources/load_balancer.yaml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 acc-testcases/resources/load_balancer.yaml diff --git a/acc-testcases/resources/load_balancer.yaml b/acc-testcases/resources/load_balancer.yaml new file mode 100644 index 00000000..2c8ec986 --- /dev/null +++ b/acc-testcases/resources/load_balancer.yaml @@ -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" From b54842b24d08801ef6e398da463d7e1abfae6490 Mon Sep 17 00:00:00 2001 From: Mahesh N Date: Fri, 24 Nov 2023 14:17:59 +0530 Subject: [PATCH 3/6] lint fix --- internal/utils/cmp_version.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/utils/cmp_version.go b/internal/utils/cmp_version.go index f3c59e88..ee499274 100644 --- a/internal/utils/cmp_version.go +++ b/internal/utils/cmp_version.go @@ -40,5 +40,6 @@ func GetCmpVersion(apiClient client.APIClientHandler) (int, error) { if err != nil { return 0, err } + return ParseVersion(cmpVersion.Appliance.BuildVersion) } From 375d5babcf704d24240f4ad3ea51d2ab71f0c634 Mon Sep 17 00:00:00 2001 From: Mahesh N Date: Fri, 24 Nov 2023 14:24:10 +0530 Subject: [PATCH 4/6] lint fix --- internal/cmp/loadbalancer.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/cmp/loadbalancer.go b/internal/cmp/loadbalancer.go index 9f1f1942..da7dc34c 100644 --- a/internal/cmp/loadbalancer.go +++ b/internal/cmp/loadbalancer.go @@ -69,7 +69,6 @@ func (lb *loadBalancer) loadBalancerAlignRequest(ctx context.Context, meta inter if v, _ := utils.ParseVersion("6.2.4"); v <= cmpVersion { // from 6.2.4 onwards the display name of NSX-T has been change to NSX nsxVar = nsx - } allTypes, _ := lb.lbClient.GetLoadBalancerTypes(ctx, map[string]string{ nameKey: nsxVar, From 243b9955e33fd1032c0edb002df7a473bfffa8e7 Mon Sep 17 00:00:00 2001 From: Mahesh N Date: Fri, 24 Nov 2023 20:03:22 +0530 Subject: [PATCH 5/6] address comment --- .../data_source_edge_cluster_test.go | 11 ++-- internal/cmp/constants.go | 9 ++-- .../cmp/data_source_lb_pool_member_group.go | 9 +--- internal/cmp/dhcp_server.go | 9 +--- internal/cmp/dhcp_server_datasource.go | 9 +--- internal/cmp/edge_cluster.go | 9 +--- internal/cmp/helper.go | 51 +++++++++++++++++++ internal/cmp/loadbalancer.go | 13 ++--- internal/cmp/resource_network.go | 13 ++--- internal/cmp/router.go | 17 ++----- internal/cmp/transport_zone.go | 9 +--- internal/utils/cmp_version.go | 45 ---------------- 12 files changed, 78 insertions(+), 126 deletions(-) delete mode 100644 internal/utils/cmp_version.go diff --git a/internal/acceptance_test/data_source_edge_cluster_test.go b/internal/acceptance_test/data_source_edge_cluster_test.go index 84981963..86030008 100644 --- a/internal/acceptance_test/data_source_edge_cluster_test.go +++ b/internal/acceptance_test/data_source_edge_cluster_test.go @@ -6,7 +6,7 @@ import ( "testing" api_client "github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/client" - "github.com/HewlettPackard/hpegl-vmaas-terraform-resources/internal/utils" + "github.com/HewlettPackard/hpegl-vmaas-terraform-resources/internal/cmp" "github.com/HewlettPackard/hpegl-vmaas-terraform-resources/pkg/atf" ) @@ -31,17 +31,12 @@ func TestAccDataSourceNetworkEdgeCluster(t *testing.T) { if len(ServerResp.NetworkServices) == 0 { return nil, err } - cmpVersion, err := utils.GetCmpVersion(iClient.Client) + nsxType, err := cmp.GetNsxTypeFromCMP(iClient.Client) if err != nil { return nil, err } - nsxVar := "NSX-T" - if v, _ := utils.ParseVersion("6.2.4"); v <= cmpVersion { - // from 6.2.4 onwards the display name of NSX-T has been change to NSX - nsxVar = "NSX" - } for i, n := range ServerResp.NetworkServices { - if n.TypeName == nsxVar { + if n.TypeName == nsxType { serverID = ServerResp.NetworkServices[i].ID break diff --git a/internal/cmp/constants.go b/internal/cmp/constants.go index 63881adf..66c3f265 100644 --- a/internal/cmp/constants.go +++ b/internal/cmp/constants.go @@ -6,10 +6,9 @@ import "time" const ( vmware = "vmware" - nsxSegment = "NSX Segment" - 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 @@ -22,10 +21,8 @@ const ( // retry related constants maxTimeout = time.Hour * 2 // router consts - nsxTier0GatewayType = "NSX Tier-0 Gateway" - tier0GatewayType = "NSX-T Tier-0 Gateway" - nsxTier1GatewayType = "NSX Tier-1 Gateway" - tier1GatewayType = "NSX-T Tier-1 Gateway" + tier0GatewayType = "Tier-0 Gateway" + tier1GatewayType = "Tier-1 Gateway" routerFirewallExternalPolicy = "GatewayPolicy" syncedTypeValue = "Synced" diff --git a/internal/cmp/data_source_lb_pool_member_group.go b/internal/cmp/data_source_lb_pool_member_group.go index f0458c54..a4876204 100644 --- a/internal/cmp/data_source_lb_pool_member_group.go +++ b/internal/cmp/data_source_lb_pool_member_group.go @@ -34,15 +34,10 @@ func (n *poolMemberGroupds) Read(ctx context.Context, d *utils.Data, meta interf if err := d.Error(); err != nil { return err } - cmpVersion, err := utils.GetCmpVersion(n.rClient.Client) + nsxType, err := GetNsxTypeFromCMP(n.rClient.Client) if err != nil { return err } - nsxVar := nsxt - if v, _ := utils.ParseVersion("6.2.4"); v <= cmpVersion { - // from 6.2.4 onwards the display name of NSX-T has been change to NSX - nsxVar = nsx - } setMeta(meta, n.rClient.Client) // Get network server ID for nsx-t serverResp, err := n.rClient.GetNetworkServices(ctx, nil) @@ -52,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 == nsxVar { + if n.TypeName == nsxType { serverID = serverResp.NetworkServices[i].ID break diff --git a/internal/cmp/dhcp_server.go b/internal/cmp/dhcp_server.go index c4f0d0ab..4493aa1d 100644 --- a/internal/cmp/dhcp_server.go +++ b/internal/cmp/dhcp_server.go @@ -122,15 +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 - cmpVersion, err := utils.GetCmpVersion(dhcp.rClient.Client) + nsxType, err := GetNsxTypeFromCMP(dhcp.rClient.Client) if err != nil { return err } - nsxVar := nsxt - if v, _ := utils.ParseVersion("6.2.4"); v <= cmpVersion { - // from 6.2.4 onwards the display name of NSX-T has been change to NSX - nsxVar = nsx - } setMeta(meta, dhcp.rClient.Client) nsRetry := utils.CustomRetry{} nsRetry.RetryParallel(ctx, meta, func(ctx context.Context) (interface{}, error) { @@ -145,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 == nsxVar { + if n.TypeName == nsxType { createReq.NetworkDhcpServer.ID = networkService.NetworkServices[i].ID break diff --git a/internal/cmp/dhcp_server_datasource.go b/internal/cmp/dhcp_server_datasource.go index 8ad299b5..f9364982 100644 --- a/internal/cmp/dhcp_server_datasource.go +++ b/internal/cmp/dhcp_server_datasource.go @@ -34,15 +34,10 @@ func (n *dhcpServerds) Read(ctx context.Context, d *utils.Data, meta interface{} if err := d.Error(); err != nil { return err } - cmpVersion, err := utils.GetCmpVersion(n.rClient.Client) + nsxType, err := GetNsxTypeFromCMP(n.rClient.Client) if err != nil { return err } - nsxVar := nsxt - if v, _ := utils.ParseVersion("6.2.4"); v <= cmpVersion { - // from 6.2.4 onwards the display name of NSX-T has been change to NSX - nsxVar = nsx - } setMeta(meta, n.rClient.Client) // Get network server ID for nsx-t serverResp, err := n.rClient.GetNetworkServices(ctx, nil) @@ -52,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 == nsxVar { + if n.TypeName == nsxType { serverID = serverResp.NetworkServices[i].ID break diff --git a/internal/cmp/edge_cluster.go b/internal/cmp/edge_cluster.go index 9341b192..f62b3af8 100644 --- a/internal/cmp/edge_cluster.go +++ b/internal/cmp/edge_cluster.go @@ -24,15 +24,10 @@ func newEdgeCluster(tClient *client.RouterAPIService) *edgeCluster { } func (r *edgeCluster) Read(ctx context.Context, d *utils.Data, meta interface{}) error { - cmpVersion, err := utils.GetCmpVersion(r.tClient.Client) + nsxType, err := GetNsxTypeFromCMP(r.tClient.Client) if err != nil { return err } - nsxVar := nsxt - if v, _ := utils.ParseVersion("6.2.4"); v <= cmpVersion { - // from 6.2.4 onwards the display name of NSX-T has been change to NSX - nsxVar = nsx - } setMeta(meta, r.tClient.Client) log.Printf("[INFO] Get Edge Cluster") var tfEdgeCluster models.NetworkEdgeClusters @@ -48,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 == nsxVar { + if n.TypeName == nsxType { serverID = serverResp.NetworkServices[i].ID break diff --git a/internal/cmp/helper.go b/internal/cmp/helper.go index 55aa3fe8..8e9f50d4 100644 --- a/internal/cmp/helper.go +++ b/internal/cmp/helper.go @@ -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" @@ -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(apiClient client.APIClientHandler) (int, error) { + c := client.CmpStatus{ + Client: apiClient, + } + + cmpVersion, err := c.GetCmpVersion(context.Background()) + if err != nil { + return 0, err + } + + return ParseVersion(cmpVersion.Appliance.BuildVersion) +} + +func GetNsxTypeFromCMP(apiClient client.APIClientHandler) (string, error) { + cmpVersion, err := GetCmpVersion(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 +} diff --git a/internal/cmp/loadbalancer.go b/internal/cmp/loadbalancer.go index da7dc34c..8a4d7c37 100644 --- a/internal/cmp/loadbalancer.go +++ b/internal/cmp/loadbalancer.go @@ -61,17 +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 { - cmpVersion, err := utils.GetCmpVersion(lb.rClient.Client) + nsxType, err := GetNsxTypeFromCMP(lb.rClient.Client) if err != nil { return err } - nsxVar := nsxt - if v, _ := utils.ParseVersion("6.2.4"); v <= cmpVersion { - // from 6.2.4 onwards the display name of NSX-T has been change to NSX - nsxVar = nsx - } allTypes, _ := lb.lbClient.GetLoadBalancerTypes(ctx, map[string]string{ - nameKey: nsxVar, + nameKey: nsxType, }) // Get network service ID @@ -89,14 +84,14 @@ func (lb *loadBalancer) loadBalancerAlignRequest(ctx context.Context, meta inter networkService := nsResp.(models.GetNetworkServicesResp) for i, n := range networkService.NetworkServices { - if n.TypeName == nsxVar { + if n.TypeName == nsxType { createReq.NetworkLoadBalancer.NetworkServerID = networkService.NetworkServices[i].ID break } } for i, n := range allTypes.LoadBalancerTypes { - if n.Name == nsxVar { + if n.Name == nsxType { createReq.NetworkLoadBalancer.Type = allTypes.LoadBalancerTypes[i].Code break diff --git a/internal/cmp/resource_network.go b/internal/cmp/resource_network.go index bb0c5f72..db7fe0d2 100644 --- a/internal/cmp/resource_network.go +++ b/internal/cmp/resource_network.go @@ -42,17 +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 { - cmpVersion, err := utils.GetCmpVersion(r.rClient.Client) + nsxType, err := GetNsxTypeFromCMP(r.rClient.Client) if err != nil { return err } - nsxVar := nsxt - nsxSegmentVar := nsxtSegment - if v, _ := utils.ParseVersion("6.2.4"); v <= cmpVersion { - // from 6.2.4 onwards the display name of NSX-T has been change to NSX - nsxVar = nsx - nsxSegmentVar = nsxSegment - } setMeta(meta, r.rClient.Client) var createReq models.CreateNetwork if err := tftags.Get(d, &createReq); err != nil { @@ -67,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: nsxSegmentVar, + nameKey: fmt.Sprintf("%s %s", nsxType, nsxSegment), }) }) // Get network server ID for nsx-t @@ -97,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 == nsxVar { + if n.TypeName == nsxType { createReq.NetworkServer.ID = networkService.NetworkServices[i].ID break diff --git a/internal/cmp/router.go b/internal/cmp/router.go index 7bd321ac..fe3ffad3 100644 --- a/internal/cmp/router.go +++ b/internal/cmp/router.go @@ -111,19 +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 { - cmpVersion, err := utils.GetCmpVersion(r.rClient.Client) + nsxType, err := GetNsxTypeFromCMP(r.rClient.Client) if err != nil { return err } - nsxVar := nsxt - tier0GatewayTypeVar := tier0GatewayType - tier1GatewayTypeVar := tier1GatewayType - if v, _ := utils.ParseVersion("6.2.4"); v <= cmpVersion { - // from 6.2.4 onwards the display name of NSX-T has been change to NSX - nsxVar = nsx - tier0GatewayTypeVar = nsxTier0GatewayType - tier1GatewayTypeVar = nsxTier1GatewayType - } 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 @@ -147,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] = tier0GatewayTypeVar + 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] = tier1GatewayTypeVar + queryParam[nameKey] = fmt.Sprintf("%s %s", nsxType, tier1GatewayType) } // Get Router type rtRetry := utils.CustomRetry{} @@ -187,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 == nsxVar { + if n.TypeName == nsxType { routerReq.NetworkRouter.NetworkServer.ID = networkService.NetworkServices[i].ID routerReq.NetworkRouter.NetworkServerID = networkService.NetworkServices[i].ID diff --git a/internal/cmp/transport_zone.go b/internal/cmp/transport_zone.go index e8aeaa20..eaa22092 100644 --- a/internal/cmp/transport_zone.go +++ b/internal/cmp/transport_zone.go @@ -23,15 +23,10 @@ func newTransportZone(tClient *client.RouterAPIService) *transportZone { } func (r *transportZone) Read(ctx context.Context, d *utils.Data, meta interface{}) error { - cmpVersion, err := utils.GetCmpVersion(r.tClient.Client) + nsxType, err := GetNsxTypeFromCMP(r.tClient.Client) if err != nil { return err } - nsxVar := nsxt - if v, _ := utils.ParseVersion("6.2.4"); v <= cmpVersion { - // from 6.2.4 onwards the display name of NSX-T has been change to NSX - nsxVar = nsx - } setMeta(meta, r.tClient.Client) var tfScope models.NetworkScope if err := tftags.Get(d, &tfScope); err != nil { @@ -46,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 == nsxVar { + if n.TypeName == nsxType { serverID = serverResp.NetworkServices[i].ID break diff --git a/internal/utils/cmp_version.go b/internal/utils/cmp_version.go deleted file mode 100644 index ee499274..00000000 --- a/internal/utils/cmp_version.go +++ /dev/null @@ -1,45 +0,0 @@ -// (C) Copyright 2023 Hewlett Packard Enterprise Development LP - -package utils - -import ( - "context" - "strconv" - "strings" - - "github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/client" -) - -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(apiClient client.APIClientHandler) (int, error) { - c := client.CmpStatus{ - Client: apiClient, - } - - cmpVersion, err := c.GetCmpVersion(context.Background()) - if err != nil { - return 0, err - } - - return ParseVersion(cmpVersion.Appliance.BuildVersion) -} From bfc947bca520e2a9830e9072fe2b4935f2e3224b Mon Sep 17 00:00:00 2001 From: Mahesh N Date: Fri, 24 Nov 2023 20:21:01 +0530 Subject: [PATCH 6/6] lint fix --- internal/acceptance_test/data_source_edge_cluster_test.go | 3 ++- internal/cmp/data_source_lb_pool_member_group.go | 2 +- internal/cmp/dhcp_server.go | 2 +- internal/cmp/dhcp_server_datasource.go | 2 +- internal/cmp/edge_cluster.go | 2 +- internal/cmp/helper.go | 8 ++++---- internal/cmp/loadbalancer.go | 2 +- internal/cmp/resource_network.go | 2 +- internal/cmp/router.go | 2 +- internal/cmp/transport_zone.go | 2 +- 10 files changed, 14 insertions(+), 13 deletions(-) diff --git a/internal/acceptance_test/data_source_edge_cluster_test.go b/internal/acceptance_test/data_source_edge_cluster_test.go index 86030008..b3499465 100644 --- a/internal/acceptance_test/data_source_edge_cluster_test.go +++ b/internal/acceptance_test/data_source_edge_cluster_test.go @@ -3,6 +3,7 @@ package acceptancetest import ( + "context" "testing" api_client "github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/client" @@ -31,7 +32,7 @@ func TestAccDataSourceNetworkEdgeCluster(t *testing.T) { if len(ServerResp.NetworkServices) == 0 { return nil, err } - nsxType, err := cmp.GetNsxTypeFromCMP(iClient.Client) + nsxType, err := cmp.GetNsxTypeFromCMP(context.Background(), iClient.Client) if err != nil { return nil, err } diff --git a/internal/cmp/data_source_lb_pool_member_group.go b/internal/cmp/data_source_lb_pool_member_group.go index a4876204..0e34b92a 100644 --- a/internal/cmp/data_source_lb_pool_member_group.go +++ b/internal/cmp/data_source_lb_pool_member_group.go @@ -34,7 +34,7 @@ func (n *poolMemberGroupds) Read(ctx context.Context, d *utils.Data, meta interf if err := d.Error(); err != nil { return err } - nsxType, err := GetNsxTypeFromCMP(n.rClient.Client) + nsxType, err := GetNsxTypeFromCMP(ctx, n.rClient.Client) if err != nil { return err } diff --git a/internal/cmp/dhcp_server.go b/internal/cmp/dhcp_server.go index 4493aa1d..63d7846c 100644 --- a/internal/cmp/dhcp_server.go +++ b/internal/cmp/dhcp_server.go @@ -122,7 +122,7 @@ 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(dhcp.rClient.Client) + nsxType, err := GetNsxTypeFromCMP(ctx, dhcp.rClient.Client) if err != nil { return err } diff --git a/internal/cmp/dhcp_server_datasource.go b/internal/cmp/dhcp_server_datasource.go index f9364982..28297ca0 100644 --- a/internal/cmp/dhcp_server_datasource.go +++ b/internal/cmp/dhcp_server_datasource.go @@ -34,7 +34,7 @@ func (n *dhcpServerds) Read(ctx context.Context, d *utils.Data, meta interface{} if err := d.Error(); err != nil { return err } - nsxType, err := GetNsxTypeFromCMP(n.rClient.Client) + nsxType, err := GetNsxTypeFromCMP(ctx, n.rClient.Client) if err != nil { return err } diff --git a/internal/cmp/edge_cluster.go b/internal/cmp/edge_cluster.go index f62b3af8..58470f10 100644 --- a/internal/cmp/edge_cluster.go +++ b/internal/cmp/edge_cluster.go @@ -24,7 +24,7 @@ func newEdgeCluster(tClient *client.RouterAPIService) *edgeCluster { } func (r *edgeCluster) Read(ctx context.Context, d *utils.Data, meta interface{}) error { - nsxType, err := GetNsxTypeFromCMP(r.tClient.Client) + nsxType, err := GetNsxTypeFromCMP(ctx, r.tClient.Client) if err != nil { return err } diff --git a/internal/cmp/helper.go b/internal/cmp/helper.go index 8e9f50d4..41b53cc6 100644 --- a/internal/cmp/helper.go +++ b/internal/cmp/helper.go @@ -40,12 +40,12 @@ func ParseVersion(version string) (int, error) { return sum, nil } -func GetCmpVersion(apiClient client.APIClientHandler) (int, error) { +func GetCmpVersion(ctx context.Context, apiClient client.APIClientHandler) (int, error) { c := client.CmpStatus{ Client: apiClient, } - cmpVersion, err := c.GetCmpVersion(context.Background()) + cmpVersion, err := c.GetCmpVersion(ctx) if err != nil { return 0, err } @@ -53,8 +53,8 @@ func GetCmpVersion(apiClient client.APIClientHandler) (int, error) { return ParseVersion(cmpVersion.Appliance.BuildVersion) } -func GetNsxTypeFromCMP(apiClient client.APIClientHandler) (string, error) { - cmpVersion, err := GetCmpVersion(apiClient) +func GetNsxTypeFromCMP(ctx context.Context, apiClient client.APIClientHandler) (string, error) { + cmpVersion, err := GetCmpVersion(ctx, apiClient) if err != nil { return "", err } diff --git a/internal/cmp/loadbalancer.go b/internal/cmp/loadbalancer.go index 8a4d7c37..36599465 100644 --- a/internal/cmp/loadbalancer.go +++ b/internal/cmp/loadbalancer.go @@ -61,7 +61,7 @@ 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(lb.rClient.Client) + nsxType, err := GetNsxTypeFromCMP(ctx, lb.rClient.Client) if err != nil { return err } diff --git a/internal/cmp/resource_network.go b/internal/cmp/resource_network.go index db7fe0d2..4ed7453c 100644 --- a/internal/cmp/resource_network.go +++ b/internal/cmp/resource_network.go @@ -42,7 +42,7 @@ 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(r.rClient.Client) + nsxType, err := GetNsxTypeFromCMP(ctx, r.rClient.Client) if err != nil { return err } diff --git a/internal/cmp/router.go b/internal/cmp/router.go index fe3ffad3..d3c48d7e 100644 --- a/internal/cmp/router.go +++ b/internal/cmp/router.go @@ -111,7 +111,7 @@ 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(r.rClient.Client) + nsxType, err := GetNsxTypeFromCMP(ctx, r.rClient.Client) if err != nil { return err } diff --git a/internal/cmp/transport_zone.go b/internal/cmp/transport_zone.go index eaa22092..54aa2dfa 100644 --- a/internal/cmp/transport_zone.go +++ b/internal/cmp/transport_zone.go @@ -23,7 +23,7 @@ func newTransportZone(tClient *client.RouterAPIService) *transportZone { } func (r *transportZone) Read(ctx context.Context, d *utils.Data, meta interface{}) error { - nsxType, err := GetNsxTypeFromCMP(r.tClient.Client) + nsxType, err := GetNsxTypeFromCMP(ctx, r.tClient.Client) if err != nil { return err }