Skip to content

Commit

Permalink
Merge pull request #67 from MysticRyuujin/master
Browse files Browse the repository at this point in the history
Include addgroup function for source_port and destination_port. Debug json dumps.
  • Loading branch information
daxm authored Mar 30, 2020
2 parents 962a21f + 7bbb447 commit b79c5f7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
3 changes: 3 additions & 0 deletions fmcapi/api_objects/apiclasstemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ def get(self, **kwargs):
)
if "id" not in self.__dict__:
logging.warning(
f"\tGET query for {self.name} is not found."
)
logging.debug(
f"\tGET query for {self.name} is not found.\n\t\tResponse: {json.dumps(response)}"
)
else:
Expand Down
68 changes: 66 additions & 2 deletions fmcapi/api_objects/policy_services/accessrules.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ def source_port(self, action, name=""):
"""
Add/modify name to sourcePorts field of AccessRules object.
:param action: (str) 'add', 'remove', or 'clear'
:param action: (str) 'add', 'addgroup', 'remove', or 'clear'
:param name: (str) Name of Port in FMC.
:return: None
"""
Expand Down Expand Up @@ -511,6 +511,38 @@ def source_port(self, action, name=""):
f'Protocol Port or Protocol Port Group: "{name}", '
f"not found. Cannot add to AccessRules."
)
elif action == "addgroup":
item = PortObjectGroups(fmc=self.fmc)
item.get(name=name)
if "id" in item.__dict__:
if "sourcePorts" in self.__dict__:
new_port = {"name": item.name, "id": item.id, "type": item.type}
duplicate = False
if "objects" not in self.sourcePorts:
self.__dict__["sourcePorts"]["objects"] = []
for obj in self.sourcePorts["objects"]:
if obj["name"] == new_port["name"]:
duplicate = True
break
if not duplicate:
self.sourcePorts["objects"].append(new_port)
logging.info(
f'Adding "{name}" to sourcePorts for this AccessRules.'
)
else:
self.sourcePorts = {
"objects": [
{"name": item.name, "id": item.id, "type": item.type}
]
}
logging.info(
f'Adding "{name}" to sourcePorts for this AccessRules.'
)
else:
logging.warning(
f'Protocol Port Port Group: "{name}", '
f"not found. Cannot add to AccessRules."
)
elif action == "remove":
pport_json = ProtocolPortObjects(fmc=self.fmc)
pport_json.get(name=name)
Expand Down Expand Up @@ -547,7 +579,7 @@ def destination_port(self, action, name=""):
"""
Add/modify name to destinationPorts field of AccessRules object.
:param action: (str) 'add', 'remove', or 'clear'
:param action: (str) 'add', 'addgroup', 'remove', or 'clear'
:param name: (str) Name of Port in FMC.
:return: None
"""
Expand Down Expand Up @@ -589,6 +621,38 @@ def destination_port(self, action, name=""):
f'Protocol Port or Protocol Port Group: "{name}", '
f"not found. Cannot add to AccessRules."
)
if action == "addgroup":
item = PortObjectGroups(fmc=self.fmc)
item.get(name=name)
if "id" in item.__dict__:
if "destinationPorts" in self.__dict__:
new_port = {"name": item.name, "id": item.id, "type": item.type}
duplicate = False
if "objects" not in self.destinationPorts:
self.__dict__["destinationPorts"]["objects"] = []
for obj in self.destinationPorts["objects"]:
if obj["name"] == new_port["name"]:
duplicate = True
break
if not duplicate:
self.destinationPorts["objects"].append(new_port)
logging.info(
f'Adding "{name}" to destinationPorts for this AccessRules.'
)
else:
self.destinationPorts = {
"objects": [
{"name": item.name, "id": item.id, "type": item.type}
]
}
logging.info(
f'Adding "{name}" to destinationPorts for this AccessRules.'
)
else:
logging.warning(
f'Protocol Port Port Group: "{name}", '
f"not found. Cannot add to AccessRules."
)
elif action == "remove":
pport_json = ProtocolPortObjects(fmc=self.fmc)
pport_json.get(name=name)
Expand Down

0 comments on commit b79c5f7

Please sign in to comment.