From e3d8f784f7f3df8a4d678f48e690e23db87e62a4 Mon Sep 17 00:00:00 2001 From: Jared Burns Date: Thu, 22 Aug 2024 19:24:45 -0400 Subject: [PATCH] fix: missing vds info with r/cluster import Updating Cluster import to solve issues where vds is blank on data source. Signed-off-by: Jared Burns --- internal/cluster/cluster_operations.go | 69 +++++++++++++++---- .../nioc_bandwidth_allocation_subresource.go | 2 - internal/network/vds_subresource.go | 10 +-- 3 files changed, 63 insertions(+), 18 deletions(-) diff --git a/internal/cluster/cluster_operations.go b/internal/cluster/cluster_operations.go index 3584a5a..768a973 100644 --- a/internal/cluster/cluster_operations.go +++ b/internal/cluster/cluster_operations.go @@ -7,6 +7,7 @@ package cluster import ( "context" "fmt" + "log" "sort" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -372,7 +373,7 @@ func FlattenCluster(ctx context.Context, clusterObj *models.Cluster, apiClient * result["is_default"] = clusterObj.IsDefault result["is_stretched"] = clusterObj.IsStretched - flattenedVdsSpecs := getFlattenedVdsSpecsForRefs(clusterObj.VdsSpecs) + flattenedVdsSpecs, err := getFlattenedVdsSpecsForRefs(ctx, clusterObj.ID, apiClient) result["vds"] = flattenedVdsSpecs flattenedHostSpecs, err := getFlattenedHostSpecsForRefs(ctx, clusterObj.Hosts, apiClient) @@ -401,7 +402,9 @@ func ImportCluster(ctx context.Context, data *schema.ResourceData, apiClient *cl _ = data.Set("primary_datastore_type", clusterObj.PrimaryDatastoreType) _ = data.Set("is_default", clusterObj.IsDefault) _ = data.Set("is_stretched", clusterObj.IsStretched) - flattenedVdsSpecs := getFlattenedVdsSpecsForRefs(clusterObj.VdsSpecs) + + flattenedVdsSpecs, err := getFlattenedVdsSpecsForRefs(ctx, clusterId, apiClient) + _ = data.Set("vds", flattenedVdsSpecs) flattenedHostSpecs, err := getFlattenedHostSpecsForRefs(ctx, clusterObj.Hosts, apiClient) @@ -454,15 +457,57 @@ func getFlattenedHostSpecsForRefs(ctx context.Context, hostRefs []*models.HostRe return flattenedHostSpecs, nil } -func getFlattenedVdsSpecsForRefs(vdsSpecs []*models.VdsSpec) []map[string]interface{} { - flattenedVdsSpecs := *new([]map[string]interface{}) - // Since backend API returns objects in random order sort VDSSpec list to ensure - // import is reproducible - sort.SliceStable(vdsSpecs, func(i, j int) bool { - return *vdsSpecs[i].Name < *vdsSpecs[j].Name - }) - for _, vdsSpec := range vdsSpecs { - flattenedVdsSpecs = append(flattenedVdsSpecs, network.FlattenVdsSpec(vdsSpec)) +func getFlattenedVdsSpecsForRefs(ctx context.Context, clusterId string, apiClient *client.VcfClient) ([]map[string]interface{}, error) { + // Fetch VDS information + vdsParams := &clusters.GetVdsesParams{ + ClusterID: clusterId, + Context: ctx, + } + vdsResponse, err := apiClient.Clusters.GetVdses(vdsParams) + if err != nil { + return nil, err + } + + if vdsResponse.Payload == nil { + log.Fatal("vdsResponse.Payload is nil") + } + + flattenedVdsSpecs := make([]map[string]interface{}, len(vdsResponse.Payload)) + for i, vds := range vdsResponse.Payload { + portGroupSpecs := make([]*models.PortgroupSpec, len(vds.PortGroups)) + for j, pg := range vds.PortGroups { + portGroupSpecs[j] = &models.PortgroupSpec{ + Name: pg.Name, + TransportType: pg.TransportType, + ActiveUplinks: pg.ActiveUplinks, + } + } + + niocSpecs := make([]*models.NiocBandwidthAllocationSpec, len(vds.NiocBandwidthAllocations)) + for k, nioc := range vds.NiocBandwidthAllocations { + if nioc != nil { + niocSpecs[k] = &models.NiocBandwidthAllocationSpec{ + Type: &nioc.Type, + NiocTrafficResourceAllocation: &models.NiocTrafficResourceAllocation{ + SharesInfo: &models.SharesInfo{ + Shares: nioc.NiocTrafficResourceAllocation.SharesInfo.Shares, + Level: nioc.NiocTrafficResourceAllocation.SharesInfo.Level, + }, + }, + } + } + } + + fmt.Println("Here is niocSpecs: ", niocSpecs) + vdsSpec := &models.VdsSpec{ + Name: vds.Name, + IsUsedByNSXT: vds.IsUsedByNSXT, + PortGroupSpecs: portGroupSpecs, + NiocBandwidthAllocationSpecs: niocSpecs, + } + + flattenedVdsSpecs[i] = network.FlattenVdsSpec(vdsSpec) } - return flattenedVdsSpecs + + return flattenedVdsSpecs, nil } diff --git a/internal/network/nioc_bandwidth_allocation_subresource.go b/internal/network/nioc_bandwidth_allocation_subresource.go index de111db..33290ad 100644 --- a/internal/network/nioc_bandwidth_allocation_subresource.go +++ b/internal/network/nioc_bandwidth_allocation_subresource.go @@ -112,9 +112,7 @@ func flattenNiocBandwidthAllocationSpec(spec *models.NiocBandwidthAllocationSpec return result } result["type"] = *spec.Type - result["limit"] = *spec.NiocTrafficResourceAllocation.Limit if spec.NiocTrafficResourceAllocation != nil { - result["reservation"] = *spec.NiocTrafficResourceAllocation.Reservation result["shares"] = spec.NiocTrafficResourceAllocation.SharesInfo.Shares result["shares_level"] = spec.NiocTrafficResourceAllocation.SharesInfo.Level } diff --git a/internal/network/vds_subresource.go b/internal/network/vds_subresource.go index 0e06d22..a40f905 100644 --- a/internal/network/vds_subresource.go +++ b/internal/network/vds_subresource.go @@ -103,16 +103,18 @@ func FlattenVdsSpec(vdsSpec *models.VdsSpec) map[string]interface{} { } result["name"] = *vdsSpec.Name result["is_used_by_nsx"] = vdsSpec.IsUsedByNSXT - flattenedNiocBandwidthAllocationSpecs := *new([]map[string]interface{}) + flattenedNiocBandwidthAllocationSpecs := make([]map[string]interface{}, 0, len(vdsSpec.NiocBandwidthAllocationSpecs)) for _, niocBandwidthAllocationSpec := range vdsSpec.NiocBandwidthAllocationSpecs { if niocBandwidthAllocationSpec != nil { - flattenedNiocBandwidthAllocationSpecs = append(flattenedNiocBandwidthAllocationSpecs, - flattenNiocBandwidthAllocationSpec(niocBandwidthAllocationSpec)) + flattenedSpec := flattenNiocBandwidthAllocationSpec(niocBandwidthAllocationSpec) + if flattenedSpec != nil { + flattenedNiocBandwidthAllocationSpecs = append(flattenedNiocBandwidthAllocationSpecs, flattenedSpec) + } } } result["nioc_bandwidth_allocations"] = flattenedNiocBandwidthAllocationSpecs - flattenedPortgroupSpecs := *new([]map[string]interface{}) + flattenedPortgroupSpecs := make([]map[string]interface{}, 0, len(vdsSpec.PortGroupSpecs)) for _, portgroupSpec := range vdsSpec.PortGroupSpecs { if portgroupSpec != nil { flattenedPortgroupSpecs = append(flattenedPortgroupSpecs,