Skip to content

Commit

Permalink
Merge pull request #169 from AmrithaNagarajan/support-dynamic-object
Browse files Browse the repository at this point in the history
fmcapi to support dynamic network objects
  • Loading branch information
marksull authored Dec 11, 2023
2 parents e2d41ca + 6718321 commit 7a5e629
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 1 deletion.
3 changes: 3 additions & 0 deletions TestingUserScript.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,11 @@ def main():
unit_tests.test__backup(fmc=fmc1) # Delete needs existing backup and may need an increased fmc timeout for large backups
unit_tests.test__objects_get_query_filters(fmc=fmc1)
unit_tests.test__usage(fmc=fmc1)
unit_tests.test__dynamic_objects(fmc=fmc1)
unit_tests.test__dynamic_objects_mappings(fmc=fmc1)
"""


"""
# Need FTD device to test
unit_tests.test__hitcounts(fmc=fmc1, device_name='hq-ftd')
Expand Down
4 changes: 4 additions & 0 deletions fmcapi/api_objects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@
from .update_packages.listapplicabledevices import ListApplicableDevices
from .update_packages.upgradepackage import Upgrades
from .update_packages.upgradepackages import UpgradePackages
from .object_services.dynamicobjects import DynamicObject
from .object_services.dynamicobjectmappings import DynamicObjectMappings

logging.debug("In the api_objects __init__.py file.")

Expand Down Expand Up @@ -202,4 +204,6 @@
"LoggingSettings",
"Backup",
"Usage",
"DynamicObject",
"DynamicObjectMappings"
]
4 changes: 4 additions & 0 deletions fmcapi/api_objects/object_services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
from .variablesets import VariableSets
from .vlangrouptags import VlanGroupTags
from .vlantags import VlanTags
from .dynamicobjects import DynamicObject
from .dynamicobjectmappings import DynamicObjectMappings

logging.debug("In the object_services __init__.py file.")

Expand Down Expand Up @@ -104,4 +106,6 @@
"VariableSets",
"VlanGroupTags",
"VlanTags",
"DynamicObjectMappings",
"DynamicObject"
]
86 changes: 86 additions & 0 deletions fmcapi/api_objects/object_services/dynamicobjectmappings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from fmcapi.api_objects.apiclasstemplate import APIClassTemplate
from .dynamicobjects import DynamicObject
import logging


class DynamicObjectMappings(APIClassTemplate):
"""The Dynamic Object in the FMC."""

VALID_JSON_DATA = []
VALID_FOR_KWARGS = VALID_JSON_DATA + []
URL_SUFFIX = "/object/dynamicobjectmappings"
VALID_CHARACTERS_FOR_NAME = """[.\w\d_\- ]"""

REQUIRED_FOR_POST = []
obj_id = ''

def __init__(self, fmc, **kwargs):
"""
Initialize Dynamic Object.
Set self.type to "DynamicObject" and parse the kwargs.
:param fmc: (object) FMC object
:param kwargs: Any other values passed during instantiation.
:return: None
"""
super().__init__(fmc, **kwargs)
logging.debug("In __init__() for Dynamic Object class.")
self.parse_kwargs(**kwargs)
if "id" in kwargs:
self.obj_id = kwargs["id"]

def mappings(self, action, value, name):

"""
Associate mappings to dynamic object.
:param action: (str) 'add', 'remove', or 'clear'
:param dynamic_objects: (DynamicObject) Dynamic object to be mapped.
"""
logging.debug("In mappings for dynamic object")

dynamic_object = DynamicObject(fmc=self.fmc, name=name)
response = dynamic_object.get()
if response:
new_obj = {
"dynamicObject": {
"name": response["name"],
"id": response["id"],
"type": response["type"]
},
"mappings": value
}
if action == "add":
if "add" in self.__dict__:
self.add.append(new_obj)

else:
self.add = [new_obj]

logging.info(f'Adding mappings to Dynamic Object.')

elif action == "remove":
if "remove" in self.__dict__:
self.remove.append(new_obj)

else:
self.remove = [new_obj]

logging.info(f'Remove mappings to Dynamic Object.')

else:
logging.warning(
f'Dynamic Object "{name}" is not found in FMC. Cannot add mappings.'
)
def get(self):
self.URL_SUFFIX = f"/object/dynamicobjects/{str(self.obj_id)}/mappings"
super().__init__(self.fmc)
return super().get()







33 changes: 33 additions & 0 deletions fmcapi/api_objects/object_services/dynamicobjects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from fmcapi.api_objects.apiclasstemplate import APIClassTemplate
import logging


class DynamicObject(APIClassTemplate):
"""The Dynamic Object in the FMC."""

VALID_JSON_DATA = ["id", "name", "description", "type", "objectType"]
VALID_FOR_KWARGS = VALID_JSON_DATA + []
URL_SUFFIX = "/object/dynamicobjects"
VALID_CHARACTERS_FOR_NAME = """[.\w\d_\- ]"""

VALID_GET_FILTERS = [
"unusedOnly",
"ids"
"nameStartsWith"
] # unusedOnly:Bool, "ids:id1,id2,..." ,nameStartsWith:String

def __init__(self, fmc, **kwargs):
"""
Initialize Dynamic Object.
Set self.type to "DynamicObject" and parse the kwargs.
:param fmc: (object) FMC object
:param kwargs: Any other values passed during instantiation.
:return: None
"""
super().__init__(fmc, **kwargs)
logging.debug("In __init__() for Dynamic Object class.")
self.parse_kwargs(**kwargs)
self.type = "DynamicObject"

6 changes: 5 additions & 1 deletion unit_tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
from .search_policy import test__policy
from .backup import test__backup
from .usage import test__usage
from .dynamic_objects import test__dynamic_objects
from .dynamic_object_mappings import test__dynamic_objects_mappings

logging.debug("In the unit-tests __init__.py file.")

Expand Down Expand Up @@ -106,7 +108,7 @@
"test__fmc_version",
"test__variable_set",
"test__ip_host",
"test__objects_get_query_filters"
"test__objects_get_query_filters",
"test__ip_network",
"test__ip_range",
"test__extended_acls",
Expand Down Expand Up @@ -144,4 +146,6 @@
"test__policy",
"test__backup",
"test__usage",
"test__dynamic_objects",
"test__dynamic_objects_mappings",
]
20 changes: 20 additions & 0 deletions unit_tests/dynamic_object_mappings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import logging
import fmcapi


def test__dynamic_objects_mappings(fmc):
logging.info("Testing Dynamic Objects Mappings")

obj1 = fmcapi.DynamicObjectMappings(fmc)
obj1.mappings(action="add", value=["10.0.0.4"], name="Sales")
obj1.post()

obj2 = fmcapi.DynamicObjectMappings(fmc=fmc, id="0050568E-B2F8-0ed3-0000-519691059889")
logging.info(f"Dynamic object mapping {obj2.get()}")







18 changes: 18 additions & 0 deletions unit_tests/dynamic_objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import logging
import fmcapi


def test__dynamic_objects(fmc):
logging.info("Testing Dynamic Objects.")
result = fmcapi.DynamicObject(fmc=fmc).get()
logging.info(f"All Dynamic Objects ---> {result}")
logging.info(f"Total items: {len(result['items'])}")

obj1 = fmcapi.DynamicObject(fmc=fmc, name="Platform",
description="IPs of Engineer department",
type="DynamicObject", objectType="IP",).post()

obj1 = fmcapi.DynamicObject(fmc=fmc, name="NWS")
obj1.get()
logging.info(f"One objectby name----> {obj1}")

0 comments on commit 7a5e629

Please sign in to comment.