Skip to content

Commit

Permalink
Transport node realization data source
Browse files Browse the repository at this point in the history
Realization data source for nsxt_transport_node resource.

Signed-off-by: Kobi Samoray <[email protected]>
  • Loading branch information
ksamoray committed Aug 30, 2023
1 parent 56f6b53 commit 0888478
Show file tree
Hide file tree
Showing 20 changed files with 1,985 additions and 1 deletion.
96 changes: 96 additions & 0 deletions nsxt/data_source_nsxt_transport_node_realization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* Copyright © 2023 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */

package nsxt

import (
"fmt"
"log"
"time"

"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-mp/nsx/model"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/transport_nodes"
)

func dataSourceNsxtTransportNodeRealization() *schema.Resource {
return &schema.Resource{
Read: dataSourceNsxtTransportNodeRealizationRead,

Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
Description: "Unique ID of this resource",
},
"timeout": {
Type: schema.TypeInt,
Description: "Realization timeout in seconds",
Optional: true,
Default: 1200,
ValidateFunc: validation.IntAtLeast(1),
},
"delay": {
Type: schema.TypeInt,
Description: "Initial delay to start realization checks in seconds",
Optional: true,
Default: 1,
ValidateFunc: validation.IntAtLeast(0),
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: "Overall state of desired configuration",
},
},
}
}

func dataSourceNsxtTransportNodeRealizationRead(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)
client := transport_nodes.NewStateClient(connector)

id := d.Get("id").(string)
delay := d.Get("delay").(int)
timeout := d.Get("timeout").(int)

pendingStates := []string{
model.TransportNodeState_STATE_PENDING,
model.TransportNodeState_STATE_IN_PROGRESS,
model.TransportNodeState_STATE_IN_SYNC,
model.TransportNodeState_STATE_UNKNOWN,
}
targetStates := []string{
model.TransportNodeState_STATE_SUCCESS,
model.TransportNodeState_STATE_FAILED,
model.TransportNodeState_STATE_PARTIAL_SUCCESS,
model.TransportNodeState_STATE_ORPHANED,
model.TransportNodeState_STATE_ERROR,
}
stateConf := &resource.StateChangeConf{
Pending: pendingStates,
Target: targetStates,
Refresh: func() (interface{}, string, error) {
state, err := client.Get(id)
if err != nil {
return state, model.TransportNodeState_STATE_ERROR, logAPIError("Error while waiting for realization of transport node", err)
}

log.Printf("[DEBUG] Current realization state for Transport Node %s is %s", id, *state.State)

d.Set("state", state.State)
return state, *state.State, nil
},
Timeout: time.Duration(timeout) * time.Second,
MinTimeout: 1 * time.Second,
Delay: time.Duration(delay) * time.Second,
}
_, err := stateConf.WaitForState()
if err != nil {
return fmt.Errorf("failed to get realization information for %s: %v", id, err)
}

return nil
}
1 change: 1 addition & 0 deletions nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ func Provider() *schema.Provider {
"nsxt_policy_gateway_prefix_list": dataSourceNsxtPolicyGatewayPrefixList(),
"nsxt_policy_gateway_route_map": dataSourceNsxtPolicyGatewayRouteMap(),
"nsxt_uplink_host_switch_profile": dataSourceNsxtUplinkHostSwitchProfile(),
"nsxt_transport_node_realization": dataSourceNsxtTransportNodeRealization(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0888478

Please sign in to comment.