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

dcnm_fabric: Fix for issue 343 #344

Merged
merged 3 commits into from
Nov 16, 2024
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
49 changes: 43 additions & 6 deletions plugins/module_utils/fabric/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
import json
import logging

from ansible_collections.cisco.dcnm.plugins.module_utils.common.api.v1.lan_fabric.rest.control.fabrics.fabrics import \
EpFabricCreate
from ansible_collections.cisco.dcnm.plugins.module_utils.fabric.common import \
FabricCommon
from ansible_collections.cisco.dcnm.plugins.module_utils.fabric.fabric_types import \
FabricTypes
from ansible_collections.cisco.dcnm.plugins.module_utils.common.api.v1.lan_fabric.rest.control.fabrics.fabrics import (
EpFabricCreate,
)
from ansible_collections.cisco.dcnm.plugins.module_utils.fabric.common import (
FabricCommon,
)
from ansible_collections.cisco.dcnm.plugins.module_utils.fabric.fabric_types import (
FabricTypes,
)


class FabricCreateCommon(FabricCommon):
Expand Down Expand Up @@ -112,6 +115,38 @@ def _set_fabric_create_endpoint(self, payload):
self.path = self.ep_fabric_create.path
self.verb = self.ep_fabric_create.verb

def _add_ext_fabric_type_to_payload(self, payload: dict) -> dict:
"""
# Summary

If the payload contains an external fabric type (e.g ISN)
and does not contain the EXT_FABRIC_TYPE key, add this
key with the default value that NDFC GUI uses for displaying
the fabric type.

# Raises

None
"""
method_name = inspect.stack()[0][3]

fabric_type = payload.get("FABRIC_TYPE")
if fabric_type not in self.fabric_types.external_fabric_types:
return payload
if "EXT_FABRIC_TYPE" in payload:
return payload
value = self.fabric_types.fabric_type_to_ext_fabric_type_map.get(fabric_type)
if value is None:
return payload
payload["EXT_FABRIC_TYPE"] = value

msg = f"{self.class_name}.{method_name}: "
msg += "Added EXT_FABRIC_TYPE to payload. "
msg += f"fabric_type: {fabric_type}, "
msg += f"value: {value}"
self.log.debug(msg)
return payload

def _send_payloads(self):
"""
- If ``check_mode`` is ``False``, send the payloads
Expand All @@ -125,6 +160,8 @@ def _send_payloads(self):
- This overrides the parent class method.
"""
for payload in self._payloads_to_commit:
payload = self._add_ext_fabric_type_to_payload(payload)

try:
self._set_fabric_create_endpoint(payload)
except ValueError as error:
Expand Down
46 changes: 46 additions & 0 deletions plugins/module_utils/fabric/fabric_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,28 @@ def _init_fabric_types(self) -> None:
self._fabric_type_to_feature_name_map["VXLAN_EVPN"] = "vxlan"
self._fabric_type_to_feature_name_map["VXLAN_EVPN_MSD"] = "vxlan"

# Map fabric type to the value that the controller GUI displays
# in the Fabric Type column at NDFC -> Manage -> Fabrics
# This is needed only for fabrics that use the External_Fabric
# template, e.g. ISN, and will be inserted into the POST request
# payload for external fabrics as (in the case of ISN fabric type):
# "EXT_FABRIC_TYPE": "Multi-Site External Network"
#
# Exposed via property fabric_type_to_ext_fabric_type_map
self._fabric_type_to_ext_fabric_type_map = {}
self._fabric_type_to_ext_fabric_type_map["ISN"] = "Multi-Site External Network"

self._valid_fabric_types = sorted(self._fabric_type_to_template_name_map.keys())

# self._external_fabric_types is used in conjunction with
# self._fabric_type_to_ext_fabric_type_map. This is used in (at least)
# FabricCreateCommon() to determine if EXT_FABRIC_TYPE key needs to be
# added to a payload.
#
# Exposed via property external_fabric_types
self._external_fabric_types = set()
self._external_fabric_types.add("ISN")

self._mandatory_parameters_all_fabrics = []
self._mandatory_parameters_all_fabrics.append("FABRIC_NAME")
self._mandatory_parameters_all_fabrics.append("FABRIC_TYPE")
Expand Down Expand Up @@ -128,6 +148,19 @@ def _init_properties(self) -> None:
self._properties["template_name"] = None
self._properties["valid_fabric_types"] = self._valid_fabric_types

@property
def external_fabric_types(self):
"""
# Summary

set() containing all external fabric types e.g. ISN.

# Raises

None
"""
return self._external_fabric_types

@property
def fabric_type(self):
"""
Expand All @@ -150,6 +183,19 @@ def fabric_type(self, value):
raise ValueError(msg)
self._properties["fabric_type"] = value

@property
def fabric_type_to_ext_fabric_type_map(self):
"""
# Summary

Returns a dictionary, keyed on fabric_type (e.g. "ISN"),
whose value is a string that the NDFC GUI uses to describe the
external fabric type. See the Fabric Type column at
NDFC -> Manage -> Fabrics for an example of how this is used
by the NDFC GUI.
"""
return self._fabric_type_to_ext_fabric_type_map

@property
def feature_name(self):
"""
Expand Down
Loading