-
Notifications
You must be signed in to change notification settings - Fork 85
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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 | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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, maybeprepare_done
? Other suggestions? @GraysonWu @salv-orlandoThere was a problem hiding this comment.
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
There was a problem hiding this comment.
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
?