Skip to content

Support for adding child fabrics in MSD fabric and manage them #208

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

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions docs/resources/fabric_vxlan_msd.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ resource "ndfc_fabric_vxlan_msd" "test_resource_fabric_vxlan_msd_1" {
anycast_gw_mac = "2020.0000.00aa"
bgw_routing_tag = 54321
border_gwy_connections = "Manual"
child_fabrics = ["TF_FABRIC_VXLAN_1", "TF_FABRIC_VXLAN_2"]
cloudsec_autoconfig = false
dci_subnet_range = "10.10.1.0/24"
dci_subnet_target_mask = 30
Expand Down Expand Up @@ -52,6 +53,7 @@ resource "ndfc_fabric_vxlan_msd" "test_resource_fabric_vxlan_msd_1" {
- `bgp_rp_asn` (String) 1-4294967295 | 1-65535[.0-65535], e.g. 65000, 65001
- `bgw_routing_tag` (Number) Routing tag associated with IP address of loopback and DCI interfaces
- `border_gwy_connections` (String) Manual, Auto Overlay EVPN Peering to Route Servers, Auto Overlay EVPN Direct Peering to Border Gateways
- `child_fabrics` (Set of String) Add child fabrics to the MSD fabric
- `cloudsec_algorithm` (String) CloudSec Cryptographic Algorithm
- `cloudsec_autoconfig` (Boolean) Auto Config CloudSec on Border Gateways
- `cloudsec_enforcement` (String) If set to 'strict', data across site must be encrypted.
Expand Down
1 change: 1 addition & 0 deletions examples/resources/ndfc_fabric_vxlan_msd/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ resource "ndfc_fabric_vxlan_msd" "test_resource_fabric_vxlan_msd_1" {
anycast_gw_mac = "2020.0000.00aa"
bgw_routing_tag = 54321
border_gwy_connections = "Manual"
child_fabrics = ["TF_FABRIC_VXLAN_1", "TF_FABRIC_VXLAN_2"]
cloudsec_autoconfig = false
dci_subnet_range = "10.10.1.0/24"
dci_subnet_target_mask = 30
Expand Down
9 changes: 9 additions & 0 deletions generator/defs/fabric_common.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,15 @@ resource:
optional: true
type: Bool

- model_name: child_fabrics
tf_name: child_fabrics
description: "Add child fabrics to the MSD fabric"
type: List:String
example: "[\"TF_FABRIC_VXLAN_1\", \"TF_FABRIC_VXLAN_2\"]"
ndfc_type: set
payload_hide: true
optional: true

- model_name: COPP_POLICY
tf_name: copp_policy
description: Fabric Wide CoPP Policy. Customized CoPP policy should be provided
Expand Down
9 changes: 9 additions & 0 deletions generator/defs/fabric_vxlan_msd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ resource:
optional: true
computed: true

- model_name: child_fabrics
tf_name: child_fabrics
description: "Add child fabrics to the MSD fabric"
type: List:String
example: "[\"TF_FABRIC_VXLAN_1\", \"TF_FABRIC_VXLAN_2\"]"
ndfc_type: set
payload_hide: true
optional: true

- model_name: CLOUDSEC_ALGORITHM
tf_name: cloudsec_algorithm
description: CloudSec Cryptographic Algorithm
Expand Down
17 changes: 15 additions & 2 deletions internal/provider/fabric_vxlan_msd_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,18 @@ func (r *fabricVxlanMsdResource) Create(ctx context.Context, req resource.Create
if r.client == nil {
panic("Client is nil")
}
// Create API call logic
// Values are not updated part of NDFC payload get, save them and set them back
deploy := data.Deploy.ValueBool()
childFabrics := data.ChildFabrics
r.client.RscCreateFabric(ctx, &resp.Diagnostics, &data, ndfc.ResourceVxlanMsdType)

// Setting back the values that are not updated by NDFC
data.ChildFabrics = childFabrics
data.Deploy = types.BoolValue(deploy)
data.Id = data.FabricName

tflog.Debug(ctx, "data.Id = "+data.Id.ValueString())

if deploy {
if resp.Diagnostics.HasError() || resp.Diagnostics.WarningsCount() > 0 {
data.DeploymentStatus = types.StringValue("Deployment pending")
Expand All @@ -86,7 +92,7 @@ func (r *fabricVxlanMsdResource) Create(ctx context.Context, req resource.Create
tflog.Error(ctx, "Create Fabric Failed")
return
}

r.client.AddChildFabricsToMsd(ctx, &resp.Diagnostics, &data)
// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, data)...)
}
Expand All @@ -109,10 +115,12 @@ func (r *fabricVxlanMsdResource) Read(ctx context.Context, req resource.ReadRequ
unique_id := data.Id.ValueString()
tflog.Info(ctx, fmt.Sprintf("Incoming ID %s", unique_id))
deploy := data.Deploy.ValueBool()
childFabrics := data.ChildFabrics

r.client.RscReadFabric(ctx, &resp.Diagnostics, &data, ndfc.ResourceVxlanMsdType)
data.Deploy = types.BoolValue(deploy)
data.Id = data.FabricName
data.ChildFabrics = childFabrics
tflog.Debug(ctx, "data.FabricName = "+data.FabricName.ValueString())
if data.FabricName.IsNull() || data.FabricName.IsUnknown() {
// make diags error empty because fabric is not present in NDFC,
Expand Down Expand Up @@ -177,6 +185,11 @@ func (r *fabricVxlanMsdResource) Delete(ctx context.Context, req resource.Delete
resp.State.RemoveResource(ctx)
return
}
tflog.Debug(ctx, fmt.Sprintf("Delete data = %+v", data))
r.client.RemoveChildFabricsFromMsd(ctx, &resp.Diagnostics, &data)
if resp.Diagnostics.HasError() {
return
}
r.client.RscDeleteFabric(ctx, &resp.Diagnostics, &data, ndfc.ResourceVxlanMsdType)
if resp.Diagnostics.HasError() {
tflog.Error(ctx, "Delete Fabric Failed")
Expand Down
23 changes: 16 additions & 7 deletions internal/provider/ndfc/api/fabric_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import (

// FabricAPI is the API client for the vpc pair resource
const (
UrlFabricGetAll = "/lan-fabric/rest/control/fabrics"
urlPerFabric = UrlFabricGetAll + "/%s"
urlFabricTemplate = urlPerFabric + "/%s"
UrlSwitchesByFabric = "/lan-fabric/rest/control/fabrics/%s/inventory/switchesByFabric"
urlFabricNameFromSerial = "/lan-fabric/rest/control/switches/%s/fabric-name"
UrlFabricGetAll = "/lan-fabric/rest/control/fabrics"
urlPerFabric = UrlFabricGetAll + "/%s"
urlFabricTemplate = urlPerFabric + "/%s"
UrlSwitchesByFabric = "/lan-fabric/rest/control/fabrics/%s/inventory/switchesByFabric"
urlFabricNameFromSerial = "/lan-fabric/rest/control/switches/%s/fabric-name"
urlAddChildFabricsToMsd = "/lan-fabric/rest/control/fabrics/msdAdd"
urlRemoveChildFabricsFromMsd = "/lan-fabric/rest/control/fabrics/msdExit"
)

type FabricAPI struct {
Expand All @@ -31,6 +33,7 @@ type FabricAPI struct {
GetSwitchesInFabric bool
Serialnumber string
FabricType string
MsdOperation string
}

func (c *FabricAPI) GetLock() *sync.Mutex {
Expand All @@ -52,9 +55,15 @@ func (c *FabricAPI) GetUrl() string {
}

func (c *FabricAPI) PostUrl() string {
return fmt.Sprintf(urlFabricTemplate, c.FabricName, c.FabricType)
switch c.MsdOperation {
case "add":
return urlAddChildFabricsToMsd
case "remove":
return urlRemoveChildFabricsFromMsd
default:
return fmt.Sprintf(urlFabricTemplate, c.FabricName, c.FabricType)
}
}

func (c *FabricAPI) PutUrl() string {
return fmt.Sprintf(urlFabricTemplate, c.FabricName, c.FabricType)
}
Expand Down
76 changes: 76 additions & 0 deletions internal/provider/ndfc/fabric_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,82 @@ func (f *NDFC) GetFabricName(ctx context.Context, serialNumber string) string {
}
return FabricNamePayload.FabricName

}
func (c NDFC) GetFabricTemplateType(ctx context.Context, dg *diag.Diagnostics, fabricName string) string {
var nvPairsModel resource_fabric_common.NdfcFabricPayload
fapi := api.NewFabricAPI(c.GetLock(ResourceFabrics), &c.apiClient)
fapi.FabricName = fabricName
payload, err := fapi.Get()
if len(payload) == 0 {
tflog.Error(ctx, "Error while getting fabric template type ", map[string]interface{}{"Err": err})
dg.AddError("Failed to read fabric template type", err.Error())
return ""
}
err = json.Unmarshal(payload, &nvPairsModel)
if err != nil {
tflog.Error(ctx, "Error while unmarshalling fabric template type ", map[string]interface{}{"Err": err})
dg.AddError("Failed to unmarshal fabric template type", err.Error())
}

return nvPairsModel.NdfcFabricNvPairs.FabricType
}
func (f *NDFC) AddChildFabricsToMsd(ctx context.Context, dg *diag.Diagnostics, tf resource_fabric_common.FabricModel) {
var msdFabric resource_fabric_common.NdfcMsdFabricPayload
fapi := api.NewFabricAPI(f.GetLock(ResourceFabrics), &f.apiClient)
fapi.MsdOperation = "add"
model := tf.GetModelData()
if len(model.ChildFabrics) == 0 {
tflog.Debug(ctx, "RemoveChildFabricsFromMsd: No child fabrics to remove")
return
}
tflog.Debug(ctx, fmt.Sprintf("AddChildFabricsToMsd: child fabrics %v", model.ChildFabrics))
for _, childFabric := range model.ChildFabrics {
msdFabric.DstFabricName = model.FabricName
msdFabric.SrcFabricName = childFabric
tflog.Debug(ctx, fmt.Sprintf("AddChildFabricsToMsd: msdFabric %v", msdFabric))
payload, err := json.Marshal(msdFabric)
if err != nil {
tflog.Error(ctx, "AddChildFabricsToMsd: Failed to marshal msd fabric data")
dg.AddError("Failed to marshal msd fabric data", fmt.Sprintf("Error: %q", err.Error()))
return
}
res, err := fapi.Post(payload)
if err != nil {
tflog.Error(ctx, "AddChildFabricsToMsd: POST failed with payload %s", map[string]interface{}{"Payload": payload})
dg.AddError("Failed to add child fabric to msd", fmt.Sprintf("Error: %q %q", err.Error(), res.String()))
return
}
}

}
func (f *NDFC) RemoveChildFabricsFromMsd(ctx context.Context, dg *diag.Diagnostics, tf resource_fabric_common.FabricModel) {
var msdFabric resource_fabric_common.NdfcMsdFabricPayload
fapi := api.NewFabricAPI(f.GetLock(ResourceFabrics), &f.apiClient)
fapi.MsdOperation = "remove"
model := tf.GetModelData()
if len(model.ChildFabrics) == 0 {
tflog.Debug(ctx, "RemoveChildFabricsFromMsd: No child fabrics to remove")
return
}
tflog.Debug(ctx, fmt.Sprintf("RemoveChildFabricsFromMsd: child fabrics %v", model.ChildFabrics))
for _, childFabric := range model.ChildFabrics {
msdFabric.DstFabricName = model.FabricName
msdFabric.SrcFabricName = childFabric
tflog.Debug(ctx, fmt.Sprintf("RemoveChildFabricsFromMsd: msdFabric %v", msdFabric))
payload, err := json.Marshal(msdFabric)
if err != nil {
tflog.Error(ctx, "RemoveChildFabricsFromMsd: Failed to marshal msd fabric data")
dg.AddError("Failed to marshal msd fabric data", fmt.Sprintf("Error: %q", err.Error()))
return
}
res, err := fapi.Post(payload)
if err != nil {
tflog.Error(ctx, "RemoveChildFabricsFromMsd: POST failed with payload %s", map[string]interface{}{"Payload": payload})
dg.AddError("Failed to add child fabric to msd", fmt.Sprintf("Error: %q %q", err.Error(), res.String()))
return
}
}

}
func (f *NDFC) DSGetFabric(ctx context.Context, dg *diag.Diagnostics, fabricName string) *datasource_fabric.FabricModel {
tflog.Debug(ctx, "DSGetFabricBulk entry")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ type NDFCFabricCommonModel struct {
BgpLbId *Int64Custom `json:"BGP_LB_ID,omitempty"`
BrownfieldNetworkNameFormat string `json:"BROWNFIELD_NETWORK_NAME_FORMAT,omitempty"`
BrownfieldSkipOverlayNetworkAttachments string `json:"BROWNFIELD_SKIP_OVERLAY_NETWORK_ATTACHMENTS,omitempty"`
ChildFabrics []string `json:"-"`
CoppPolicy string `json:"COPP_POLICY,omitempty"`
DciSubnetRange string `json:"DCI_SUBNET_RANGE,omitempty"`
DciSubnetTargetMask *Int64Custom `json:"DCI_SUBNET_TARGET_MASK,omitempty"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ type NdfcFabricPayload struct {
type NdfcFabricNamePayload struct {
FabricName string `json:"fabricName,omitempty"`
}

type NdfcMsdFabricPayload struct {
DstFabricName string `json:"destFabric,omitempty"`
SrcFabricName string `json:"sourceFabric,omitempty"`
}
type CustomNdfcFabricNamePayload NdfcFabricNamePayload
type CustomNdfcFabricPayload NdfcFabricPayload

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
package resource_fabric_vxlan_msd

import (
"context"
"log"
"strconv"
. "terraform-provider-ndfc/internal/provider/types"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"

Expand Down Expand Up @@ -64,6 +67,24 @@ func (v *FabricVxlanMsdModel) SetModelData(jsonData *resource_fabric_common.NDFC
v.BorderGwyConnections = types.StringNull()
}

if len(jsonData.ChildFabrics) == 0 {
log.Printf("v.ChildFabrics is empty")
v.ChildFabrics, err = types.SetValue(types.StringType, []attr.Value{})
if err != nil {
log.Printf("Error in converting []string to List %v", err)
return err
}
} else {
listData := make([]attr.Value, len(jsonData.ChildFabrics))
for i, item := range jsonData.ChildFabrics {
listData[i] = types.StringValue(item)
}
v.ChildFabrics, err = types.SetValue(types.StringType, listData)
if err != nil {
log.Printf("Error in converting []string to List")
return err
}
}
if jsonData.CloudsecAlgorithm != "" {
v.CloudsecAlgorithm = types.StringValue(jsonData.CloudsecAlgorithm)
} else {
Expand Down Expand Up @@ -519,6 +540,16 @@ func (v FabricVxlanMsdModel) GetModelData() *resource_fabric_common.NDFCFabricCo
data.BorderGwyConnections = ""
}

if !v.ChildFabrics.IsNull() && !v.ChildFabrics.IsUnknown() {
listStringData := make([]string, len(v.ChildFabrics.Elements()))
dg := v.ChildFabrics.ElementsAs(context.Background(), &listStringData, false)
if dg.HasError() {
panic(dg.Errors())
}
data.ChildFabrics = make([]string, len(listStringData))
copy(data.ChildFabrics, listStringData)
}

if !v.CloudsecAlgorithm.IsNull() && !v.CloudsecAlgorithm.IsUnknown() {
data.CloudsecAlgorithm = v.CloudsecAlgorithm.ValueString()
} else {
Expand Down