Skip to content

Commit

Permalink
feat: add name option to d/domain (#228)
Browse files Browse the repository at this point in the history
Updated `d/vcf_domain` to support `name` alongside the existing `domain_id`.

Signed-off-by: Jared Burns <[email protected]>
Co-authored-by: Ryan Johnson <[email protected]>
  • Loading branch information
burnsjared0415 and tenthirtyam authored Aug 22, 2024
1 parent bf1b271 commit 6aa3067
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 90 deletions.
57 changes: 28 additions & 29 deletions docs/data-sources/domain.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,45 @@
page_title: "vcf_domain Data Source - terraform-provider-vcf"
subcategory: ""
description: |-
---

# vcf_domain (Data Source)

A workload domain is a policy based resource container with specific availability and performance attributes that combines compute (vSphere),
storage (vSAN/NFS/VMFS on FC/VVOL) and networking (NSX) into a single consumable entity.
A workload domain is a policy based resource container with specific availability and performance
attributes that combines compute (vSphere), storage (vSAN/NFS/VMFS on FC/VVOL) and networking (NSX)
into a single consumable entity.

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `domain_id` (String) The ID of the Domain to be used as data source

### Optional
- `domain_id` (String) The ID of the workload domain.
- `name` (String) The name of the workload domain.

- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))

### Read-Only

