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" diff --git a/internal/acceptance_test/data_source_edge_cluster_test.go b/internal/acceptance_test/data_source_edge_cluster_test.go index d861cebf..b3499465 100644 --- a/internal/acceptance_test/data_source_edge_cluster_test.go +++ b/internal/acceptance_test/data_source_edge_cluster_test.go @@ -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" ) @@ -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 diff --git a/internal/cmp/constants.go b/internal/cmp/constants.go index ab708104..66c3f265 100644 --- a/internal/cmp/constants.go +++ b/internal/cmp/constants.go @@ -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 @@ -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" diff --git a/internal/cmp/data_source_lb_pool_member_group.go b/internal/cmp/data_source_lb_pool_member_group.go index d918da00..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,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 + } setMeta(meta, n.rClient.Client) // Get network server ID for nsx-t serverResp, err := n.rClient.GetNetworkServices(ctx, nil) @@ -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 diff --git a/internal/cmp/dhcp_server.go b/internal/cmp/dhcp_server.go index 59fe3bb4..63d7846c 100644 --- a/internal/cmp/dhcp_server.go +++ b/internal/cmp/dhcp_server.go @@ -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 + } setMeta(meta, dhcp.rClient.Client) nsRetry := utils.CustomRetry{} nsRetry.RetryParallel(ctx, meta, func(ctx context.Context) (interface{}, error) { @@ -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 diff --git a/internal/cmp/dhcp_server_datasource.go b/internal/cmp/dhcp_server_datasource.go index 981a3742..28297ca0 100644 --- a/internal/cmp/dhcp_server_datasource.go +++ b/internal/cmp/dhcp_server_datasource.go @@ -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) @@ -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 diff --git a/internal/cmp/edge_cluster.go b/internal/cmp/edge_cluster.go index ff64d26a..58470f10 100644 --- a/internal/cmp/edge_cluster.go +++ b/internal/cmp/edge_cluster.go @@ -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 @@ -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 diff --git a/internal/cmp/helper.go b/internal/cmp/helper.go index 55aa3fe8..41b53cc6 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(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 +} diff --git a/internal/cmp/loadbalancer.go b/internal/cmp/loadbalancer.go index 982e9d2d..36599465 100644 --- a/internal/cmp/loadbalancer.go +++ b/internal/cmp/loadbalancer.go @@ -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 + } allTypes, _ := lb.lbClient.GetLoadBalancerTypes(ctx, map[string]string{ - nameKey: nsxt, + nameKey: nsxType, }) // Get network service ID @@ -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 } diff --git a/internal/cmp/resource_network.go b/internal/cmp/resource_network.go index 74e90f4f..4ed7453c 100644 --- a/internal/cmp/resource_network.go +++ b/internal/cmp/resource_network.go @@ -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 { @@ -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 @@ -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 diff --git a/internal/cmp/router.go b/internal/cmp/router.go index b205af38..d3c48d7e 100644 --- a/internal/cmp/router.go +++ b/internal/cmp/router.go @@ -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 @@ -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{} @@ -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 diff --git a/internal/cmp/transport_zone.go b/internal/cmp/transport_zone.go index 2ebd010d..54aa2dfa 100644 --- a/internal/cmp/transport_zone.go +++ b/internal/cmp/transport_zone.go @@ -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 { @@ -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