Skip to content

Commit

Permalink
Changing behavior of ImagePolicies, more...
Browse files Browse the repository at this point in the history
ImagePolicies() called fail_json() when the caller asked for a policy_name that did not exist on the controller.

During initial work on dcnm_image_policy module, we need this to return None instead.  Hence, changing the behavior.

This does not require any other changes to dcnm_image_upgrade, other than modifying one of the unit tests to expect the new behavior.
  • Loading branch information
allenrobel committed Dec 27, 2023
1 parent 3701dbd commit 55dd467
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
17 changes: 13 additions & 4 deletions plugins/module_utils/image_mgmt/image_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ def _get(self, item):
self.module.fail_json(msg)

if self.policy_name not in self.properties["response_data"]:
msg = f"{self.class_name}.{self.method_name}: "
msg += f"policy_name {self.policy_name} is not defined "
msg += "on the controller."
self.module.fail_json(msg)
return None

if item == "policy":
return self.properties["response_data"][self.policy_name]

if item not in self.properties["response_data"][self.policy_name]:
msg = f"{self.class_name}.{self.method_name}: "
Expand Down Expand Up @@ -195,6 +195,15 @@ def policy_name(self):
def policy_name(self, value):
self.properties["policy_name"] = value

@property
def policy(self):
"""
Return the policy data of the policy matching self.policy_name,
if it exists.
Return None otherwise
"""
return self._get("policy")

@property
def policy_type(self):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,11 @@ def test_image_mgmt_image_policies_00024(monkeypatch, image_policies) -> None:
"""
Function
- refresh
- policy_name
Test
- fail_json() is called if response does not contain policy_name.
- i.e. image policy with name FOO has not yet been created on the controller.
- instance.policy_name is set to a policy that does not exist on the controller.
- instance.policy returns None
Endpoint
- /appcenter/cisco/ndfc/api/v1/imagemanagement/rest/policymgnt/policies
Expand All @@ -243,16 +244,19 @@ def mock_dcnm_send_image_policies(*args) -> Dict[str, Any]:

monkeypatch.setattr(DCNM_SEND_IMAGE_POLICIES, mock_dcnm_send_image_policies)

image_policies.refresh()
image_policies.policy_name = "FOO"
with does_not_raise():
instance = image_policies
instance.refresh()
image_policies.policy_name = "FOO"

match = "ImagePolicies._get: "
match += "policy_name FOO is not defined on the controller."
assert image_policies.policy is None
# match = "ImagePolicies._get: "
# match += "policy_name FOO is not defined on the controller."

instance = image_policies
with pytest.raises(AnsibleFailJson, match=match):
if instance.policy_type == "PLATFORM":
pass
# instance = image_policies
# with pytest.raises(AnsibleFailJson, match=match):
# if instance.policy_type == "PLATFORM":
# pass


def test_image_mgmt_image_policies_00025(monkeypatch, image_policies) -> None:
Expand Down

0 comments on commit 55dd467

Please sign in to comment.