diff --git a/nsxt/data_source_nsxt_compute_manager_realization.go b/nsxt/data_source_nsxt_compute_manager_realization.go new file mode 100644 index 000000000..3e8ecd49d --- /dev/null +++ b/nsxt/data_source_nsxt_compute_manager_realization.go @@ -0,0 +1,163 @@ +/* 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/runtime/protocol/client" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/fabric/compute_managers" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/model" +) + +func dataSourceNsxtComputeManagerRealization() *schema.Resource { + return &schema.Resource{ + Read: dataSourceNsxtComputeManagerRealizationRead, + + 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", + }, + "check_registration": { + Type: schema.TypeBool, + Optional: true, + Default: true, + Description: "Check if registration of compute manager is complete", + }, + "registration_status": { + Type: schema.TypeString, + Computed: true, + Description: "Overall registration status of desired configuration", + }, + }, + } +} + +func dataSourceNsxtComputeManagerRealizationRead(d *schema.ResourceData, m interface{}) error { + connector := getPolicyConnector(m) + + checkRegistration := d.Get("check_registration").(bool) + + err := dataSourceNsxtComputeManagerRealizationWait(d, connector) + + if !checkRegistration { + return err + } + + return dataSourceNsxtComputeManagerRegistrationWait(d, connector) +} + +func dataSourceNsxtComputeManagerRealizationWait(d *schema.ResourceData, connector client.Connector) error { + id := d.Get("id").(string) + delay := d.Get("delay").(int) + timeout := d.Get("timeout").(int) + + client := compute_managers.NewStateClient(connector) + + pendingStates := []string{ + model.ConfigurationState_STATE_PENDING, + model.ConfigurationState_STATE_IN_PROGRESS, + model.ConfigurationState_STATE_UNKNOWN, + model.ConfigurationState_STATE_IN_SYNC, + } + targetStates := []string{ + model.ConfigurationState_STATE_SUCCESS, + model.ConfigurationState_STATE_FAILED, + model.ConfigurationState_STATE_PARTIAL_SUCCESS, + model.ConfigurationState_STATE_ORPHANED, + model.ConfigurationState_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.ConfigurationState_STATE_ERROR, logAPIError("Error while waiting for realization of Compute Manager", err) + } + + log.Printf("[DEBUG] Current realization state for Compute Manager %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 +} + +func dataSourceNsxtComputeManagerRegistrationWait(d *schema.ResourceData, connector client.Connector) error { + id := d.Get("id").(string) + delay := d.Get("delay").(int) + timeout := d.Get("timeout").(int) + + client := compute_managers.NewStatusClient(connector) + + pendingStates := []string{ + model.ComputeManagerStatus_CONNECTION_STATUS_CONNECTING, + model.ComputeManagerStatus_REGISTRATION_STATUS_REGISTERING, + } + targetStates := []string{ + model.ComputeManagerStatus_REGISTRATION_STATUS_REGISTERED, + model.ComputeManagerStatus_REGISTRATION_STATUS_UNREGISTERED, + model.ComputeManagerStatus_REGISTRATION_STATUS_REGISTERED_WITH_ERRORS, + } + + stateConf := &resource.StateChangeConf{ + Pending: pendingStates, + Target: targetStates, + Refresh: func() (interface{}, string, error) { + status, err := client.Get(id) + if err != nil { + return status, model.ComputeManagerStatus_REGISTRATION_STATUS_REGISTERED_WITH_ERRORS, logAPIError("Error while waiting for realization of Compute Manager", err) + } + + log.Printf("[DEBUG] Current registration status for Compute Manager %s is %s", id, *status.RegistrationStatus) + + d.Set("registration_status", status.RegistrationStatus) + return status, *status.RegistrationStatus, 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 registration information for %s: %v", id, err) + } + return nil +} diff --git a/nsxt/data_source_nsxt_transport_node_realization.go b/nsxt/data_source_nsxt_transport_node_realization.go index cc4eecb47..d21b4d38d 100644 --- a/nsxt/data_source_nsxt_transport_node_realization.go +++ b/nsxt/data_source_nsxt_transport_node_realization.go @@ -75,7 +75,7 @@ func dataSourceNsxtTransportNodeRealizationRead(d *schema.ResourceData, m interf 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) + 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) diff --git a/nsxt/provider.go b/nsxt/provider.go index ed2dece89..aecf0a78b 100644 --- a/nsxt/provider.go +++ b/nsxt/provider.go @@ -296,6 +296,7 @@ func Provider() *schema.Provider { "nsxt_transport_node_realization": dataSourceNsxtTransportNodeRealization(), "nsxt_failure_domain": dataSourceNsxtFailureDomain(), "nsxt_compute_collection": dataSourceNsxtComputeCollection(), + "nsxt_compute_manager_realization": dataSourceNsxtComputeManagerRealization(), }, ResourcesMap: map[string]*schema.Resource{ diff --git a/website/docs/d/compute_manager_realization.html.markdown b/website/docs/d/compute_manager_realization.html.markdown new file mode 100644 index 000000000..f19614690 --- /dev/null +++ b/website/docs/d/compute_manager_realization.html.markdown @@ -0,0 +1,52 @@ +--- +subcategory: "Realization" +layout: "nsxt" +page_title: "NSXT: compute_manager_realization" +description: Compute manager resource realization information. +--- + +# nsxt_compute_manager_realization + +This data source provides information about the realization of a compute manager 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 compute manager realization. + +## Example Usage + +```hcl +resource "nsxt_compute_manager" "test" { + description = "Terraform provisioned Compute Manager" + display_name = "test" + tag { + scope = "scope1" + tag = "tag1" + } + + server = "192.168.244.144" + + credential { + username_password_login { + username = "user" + password = "pass" + } + } + origin_type = "vCenter" +} + +data "nsxt_compute_manager_realization" "test" { + id = nsxt_compute_manager.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. +* `check_registration` - (Optional) Check if registration of compute manager is complete. + +## 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". +* `registration_status` - Overall registration status of desired configuration. Transitional statuses are "CONNECTING", "REGISTERING". Target statuses are: "REGISTERED", "UNREGISTERED", "REGISTERED_WITH_ERRORS" \ No newline at end of file diff --git a/website/docs/r/policy_transport_zone.html.markdown b/website/docs/r/policy_transport_zone.html.markdown index cb11d1a6a..df05c7679 100644 --- a/website/docs/r/policy_transport_zone.html.markdown +++ b/website/docs/r/policy_transport_zone.html.markdown @@ -9,7 +9,7 @@ description: A resource to configure Policy Transport Zone. This resource provides a method for the management of Policy based Transport Zones (TZ). A Transport Zone defines the scope to which a network can extend in NSX. For example an overlay based Transport Zone is associated with both hypervisors and segments and defines which hypervisors will be able to serve the defined segment. Virtual machines on the hypervisor associated with a Transport Zone can be attached to segments in that same Transport Zone. -This data source is applicable to NSX Policy Manager. +This resource is applicable to NSX Policy Manager. ## Example Usage