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 a427082
Showing 1 changed file with 20 additions and 30 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

0 comments on commit a427082

Please sign in to comment.