Skip to content

Commit

Permalink
Resource to prepare for NSXT cluster upgrade
Browse files Browse the repository at this point in the history
Signed-off-by: Shizhao Liu <[email protected]>
  • Loading branch information
Shizhao Liu committed Feb 23, 2024
1 parent 863b555 commit 8ad37c7
Show file tree
Hide file tree
Showing 7 changed files with 821 additions and 0 deletions.
62 changes: 62 additions & 0 deletions nsxt/data_source_nsxt_upgrade_prepare_ready.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* Copyright © 2024 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */

package nsxt

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
nsxModel "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/model"
)

func dataSourceNsxtUpgradePrepareReady() *schema.Resource {
return &schema.Resource{
Read: dataSourceNsxtUpgradePrepareReadyRead,

Schema: map[string]*schema.Schema{
"id": getDataSourceIDSchema(),
"upgrade_prepare_id": {
Type: schema.TypeString,
Description: "ID of corresponding nsxt_upgrade_prepare resource",
Required: true,
},
},
}
}

func dataSourceNsxtUpgradePrepareReadyRead(d *schema.ResourceData, m interface{}) error {
precheckErrors, err := getPrecheckErrors(m, nil)
if err != nil {
return fmt.Errorf("Error while reading precheck failures: %v", err)
}
var precheckFailureIDs []string
var unacknowledgedWarningIDs []string
for _, precheckError := range precheckErrors {
errType := *precheckError.Type_
if errType == nsxModel.UpgradeCheckFailure_TYPE_FAILURE {
precheckFailureIDs = append(precheckFailureIDs, *precheckError.Id)
} else {
if !(*precheckError.Acked) {
unacknowledgedWarningIDs = append(unacknowledgedWarningIDs, *precheckError.Id)
}
}
}
var errMessage string
if len(precheckFailureIDs) > 0 {
errMessage += fmt.Sprintf("There are failures in precheck: %s, please check their status from nsxt_upgrade_prepare resource and address these failures on NSX", precheckFailureIDs)
}
if len(unacknowledgedWarningIDs) > 0 {
errMessage += fmt.Sprintf("\nThere are unacknowledged warnings in precheck: %s, please address these errors from NSX or using nsxt_upgrade_precheck_acknowledge resource", unacknowledgedWarningIDs)
}
if len(errMessage) > 0 {
return fmt.Errorf(errMessage)
}
objID := d.Get("id").(string)
if objID == "" {
objID = newUUID()
}
d.SetId(objID)

return nil
}
3 changes: 3 additions & 0 deletions nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ func Provider() *schema.Provider {
"nsxt_host_upgrade_group": dataSourceNsxtHostUpgradeGroup(),
"nsxt_policy_gateway_interface_realization_info": dataSourceNsxtPolicyGatewayInterfaceRealizationInfo(),
"nsxt_upgrade_postcheck": dataSourceNsxtUpgradePostCheck(),
"nsxt_upgrade_prepare_ready": dataSourceNsxtUpgradePrepareReady(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -473,6 +474,8 @@ func Provider() *schema.Provider {
"nsxt_policy_lb_udp_monitor_profile": resourceNsxtPolicyLBUdpMonitorProfile(),
"nsxt_policy_tier0_gateway_gre_tunnel": resourceNsxtPolicyTier0GatewayGRETunnel(),
"nsxt_upgrade_run": resourceNsxtUpgradeRun(),
"nsxt_upgrade_prepare": resourceNsxtUpgradePrepare(),
"nsxt_upgrade_precheck_acknowledge": resourceNsxtUpgradePrecheckAcknowledge(),
},

ConfigureFunc: providerConfigure,
Expand Down
139 changes: 139 additions & 0 deletions nsxt/resource_nsxt_upgrade_precheck_acknowledge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/* Copyright © 2023 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */

package nsxt

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
nsxModel "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/model"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/upgrade"
)

func resourceNsxtUpgradePrecheckAcknowledge() *schema.Resource {
return &schema.Resource{
Create: resourceNsxtUpgradePrecheckAcknowledgeCreate,
Read: resourceNsxtUpgradePrecheckAcknowledgeRead,
Update: resourceNsxtUpgradePrecheckAcknowledgeUpdate,
Delete: resourceNsxtUpgradePrecheckAcknowledgeDelete,

Schema: map[string]*schema.Schema{
"precheck_ids": {
Type: schema.TypeList,
Description: "IDs of precheck warnings that need to be acknowledged",
Elem: &schema.Schema{
Type: schema.TypeString,
},
Required: true,
},
"precheck_warnings": {
Type: schema.TypeList,
Description: "List of warnings from precheck",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "ID of the precheck warning",
Computed: true,
},
"message": {
Type: schema.TypeString,
Description: "Message of the precheck warning",
Computed: true,
},
"is_acknowledged": {
Type: schema.TypeBool,
Description: "Boolean value which identifies if the precheck warning has been acknowledged.",
Computed: true,
},
},
},
Computed: true,
},
},
}
}

