Skip to content

Commit

Permalink
Recalibrate 'vcd_vcenter' data source to be backwards compatible
Browse files Browse the repository at this point in the history
Signed-off-by: Dainius Serplis <dserplis@vmware.com>
  • Loading branch information
Didainius committed Nov 21, 2024
1 parent 7270276 commit 91e9c5d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .changes/v4.0.0/1343-features.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
* **New Resource:** `vcd_vcenter` to manage vCenter servers [GH-1343, GH-1348]
* **New Resource:** `vcd_vcenter` to manage vCenter servers [GH-1343, GH-1348, GH-1355]
* **New Resource:** `vcd_tm_nsxt_manager` to manage NSX-T Managers [GH-1343, GH-1355]
* **New Data Source:** `vcd_tm_nsxt_manager` to manage NSX-T Managers [GH-1343, GH-1355]
46 changes: 44 additions & 2 deletions vcd/datasource_vcd_vcenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vcd
import (
"context"
"fmt"
"net/url"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -57,7 +58,7 @@ func datasourceVcdVcenter() *schema.Resource {
Computed: true,
Description: fmt.Sprintf("Mode of %s", labelVirtualCenter),
},
"listener_state": {
"connection_status": {
Type: schema.TypeString,
Computed: true,
Description: fmt.Sprintf("Listener state of %s", labelVirtualCenter),
Expand All @@ -67,7 +68,7 @@ func datasourceVcdVcenter() *schema.Resource {
Computed: true,
Description: fmt.Sprintf("Mode of %s", labelVirtualCenter),
},
"version": {
"vcenter_version": {
Type: schema.TypeString,
Computed: true,
Description: fmt.Sprintf("Version of %s", labelVirtualCenter),
Expand All @@ -77,16 +78,57 @@ func datasourceVcdVcenter() *schema.Resource {
Computed: true,
Description: fmt.Sprintf("%s UUID", labelVirtualCenter),
},
"vcenter_host": {
Type: schema.TypeString,
Computed: true,
Description: fmt.Sprintf("%s hostname", labelVirtualCenter),
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "vCenter status",
},
},
}
}

func datasourceVcdVcenterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vcdClient := meta.(*VCDClient)
if err := classicVcdVcenterReadStatus(vcdClient, d); err != nil {
return err
}

c := crudConfig[*govcd.VCenter, types.VSphereVirtualCenter]{
entityLabel: labelVirtualCenter,
getEntityFunc: vcdClient.GetVCenterByName,
stateStoreFunc: setTmVcenterData,
}
return readDatasource(ctx, d, meta, c)
}

// classicVcdVcenterReadStatus
func classicVcdVcenterReadStatus(vcdClient *VCDClient, d *schema.ResourceData) diag.Diagnostics {
if vcdClient.Client.IsTm() {
return nil
}
vCenterName := d.Get("name").(string)

vcs, err := govcd.QueryVirtualCenters(vcdClient.VCDClient, "name=="+url.QueryEscape(vCenterName))
if err != nil {
return diag.Errorf("error occurred while querying vCenters: %s", err)
}

if len(vcs) == 0 {
return diag.Errorf("%s: could not identify single vCenter. Got %d with name '%s'",
govcd.ErrorEntityNotFound, len(vcs), vCenterName)
}

if len(vcs) > 1 {
return diag.Errorf("could not identify single vCenter. Got %d with name '%s'",
len(vcs), vCenterName)
}

d.Set("status", vcs[0].Status)

return nil
}
23 changes: 19 additions & 4 deletions vcd/resource_vcd_vcenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func resourceVcdVcenter() *schema.Resource {
Computed: true,
Description: fmt.Sprintf("Mode of %s", labelVirtualCenter),
},
"listener_state": {
"connection_status": {
Type: schema.TypeString,
Computed: true,
Description: fmt.Sprintf("Listener state of %s", labelVirtualCenter),
Expand All @@ -98,7 +98,7 @@ func resourceVcdVcenter() *schema.Resource {
Computed: true,
Description: fmt.Sprintf("Mode of %s", labelVirtualCenter),
},
"version": {
"vcenter_version": {
Type: schema.TypeString,
Computed: true,
Description: fmt.Sprintf("Version of %s", labelVirtualCenter),
Expand All @@ -108,6 +108,16 @@ func resourceVcdVcenter() *schema.Resource {
Computed: true,
Description: fmt.Sprintf("%s UUID", labelVirtualCenter),
},
"vcenter_host": {
Type: schema.TypeString,
Computed: true,
Description: fmt.Sprintf("%s hostname", labelVirtualCenter),
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "vCenter status",
},
},
}
}
Expand Down Expand Up @@ -140,10 +150,15 @@ func setTmVcenterData(d *schema.ResourceData, v *govcd.VCenter) error {
dSet(d, "has_proxy", v.VSphereVCenter.HasProxy)
dSet(d, "is_connected", v.VSphereVCenter.IsConnected)
dSet(d, "mode", v.VSphereVCenter.Mode)
dSet(d, "listener_state", v.VSphereVCenter.ListenerState)
dSet(d, "connection_status", v.VSphereVCenter.ListenerState)
dSet(d, "cluster_health_status", v.VSphereVCenter.ClusterHealthStatus)
dSet(d, "version", v.VSphereVCenter.VcVersion)
dSet(d, "vcenter_version", v.VSphereVCenter.VcVersion)
dSet(d, "uuid", v.VSphereVCenter.Uuid)
host, err := url.Parse(v.VSphereVCenter.Url)
if err != nil {
return fmt.Errorf("error parsing URL for storing 'vcenter_host': %s", err)
}
dSet(d, "vcenter_host", host.Host)

d.SetId(v.VSphereVCenter.VcId)

Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/vcenter.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ The following attributes are exported on this resource:
* `is_connected` - Defines if the vCenter server is connected.
* `mode` - One of `NONE`, `IAAS` (scoped to the provider), `SDDC` (scoped to tenants), `MIXED` (both
uses are possible)
* `listener_state` - `INITIAL`, `INVALID_SETTINGS`, `UNSUPPORTED`, `DISCONNECTED`, `CONNECTING`,
* `connection_status` - `INITIAL`, `INVALID_SETTINGS`, `UNSUPPORTED`, `DISCONNECTED`, `CONNECTING`,
`CONNECTED_SYNCING`, `CONNECTED`, `STOP_REQ`, `STOP_AND_PURGE_REQ`, `STOP_ACK`
* `cluster_health_status` - Cluster health status. One of `GRAY` , `RED` , `YELLOW` , `GREEN`
* `version` - vCenter version
* `uuid` - UUID of vCenter
* `vcenter_host` - Host of Vcenter server

## Importing

Expand Down

0 comments on commit 91e9c5d

Please sign in to comment.