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 Jan 29, 2024
1 parent f1e5494 commit cfed3fb
Show file tree
Hide file tree
Showing 5 changed files with 556 additions and 0 deletions.
2 changes: 2 additions & 0 deletions nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,8 @@ 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_prepare": resourceNsxtUpgradePrepare(),
"nsxt_upgrade_precheck_acknowledge": resourceNsxtUpgradePrecheckAcknowledge(),
},

ConfigureFunc: providerConfigure,
Expand Down
96 changes: 96 additions & 0 deletions nsxt/resource_nsxt_upgrade_precheck_acknowledge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* 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,
},
},
}
}

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()
precheckWarnings, err := getPrecheckErrors(m, nsxModel.UpgradeCheckFailure_TYPE_WARNING)
if err != nil {
return handleReadError(d, "NsxtUpgradePrecheckAcknowledge", id, err)
}
err = setAcknowledgedPrecheckIDsInSchema(d, precheckWarnings)
if err != nil {
return handleReadError(d, "NsxtUpgradePrecheckAcknowledge", id, err)
}
return nil
}

func setAcknowledgedPrecheckIDsInSchema(d *schema.ResourceData, precheckWarnings []nsxModel.UpgradeCheckFailure) error {
var precheckWarningIDs []string
for _, precheckWarning := range precheckWarnings {
if !(*precheckWarning.NeedsAck) {
id := *precheckWarning.Id
precheckWarningIDs = append(precheckWarningIDs, id)
}
}
return d.Set("precheck_ids", precheckWarningIDs)
}

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
}
Loading

0 comments on commit cfed3fb

Please sign in to comment.