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

Implement edge cluster resource #919

Merged
merged 1 commit into from
Aug 1, 2023
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
1 change: 1 addition & 0 deletions nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ func Provider() *schema.Provider {
"nsxt_policy_spoof_guard_profile": resourceNsxtPolicySpoofGuardProfile(),
"nsxt_policy_gateway_qos_profile": resourceNsxtPolicyGatewayQosProfile(),
"nsxt_policy_project": resourceNsxtPolicyProject(),
"nsxt_edge_cluster": resourceNsxtEdgeCluster(),
},

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

package nsxt

import (
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/model"
)

func resourceNsxtEdgeCluster() *schema.Resource {
return &schema.Resource{
Create: resourceNsxtEdgeClusterCreate,
Read: resourceNsxtEdgeClusterRead,
Update: resourceNsxtEdgeClusterUpdate,
Delete: resourceNsxtEdgeClusterDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"revision": getRevisionSchema(),
"description": getDescriptionSchema(),
"display_name": getDisplayNameSchema(),
"tag": getTagsSchema(),
"edge_ha_profile_id": {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it possible to add multiple ha profiles? Same about below

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is odd - the API has a list there and there are two types (bridge, edge). Yet I can't create a bridge profile using curl (server returns 201 but nothing is created), and UI lets me select only one profile.
Not sure what we do with this (nor where we gather more info).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't see it in the doc past NSX 3.2

Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like only single HA profile can be assigned - hence TypeList is not needed

Type: schema.TypeString,
Description: "Edge high availability cluster profile Id",
Optional: true,
Computed: true,
},
"member_node_type": {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is not an enum?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Same applies here: this member_node_type is readonly

Type: schema.TypeString,
Description: "Node type of the cluster members",
Computed: true,
},
"member": {
Type: schema.TypeList,
Description: "Edge cluster members",
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Description: "Description of this resource",
Optional: true,
},
"display_name": {
Type: schema.TypeString,
Description: "The display name of this resource. Defaults to ID if not set",
Optional: true,
Computed: true,
},
"member_index": {
Type: schema.TypeInt,
Description: "System generated index for cluster member",
Computed: true,
},
"transport_node_id": {
Type: schema.TypeString,
Description: "UUID of edge transport node",
Required: true,
},
},
},
},
"node_rtep_ips": {
Copy link
Collaborator

Choose a reason for hiding this comment

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

According to the spec, this whole section should be Computed, is that correct?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Indeed. I'm not sure whether we need it or not too. What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think those IPs could be used elsewhere in config, so lets leave them

Type: schema.TypeList,
Description: "Remote tunnel endpoint ip address",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"member_index": {
Type: schema.TypeInt,
Description: "System generated index for cluster member",
Computed: true,
},
"rtep_ips": {
Type: schema.TypeList,
Description: "Remote tunnel endpoint ip address",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validateSingleIP(),
},
},
"transport_node_id": {
Type: schema.TypeString,
Description: "UUID of edge transport node",
Computed: true,
},
},
},
},
},
}
}

func resourceNsxtEdgeClusterCreate(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)
client := nsx.NewEdgeClustersClient(connector)

description := d.Get("description").(string)
displayName := d.Get("display_name").(string)
tags := getMPTagsFromSchema(d)
clusterProfileBindings := getClusterProfileBindingsFromSchema(d)

members := getEdgeClusterMembersFromSchema(d)
obj := model.EdgeCluster{
Description: &description,
DisplayName: &displayName,
Tags: tags,
ClusterProfileBindings: clusterProfileBindings,
Members: members,
}

obj, err := client.Create(obj)
if err != nil {
id := ""
if obj.Id != nil {
id = *obj.Id
}
return handleCreateError("Edge Cluster", id, err)
}

log.Printf("[INFO] Creating Edge Cluster with ID %s", *obj.Id)

d.SetId(*obj.Id)
return resourceNsxtEdgeClusterRead(d, m)
}

func getClusterProfileBindingsFromSchema(d *schema.ResourceData) []model.ClusterProfileTypeIdEntry {
resourceType := model.ClusterProfileTypeIdEntry_RESOURCE_TYPE_EDGEHIGHAVAILABILITYPROFILE
clusterProfileBinding := d.Get("edge_ha_profile_id").(string)
var clusterProfileBindings []model.ClusterProfileTypeIdEntry
if clusterProfileBinding != "" {
clusterProfileBindings = []model.ClusterProfileTypeIdEntry{
{
ProfileId: &clusterProfileBinding,
ResourceType: &resourceType,
},
}
}
return clusterProfileBindings
}

