Skip to content

Handle case when update_status.json doesnt exist #174

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

Merged
merged 4 commits into from
Mar 21, 2023
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 changelogs/fragments/version_update_status_info_bugfix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
bugfixes:
- This fix now handles case if no update_status.json exists yet. (https://github.com/ScaleComputing/HyperCoreAnsibleCollection/pull/174)
15 changes: 15 additions & 0 deletions examples/version_update_status_info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
- name: Show HyperCore software version update status info
hosts: localhost
connection: local
gather_facts: false

tasks:
# ------------------------------------------------------
- name: Get version update status
scale_computing.hypercore.version_update_status_info:
register: version_update_status_info_result

- name: Show version update status
debug:
var: version_update_status_info_result
8 changes: 5 additions & 3 deletions plugins/module_utils/hypercore_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ def __eq__(self, other: object) -> bool:
)

@classmethod
def get(cls, rest_client: RestClient, check_mode: bool = False) -> UpdateStatus:
update_status = rest_client.client.get("update/update_status.json").json
return cls.from_hypercore(update_status)
def get(cls, rest_client: RestClient) -> Optional[UpdateStatus]:
response = rest_client.client.get("update/update_status.json")
if response.status == 404: # .get() lets 200 and 404 through
return None
return cls.from_hypercore(response.json)
9 changes: 7 additions & 2 deletions plugins/modules/version_update_status_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
record:
description:
- Status of the latest update applied
- None/null is returned if no update was ever applied.
returned: success
type: dict
sample:
Expand All @@ -47,6 +48,7 @@
to_version: 9.1.18.209840
"""

from typing import Optional
from ansible.module_utils.basic import AnsibleModule

from ..module_utils import arguments, errors
Expand All @@ -56,8 +58,11 @@
from ..module_utils.typed_classes import TypedUpdateStatusToAnsible


def run(rest_client: RestClient) -> TypedUpdateStatusToAnsible:
return UpdateStatus.get(rest_client).to_ansible()
def run(rest_client: RestClient) -> Optional[TypedUpdateStatusToAnsible]:
status = UpdateStatus.get(rest_client)
if status:
return status.to_ansible()
return None


def main() -> None:
Expand Down
56 changes: 55 additions & 1 deletion tests/unit/plugins/module_utils/test_hypercore_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
__metaclass__ = type

import sys

import pytest
import json

from ansible_collections.scale_computing.hypercore.plugins.module_utils.hypercore_version import (
HyperCoreVersion,
Expand All @@ -22,6 +22,9 @@
from ansible_collections.scale_computing.hypercore.plugins.module_utils.utils import (
MIN_PYTHON_VERSION,
)
from ansible_collections.scale_computing.hypercore.plugins.module_utils.client import (
Response,
)

pytestmark = pytest.mark.skipif(
sys.version_info < MIN_PYTHON_VERSION,
Expand Down Expand Up @@ -351,3 +354,54 @@ def test_update_status_to_ansible(self):
)

assert update_status.to_ansible() == ansible_dict

def test_get_200(self, rest_client):
rest_client.client.get.return_value = Response(
status=200,
data=json.dumps(
dict(
prepareStatus="",
updateStatus=dict(
masterStateID="4",
masterState="COMPLETE",
fromBuild="207183",
toBuild="209840",
toVersion="9.1.18.209840",
currentComponent="2075",
totalComponents="2075",
percent="100",
status=dict(
component="7540/9999",
node="173.16.93.134",
statusdetails="Update Complete. Press 'Reload' to reconnect",
usernotes="Press 'Reload' to reconnect",
),
),
),
),
)
update_status = UpdateStatus.get(rest_client)

rest_client.client.get.assert_called_with("update/update_status.json")

assert update_status == UpdateStatus(
from_build="207183",
percent="100",
prepare_status="",
update_status="COMPLETE",
update_status_details="Update Complete. Press 'Reload' to reconnect",
usernotes="Press 'Reload' to reconnect",
to_build="209840",
to_version="9.1.18.209840",
)

def test_get_404(self, rest_client):
rest_client.client.get.return_value = Response(
status=404,
data="",
)
update_status = UpdateStatus.get(rest_client)

rest_client.client.get.assert_called_with("update/update_status.json")

assert update_status is None
9 changes: 9 additions & 0 deletions tests/unit/plugins/modules/test_version_update_status_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,12 @@ def test_run(self, rest_client, mocker):
to_build="209840",
to_version="9.1.18.209840",
)

def test_run_no_record(self, rest_client, mocker):
mocker.patch(
"ansible_collections.scale_computing.hypercore.plugins.modules.version_update_status_info.UpdateStatus.get"
).return_value = None

record = version_update_status_info.run(rest_client)

assert record is None