Skip to content

Commit

Permalink
Change input of TNC realization from id to path
Browse files Browse the repository at this point in the history
Since corresponding resource is policy resource, it makes more sense
to use path and remove the need for site_path parameter, which is part
of the path.

In addition, make sure the error returned is never empty.

Signed-off-by: Anna Khmelnitsky <[email protected]>
  • Loading branch information
annakhm committed Apr 11, 2024
1 parent 4946ad7 commit 624d70a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/sites/enforcement_points"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/sites/enforcement_points/transport_node_collections"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
)
Expand All @@ -21,7 +20,12 @@ func dataSourceNsxtPolicyHostTransportNodeCollectionRealization() *schema.Resour
Read: dataSourceNsxtPolicyHostTransportNodeCollectionRealizationRead,

Schema: map[string]*schema.Schema{
"id": getDataSourceIDSchema(),
"path": {
Type: schema.TypeString,
Description: "Path of this Transport Node Collection",
Optional: true,
ValidateFunc: validatePolicyPath(),
},
"timeout": {
Type: schema.TypeInt,
Description: "Realization timeout in seconds",
Expand All @@ -36,12 +40,6 @@ func dataSourceNsxtPolicyHostTransportNodeCollectionRealization() *schema.Resour
Default: 1,
ValidateFunc: validation.IntAtLeast(0),
},
"site_path": {
Type: schema.TypeString,
Description: "Path of the site this Transport Node Collection belongs to",
Optional: true,
ValidateFunc: validatePolicyPath(),
},
"state": {
Type: schema.TypeString,
Description: "Application state of transport node profile on compute collection",
Expand All @@ -58,11 +56,9 @@ func dataSourceNsxtPolicyHostTransportNodeCollectionRealizationRead(d *schema.Re
connector := getPolicyConnector(m)
client := transport_node_collections.NewStateClient(connector)

objID := d.Get("id").(string)
path := d.Get("path").(string)
delay := d.Get("delay").(int)
timeout := d.Get("timeout").(int)
objSitePath := d.Get("site_path").(string)
objPolicyEnforcementPoint := getPolicyEnforcementPoint(m)

pendingStates := []string{model.TransportNodeCollectionState_STATE_IN_PROGRESS}
targetStates := []string{
Expand All @@ -72,31 +68,25 @@ func dataSourceNsxtPolicyHostTransportNodeCollectionRealizationRead(d *schema.Re
model.TransportNodeCollectionState_STATE_SUCCESS,
}

// For local manager, if site path is not provided, use default site
if objSitePath == "" {
objSitePath = defaultSite
site, err := getParameterFromPolicyPath("/sites/", "/enforcement-points/", path)
if err != nil {
return fmt.Errorf("Invalid transport node collection path %s", path)
}
if objID == "" {
// Find the host transport node collection if exists
tncClient := enforcement_points.NewTransportNodeCollectionsClient(connector)
objList, err := tncClient.List(objSitePath, objPolicyEnforcementPoint, nil, nil, nil)
if err != nil {
return handleListError("HostTransportNodeCollection", err)
}

if len(objList.Results) != 1 {
return fmt.Errorf("Single HostTransportNodeCollection was not identified on %s/%s", objSitePath, objPolicyEnforcementPoint)
}
objID = *objList.Results[0].Id

ep, err1 := getParameterFromPolicyPath("/enforcement-points/", "/transport-node-collections/", path)
if err1 != nil {
return fmt.Errorf("Invalid transport node collection path %s", path)
}

objID := getPolicyIDFromPath(path)

stateConf := &resource.StateChangeConf{
Pending: pendingStates,
Target: targetStates,
Refresh: func() (interface{}, string, error) {
state, err := client.Get(objSitePath, getPolicyEnforcementPoint(m), objID)
state, err := client.Get(site, ep, objID)
if err != nil {
return state, model.TransportNodeCollectionState_STATE_IN_PROGRESS, logAPIError("Error while waiting for realization of Transport Node Collection", err)
return state, model.TransportNodeCollectionState_STATE_IN_PROGRESS, logAPIError("Error getting collection state", err)
}

log.Printf("[DEBUG] Current realization state for Transport Node Collection %s is %s", objID, *state.State)
Expand All @@ -113,15 +103,15 @@ func dataSourceNsxtPolicyHostTransportNodeCollectionRealizationRead(d *schema.Re
MinTimeout: 1 * time.Second,
Delay: time.Duration(delay) * time.Second,
}
_, err := stateConf.WaitForState()
_, err = stateConf.WaitForState()
if err != nil {
return err
}
return nil
}

func getErrorFromState(state *model.TransportNodeCollectionState) error {
var result string
result := fmt.Sprintf("state: %s\n", *state.State)
if state.ClusterLevelError != nil {
result += fmt.Sprintf("cluster level error: %v\n", *state.ClusterLevelError)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,24 @@ description: Host transport node collection realization information.
# nsxt_policy_host_transport_node_collection_realization

This data source provides information about the realization of host transport
node collection configured on NSX. This data source will wait until realization is
determined as either success or error. It is recommended to use this data source
if further configuration depends on host transport node collection realization.
node collection configured on NSX. This data source will fail if transport node collection
fails to realize. It is recommended to use this data source if further configuration
depends on host transport node collection realization.
This data source is applicable to NSX Policy Manager.

## Example Usage

```hcl
data "nsxt_policy_host_transport_node_collection_realization" "test" {
id = data.nsxt_policy_host_transport_node_collection.id
path = data.nsxt_policy_host_transport_node_collection.path
}
```

## Argument Reference

* `id` - (Optional) The ID of host transport node collection to retrieve information.
* `path` - (Optional) Policy path of Transport Node Collection.
* `delay` - (Optional) Delay (in seconds) before realization polling is started. Default is set to 1.
* `timeout` - (Optional) Timeout (in seconds) for realization polling. Default is set to 1200.
* `site_path` - (Optional) The path of the site which the Transport Node Collection belongs to. `path` field of the existing `nsxt_policy_site` can be used here.

## Attributes Reference

Expand Down

0 comments on commit 624d70a

Please sign in to comment.