-
Notifications
You must be signed in to change notification settings - Fork 14
fix: missing vds info with r/cluster
import
#235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,9 +12,9 @@ import ( | |
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/vmware/terraform-provider-vcf/internal/api_client" | ||
"github.com/vmware/vcf-sdk-go/vcf" | ||
|
||
"github.com/vmware/terraform-provider-vcf/internal/api_client" | ||
"github.com/vmware/terraform-provider-vcf/internal/datastores" | ||
"github.com/vmware/terraform-provider-vcf/internal/network" | ||
"github.com/vmware/terraform-provider-vcf/internal/resource_utils" | ||
|
@@ -370,19 +370,19 @@ func FlattenCluster(ctx context.Context, clusterObj *vcf.Cluster, apiClient *vcf | |
result["is_default"] = clusterObj.IsDefault | ||
result["is_stretched"] = clusterObj.IsStretched | ||
|
||
if clusterObj.VdsSpecs != nil { | ||
flattenedVdsSpecs := getFlattenedVdsSpecsForRefs(*clusterObj.VdsSpecs) | ||
result["vds"] = flattenedVdsSpecs | ||
flattenedVdsSpecs, err := getFlattenedVdsSpecsForRefs(ctx, *clusterObj.Id, apiClient) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get flattened VDS specs: %w", err) | ||
} | ||
result["vds"] = flattenedVdsSpecs | ||
|
||
if clusterObj.Hosts != nil { | ||
flattenedHostSpecs, err := getFlattenedHostSpecsForRefs(ctx, *clusterObj.Hosts, apiClient) | ||
if err != nil { | ||
return nil, err | ||
} | ||
result["host"] = flattenedHostSpecs | ||
flattenedHostSpecs, err := getFlattenedHostSpecsForRefs(ctx, *clusterObj.Hosts, apiClient) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
result["host"] = flattenedHostSpecs | ||
|
||
return &result, nil | ||
} | ||
|
||
|
@@ -404,18 +404,17 @@ func ImportCluster(ctx context.Context, data *schema.ResourceData, apiClient *vc | |
_ = data.Set("primary_datastore_type", clusterObj.PrimaryDatastoreType) | ||
_ = data.Set("is_default", clusterObj.IsDefault) | ||
_ = data.Set("is_stretched", clusterObj.IsStretched) | ||
if clusterObj.VdsSpecs != nil { | ||
flattenedVdsSpecs := getFlattenedVdsSpecsForRefs(*clusterObj.VdsSpecs) | ||
_ = data.Set("vds", flattenedVdsSpecs) | ||
flattenedVdsSpecs, err := getFlattenedVdsSpecsForRefs(ctx, *clusterObj.Id, apiClient) | ||
if err != nil { | ||
return nil, err | ||
} | ||
_ = data.Set("vds", flattenedVdsSpecs) | ||
|
||
if clusterObj.Hosts != nil { | ||
flattenedHostSpecs, err := getFlattenedHostSpecsForRefs(ctx, *clusterObj.Hosts, apiClient) | ||
if err != nil { | ||
return nil, err | ||
} | ||
_ = data.Set("host", flattenedHostSpecs) | ||
flattenedHostSpecs, err := getFlattenedHostSpecsForRefs(ctx, *clusterObj.Hosts, apiClient) | ||
if err != nil { | ||
return nil, err | ||
} | ||
_ = data.Set("host", flattenedHostSpecs) | ||
|
||
// get all domains and find our cluster to set the "domain_id" attribute, because | ||
// cluster API doesn't provide parent domain ID. | ||
|
@@ -462,20 +461,63 @@ func getFlattenedHostSpecsForRefs(ctx context.Context, hostRefs []vcf.HostRefere | |
api_client.LogError(vcfErr) | ||
return nil, errors.New(*vcfErr.Message) | ||
} | ||
flattenedHostSpecs = append(flattenedHostSpecs, *FlattenHost(*hostObj)) | ||
flattenedHostSpecs = append(flattenedHostSpecs, FlattenHost(*hostObj)) | ||
} | ||
return flattenedHostSpecs, nil | ||
} | ||
|
||
func getFlattenedVdsSpecsForRefs(vdsSpecs []vcf.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 *vcf.ClientWithResponses) ([]map[string]interface{}, error) { | ||
res, err := apiClient.GetVdsesWithResponse(ctx, clusterId) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return flattenedVdsSpecs | ||
|
||
switcheObj, vcfErr := api_client.GetResponseAs[[]vcf.Vds](res) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if vcfErr != nil { | ||
api_client.LogError(vcfErr) | ||
return nil, errors.New(*vcfErr.Message) | ||
} | ||
|
||
flattenedVdsSpecs := make([]map[string]interface{}, len(*switcheObj)) | ||
for i, vds := range *switcheObj { | ||
portGroups := vds.PortGroups | ||
portGroupSpecs := make([]vcf.PortgroupSpec, len(*portGroups)) | ||
for j, pg := range *portGroups { | ||
portGroupSpecs[j] = vcf.PortgroupSpec{ | ||
Name: pg.Name, | ||
TransportType: pg.TransportType, | ||
ActiveUplinks: pg.ActiveUplinks, | ||
} | ||
} | ||
|
||
niocBandwidthAllocations := vds.NiocBandwidthAllocations | ||
niocSpecs := make([]vcf.NiocBandwidthAllocationSpec, len(*niocBandwidthAllocations)) | ||
for k, nioc := range *niocBandwidthAllocations { | ||
niocType := "" | ||
if nioc.Type != nil { | ||
niocType = *nioc.Type | ||
} | ||
|
||
niocSpecs[k] = vcf.NiocBandwidthAllocationSpec{ | ||
Type: niocType, | ||
NiocTrafficResourceAllocation: vcf.NiocTrafficResourceAllocation{ | ||
SharesInfo: &vcf.SharesInfo{ | ||
Shares: nioc.NiocTrafficResourceAllocation.SharesInfo.Shares, | ||
Level: nioc.NiocTrafficResourceAllocation.SharesInfo.Level, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
vdsSpec := vcf.VdsSpec{ | ||
Name: vds.Name, | ||
IsUsedByNsxt: vds.IsUsedByNsxt, | ||
PortGroupSpecs: &portGroupSpecs, | ||
NiocBandwidthAllocationSpecs: &niocSpecs, | ||
} | ||
|
||
flattenedVdsSpecs[i] = network.FlattenVdsSpec(&vdsSpec) | ||
} | ||
|
||
return flattenedVdsSpecs, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,22 +82,49 @@ func HostSpecSchema() *schema.Resource { | |
Type: schema.TypeList, | ||
Optional: true, | ||
Description: "vmnic configuration for the ESXi host", | ||
Elem: network.VMNicSchema(), | ||
Elem: &schema.Resource{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems that |
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "Name of the physical NIC", | ||
}, | ||
"mac_address": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "MAC address of the physical NIC", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func FlattenHost(host vcf.Host) *map[string]interface{} { | ||
func FlattenHost(host vcf.Host) map[string]interface{} { | ||
result := make(map[string]interface{}) | ||
result["id"] = host.Id | ||
result["host_name"] = host.Fqdn | ||
ipAddresses := *host.IpAddresses | ||
if len(ipAddresses) > 0 { | ||
result["ip_address"] = ipAddresses[0].IpAddress | ||
|
||
if host.IpAddresses != nil { | ||
ipAddresses := *host.IpAddresses | ||
if len(ipAddresses) > 0 { | ||
result["ip_address"] = ipAddresses[0].IpAddress | ||
} | ||
} | ||
|
||
if host.PhysicalNics != nil { | ||
var physicalNics []map[string]interface{} | ||
for _, nic := range *host.PhysicalNics { | ||
nicMap := make(map[string]interface{}) | ||
nicMap["name"] = nic.DeviceName | ||
nicMap["mac_address"] = nic.MacAddress | ||
physicalNics = append(physicalNics, nicMap) | ||
} | ||
result["vmnic"] = physicalNics | ||
} | ||
|
||
return &result | ||
return result | ||
} | ||
|
||
func TryConvertToHostSpec(object map[string]interface{}) (*vcf.HostSpec, error) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.