- `cluster` (List of Object) Specification representing the clusters in the workload domain (see [below for nested schema](#nestedatt--cluster))
- `cluster` (List of Object) The cluster references associated with the workload domain (see [below for nested schema](#nestedatt--cluster))
- `id` (String) The ID of this resource.
- `is_management_sso_domain` (Boolean) Shows whether the domain is joined to the management domain SSO
- `name` (String) Name of the domain
- `nsx_configuration` (List of Object) Represents NSX Manager cluster references associated with the domain (see [below for nested schema](#nestedatt--nsx_configuration))
- `sso_id` (String) ID of the SSO domain associated with the workload domain
- `sso_name` (String) Name of the SSO domain associated with the workload domain
- `status` (String) Status of the workload domain
- `type` (String) Type of the workload domain
- `vcenter_configuration` (List of Object) Specification describing vCenter Server instance settings (see [below for nested schema](#nestedatt--vcenter_configuration))
- `is_management_sso_domain` (Boolean) Indicates if the workload domain is joined to the management domain's SSO domain.
- `nsx_configuration` (List of Object) The NSX Manager cluster references associated with the workload domain. (see [below for nested schema](#nestedatt--nsx_configuration))
- `sso_id` (String) The ID of the SSO domain associated with the workload domain.
- `sso_name` (String) The name of the SSO domain associated with the workload domain.
- `status` (String) The status of the workload domain.
- `type` (String) The type of workload domain.
- `vcenter_configuration` (List of Object) The vCenter Server instance references associated with the workload domain. (see [below for nested schema](#nestedatt--vcenter_configuration))

<a id="nestedblock--timeouts"></a>

### Nested Schema for `timeouts`

Optional:

- `read` (String)


<a id="nestedatt--cluster"></a>

### Nested Schema for `cluster`

Read-Only:
Expand All @@ -67,6 +65,7 @@ Read-Only:
- `vvol_datastores` (List of Object) Cluster storage configuration for VVOL (see [below for nested schema](#nestedobjatt--cluster--vvol_datastores))

<a id="nestedobjatt--cluster--host"></a>

### Nested Schema for `cluster.host`

Read-Only:
Expand All @@ -83,6 +82,7 @@ Read-Only:
- `vmnic` (List of Object) vmnic configuration for the ESXi host (see [below for nested schema](#nestedobjatt--cluster--host--vmnic))

<a id="nestedobjatt--cluster--host--vmnic"></a>

### Nested Schema for `cluster.host.vmnic`

Read-Only:
Expand All @@ -91,9 +91,8 @@ Read-Only:
- `uplink` (String) Uplink associated with vmnic
- `vds_name` (String) Name of the VDS associated with the ESXi host



<a id="nestedobjatt--cluster--nfs_datastores"></a>

### Nested Schema for `cluster.nfs_datastores`

Read-Only:
Expand All @@ -104,8 +103,8 @@ Read-Only:
- `server_name` (String) Fully qualified domain name or IP address of the NFS endpoint
- `user_tag` (String) User tag used to annotate NFS share


<a id="nestedobjatt--cluster--vds"></a>

### Nested Schema for `cluster.vds`

Read-Only:
Expand All @@ -116,6 +115,7 @@ Read-Only:
- `portgroup` (List of Object) List of portgroups associated with the vSphere Distributed Switch (see [below for nested schema](#nestedobjatt--cluster--vds--portgroup))

<a id="nestedobjatt--cluster--vds--nioc_bandwidth_allocations"></a>

### Nested Schema for `cluster.vds.nioc_bandwidth_allocations`

Read-Only:
Expand All @@ -126,8 +126,8 @@ Read-Only:
- `shares_level` (String) The allocation level. The level is a simplified view of shares. Levels map to a pre-determined set of numeric values for shares.
- `type` (String) Host infrastructure traffic type.


<a id="nestedobjatt--cluster--vds--portgroup"></a>

### Nested Schema for `cluster.vds.portgroup`

Read-Only:
Expand All @@ -136,17 +136,16 @@ Read-Only:
- `active_uplinks` (List of String) List of active uplinks associated with portgroup.
- `transport_type` (String) Port group transport type



<a id="nestedobjatt--cluster--vmfs_datastore"></a>

### Nested Schema for `cluster.vmfs_datastore`

Read-Only:

- `datastore_names` (List of String) VMFS datastore names used for VMFS on FC for cluster creation


<a id="nestedobjatt--cluster--vsan_datastore"></a>

### Nested Schema for `cluster.vsan_datastore`

Read-Only:
Expand All @@ -156,16 +155,16 @@ Read-Only:
- `failures_to_tolerate` (Number) Number of ESXi host failures to tolerate in the vSAN cluster
- `license_key` (String) vSAN license key used


<a id="nestedobjatt--cluster--vsan_remote_datastore_cluster"></a>

### Nested Schema for `cluster.vsan_remote_datastore_cluster`

Read-Only:

- `datastore_uuids` (List of String) vSAN HCI Mesh remote datastore UUIDs


<a id="nestedobjatt--cluster--vvol_datastores"></a>

### Nested Schema for `cluster.vvol_datastores`

Read-Only:
Expand All @@ -184,6 +183,7 @@ Read-Only:
- `vip_fqdn` (String) Fully qualified domain name of the NSX Manager cluster VIP

<a id="nestedobjatt--nsx_configuration--nsx_manager_node"></a>

### Nested Schema for `nsx_configuration.nsx_manager_node`

Read-Only:
Expand All @@ -192,9 +192,8 @@ Read-Only:
- `ip_address` (String) IPv4 address of the NSX Manager appliance
- `name` (String) Name of the NSX Manager appliance, e.g., sfo-w01-nsx01



<a id="nestedatt--vcenter_configuration"></a>

### Nested Schema for `vcenter_configuration`

Read-Only:
Expand Down
25 changes: 14 additions & 11 deletions examples/data-sources/domain/variables.tf
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
variable "sddc_manager_host" {
description = "The fully qualified domain name of the SDDC Manager instance."
}

variable "sddc_manager_username" {
description = "Username used to authenticate against an SDDC Manager instance"
default = ""
description = "The username to authenticate to the SDDC Manager instance."
sensitive = true
}

variable "sddc_manager_password" {
description = "Password used to authenticate against an SDDC Manager instance"
default = ""
description = "The password to authenticate to the SDDC Manager instance."
sensitive = true
}

variable "sddc_manager_host" {
description = "Fully qualified domain name of an SDDC Manager instance"
default = ""
variable "domain_id" {
description = "The ID of the workload domain."
}

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 "domain_name" {
description = "The name of the workload domain."
name = "sfo-w01"
}
8 changes: 6 additions & 2 deletions examples/data-sources/domain/vcf_domain.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ provider "vcf" {
sddc_manager_host = var.sddc_manager_host
}

data "vcf_domain" "domain1" {
domain_id = var.vcf_domain_id
data "vcf_domain" "sfo-m01" {
domain_id = var.domain_id
}

data "vcf_domain" "sfo-w01" {
name = var.domain_name
}
4 changes: 4 additions & 0 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ const (
// Typically, the id of management domain is used as it is already created during bringup.
VcfTestDomainDataSourceId = "VCF_DOMAIN_DATA_SOURCE_ID"

// VcfTestDomainDataSourceName name of a workload domain used in workload domain data source acceptance test.
// Typically, the name of management domain is used as it is already created during bringup.
VcfTestDomainDataSourceName = "VCF_DOMAIN_DATA_SOURCE_NAME"

// VcfTestClusterDataSourceId id of cluster used in cluster data source acceptance test.
// Typically, the id of the default cluster in the management domain is used as it is
// already created during bringup.
Expand Down
68 changes: 56 additions & 12 deletions internal/provider/data_source_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ package provider

import (
"context"
"errors"
"fmt"
"time"

"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/vcf-sdk-go/client"
"github.com/vmware/vcf-sdk-go/client/domains"
"github.com/vmware/vcf-sdk-go/models"

"github.com/vmware/terraform-provider-vcf/internal/api_client"
"github.com/vmware/terraform-provider-vcf/internal/constants"
"github.com/vmware/terraform-provider-vcf/internal/domain"
"github.com/vmware/terraform-provider-vcf/internal/network"
"github.com/vmware/terraform-provider-vcf/internal/vcenter"
Expand All @@ -27,69 +33,107 @@ func DataSourceDomain() *schema.Resource {
Schema: map[string]*schema.Schema{
"domain_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
Description: "The ID of the Domain to be used as data source",
Optional: true,
Description: "The ID of the workload domain.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the domain",
Optional: true,
Description: "The name of the workload domain.",
},
"cluster": {
Type: schema.TypeList,
Computed: true,
Description: "Specification representing the clusters in the workload domain",
Description: "The cluster references associated with the workload domain",
Elem: clusterSubresourceSchema(),
},
"nsx_configuration": {
Type: schema.TypeList,
Computed: true,
Description: "Represents NSX Manager cluster references associated with the domain",
Description: "The NSX Manager cluster references associated with the workload domain.",
Elem: network.NsxSchema(),
},
"vcenter_configuration": {
Type: schema.TypeList,
Computed: true,
Description: "Specification describing vCenter Server instance settings",
Description: "The vCenter Server instance references associated with the workload domain.",
Elem: vcenter.VCSubresourceSchema(),
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "Status of the workload domain",
Description: "The status of the workload domain.",
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: "Type of the workload domain",
Description: "The type of workload domain.",
},
"sso_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the SSO domain associated with the workload domain",
Description: "The ID of the SSO domain associated with the workload domain.",
},
"sso_name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the SSO domain associated with the workload domain",
Description: "The name of the SSO domain associated with the workload domain.",
},
"is_management_sso_domain": {
Type: schema.TypeBool,
Computed: true,
Description: "Shows whether the domain is joined to the management domain SSO",
Description: "Indicates if the workload domain is joined to the management domain's SSO domain.",
},
},
}
}

func dataSourceDomainRead(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
apiClient := meta.(*api_client.SddcManagerClient).ApiClient

domainId := data.Get("domain_id").(string)
domainName := data.Get("name").(string)

if domainId == "" {
if domainName == "" {
return diag.Errorf("either 'domain_id' or 'name' must be provided")
}

domainInfo, err := getDomainByName(ctx, apiClient, domainName)
if err != nil {
return diag.FromErr(err)
}
domainId = domainInfo.ID
}

_, err := domain.ImportDomain(ctx, data, apiClient, domainId, true)
if err != nil {
return diag.FromErr(err)
}

return nil
}

func getDomainByName(ctx context.Context, apiClient *client.VcfClient, name string) (*models.Domain, error) {
params := domains.NewGetDomainsParamsWithContext(ctx).
WithTimeout(constants.DefaultVcfApiCallTimeout)

domainsResponse, err := apiClient.Domains.GetDomains(params)
if err != nil {
return nil, err
}

if domainsResponse.Payload == nil {
return nil, errors.New("no domains found")
}

for _, domainElement := range domainsResponse.Payload.Elements {
domain := *domainElement
if domain.Name == name {
return &domain, nil
}
}

return nil, fmt.Errorf("domain name '%s' not found", name)
}
Loading

0 comments on commit 6aa3067

Please sign in to comment.