Skip to content

Commit

Permalink
fix: missing vds info with r/cluster import
Browse files Browse the repository at this point in the history
Updating Cluster import to solve issues where vds is blank on data source.

Signed-off-by: Jared Burns <jared.burns@broadcom.com>
  • Loading branch information
burnsjared0415 committed Aug 22, 2024
1 parent 0b6bd8a commit f364358
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
62 changes: 50 additions & 12 deletions internal/cluster/cluster_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package cluster
import (
"context"
"fmt"
"log"
"sort"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -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)

Check failure on line 376 in internal/cluster/cluster_operations.go

View workflow job for this annotation

GitHub Actions / Build

ineffectual assignment to err (ineffassign)
result["vds"] = flattenedVdsSpecs

flattenedHostSpecs, err := getFlattenedHostSpecsForRefs(ctx, clusterObj.Hosts, apiClient)
Expand Down Expand Up @@ -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)

Check failure on line 406 in internal/cluster/cluster_operations.go

View workflow job for this annotation

GitHub Actions / Build

ineffectual assignment to err (ineffassign)

_ = data.Set("vds", flattenedVdsSpecs)

flattenedHostSpecs, err := getFlattenedHostSpecsForRefs(ctx, clusterObj.Hosts, apiClient)
Expand Down Expand Up @@ -454,15 +457,50 @@ 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")
}
return flattenedVdsSpecs

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,
}
}

//for _, nioc := range vds.NiocBandwidthAllocations {
// if nioc != nil {
// fmt.Printf("NiocBandwidthAllocation: {Type: %s, shares_Allocation: %d, shares_leve: %s}\n",
// nioc.Type, nioc.NiocTrafficResourceAllocation.SharesInfo.Shares, nioc.NiocTrafficResourceAllocation.SharesInfo.Level)
// fmt.Println("Type:", reflect.TypeOf(nioc.Type))
// fmt.Println("Shares:", reflect.TypeOf(nioc.NiocTrafficResourceAllocation.SharesInfo.Shares))
// fmt.Println("Level:", reflect.TypeOf(nioc.NiocTrafficResourceAllocation.SharesInfo.Level))
// } else {
// fmt.Println("NiocBandwidthAllocation: <nil>")
// }
//}

vdsSpec := &models.VdsSpec{
Name: vds.Name,
IsUsedByNSXT: vds.IsUsedByNSXT,
PortGroupSpecs: portGroupSpecs,
}
flattenedVdsSpecs[i] = network.FlattenVdsSpec(vdsSpec)
}

return flattenedVdsSpecs, nil
}
2 changes: 1 addition & 1 deletion internal/network/vds_subresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func FlattenVdsSpec(vdsSpec *models.VdsSpec) map[string]interface{} {
}
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,
Expand Down

0 comments on commit f364358

Please sign in to comment.