From 8d23514f95ad70fabc97a8b42f70618dc7eeba69 Mon Sep 17 00:00:00 2001 From: Jared Burns Date: Tue, 20 Aug 2024 11:55:03 -0400 Subject: [PATCH] feat: add domain name to data source added domain name to data source so that data can be retrieve by name or domain_id Signed-off-by: Jared Burns --- docs/data-sources/domain.md | 6 +- examples/data-sources/domain/variables.tf | 5 ++ internal/provider/data_source_domain.go | 59 ++++++++++++++++++-- internal/provider/data_source_domain_test.go | 31 ++++++++-- 4 files changed, 87 insertions(+), 14 deletions(-) diff --git a/docs/data-sources/domain.md b/docs/data-sources/domain.md index 1aabfe9..fee92c3 100644 --- a/docs/data-sources/domain.md +++ b/docs/data-sources/domain.md @@ -14,11 +14,11 @@ storage (vSAN/NFS/VMFS on FC/VVOL) and networking (NSX) into a single consumable ## Schema -### Required +### Optional - `domain_id` (String) The ID of the Domain to be used as data source - -### Optional + +- `name` (String) The Name of the Domain to be used as data source - `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts)) diff --git a/examples/data-sources/domain/variables.tf b/examples/data-sources/domain/variables.tf index b2a8c03..e067b8e 100644 --- a/examples/data-sources/domain/variables.tf +++ b/examples/data-sources/domain/variables.tf @@ -16,4 +16,9 @@ variable "sddc_manager_host" { variable "vcf_domain_id" { description = "Id of the domain that is to be used as a data source. Note: management domain ID can be used to refer to some of it's attributes" default = "" +} + +variable "vcf_domain_name" { + description = "Name of the domain that is to be used as a data source." + default = "" } \ No newline at end of file diff --git a/internal/provider/data_source_domain.go b/internal/provider/data_source_domain.go index c6e9698..09c4590 100644 --- a/internal/provider/data_source_domain.go +++ b/internal/provider/data_source_domain.go @@ -5,6 +5,8 @@ package provider import ( "context" + "fmt" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" @@ -12,6 +14,7 @@ import ( "github.com/vmware/terraform-provider-vcf/internal/domain" "github.com/vmware/terraform-provider-vcf/internal/network" "github.com/vmware/terraform-provider-vcf/internal/vcenter" + "github.com/vmware/vcf-sdk-go/client/domains" "time" ) @@ -24,13 +27,13 @@ func DataSourceDomain() *schema.Resource { Schema: map[string]*schema.Schema{ "domain_id": { Type: schema.TypeString, - Required: true, ValidateFunc: validation.NoZeroValues, + Optional: true, Description: "The ID of the Domain to be used as data source", }, "name": { Type: schema.TypeString, - Computed: true, + Optional: true, Description: "Name of the domain", }, "cluster": { @@ -80,14 +83,58 @@ func DataSourceDomain() *schema.Resource { } } + func dataSourceDomainRead(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { vcfClient := meta.(*api_client.SddcManagerClient) apiClient := vcfClient.ApiClient + fmt.Printf("vcfClient: %+v\n", vcfClient) + fmt.Printf("apiClient: %+v\n", apiClient) + domainId := data.Get("domain_id").(string) + domainName := data.Get("name").(string) + + if domainId == "" { + if domainName == "" { + return diag.Errorf("both domain_id and name are empty") + } + + params := &domains.GetDomainsParams{ + Context: ctx, + } + + // Make an API call to /v1/domains to get all domains + domainsResponse, err := apiClient.Domains.GetDomains(params) + if err != nil { + return diag.FromErr(err) + } + - _, err := domain.ImportDomain(ctx, data, apiClient, domainId, true) - if err != nil { - return diag.FromErr(err) + // Extract the domains from the response + domainsList := domainsResponse.Payload.Elements + + + // Find the domain ID based on the name + for _, domainPtr := range domainsList { + domain := *domainPtr + if domain.Name == domainName { + domainId = domain.ID + break + } + } + + + if domainId == "" { + return diag.Errorf("no domain found with name \"%s\"", domainName) + } } - return nil + + + //domainInfo, err := domain.ImportDomain(ctx, data, apiClient, domainId, true) +_, err := domain.ImportDomain(ctx, data, apiClient, domainId, true) +if err != nil { + return diag.FromErr(err) } + +return nil + +} \ No newline at end of file diff --git a/internal/provider/data_source_domain_test.go b/internal/provider/data_source_domain_test.go index bf9881c..fe40394 100644 --- a/internal/provider/data_source_domain_test.go +++ b/internal/provider/data_source_domain_test.go @@ -11,6 +11,8 @@ import ( "testing" ) +run test | debug + func TestAccDataSourceVcfDomain(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -48,9 +50,28 @@ func TestAccDataSourceVcfDomain(t *testing.T) { }) } -func testAccVcfDomainDataSourceConfig(domainId string) string { - return fmt.Sprintf(` - data "vcf_domain" "domain1" { - domain_id = %q - }`, domainId) +//func testAccVcfDomainDataSourceConfig(domainId string) string { +// return fmt.Sprintf(` +// data "vcf_domain" "domain1" { +// domain_id = %q +// }`, domainId) +//} + + +func testAccVcfDomainDataSourceConfig(domainId, name string) string { + var config string + + if domainId != "" { + config += fmt.Sprintf(` + domain_id = %q`, domainId) + } + + if name != "" { + config += fmt.Sprintf(` + name = %q`, name) + } + + return fmt.Sprintf(` + data "vcf_domain" "domain1" {%s + }`, config) }