Skip to content

Commit

Permalink
Merge pull request HewlettPackard#63 from Rainforest/srv_hw_mpFirmwar…
Browse files Browse the repository at this point in the history
…eVersion

Add support to mpFirmwareVersion
  • Loading branch information
Camila Balestrin committed Aug 5, 2016
2 parents c517cf3 + c787db0 commit 6f9bc31
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
9 changes: 9 additions & 0 deletions examples/oneview_server_hardware.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@
delegate_to: localhost


- name: Update the Server Hardware iLO firmware version
oneview_server_hardware:
config: "{{ config }}"
state: ilo_firmware_version_updated
data:
hostname : '{{ server_hardware_hostname }}'
delegate_to: localhost


- name: Remove the server hardware by its IP
oneview_server_hardware:
config: "{{ config }}"
Expand Down
20 changes: 16 additions & 4 deletions library/oneview_server_hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
'absent' will remove the resource from OneView, if it exists.
'power_state_set' will set the power state of the Server Hardware.
'refresh_state_set will set the refresh state of the Server Hardware.
choices: ['present', 'absent', 'set_power_state', 'set_refresh_state']
'ilo_firmware_version_updated' will update the iLO firmware version of the Server Hardware.
choices: ['present', 'absent', 'set_power_state', 'set_refresh_state', 'ilo_firmware_version_updated']
required: true
data:
description:
Expand All @@ -59,8 +60,8 @@
state: present
data:
hostname : "172.18.6.15"
username : ""
password : ""
username : "username"
password : "password"
force : false
licensingIntent: "OneView"
configurationState: "Managed"
Expand Down Expand Up @@ -103,6 +104,7 @@
SERVER_HARDWARE_MANDATORY_FIELD_MISSING = "Mandatory field was not informed: data.hostname"
SERVER_HARDWARE_POWER_STATE_UPDATED = 'Server Hardware power state changed successfully.'
SERVER_HARDWARE_REFRESH_STATE_UPDATED = 'Server Hardware refresh state changed successfully.'
SERVER_HARDWARE_ILO_FIRMWARE_VERSION_UPDATED = 'Server Hardware iLO firmware version updated successfully.'
SERVER_HARDWARE_NOT_FOUND = 'Server Hardware is required for this operation.'


Expand All @@ -111,7 +113,7 @@ class ServerHardwareModule(object):
config=dict(required=True, type='str'),
state=dict(
required=True,
choices=['present', 'absent', 'power_state_set', 'refresh_state_set']
choices=['present', 'absent', 'power_state_set', 'refresh_state_set', 'ilo_firmware_version_updated']
),
data=dict(required=True, type='dict')
)
Expand Down Expand Up @@ -139,6 +141,8 @@ def run(self):
changed, msg, ansible_facts = self.__set_power_state(data, resource)
elif state == 'refresh_state_set':
changed, msg, ansible_facts = self.__set_refresh_state(data, resource)
elif state == 'ilo_firmware_version_updated':
changed, msg, ansible_facts = self.__update_mp_firware_version(resource)

self.module.exit_json(changed=changed,
msg=msg,
Expand Down Expand Up @@ -184,6 +188,14 @@ def __set_refresh_state(self, data, resource):

return True, SERVER_HARDWARE_REFRESH_STATE_UPDATED, dict(oneview_server_hardware=resource)

def __update_mp_firware_version(self, resource):
if not resource:
raise Exception(SERVER_HARDWARE_NOT_FOUND)

resource = self.oneview_client.server_hardware.update_mp_firware_version(resource['uri'])

return True, SERVER_HARDWARE_ILO_FIRMWARE_VERSION_UPDATED, dict(oneview_server_hardware=resource)


def main():
ServerHardwareModule().run()
Expand Down
48 changes: 47 additions & 1 deletion test/test_oneview_server_hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
from hpOneView.oneview_client import OneViewClient
from oneview_server_hardware import ServerHardwareModule, SERVER_HARDWARE_ADDED, SERVER_HARDWARE_ALREADY_ADDED, \
SERVER_HARDWARE_DELETED, SERVER_HARDWARE_ALREADY_ABSENT, SERVER_HARDWARE_MANDATORY_FIELD_MISSING, \
SERVER_HARDWARE_POWER_STATE_UPDATED, SERVER_HARDWARE_NOT_FOUND, SERVER_HARDWARE_REFRESH_STATE_UPDATED
SERVER_HARDWARE_POWER_STATE_UPDATED, SERVER_HARDWARE_NOT_FOUND, SERVER_HARDWARE_REFRESH_STATE_UPDATED, \
SERVER_HARDWARE_ILO_FIRMWARE_VERSION_UPDATED

FAKE_MSG_ERROR = 'Fake message error'

Expand Down Expand Up @@ -62,6 +63,13 @@
refreshState : "RefreshPending"
"""

YAML_SERVER_HARDWARE_ILO_FIRMWARE = """
config: "{{ config }}"
state: ilo_firmware_version_updated
data:
hostname : '{{ server_hardware_hostname }}'
"""

DICT_DEFAULT_SERVER_HARDWARE = yaml.load(YAML_SERVER_HARDWARE)["data"]


Expand Down Expand Up @@ -277,5 +285,43 @@ def test_should_fail_when_the_server_hardware_was_not_found(self, mock_ansible_m
)


class ServerHardwareIloFirmwareUpdateStateSpec(unittest.TestCase):
@mock.patch.object(OneViewClient, 'from_json_file')
@mock.patch('oneview_server_hardware.AnsibleModule')
def test_should_set_refresh_state(self, mock_ansible_module, mock_ov_client_from_json_file):
mock_ov_instance = mock.Mock()
mock_ov_instance.server_hardware.get_by.return_value = [{"uri": "resourceuri"}]
mock_ov_instance.server_hardware.update_mp_firware_version.return_value = {"name": "name"}

mock_ov_client_from_json_file.return_value = mock_ov_instance
mock_ansible_instance = create_ansible_mock(YAML_SERVER_HARDWARE_ILO_FIRMWARE)
mock_ansible_module.return_value = mock_ansible_instance

ServerHardwareModule().run()

mock_ansible_instance.exit_json.assert_called_once_with(
changed=True,
msg=SERVER_HARDWARE_ILO_FIRMWARE_VERSION_UPDATED,
ansible_facts=dict(oneview_server_hardware={"name": "name"})
)

@mock.patch.object(OneViewClient, 'from_json_file')
@mock.patch('oneview_server_hardware.AnsibleModule')
def test_should_fail_when_the_server_hardware_was_not_found(self, mock_ansible_module,
mock_ov_client_from_json_file):
mock_ov_instance = mock.Mock()
mock_ov_instance.server_hardware.get_by.return_value = []

mock_ov_client_from_json_file.return_value = mock_ov_instance
mock_ansible_instance = create_ansible_mock(YAML_SERVER_HARDWARE_ILO_FIRMWARE)
mock_ansible_module.return_value = mock_ansible_instance

ServerHardwareModule().run()

mock_ansible_instance.fail_json.assert_called_once_with(
msg=SERVER_HARDWARE_NOT_FOUND
)


if __name__ == '__main__':
unittest.main()

0 comments on commit 6f9bc31

Please sign in to comment.