func getEdgeClusterMembersFromSchema(d *schema.ResourceData) []model.EdgeClusterMember {
memberList := d.Get("member").([]interface{})
var members []model.EdgeClusterMember
for _, member := range memberList {
data := member.(map[string]interface{})
description := data["description"].(string)
displayName := data["display_name"].(string)
memberIndex := data["member_index"].(int64)
transportNodeID := data["transport_node_id"].(string)
elem := model.EdgeClusterMember{
Description: &description,
DisplayName: &displayName,
MemberIndex: &memberIndex,
TransportNodeId: &transportNodeID,
}
members = append(members, elem)
}
return members
}

func resourceNsxtEdgeClusterRead(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)
id := d.Id()
if id == "" {
return fmt.Errorf("error obtaining logical object id")
}

client := nsx.NewEdgeClustersClient(connector)
obj, err := client.Get(id)
if err != nil {
return fmt.Errorf("error during Edge Cluster read: %v", err)
}

d.Set("revision", obj.Revision)
d.Set("description", obj.Description)
d.Set("display_name", obj.DisplayName)
setMPTagsInSchema(d, obj.Tags)

setClusterProfileBindingsInSchema(d, obj)

d.Set("member_node_type", obj.MemberNodeType)
setMemberListInSchema(d, obj.Members)
setNodeRtepIPsInSchema(d, obj.NodeRtepIps)
return nil
}

func setClusterProfileBindingsInSchema(d *schema.ResourceData, obj model.EdgeCluster) {
for _, cpb := range obj.ClusterProfileBindings {
if *cpb.ResourceType == model.ClusterProfileTypeIdEntry_RESOURCE_TYPE_EDGEHIGHAVAILABILITYPROFILE {
d.Set("edge_ha_profile_id", *cpb.ProfileId)
// Model contains a single profile id
return
}
log.Printf("Unsupported resource %s", *cpb.ResourceType)
}
}

func setNodeRtepIPsInSchema(d *schema.ResourceData, nodeRtepIPs []model.NodeRtepIpsConfig) error {
var expressionList []map[string]interface{}
for _, rtepIP := range nodeRtepIPs {
elem := make(map[string]interface{})
elem["member_index"] = rtepIP.MemberIndex
elem["rtep_ips"] = rtepIP.RtepIps
elem["transport_node_id"] = rtepIP.TransportNodeId
expressionList = append(expressionList, elem)
}
return d.Set("node_rtep_ips", expressionList)
}

func setMemberListInSchema(d *schema.ResourceData, members []model.EdgeClusterMember) error {
var expresionList []map[string]interface{}
for _, member := range members {
elem := make(map[string]interface{})
elem["description"] = member.Description
elem["display_name"] = member.DisplayName
elem["member_index"] = member.MemberIndex
elem["transport_node_id"] = member.TransportNodeId
expresionList = append(expresionList, elem)
}
return d.Set("member", expresionList)
}

func resourceNsxtEdgeClusterUpdate(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)
id := d.Id()
if id == "" {
return fmt.Errorf("error obtaining logical object id")
}

client := nsx.NewEdgeClustersClient(connector)

revision := int64(d.Get("revision").(int))
description := d.Get("description").(string)
displayName := d.Get("display_name").(string)
tags := getMPTagsFromSchema(d)
members := getEdgeClusterMembersFromSchema(d)
clusterProfileBindings := getClusterProfileBindingsFromSchema(d)
obj := model.EdgeCluster{
Revision: &revision,
Description: &description,
DisplayName: &displayName,
Tags: tags,
ClusterProfileBindings: clusterProfileBindings,
Members: members,
}

_, err := client.Update(id, obj)
if err != nil {
return fmt.Errorf("error during Edge Cluster %s update: %v", id, err)
}

return resourceNsxtEdgeClusterRead(d, m)
}

func resourceNsxtEdgeClusterDelete(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)

id := d.Id()
if id == "" {
return fmt.Errorf("error obtaining logical object id")
}

client := nsx.NewEdgeClustersClient(connector)

err := client.Delete(id)
if err != nil {
return fmt.Errorf("error during Edge Cluster delete: %v", err)
}
return nil
}
Loading