From 48147d56aedd81d392b39704a89aaed6c7274798 Mon Sep 17 00:00:00 2001 From: graysonwu Date: Thu, 4 Jan 2024 14:11:08 -0800 Subject: [PATCH 1/2] Add upgradeGroup data source Signed-off-by: graysonwu --- nsxt/data_source_nsxt_edge_upgrade_group.go | 147 ++++++++++++++++++ nsxt/data_source_nsxt_host_upgrade_group.go | 29 ++++ nsxt/provider.go | 2 + .../docs/d/edge_upgrade_group.html.markdown | 31 ++++ .../docs/d/host_upgrade_group.html.markdown | 31 ++++ 5 files changed, 240 insertions(+) create mode 100644 nsxt/data_source_nsxt_edge_upgrade_group.go create mode 100644 nsxt/data_source_nsxt_host_upgrade_group.go create mode 100644 website/docs/d/edge_upgrade_group.html.markdown create mode 100644 website/docs/d/host_upgrade_group.html.markdown diff --git a/nsxt/data_source_nsxt_edge_upgrade_group.go b/nsxt/data_source_nsxt_edge_upgrade_group.go new file mode 100644 index 000000000..06cc045b0 --- /dev/null +++ b/nsxt/data_source_nsxt_edge_upgrade_group.go @@ -0,0 +1,147 @@ +/* Copyright © 2024 VMware, Inc. All Rights Reserved. + SPDX-License-Identifier: MPL-2.0 */ + +package nsxt + +import ( + "fmt" + "strings" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/model" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/upgrade" +) + +var ( + edgeUpgradeGroup = "EDGE" + hostUpgradeGroup = "HOST" + + timeoutUUGCreat = 1 * time.Minute + intervalUUGCreat = 5 * time.Second +) + +func dataSourceNsxtEdgeUpgradeGroup() *schema.Resource { + return &schema.Resource{ + Read: dataSourceNsxtEdgeUpgradeGroupRead, + + Schema: map[string]*schema.Schema{ + "upgrade_prepare_id": { + Type: schema.TypeString, + Description: "ID of corresponding nsxt_upgrade_prepare resource", + Required: true, + }, + "id": getDataSourceIDSchema(), + "display_name": getDataSourceDisplayNameSchema(), + "description": getDataSourceDescriptionSchema(), + }, + } +} + +func dataSourceNsxtEdgeUpgradeGroupRead(d *schema.ResourceData, m interface{}) error { + return upgradeGroupRead(d, m, edgeUpgradeGroup) +} + +func upgradeGroupRead(d *schema.ResourceData, m interface{}, groupType string) error { + connector := getPolicyConnector(m) + client := upgrade.NewUpgradeUnitGroupsClient(connector) + + err := waitUpgradeGroupCreate(client, groupType) + if err != nil { + return fmt.Errorf("unable to fetch UpgradeUnitGroup: %v", err) + } + + objID := d.Get("id").(string) + objName := d.Get("display_name").(string) + + var obj model.UpgradeUnitGroup + if objID != "" { + // Get by id + objGet, err := client.Get(objID, nil) + if isNotFoundError(err) { + return fmt.Errorf("%s UpgradeUnitGroup with ID %s was not found", groupType, objID) + } + + if err != nil { + return fmt.Errorf("error while reading %s UpgradeUnitGroup %s: %v", groupType, objID, err) + } + + if *objGet.Type_ != groupType { + return fmt.Errorf("%s UpgradeUnitGroup with ID %s was not found", groupType, objID) + } + obj = objGet + + } else if objName == "" { + return fmt.Errorf("error obtaining %s UpgradeUnitGroup ID or name during read", groupType) + } else { + // Get by full name/prefix + objList, err := client.List(&groupType, nil, nil, nil, nil, nil, nil, nil) + if err != nil { + return fmt.Errorf("error while reading %s UpgradeUnitGroup: %v", groupType, err) + } + // go over the list to find the correct one (prefer a perfect match. If not - prefix match) + var perfectMatch []model.UpgradeUnitGroup + var prefixMatch []model.UpgradeUnitGroup + for _, objInList := range objList.Results { + if strings.HasPrefix(*objInList.DisplayName, objName) { + prefixMatch = append(prefixMatch, objInList) + } + if *objInList.DisplayName == objName { + perfectMatch = append(perfectMatch, objInList) + } + } + if len(perfectMatch) > 0 { + if len(perfectMatch) > 1 { + return fmt.Errorf("found multiple %s UpgradeUnitGroup with name '%s'", groupType, objName) + } + obj = perfectMatch[0] + } else if len(prefixMatch) > 0 { + if len(prefixMatch) > 1 { + return fmt.Errorf("found multiple %s UpgradeUnitGroup with name starting with '%s'", groupType, objName) + } + obj = prefixMatch[0] + } else { + return fmt.Errorf("%s UpgradeUnitGroup with name '%s' was not found", groupType, objName) + } + } + + d.SetId(*obj.Id) + d.Set("display_name", obj.DisplayName) + d.Set("description", obj.Description) + + return nil +} + +func waitUpgradeGroupCreate(client upgrade.UpgradeUnitGroupsClient, groupType string) error { + + resultChan := make(chan error, 1) + + poll := func(resultChan chan error) { + preLen := 0 + for { + objList, err := client.List(&groupType, nil, nil, nil, nil, nil, nil, nil) + if err != nil && !isNotFoundError(err) { + resultChan <- err + return + } + if err == nil && len(objList.Results) > 0 { + if preLen == 0 { + preLen = len(objList.Results) + } else if preLen == len(objList.Results) { + resultChan <- nil + return + } + } + time.Sleep(intervalUUGCreat) + } + } + + go poll(resultChan) + + select { + case res := <-resultChan: + return res + case <-time.After(timeoutUUGCreat): + return fmt.Errorf("timeout reached") + } +} diff --git a/nsxt/data_source_nsxt_host_upgrade_group.go b/nsxt/data_source_nsxt_host_upgrade_group.go new file mode 100644 index 000000000..92c997024 --- /dev/null +++ b/nsxt/data_source_nsxt_host_upgrade_group.go @@ -0,0 +1,29 @@ +/* Copyright © 2024 VMware, Inc. All Rights Reserved. + SPDX-License-Identifier: MPL-2.0 */ + +package nsxt + +import ( + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceNsxtHostUpgradeGroup() *schema.Resource { + return &schema.Resource{ + Read: dataSourceNsxtHostUpgradeGroupRead, + + Schema: map[string]*schema.Schema{ + "upgrade_prepare_id": { + Type: schema.TypeString, + Description: "ID of corresponding nsxt_upgrade_prepare resource", + Required: true, + }, + "id": getDataSourceIDSchema(), + "display_name": getDataSourceDisplayNameSchema(), + "description": getDataSourceDescriptionSchema(), + }, + } +} + +func dataSourceNsxtHostUpgradeGroupRead(d *schema.ResourceData, m interface{}) error { + return upgradeGroupRead(d, m, hostUpgradeGroup) +} diff --git a/nsxt/provider.go b/nsxt/provider.go index cd8ca7e96..28a5b8222 100644 --- a/nsxt/provider.go +++ b/nsxt/provider.go @@ -314,6 +314,8 @@ func Provider() *schema.Provider { "nsxt_policy_host_transport_node_profile": dataSourceNsxtPolicyHostTransportNodeProfile(), "nsxt_transport_node": dataSourceNsxtEdgeTransportNode(), "nsxt_discover_node": dataSourceNsxtDiscoverNode(), + "nsxt_edge_upgrade_group": dataSourceNsxtEdgeUpgradeGroup(), + "nsxt_host_upgrade_group": dataSourceNsxtHostUpgradeGroup(), }, ResourcesMap: map[string]*schema.Resource{ diff --git a/website/docs/d/edge_upgrade_group.html.markdown b/website/docs/d/edge_upgrade_group.html.markdown new file mode 100644 index 000000000..6f8200fb4 --- /dev/null +++ b/website/docs/d/edge_upgrade_group.html.markdown @@ -0,0 +1,31 @@ +--- +subcategory: "Beta" +layout: "nsxt" +page_title: "NSXT: edge_upgrade_group" +description: An Edge Upgrade Group data source. +--- + +# nsxt_edge_upgrade_group + +This data source provides information about an Edge Upgrade Group configured on NSX. + +## Example Usage + +```hcl +data "nsxt_edge_upgrade_group" "test" { + upgrade_prepare_id = nsxt_upgrade_prepare.test.id + display_name = "edgeupgradegroup1" +} +``` + +## Argument Reference + +* `upgrade_prepare_id` - (Required) ID of corresponding `nsxt_upgrade_prepare` resource. +* `id` - (Optional) The ID of Edge Upgrade Group to retrieve +* `display_name` - (Optional) The Display Name of the Edge Upgrade Group to retrieve. + +## Attributes Reference + +In addition to arguments listed above, the following attributes are exported: + +* `description` - The description of the resource. diff --git a/website/docs/d/host_upgrade_group.html.markdown b/website/docs/d/host_upgrade_group.html.markdown new file mode 100644 index 000000000..8a3c2141f --- /dev/null +++ b/website/docs/d/host_upgrade_group.html.markdown @@ -0,0 +1,31 @@ +--- +subcategory: "Beta" +layout: "nsxt" +page_title: "NSXT: host_upgrade_group" +description: A Host Upgrade Group data source. +--- + +# nsxt_host_upgrade_group + +This data source provides information about a Host Upgrade Group configured on NSX. + +## Example Usage + +```hcl +data "nsxt_host_upgrade_group" "test" { + upgrade_prepare_id = nsxt_upgrade_prepare.test.id + display_name = "hostupgradegroup1" +} +``` + +## Argument Reference + +* `upgrade_prepare_id` - (Required) ID of corresponding `nsxt_upgrade_prepare` resource. +* `id` - (Optional) The ID of Host Upgrade Group to retrieve +* `display_name` - (Optional) The Display Name of the Host Upgrade Group to retrieve. + +## Attributes Reference + +In addition to arguments listed above, the following attributes are exported: + +* `description` - The description of the resource. From 43b38504fe991e8579598caab2456a43be9d2f59 Mon Sep 17 00:00:00 2001 From: graysonwu Date: Tue, 6 Feb 2024 07:13:13 -0800 Subject: [PATCH 2/2] Use waitForState Signed-off-by: graysonwu --- nsxt/data_source_nsxt_edge_upgrade_group.go | 43 --------------------- 1 file changed, 43 deletions(-) diff --git a/nsxt/data_source_nsxt_edge_upgrade_group.go b/nsxt/data_source_nsxt_edge_upgrade_group.go index 06cc045b0..efce21d90 100644 --- a/nsxt/data_source_nsxt_edge_upgrade_group.go +++ b/nsxt/data_source_nsxt_edge_upgrade_group.go @@ -6,7 +6,6 @@ package nsxt import ( "fmt" "strings" - "time" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/model" @@ -16,9 +15,6 @@ import ( var ( edgeUpgradeGroup = "EDGE" hostUpgradeGroup = "HOST" - - timeoutUUGCreat = 1 * time.Minute - intervalUUGCreat = 5 * time.Second ) func dataSourceNsxtEdgeUpgradeGroup() *schema.Resource { @@ -46,11 +42,6 @@ func upgradeGroupRead(d *schema.ResourceData, m interface{}, groupType string) e connector := getPolicyConnector(m) client := upgrade.NewUpgradeUnitGroupsClient(connector) - err := waitUpgradeGroupCreate(client, groupType) - if err != nil { - return fmt.Errorf("unable to fetch UpgradeUnitGroup: %v", err) - } - objID := d.Get("id").(string) objName := d.Get("display_name").(string) @@ -111,37 +102,3 @@ func upgradeGroupRead(d *schema.ResourceData, m interface{}, groupType string) e return nil } - -func waitUpgradeGroupCreate(client upgrade.UpgradeUnitGroupsClient, groupType string) error { - - resultChan := make(chan error, 1) - - poll := func(resultChan chan error) { - preLen := 0 - for { - objList, err := client.List(&groupType, nil, nil, nil, nil, nil, nil, nil) - if err != nil && !isNotFoundError(err) { - resultChan <- err - return - } - if err == nil && len(objList.Results) > 0 { - if preLen == 0 { - preLen = len(objList.Results) - } else if preLen == len(objList.Results) { - resultChan <- nil - return - } - } - time.Sleep(intervalUUGCreat) - } - } - - go poll(resultChan) - - select { - case res := <-resultChan: - return res - case <-time.After(timeoutUUGCreat): - return fmt.Errorf("timeout reached") - } -}