Skip to content

Commit

Permalink
Forgot to commit image_policies.py and query.py
Browse files Browse the repository at this point in the history
Changes include:

- ImagePolicies: remove response, response_data, and ressult properties so that these are inherited from ImagePolicyCommon

- ImagePolicyQuery: Set _policies_to_query in __init__

- ImagePolicyQuery: Set failed to False in __init__

- ImagePolicyQuery: make image_policies private (i.e. self._image_policies)

- ImagePolicyQuery: policy_names setter. Add checks for empty list, and list containing other than string values.

- ImagePolicyQuery._get_policies_to_query: set self._policies_to_query rather than returning _policies_to_query (easier for unit tests).

- ImagePolicyQuery.commit: Don't re-instantiate ImagePolicies, use instance from __init__().
  • Loading branch information
allenrobel committed Feb 14, 2024
1 parent 04ce4e6 commit e5d1ce4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 48 deletions.
25 changes: 0 additions & 25 deletions plugins/module_utils/image_policy/image_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ def _init_properties(self):
# self.properties is already initialized in the parent class
self.properties["all_policies"] = None
self.properties["policy_name"] = None
self.properties["response_data"] = {}
self.properties["response"] = None
self.properties["result"] = None

def refresh(self):
"""
Expand Down Expand Up @@ -186,28 +183,6 @@ def name(self):
"""
return self._get("policyName")

@property
def response_data(self):
"""
Return the parsed data from the response as a dictionary,
keyed on policy_name.
"""
return self.properties["response_data"]

@property
def response(self):
"""
Return the raw response from the controller.
"""
return self.properties["response"]

@property
def result(self):
"""
Return the raw result.
"""
return self.properties["result"]

@property
def policy_name(self):
"""
Expand Down
59 changes: 36 additions & 23 deletions plugins/module_utils/image_policy/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

from ansible_collections.cisco.dcnm.plugins.module_utils.image_policy.common import \
ImagePolicyCommon
from ansible_collections.cisco.dcnm.plugins.module_utils.image_policy.endpoints import \
ApiEndpoints
from ansible_collections.cisco.dcnm.plugins.module_utils.image_policy.image_policies import \
ImagePolicies

Expand All @@ -50,12 +48,13 @@ def __init__(self, ansible_module):
msg = "ENTERED ImagePolicyQuery()"
self.log.debug(msg)

self._policies_to_query = []
self._build_properties()
self.endpoints = ApiEndpoints()
self.image_policies = ImagePolicies(self.ansible_module)
self._image_policies = ImagePolicies(self.ansible_module)

self.action = "query"
self.changed = False
self.failed = False

def _build_properties(self):
"""
Expand All @@ -80,20 +79,31 @@ def policy_names(self, value):
msg += f"got {type(value).__name__} for "
msg += f"value {value}"
self.ansible_module.fail_json(msg)
if len(value) == 0:
msg = f"{self.class_name}.{method_name}: "
msg += "policy_names must be a list of at least one string. "
msg += f"got {value}."
self.ansible_module.fail_json(msg)
for item in value:
if not isinstance(item, str):
msg = f"{self.class_name}.{method_name}: "
msg += "policy_names must be a list of strings. "
msg += f"got {type(item).__name__} for "
msg += f"value {item}"
self.ansible_module.fail_json(msg)
self.properties["policy_names"] = value

def _get_policies_to_query(self):
def _get_policies_to_query(self) -> None:
"""
Retrieve policies from the controller and return the list of
Retrieve policies from the controller and set the list of
controller policies that are in our policy_names list.
"""
self.image_policies.refresh()
self._image_policies.refresh()

policies_to_query = []
self._policies_to_query = []
for policy_name in self.policy_names:
if policy_name in self.image_policies.all_policies:
policies_to_query.append(policy_name)
return policies_to_query
if policy_name in self._image_policies.all_policies:
self._policies_to_query.append(policy_name)

def commit(self):
"""
Expand All @@ -105,23 +115,26 @@ def commit(self):
msg += "policy_names must be set prior to calling commit."
self.ansible_module.fail_json(msg, **self.failed_result)

policies_to_query = self._get_policies_to_query()
self._get_policies_to_query()

if len(policies_to_query) == 0:
msg = f"self._policies_to_query: {self._policies_to_query}"
self.log.debug(msg)
if len(self._policies_to_query) == 0:
self.changed = False
self.failed = False
return

msg = f"Querying policies {policies_to_query}"
msg = f"Querying policies {self._policies_to_query}"
self.log.debug(msg)

instance = ImagePolicies(self.ansible_module)
instance.refresh()
self._image_policies.refresh()

for policy_name in policies_to_query:
if policy_name in instance.all_policies:
policy = copy.deepcopy(instance.all_policies[policy_name])
for policy_name in self._policies_to_query:
if policy_name in self._image_policies.all_policies:
policy = copy.deepcopy(self._image_policies.all_policies[policy_name])
policy["action"] = self.action
self.diff = policy
self.response = copy.deepcopy(instance.response)
self.response_current = copy.deepcopy(instance.response)
self.result = copy.deepcopy(instance.result)
self.result_current = copy.deepcopy(instance.result_current)
self.response = copy.deepcopy(self._image_policies.response)
self.response_current = copy.deepcopy(self._image_policies.response_current)
self.result = copy.deepcopy(self._image_policies.result)
self.result_current = copy.deepcopy(self._image_policies.result_current)

0 comments on commit e5d1ce4

Please sign in to comment.