Skip to content

Commit

Permalink
Make 'status' a computed field
Browse files Browse the repository at this point in the history
Signed-off-by: Dainius Serplis <[email protected]>
  • Loading branch information
Didainius committed Nov 26, 2024
1 parent ecf2880 commit 2996229
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 32 deletions.
31 changes: 0 additions & 31 deletions vcd/datasource_vcd_vcenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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 @@ -92,9 +91,6 @@ func datasourceVcdVcenter() *schema.Resource {

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,
Expand All @@ -103,30 +99,3 @@ func datasourceVcdVcenterRead(ctx context.Context, d *schema.ResourceData, meta
}
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)
}

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

return nil
}
2 changes: 1 addition & 1 deletion vcd/datasource_vcd_vcenter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestAccVcdVcenterDS(t *testing.T) {
resource.TestMatchResourceAttr("data.vcd_vcenter.vc", "id", regexp.MustCompile("^urn:vcloud:vimserver:.*")),
resource.TestCheckResourceAttrSet("data.vcd_vcenter.vc", "vcenter_version"),
resource.TestCheckResourceAttrSet("data.vcd_vcenter.vc", "vcenter_host"),
resource.TestCheckResourceAttrSet("data.vcd_vcenter.vc", "status"),
resource.TestCheckResourceAttr("data.vcd_vcenter.vc", "status", "READY"),
resource.TestCheckResourceAttrSet("data.vcd_vcenter.vc", "is_enabled"),
resource.TestCheckResourceAttrSet("data.vcd_vcenter.vc", "connection_status"),
),
Expand Down
11 changes: 11 additions & 0 deletions vcd/resource_vcd_vcenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ func setTmVcenterData(d *schema.ResourceData, v *govcd.VCenter) error {
}
dSet(d, "vcenter_host", host.Host)

// Status is a derivative value that was present in XML Query API, but is no longer maintained
// The value was derived from multiple fields based on a complex logic. Instead, evaluating if
// vCenter is ready for operations, would be to rely on `is_enabled`, `is_connected` and
// optionally `cluster_health_status` fields.
//
// The `status` is a rough approximation of this value
dSet(d, "status", "NOT_READY")
if v.VSphereVCenter.IsConnected && v.VSphereVCenter.ListenerState == "CONNECTED" {
dSet(d, "status", "READY")
}

d.SetId(v.VSphereVCenter.VcId)

return nil
Expand Down
1 change: 1 addition & 0 deletions vcd/resource_vcd_vcenter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func TestAccVcdVcenter(t *testing.T) {
resource.TestCheckResourceAttrSet("vcd_vcenter.test", "mode"),
resource.TestCheckResourceAttrSet("vcd_vcenter.test", "uuid"),
resource.TestCheckResourceAttrSet("vcd_vcenter.test", "vcenter_version"),
resource.TestCheckResourceAttr("vcd_vcenter.test", "status", "READY"),
),
},
{
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/vcenter.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ The following attributes are exported on this resource:
* `version` - vCenter version
* `uuid` - UUID of vCenter
* `vcenter_host` - Host of Vcenter server
* `status` - Status can be `READY` or `NOT_READY`. It is a derivative field of `is_connected` and
`connection_status` so relying on those fields could be more precise.

## Importing

Expand Down

0 comments on commit 2996229

Please sign in to comment.