|
| 1 | +// © Broadcom. All Rights Reserved. |
| 2 | +// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. |
| 3 | +// SPDX-License-Identifier: MPL-2.0 |
| 4 | + |
| 5 | +package nsxt |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 13 | + "github.com/vmware/vsphere-automation-sdk-go/lib/vapi/std/errors" |
| 14 | + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/sites/enforcement_points" |
| 15 | + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" |
| 16 | +) |
| 17 | + |
| 18 | +var rtepAssignments = []string{ |
| 19 | + "static_ipv4_list", |
| 20 | + "static_ipv4_pool", |
| 21 | +} |
| 22 | + |
| 23 | +func resourceNsxtPolicyEdgeTransportNodeRTEP() *schema.Resource { |
| 24 | + return &schema.Resource{ |
| 25 | + Create: resourceNsxtPolicyEdgeTransportNodeRTEPCreate, |
| 26 | + Read: resourceNsxtPolicyEdgeTransportNodeRTEPRead, |
| 27 | + Update: resourceNsxtPolicyEdgeTransportNodeRTEPUpdate, |
| 28 | + Delete: resourceNsxtPolicyEdgeTransportNodeRTEPDelete, |
| 29 | + Importer: &schema.ResourceImporter{ |
| 30 | + State: resourceNsxtPolicyEdgeTransportNodeRTEPImporter, |
| 31 | + }, |
| 32 | + Schema: map[string]*schema.Schema{ |
| 33 | + "edge_transport_node_path": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Description: "Policy path of Edge transport node to associate with remote tunnel endpoint.", |
| 36 | + Required: true, |
| 37 | + ForceNew: true, |
| 38 | + }, |
| 39 | + "host_switch_name": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Description: "The host switch name to be used for the remote tunnel endpoint", |
| 42 | + Required: true, |
| 43 | + ForceNew: true, |
| 44 | + }, |
| 45 | + "ip_assignment": getPolicyIPAssignmentSchema(false, 1, 1, rtepAssignments), |
| 46 | + "named_teaming_policy": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Description: "The named teaming policy to be used by the remote tunnel endpoint", |
| 49 | + Optional: true, |
| 50 | + }, |
| 51 | + "vlan": { |
| 52 | + Type: schema.TypeInt, |
| 53 | + Description: "VLAN id for remote tunnel endpoint", |
| 54 | + Required: true, |
| 55 | + ValidateFunc: validation.IntBetween(0, 4094), |
| 56 | + }, |
| 57 | + }, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func getEdgeTransportNodeKeysFromPath(path string) (string, string, string) { |
| 62 | + siteID := getResourceIDFromResourcePath(path, "sites") |
| 63 | + epID := getResourceIDFromResourcePath(path, "enforcement-points") |
| 64 | + id := getResourceIDFromResourcePath(path, "edge-transport-nodes") |
| 65 | + |
| 66 | + return siteID, epID, id |
| 67 | +} |
| 68 | + |
| 69 | +func setNsxtPolicyEdgeTransportNodeRTEP(d *schema.ResourceData, m interface{}, op string) error { |
| 70 | + connector := getPolicyConnector(m) |
| 71 | + |
| 72 | + tnPath := d.Get("edge_transport_node_path").(string) |
| 73 | + client := enforcement_points.NewEdgeTransportNodesClient(connector) |
| 74 | + |
| 75 | + siteID, epID, edgeID := getEdgeTransportNodeKeysFromPath(tnPath) |
| 76 | + obj, err := client.Get(siteID, epID, edgeID) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + hswName := d.Get("host_switch_name").(string) |
| 82 | + found := false |
| 83 | + |
| 84 | + for i, hsw := range obj.SwitchSpec.Switches { |
| 85 | + if *hsw.SwitchName == hswName { |
| 86 | + { |
| 87 | + found = true |
| 88 | + if len(hsw.RemoteTunnelEndpoint) > 0 && op == "create" { |
| 89 | + return fmt.Errorf("remote tunnel endpoint for Edge transport node %s already exists", tnPath) |
| 90 | + } |
| 91 | + |
| 92 | + var rteps []model.PolicyEdgeTransportNodeRtepConfig |
| 93 | + |
| 94 | + if op != "delete" { |
| 95 | + ipAssignments, err := getPolicyIPAssignmentsFromSchema(d.Get("ip_assignment")) |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + namedTeamingPolicy := d.Get("named_teaming_policy").(string) |
| 100 | + vlan := int64(d.Get("vlan").(int)) |
| 101 | + rteps = []model.PolicyEdgeTransportNodeRtepConfig{ |
| 102 | + { |
| 103 | + IpAssignmentSpecs: ipAssignments, |
| 104 | + NamedTeamingPolicy: &namedTeamingPolicy, |
| 105 | + Vlan: &vlan, |
| 106 | + }, |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + obj.SwitchSpec.Switches[i].RemoteTunnelEndpoint = rteps |
| 111 | + |
| 112 | + err = client.Patch(siteID, epID, edgeID, obj) |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + if op == "create" { |
| 118 | + d.SetId(fmt.Sprintf("%s:%s", tnPath, hswName)) |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + if !found { |
| 125 | + return fmt.Errorf("switch %s not found for Edge transport node %s", hswName, tnPath) |
| 126 | + } |
| 127 | + |
| 128 | + return resourceNsxtPolicyEdgeTransportNodeRTEPRead(d, m) |
| 129 | +} |
| 130 | + |
| 131 | +func resourceNsxtPolicyEdgeTransportNodeRTEPCreate(d *schema.ResourceData, m interface{}) error { |
| 132 | + return setNsxtPolicyEdgeTransportNodeRTEP(d, m, "create") |
| 133 | +} |
| 134 | + |
| 135 | +func resourceNsxtPolicyEdgeTransportNodeRTEPRead(d *schema.ResourceData, m interface{}) error { |
| 136 | + connector := getPolicyConnector(m) |
| 137 | + |
| 138 | + tnPath := d.Get("edge_transport_node_path").(string) |
| 139 | + client := enforcement_points.NewEdgeTransportNodesClient(connector) |
| 140 | + |
| 141 | + siteID, epID, edgeID := getEdgeTransportNodeKeysFromPath(tnPath) |
| 142 | + obj, err := client.Get(siteID, epID, edgeID) |
| 143 | + if err != nil { |
| 144 | + return err |
| 145 | + } |
| 146 | + |
| 147 | + hswName := d.Get("host_switch_name").(string) |
| 148 | + |
| 149 | + for _, hsw := range obj.SwitchSpec.Switches { |
| 150 | + if *hsw.SwitchName == hswName { |
| 151 | + { |
| 152 | + if len(hsw.RemoteTunnelEndpoint) > 0 { |
| 153 | + return errors.NotFound{} |
| 154 | + } |
| 155 | + |
| 156 | + ipAssignment, err := setPolicyIPAssignmentsInSchema(obj.ManagementInterface.IpAssignmentSpecs) |
| 157 | + if err != nil { |
| 158 | + return err |
| 159 | + } |
| 160 | + |
| 161 | + // Only one RTEP is supported |
| 162 | + d.Set("ip_assignment", ipAssignment) |
| 163 | + d.Set("named_teaming_policy", hsw.RemoteTunnelEndpoint[0].NamedTeamingPolicy) |
| 164 | + d.Set("vlan", hsw.RemoteTunnelEndpoint[0].Vlan) |
| 165 | + |
| 166 | + return nil |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + return errors.NotFound{} |
| 172 | +} |
| 173 | + |
| 174 | +func resourceNsxtPolicyEdgeTransportNodeRTEPUpdate(d *schema.ResourceData, m interface{}) error { |
| 175 | + return setNsxtPolicyEdgeTransportNodeRTEP(d, m, "update") |
| 176 | +} |
| 177 | + |
| 178 | +func resourceNsxtPolicyEdgeTransportNodeRTEPDelete(d *schema.ResourceData, m interface{}) error { |
| 179 | + return setNsxtPolicyEdgeTransportNodeRTEP(d, m, "delete") |
| 180 | +} |
| 181 | + |
| 182 | +func resourceNsxtPolicyEdgeTransportNodeRTEPImporter(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) { |
| 183 | + importID := d.Id() |
| 184 | + segs := strings.Split(importID, ":") |
| 185 | + if len(segs) != 2 { |
| 186 | + return []*schema.ResourceData{d}, fmt.Errorf("import parameter %s should have a policy path to PolicyEdgeTransportNode and a switch_name, separated by ':'", importID) |
| 187 | + } |
| 188 | + tnPath := segs[0] |
| 189 | + |
| 190 | + err := validateImportPolicyPath(tnPath) |
| 191 | + if err != nil { |
| 192 | + return []*schema.ResourceData{d}, err |
| 193 | + } |
| 194 | + |
| 195 | + hswName := segs[1] |
| 196 | + |
| 197 | + d.Set("edge_transport_node_path", tnPath) |
| 198 | + d.Set("host_switch_name", hswName) |
| 199 | + |
| 200 | + d.SetId(fmt.Sprintf("%s:%s", tnPath, hswName)) |
| 201 | + |
| 202 | + return []*schema.ResourceData{d}, nil |
| 203 | +} |
0 commit comments