Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource to prepare for NSXT cluster upgrade #1069

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a naming comment - prepare_ready sounds non-optimal to me, maybe prepare_done? Other suggestions? @GraysonWu @salv-orlando

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prepare_complete? I'm ok with both

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just nsxt_upgrade_ready?

},

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
annakhm marked this conversation as resolved.
Show resolved Hide resolved
}

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
Loading