diff --git a/TestingUserScript.py b/TestingUserScript.py index 20e0c65..9db3093 100644 --- a/TestingUserScript.py +++ b/TestingUserScript.py @@ -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') diff --git a/fmcapi/api_objects/__init__.py b/fmcapi/api_objects/__init__.py index 69eee49..05e4120 100644 --- a/fmcapi/api_objects/__init__.py +++ b/fmcapi/api_objects/__init__.py @@ -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.") @@ -202,4 +204,6 @@ "LoggingSettings", "Backup", "Usage", + "DynamicObject", + "DynamicObjectMappings" ] diff --git a/fmcapi/api_objects/object_services/__init__.py b/fmcapi/api_objects/object_services/__init__.py index 09a7a09..640ad94 100644 --- a/fmcapi/api_objects/object_services/__init__.py +++ b/fmcapi/api_objects/object_services/__init__.py @@ -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.") @@ -104,4 +106,6 @@ "VariableSets", "VlanGroupTags", "VlanTags", + "DynamicObjectMappings", + "DynamicObject" ] diff --git a/fmcapi/api_objects/object_services/dynamicobjectmappings.py b/fmcapi/api_objects/object_services/dynamicobjectmappings.py new file mode 100644 index 0000000..2a9bdc4 --- /dev/null +++ b/fmcapi/api_objects/object_services/dynamicobjectmappings.py @@ -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() + + + + + + + diff --git a/fmcapi/api_objects/object_services/dynamicobjects.py b/fmcapi/api_objects/object_services/dynamicobjects.py new file mode 100644 index 0000000..b8501cd --- /dev/null +++ b/fmcapi/api_objects/object_services/dynamicobjects.py @@ -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" + diff --git a/unit_tests/__init__.py b/unit_tests/__init__.py index ff86f8d..3009f5e 100644 --- a/unit_tests/__init__.py +++ b/unit_tests/__init__.py @@ -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.") @@ -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", @@ -144,4 +146,6 @@ "test__policy", "test__backup", "test__usage", + "test__dynamic_objects", + "test__dynamic_objects_mappings", ] diff --git a/unit_tests/dynamic_object_mappings.py b/unit_tests/dynamic_object_mappings.py new file mode 100644 index 0000000..b9603b8 --- /dev/null +++ b/unit_tests/dynamic_object_mappings.py @@ -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()}") + + + + + + + diff --git a/unit_tests/dynamic_objects.py b/unit_tests/dynamic_objects.py new file mode 100644 index 0000000..04bc792 --- /dev/null +++ b/unit_tests/dynamic_objects.py @@ -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}") +