From 0b50ecce45965dab0d61153dc484c4d0d7965d42 Mon Sep 17 00:00:00 2001 From: Anna Khmelnitsky Date: Thu, 7 May 2020 17:31:32 -0700 Subject: [PATCH 1/3] Expose network_name in segment realization data This should make it more intuitive for users to define vsphere network based on this attribute, in which case implicit dependency will be created. With the dependency in place, vm resource will wait until network is realized. --- ...a_source_nsxt_policy_segment_realization.go | 18 ++++++++++++++++++ ...rce_nsxt_policy_segment_realization_test.go | 1 + .../d/policy_segment_realization.html.markdown | 1 + 3 files changed, 20 insertions(+) diff --git a/nsxt/data_source_nsxt_policy_segment_realization.go b/nsxt/data_source_nsxt_policy_segment_realization.go index 9ee93efe6..c0fe833a0 100644 --- a/nsxt/data_source_nsxt_policy_segment_realization.go +++ b/nsxt/data_source_nsxt_policy_segment_realization.go @@ -7,6 +7,7 @@ import ( "fmt" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra" "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/segments" "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" "time" @@ -29,6 +30,11 @@ func dataSourceNsxtPolicySegmentRealization() *schema.Resource { Description: "The state of the realized resource on hypervisors", Computed: true, }, + "network_name": { + Type: schema.TypeString, + Description: "Network name on the hypervisors", + Computed: true, + }, }, } } @@ -55,6 +61,7 @@ func dataSourceNsxtPolicySegmentRealizationRead(d *schema.ResourceData, m interf model.SegmentConfigurationState_STATE_IN_SYNC, model.SegmentConfigurationState_STATE_UNKNOWN} targetStates := []string{model.SegmentConfigurationState_STATE_SUCCESS, + model.SegmentConfigurationState_STATE_PARTIAL_SUCCESS, model.SegmentConfigurationState_STATE_FAILED, model.SegmentConfigurationState_STATE_ERROR, model.SegmentConfigurationState_STATE_ORPHANED} @@ -82,5 +89,16 @@ func dataSourceNsxtPolicySegmentRealizationRead(d *schema.ResourceData, m interf return fmt.Errorf("Failed to get realization information for %s: %v", path, err) } + // We need to fetch network name to use in vpshere provider. However, state API does not + // return it in details yet. For now, we'll use segment display name, since its always + // translates to network name + segClient := infra.NewDefaultSegmentsClient(connector) + obj, err := segClient.Get(segmentID) + if err != nil { + return handleReadError(d, "Segment", segmentID, err) + } + + d.Set("network_name", obj.DisplayName) + return nil } diff --git a/nsxt/data_source_nsxt_policy_segment_realization_test.go b/nsxt/data_source_nsxt_policy_segment_realization_test.go index 141697f04..903985c24 100644 --- a/nsxt/data_source_nsxt_policy_segment_realization_test.go +++ b/nsxt/data_source_nsxt_policy_segment_realization_test.go @@ -20,6 +20,7 @@ func testAccDataSourceNsxtPolicySegmentRealization(t *testing.T, vlan bool) { Config: testAccNsxtPolicySegmentRealizationTemplate(vlan), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(testResourceName, "state", "success"), + resource.TestCheckResourceAttr(testResourceName, "network_name", "terra-test"), resource.TestCheckResourceAttrSet(testResourceName, "path"), ), }, diff --git a/website/docs/d/policy_segment_realization.html.markdown b/website/docs/d/policy_segment_realization.html.markdown index 8837bb4b1..6d93977ed 100644 --- a/website/docs/d/policy_segment_realization.html.markdown +++ b/website/docs/d/policy_segment_realization.html.markdown @@ -34,3 +34,4 @@ data "nsxt_policy_segment_realization" "info" { In addition to arguments listed above, the following attributes are exported: * `state` - The realization state of the resource: `success`, `partial_success`, `orphaned`, `failed` or `error`. +* `network_name` - Network name on the hypervisor. This attribute can be used in vsphere provider in order to ensure implicit dependency on segment realization. From 6fbce85fe45b741ab78ba4b2050ed3bea58243e9 Mon Sep 17 00:00:00 2001 From: Anna Khmelnitsky Date: Fri, 8 May 2020 10:25:47 -0700 Subject: [PATCH 2/3] Add vsphere network sample for segment realization --- website/docs/d/policy_segment_realization.html.markdown | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/website/docs/d/policy_segment_realization.html.markdown b/website/docs/d/policy_segment_realization.html.markdown index 6d93977ed..4deee5ef4 100644 --- a/website/docs/d/policy_segment_realization.html.markdown +++ b/website/docs/d/policy_segment_realization.html.markdown @@ -20,9 +20,15 @@ resource "nsxt_policy_segment" "s1" { transport_zone_path = data.nsxt_transport_zone.tz1.path } -data "nsxt_policy_segment_realization" "info" { +data "nsxt_policy_segment_realization" "s1" { path = data.nsxt_policy_segment.s1.path } + +# usage in vsphere provider +data "vsphere_network" "net" { + name = nsxt_policy_segment_realization.s1.network_name + datacenter_id = data.vsphere_datacenter.datacenter.id +} ``` ## Argument Reference From 177663bf44d17265bc5189371748e6b3f2be98ae Mon Sep 17 00:00:00 2001 From: Anna Khmelnitsky Date: Fri, 8 May 2020 18:19:37 -0700 Subject: [PATCH 3/3] Remove accidental change --- nsxt/data_source_nsxt_policy_segment_realization.go | 1 - 1 file changed, 1 deletion(-) diff --git a/nsxt/data_source_nsxt_policy_segment_realization.go b/nsxt/data_source_nsxt_policy_segment_realization.go index c0fe833a0..6f23860bd 100644 --- a/nsxt/data_source_nsxt_policy_segment_realization.go +++ b/nsxt/data_source_nsxt_policy_segment_realization.go @@ -61,7 +61,6 @@ func dataSourceNsxtPolicySegmentRealizationRead(d *schema.ResourceData, m interf model.SegmentConfigurationState_STATE_IN_SYNC, model.SegmentConfigurationState_STATE_UNKNOWN} targetStates := []string{model.SegmentConfigurationState_STATE_SUCCESS, - model.SegmentConfigurationState_STATE_PARTIAL_SUCCESS, model.SegmentConfigurationState_STATE_FAILED, model.SegmentConfigurationState_STATE_ERROR, model.SegmentConfigurationState_STATE_ORPHANED}