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 16, 2024
1 parent b8b33d1 commit f179c93
Show file tree
Hide file tree
Showing 5 changed files with 478 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 @@ -441,6 +441,8 @@ func Provider() *schema.Provider {
"nsxt_policy_lb_http_application_profile": resourceNsxtPolicyLBHttpApplicationProfile(),
"nsxt_policy_security_policy_rule": resourceNsxtPolicySecurityPolicyRule(),
"nsxt_policy_parent_security_policy": resourceNsxtPolicyParentSecurityPolicy(),
"nsxt_upgrade_prepare": resourceNsxtUpgradePrepare(),
"nsxt_upgrade_precheck_acknowledge": resourceNsxtUpgradePrecheckAcknowledge(),
},

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

package nsxt

import (
"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_id": {
Type: schema.TypeList,
Description: "IDs of failed prechecks that user wants to acknowledge before upgrade",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "ID of failed precheck to acknowledge",
Required: true,
},
},
},
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_id").([]interface{}))
connector := getPolicyConnector(m)
client := upgrade.NewPreUpgradeChecksClient(connector)
for _, precheckID := range precheckIDs {
err := client.Acknowledge(precheckID)
if err != nil {
return handleCreateError("NsxtUpgradePrecheckAcknowledge", id, err)
}
}
return resourceNsxtUpgradePrecheckAcknowledgeRead(d, m)
}

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 []map[string]interface{}
for _, precheckWarning := range precheckWarnings {
elem := make(map[string]interface{})
if !(*precheckWarning.NeedsAck) {
id := *precheckWarning.Id
elem["id"] = id
precheckWarningIDs = append(precheckWarningIDs, elem)
}
}
return d.Set("precheck_id", precheckWarningIDs)
}

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

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

0 comments on commit f179c93

Please sign in to comment.