Skip to content

Commit

Permalink
Add Upgrade Run Resource
Browse files Browse the repository at this point in the history
Signed-off-by: graysonwu <[email protected]>
  • Loading branch information
GraysonWu committed Feb 8, 2024
1 parent 1174ffd commit fbb6766
Show file tree
Hide file tree
Showing 7 changed files with 947 additions and 0 deletions.
1 change: 1 addition & 0 deletions nsxt/data_source_nsxt_edge_upgrade_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
var (
edgeUpgradeGroup = "EDGE"
hostUpgradeGroup = "HOST"
mpUpgradeGroup = "MP"
)

func dataSourceNsxtEdgeUpgradeGroup() *schema.Resource {
Expand Down
155 changes: 155 additions & 0 deletions nsxt/data_source_nsxt_upgrade_postcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/* Copyright © 2024 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */

package nsxt

import (
"fmt"
"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/upgrade/upgrade_unit_groups"
)

var (
// Default waiting setup in seconds
defaultUpgradePostcheckInterval = 30
defaultUpgradePostcheckTimeout = 1800
defaultUpgradePostcheckDelay = 30
)

func dataSourceNsxtUpgradePostCheck() *schema.Resource {
return &schema.Resource{
Read: dataSourceNsxtUpgradePostCheckRead,

Schema: map[string]*schema.Schema{
"upgrade_run_id": {
Type: schema.TypeString,
Description: "ID of corresponding nsxt_upgrade_run resource",
Required: true,
},
"type": {
Type: schema.TypeString,
Description: "Component Type",
Required: true,
ValidateFunc: validation.StringInSlice([]string{edgeUpgradeGroup, hostUpgradeGroup}, false),
},
"timeout": {
Type: schema.TypeInt,
Description: "Post-check status checks timeout in seconds",
Optional: true,
Default: defaultUpgradePostcheckTimeout,
ValidateFunc: validation.IntAtLeast(1),
},
"interval": {
Type: schema.TypeInt,
Description: "Interval to check Post-check status in seconds",
Optional: true,
Default: defaultUpgradePostcheckInterval,
ValidateFunc: validation.IntAtLeast(1),
},
"delay": {
Type: schema.TypeInt,
Description: "Initial delay to start Post-check status checks in seconds",
Optional: true,
Default: defaultUpgradePostcheckDelay,
ValidateFunc: validation.IntAtLeast(0),
},
"failed_group": {
Type: schema.TypeList,
Description: "Upgrade unit groups that failed in upgrade post-checks",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "Unique identifier of upgrade unit group",
Required: true,
},
"status": {
Type: schema.TypeString,
Description: "Status of execution of upgrade post-checks",
Computed: true,
},
"details": {
Type: schema.TypeString,
Description: "Details about current execution of upgrade post-checks",
Computed: true,
},
"failure_count": {
Type: schema.TypeInt,
Description: "Total count of generated failures or warnings in last execution of upgrade post-checks",
Computed: true,
},
},
},
},
},
}
}

func dataSourceNsxtUpgradePostCheckRead(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)
aggregateInfoClient := upgrade_unit_groups.NewAggregateInfoClient(connector)
component := d.Get("type").(string)
timeout := d.Get("timeout").(int)
interval := d.Get("interval").(int)
delay := d.Get("delay").(int)

stateConf := &resource.StateChangeConf{
Pending: []string{model.UpgradeChecksExecutionStatus_STATUS_IN_PROGRESS},
Target: []string{model.UpgradeChecksExecutionStatus_STATUS_COMPLETED},
Refresh: func() (interface{}, string, error) {
listRes, err := aggregateInfoClient.List(&component, nil, nil, nil, nil, nil, nil, nil)
if err != nil {
return nil, "", fmt.Errorf("failed to retrieve %s upgrade post-check results: %v", component, err)
}
for _, res := range listRes.Results {
if *res.PostUpgradeStatus.Status == model.UpgradeChecksExecutionStatus_STATUS_IN_PROGRESS {
return res.PostUpgradeStatus, model.UpgradeChecksExecutionStatus_STATUS_IN_PROGRESS, nil
}
}
// Return UpgradeChecksExecutionStatus_STATUS_COMPLETED here doesn't mean all UpgradeUnitGroup post-checks are completed.
// Just to stop polling the states as none of UpgradeUnitGroup is in in_progress state.
return listRes, model.UpgradeChecksExecutionStatus_STATUS_COMPLETED, nil
},
Timeout: time.Duration(timeout) * time.Second,
PollInterval: time.Duration(interval) * time.Second,
Delay: time.Duration(delay) * time.Second,
}

_, err := stateConf.WaitForState()
if err != nil {
return err
}

listRes, err := aggregateInfoClient.List(&component, nil, nil, nil, nil, nil, nil, nil)
if err != nil {
return fmt.Errorf("failed to retrieve %s upgrade post-check result: %v", component, err)
}
var failedGroup []map[string]interface{}
for _, res := range listRes.Results {
failureCounts := 0
if res.PostUpgradeStatus.FailureCount != nil {
failureCounts = int(*res.PostUpgradeStatus.FailureCount)
} else if res.PostUpgradeStatus.NodeWithIssuesCount != nil {
failureCounts = int(*res.PostUpgradeStatus.NodeWithIssuesCount)
}
if *res.PostUpgradeStatus.Status != model.UpgradeChecksExecutionStatus_STATUS_COMPLETED || failureCounts > 0 {
elem := make(map[string]interface{}, 4)
elem["id"] = *res.Id
elem["status"] = *res.PostUpgradeStatus.Status
elem["details"] = *res.PostUpgradeStatus.Details
elem["failure_count"] = failureCounts
failedGroup = append(failedGroup, elem)
}
}
if len(failedGroup) > 0 {
d.Set("failed_group", failedGroup)
}

return nil
}
7 changes: 7 additions & 0 deletions nsxt/policy_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ func isNotFoundError(err error) bool {
return false
}

func isServiceUnavailableError(err error) bool {
if _, ok := err.(errors.ServiceUnavailable); ok {
return true
}
return false
}

func handleCreateError(resourceType string, resource string, err error) error {
msg := fmt.Sprintf("Failed to create %s %s", resourceType, resource)
return logAPIError(msg, err)
Expand Down
2 changes: 2 additions & 0 deletions nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ func Provider() *schema.Provider {
"nsxt_discover_node": dataSourceNsxtDiscoverNode(),
"nsxt_edge_upgrade_group": dataSourceNsxtEdgeUpgradeGroup(),
"nsxt_host_upgrade_group": dataSourceNsxtHostUpgradeGroup(),
"nsxt_upgrade_postcheck": dataSourceNsxtUpgradePostCheck(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -469,6 +470,7 @@ func Provider() *schema.Provider {
"nsxt_policy_lb_passive_monitor_profile": resourceNsxtPolicyLBPassiveMonitorProfile(),
"nsxt_policy_lb_tcp_monitor_profile": resourceNsxtPolicyLBTcpMonitorProfile(),
"nsxt_policy_lb_udp_monitor_profile": resourceNsxtPolicyLBUdpMonitorProfile(),
"nsxt_upgrade_run": resourceNsxtUpgradeRun(),
},

ConfigureFunc: providerConfigure,
Expand Down
Loading

0 comments on commit fbb6766

Please sign in to comment.