Skip to content

Commit

Permalink
feat: add domain name to data source
Browse files Browse the repository at this point in the history
added domain name to data source so that data can be retrieve by name or domain_id

Signed-off-by: Jared Burns <[email protected]>
  • Loading branch information
burnsjared0415 committed Aug 20, 2024
1 parent 8509e75 commit 8d23514
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 14 deletions.
6 changes: 3 additions & 3 deletions docs/data-sources/domain.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ storage (vSAN/NFS/VMFS on FC/VVOL) and networking (NSX) into a single consumable
<!-- schema generated by tfplugindocs -->
## 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))

Expand Down
5 changes: 5 additions & 0 deletions examples/data-sources/domain/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
}
59 changes: 53 additions & 6 deletions internal/provider/data_source_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ 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"
"github.com/vmware/terraform-provider-vcf/internal/api_client"
"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"
)

Expand All @@ -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": {
Expand Down Expand Up @@ -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

}
31 changes: 26 additions & 5 deletions internal/provider/data_source_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"testing"
)

run test | debug

func TestAccDataSourceVcfDomain(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -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)
}

0 comments on commit 8d23514

Please sign in to comment.