func resourceNsxtUpgradePrecheckAcknowledgeCreate(d *schema.ResourceData, m interface{}) error {
id := d.Id()
if id == "" {
id = newUUID()
}
d.SetId(id)
precheckIDs := interface2StringList(d.Get("precheck_ids").([]interface{}))
err := acknowledgePrecheckWarnings(m, precheckIDs)
if err != nil {
return handleCreateError("NsxtPrecheckAcknowledge", id, err)
}
return resourceNsxtUpgradePrecheckAcknowledgeRead(d, m)
}

func acknowledgePrecheckWarnings(m interface{}, precheckIDs []string) error {
connector := getPolicyConnector(m)
client := upgrade.NewPreUpgradeChecksClient(connector)
for _, precheckID := range precheckIDs {
err := client.Acknowledge(precheckID)
if err != nil {
msg := fmt.Sprintf("Failed to acknowledge precheck warning with ID %s", precheckID)
return logAPIError(msg, err)
}
}
return nil
}

func resourceNsxtUpgradePrecheckAcknowledgeRead(d *schema.ResourceData, m interface{}) error {
id := d.Id()
typeParam := nsxModel.UpgradeCheckFailure_TYPE_WARNING
precheckWarnings, err := getPrecheckErrors(m, &typeParam)
if err != nil {
return handleReadError(d, "NsxtUpgradePrecheckAcknowledge", id, err)
}
err = setPrecheckWarningsInSchema(d, precheckWarnings)
if err != nil {
return handleReadError(d, "NsxtUpgradePrecheckAcknowledge", id, err)
}
return nil
}

func setPrecheckWarningsInSchema(d *schema.ResourceData, precheckWarnings []nsxModel.UpgradeCheckFailure) error {
var precheckWarningList []map[string]interface{}
for _, precheckWarning := range precheckWarnings {
elem := make(map[string]interface{})
elem["id"] = precheckWarning.Id
elem["message"] = precheckWarning.Message.Message
elem["is_acknowledged"] = precheckWarning.Acked
precheckWarningList = append(precheckWarningList, elem)
}
return d.Set("precheck_warnings", precheckWarningList)
}

func resourceNsxtUpgradePrecheckAcknowledgeUpdate(d *schema.ResourceData, m interface{}) error {
id := d.Id()
precheckIDs := interface2StringList(d.Get("precheck_ids").([]interface{}))
err := acknowledgePrecheckWarnings(m, precheckIDs)
if err != nil {
return handleUpdateError("NsxtPrecheckAcknowledge", id, err)
}
return resourceNsxtUpgradePrecheckAcknowledgeRead(d, m)
}

func resourceNsxtUpgradePrecheckAcknowledgeDelete(d *schema.ResourceData, m interface{}) error {
return nil
}

func getAcknowledgedPrecheckIDs(m interface{}) ([]string, error) {
var result []string
typeParam := nsxModel.UpgradeCheckFailure_TYPE_WARNING
precheckWarnings, err := getPrecheckErrors(m, &typeParam)
if err != nil {
return result, err
}
for _, warning := range precheckWarnings {
acked := *warning.Acked
if acked {
result = append(result, *warning.Id)
}
}
return result, nil
}
Loading

0 comments on commit 8ad37c7

Please sign in to comment.