From 7b50744e122860dac31c3f521a65e1e1a34db992 Mon Sep 17 00:00:00 2001 From: Jared Burns Date: Wed, 21 Aug 2024 14:00:55 -0400 Subject: [PATCH] fix: missing vds info with `r/cluster` import Address missing `vds` info with `r/cluster` import. Signed-off-by: Jared Burns --- internal/cluster/cluster_operations.go | 26 +++++-------- internal/cluster/host_spec_subresource.go | 37 ++++++++++++++++++- internal/provider/data_source_cluster_test.go | 1 + internal/provider/resource_cluster_test.go | 4 ++ 4 files changed, 49 insertions(+), 19 deletions(-) diff --git a/internal/cluster/cluster_operations.go b/internal/cluster/cluster_operations.go index cce8743..994ad34 100644 --- a/internal/cluster/cluster_operations.go +++ b/internal/cluster/cluster_operations.go @@ -375,14 +375,13 @@ func FlattenCluster(ctx context.Context, clusterObj *vcf.Cluster, apiClient *vcf 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 +403,11 @@ 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 := getFlattenedVdsSpecsForRefs(clusterObj.VdsSpecs) + _ = 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) + _ = 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. diff --git a/internal/cluster/host_spec_subresource.go b/internal/cluster/host_spec_subresource.go index f49d9c9..efc277d 100644 --- a/internal/cluster/host_spec_subresource.go +++ b/internal/cluster/host_spec_subresource.go @@ -81,8 +81,21 @@ func HostSpecSchema() *schema.Resource { "vmnic": { Type: schema.TypeList, Optional: true, - Description: "vmnic configuration for the ESXi host", - Elem: network.VMNicSchema(), + Description: "Physical NIC configuration for the ESXi host", + Elem: &schema.Resource{ + 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", + }, + }, + }, }, }, } @@ -96,6 +109,26 @@ func FlattenHost(host vcf.Host) *map[string]interface{} { if len(ipAddresses) > 0 { result["ip_address"] = ipAddresses[0].IpAddress } + if len(host.PhysicalNics) > 0 { + 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 + } + if len(host.PhysicalNics) > 0 { + 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 } diff --git a/internal/provider/data_source_cluster_test.go b/internal/provider/data_source_cluster_test.go index 4a764e4..731f0a4 100644 --- a/internal/provider/data_source_cluster_test.go +++ b/internal/provider/data_source_cluster_test.go @@ -29,6 +29,7 @@ func TestAccDataSourceVcfCluster(t *testing.T) { resource.TestCheckResourceAttrSet("data.vcf_cluster.cluster1", "primary_datastore_type"), resource.TestCheckResourceAttrSet("data.vcf_cluster.cluster1", "is_default"), resource.TestCheckResourceAttrSet("data.vcf_cluster.cluster1", "is_stretched"), + resource.TestCheckResourceAttrSet("data.vcf_cluster.cluster1", "vds"), resource.TestCheckResourceAttrSet("data.vcf_cluster.cluster1", "host.0.id"), resource.TestCheckResourceAttrSet("data.vcf_cluster.cluster1", "host.0.host_name"), resource.TestCheckResourceAttrSet("data.vcf_cluster.cluster1", "host.0.ip_address"), diff --git a/internal/provider/resource_cluster_test.go b/internal/provider/resource_cluster_test.go index 34681e6..959fd9b 100644 --- a/internal/provider/resource_cluster_test.go +++ b/internal/provider/resource_cluster_test.go @@ -45,6 +45,7 @@ func TestAccResourceVcfClusterCreate(t *testing.T) { resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "primary_datastore_type"), resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "is_default"), resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "is_stretched"), + resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "vds"), resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "host.0.id"), resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "host.1.id"), resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "host.2.id"), @@ -116,6 +117,7 @@ func TestAccResourceVcfClusterFull(t *testing.T) { resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "primary_datastore_type"), resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "is_default"), resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "is_stretched"), + resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "vds"), resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "host.0.id"), resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "host.1.id"), resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "host.2.id"), @@ -151,6 +153,7 @@ func TestAccResourceVcfClusterFull(t *testing.T) { resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "primary_datastore_type"), resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "is_default"), resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "is_stretched"), + resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "vds"), resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "host.0.id"), resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "host.1.id"), resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "host.2.id"), @@ -180,6 +183,7 @@ func TestAccResourceVcfClusterFull(t *testing.T) { resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "primary_datastore_type"), resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "is_default"), resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "is_stretched"), + resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "vds"), resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "host.0.id"), resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "host.1.id"), resource.TestCheckResourceAttrSet("vcf_cluster.cluster1", "host.2.id"),