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 <[email protected]>
  • Loading branch information
burnsjared0415 committed Aug 27, 2024
1 parent 0b6bd8a commit e3d8f78
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 18 deletions.
69 changes: 57 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,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
}
2 changes: 0 additions & 2 deletions internal/network/nioc_bandwidth_allocation_subresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 6 additions & 4 deletions internal/network/vds_subresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e3d8f78

Please sign in to comment.