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

Include addgroup function for source_port and destination_port. Debug json dumps. #67

Merged
merged 3 commits into from
Mar 30, 2020
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
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