-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #169 from AmrithaNagarajan/support-dynamic-object
fmcapi to support dynamic network objects
- Loading branch information
Showing
8 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
fmcapi/api_objects/object_services/dynamicobjectmappings.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}") | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") | ||
|