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 Sep 6, 2023
1 parent fffa55a commit 63074c9
Show file tree
Hide file tree
Showing 4 changed files with 173 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
75 changes: 75 additions & 0 deletions website/docs/d/transport_node_realization.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
subcategory: "Realization"
layout: "nsxt"
page_title: "NSXT: transport_node_realization"
description: Transport node resource realization information.
---

# nsxt_transport_node_realization

This data source provides information about the realization of a transport node resource on NSX manager. 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 transport node realization.

## Example Usage

```hcl
resource "nsxt_transport_node" "test" {
description = "Terraform-deployed edge node"
display_name = "tf_edge_node"
standard_host_switch {
host_switch_mode = "STANDARD"
host_switch_type = "NVDS"
ip_assignment {
static_ip_pool_id = data.nsxt_ip_pool.ipp1.id
}
transport_zone_endpoint {
transport_zone_id = data.nsxt_transport_zone.tz1.id
transport_zone_profile_id = ["52035bb3-ab02-4a08-9884-18631312e50a"]
}
host_switch_profile_id = [nsxt_uplink_host_switch_profile.hsw_profile1.id]
is_migrate_pnics = false
pnics {
device_name = "fp-eth0"
uplink_name = "uplink1"
}
}
edge_node {
deployment_config {
form_factor = "SMALL"
node_user_settings {
cli_password = "some_cli_password"
root_password = "some_other_password"
}
vm_deployment_config {
management_network_id = data.vsphere_network.network1.id
data_network_ids = [data.vsphere_network.network1.id]
compute_id = data.vsphere_compute_cluster.compute_cluster1.id
storage_id = data.vsphere_datastore.datastore1.id
vc_id = nsxt_compute_manager.vc1.id
host_id = data.vsphere_host.host1.id
}
}
node_settings {
hostname = "tf_edge_node"
allow_ssh_root_login = true
enable_ssh = true
}
}
}
data "nsxt_transport_node_realization" "test" {
id = nsxt_transport_node.test.id
timeout = 60
}
```

## Argument Reference

* `id` - (Required) ID of the resource.
* `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.

## Attributes Reference

In addition to arguments listed above, the following attributes are exported:

* `state` - The realization state of the resource. Transitional states are: "pending", "in_progress", "in_sync", "unknown". Target states are: "success", "failed", "partial_success", "orphaned", "error".
2 changes: 1 addition & 1 deletion website/docs/r/manager_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ The following arguments are supported:

## Importing

Importing is not supported for this resource.
Importing is not supported for this resource.

0 comments on commit 63074c9

Please sign in to comment.