-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resource to prepare for NSXT cluster upgrade
Signed-off-by: Shizhao Liu <[email protected]>
- Loading branch information
Shizhao Liu
committed
Feb 17, 2024
1 parent
726a519
commit 5656b68
Showing
7 changed files
with
827 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* 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 { | ||
return fmt.Errorf("Failed to acknowledge precheck warning with ID %s: %s", precheckID, 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 { | ||
id := *precheckWarning.Id | ||
message := *(precheckWarning.Message.Message) | ||
acked := *precheckWarning.Acked | ||
elem := make(map[string]interface{}) | ||
elem["id"] = id | ||
elem["message"] = message | ||
elem["is_acknowledged"] = 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 | ||
} |
Oops, something went wrong.