Skip to content

Commit

Permalink
Merge pull request ansible#48 from HewlettPackard/sas-logical-interco…
Browse files Browse the repository at this point in the history
…nnect-facts

Add support to SAS Logical interconnect Facts
  • Loading branch information
GustavoHennig committed Apr 12, 2017
2 parents 80b0791 + bd465c3 commit 005bd90
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (2016-2017) Hewlett Packard Enterprise Development LP
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.

ANSIBLE_METADATA = {'status': ['stableinterface'],
'supported_by': 'curated',
'metadata_version': '1.0'}

DOCUMENTATION = '''
---
module: oneview_sas_logical_interconnect_facts
short_description: Retrieve facts about one or more of the OneView SAS Logical Interconnects.
description:
- Retrieve facts about one or more of the OneView SAS Logical Interconnects.
version_added: "2.3"
requirements:
- "python >= 2.7.9"
- "hpOneView >= 3.0.0"
author:
- "Gustavo Hennig (@GustavoHennig)"
options:
name:
description:
- SAS Logical Interconnect name.
required: false
options:
description:
- List with options to gather additional facts about SAS Logical Interconnect.
C(firmware) gets the installed firmware for a SAS Logical Interconnect.
- These options are valid just when a C(name) is provided. Otherwise it will be ignored.
required: false
notes:
- This resource is only available on HPE Synergy
extends_documentation_fragment:
- oneview
- oneview.factsparams
'''

EXAMPLES = '''
- name: Gather facts about all SAS Logical Interconnects
oneview_sas_logical_interconnect_facts:
config: "{{ config }}"
delegate_to: localhost
- debug: var=sas_logical_interconnects
- name: Gather paginated, filtered and sorted facts about SAS Logical Interconnects
oneview_sas_logical_interconnect_facts:
config: "{{ config }}"
params:
start: 0
count: 2
sort: 'name:descending'
filter: "status='OK'"
- debug: var=sas_logical_interconnects
- name: Gather facts about a SAS Logical Interconnect by name
oneview_sas_logical_interconnect_facts:
config: "{{ config }}"
name: "LOG_EN-LIG_SAS-1"
delegate_to: localhost
- debug: var=sas_logical_interconnects
- name: Gather facts about an installed firmware for a SAS Logical Interconnect that matches the specified name
oneview_sas_logical_interconnect_facts:
config: "{{ config }}"
name: "LOG_EN-LIG_SAS-1"
options:
- firmware
delegate_to: localhost
- debug: var=sas_logical_interconnect_firmware
'''

RETURN = '''
sas_logical_interconnects:
description: The list of SAS Logical Interconnects.
returned: Always, but can be null.
type: list
sas_logical_interconnect_firmware:
description: The installed firmware for a SAS Logical Interconnect.
returned: When requested, but can be null.
type: complex
'''

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.oneview import OneViewModuleBase


class SasLogicalInterconnectFactsModule(OneViewModuleBase):
def __init__(self):
argument_spec = dict(
name=dict(required=False, type='str'),
options=dict(required=False, type='list'),
params=dict(required=False, type='dict')
)

super(SasLogicalInterconnectFactsModule, self).__init__(additional_arg_spec=argument_spec)

self.resource_client = self.oneview_client.sas_logical_interconnects

def execute_module(self):
ansible_facts = {}

if self.module.params['name']:
sas_logical_interconnects = self.resource_client.get_by('name', self.module.params['name'])

if sas_logical_interconnects and self.options:
options_facts = self.__gather_option_facts(sas_logical_interconnects[0])
ansible_facts.update(options_facts)
else:
sas_logical_interconnects = self.resource_client.get_all(**self.facts_params)

ansible_facts['sas_logical_interconnects'] = sas_logical_interconnects

return dict(changed=False, ansible_facts=ansible_facts)

def __gather_option_facts(self, resource):
ansible_facts = {}

if self.options.get('firmware'):
ansible_facts['sas_logical_interconnect_firmware'] = self.resource_client.get_firmware(resource['uri'])

return ansible_facts


def main():
SasLogicalInterconnectFactsModule().run()


if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion test/units/modules/cloud/hpe/oneview_module_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
from ansible.modules.cloud.hpe.oneview_sas_interconnect_facts import SasInterconnectFactsModule
from ansible.modules.cloud.hpe.oneview_sas_interconnect_type_facts import SasInterconnectTypeFactsModule
from ansible.modules.cloud.hpe.oneview_sas_logical_interconnect import SasLogicalInterconnectModule
# from ansible.modules.cloud.hpe.oneview_sas_logical_interconnect_facts import SasLogicalInterconnectFactsModule
from ansible.modules.cloud.hpe.oneview_sas_logical_interconnect_facts import SasLogicalInterconnectFactsModule
from ansible.modules.cloud.hpe.oneview_sas_logical_interconnect_group import SasLogicalInterconnectGroupModule
from ansible.modules.cloud.hpe.oneview_sas_logical_interconnect_group_facts import SasLogicalInterconnectGroupFactsModule
from ansible.modules.cloud.hpe.oneview_sas_logical_jbod_attachment_facts import SasLogicalJbodAttachmentFactsModule
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (2016-2017) Hewlett Packard Enterprise Development LP
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.


import unittest

from oneview_module_loader import SasLogicalInterconnectFactsModule
from hpe_test_utils import FactsParamsTestCase

ERROR_MSG = 'Fake message error'

PARAMS_GET_ALL = dict(
config='config.json',
name=None
)

PARAMS_GET_BY_NAME = dict(
config='config.json',
name="Sas Logical Interconnect Name",
)

PARAMS_GET_BY_NAME_WITH_FIRMWARE = dict(
config='config.json',
name="Sas Logical Interconnect Name",
options=['firmware']
)

SAS_LOGICAL_INTERCONNECT = dict(
name="Sas Logical Interconnect Name",
uri="/rest/sas-logical-interconnects/d1c7b09a-6c7b-4ae0-b68e-ed208ccde1b0"
)

ALL_INTERCONNECTS = [SAS_LOGICAL_INTERCONNECT]


class SasLogicalInterconnectFactsSpec(unittest.TestCase,
FactsParamsTestCase):

def setUp(self):
self.configure_mocks(self, SasLogicalInterconnectFactsModule)
self.resource = self.mock_ov_client.sas_logical_interconnects
FactsParamsTestCase.configure_client_mock(self, self.resource)

def test_should_get_all_sas_logical_interconnects(self):
self.resource.get_all.return_value = ALL_INTERCONNECTS
self.mock_ansible_module.params = PARAMS_GET_ALL

SasLogicalInterconnectFactsModule().run()

self.mock_ansible_module.exit_json.assert_called_once_with(
changed=False,
ansible_facts=dict(sas_logical_interconnects=ALL_INTERCONNECTS)
)

def test_should_get_a_sas_logical_interconnects_by_name(self):
self.resource.get_by.return_value = ALL_INTERCONNECTS
self.mock_ansible_module.params = PARAMS_GET_BY_NAME

SasLogicalInterconnectFactsModule().run()

self.mock_ansible_module.exit_json.assert_called_once_with(
changed=False,
ansible_facts=dict(sas_logical_interconnects=[SAS_LOGICAL_INTERCONNECT])
)

def test_should_get_a_sas_logical_interconnects_by_name_with_firmware(self):
self.resource.get_by.return_value = ALL_INTERCONNECTS
self.resource.get_firmware.return_value = {"firmware": "data"}
self.mock_ansible_module.params = PARAMS_GET_BY_NAME_WITH_FIRMWARE

SasLogicalInterconnectFactsModule().run()

self.resource.get_firmware.assert_called_once_with(SAS_LOGICAL_INTERCONNECT['uri'])

self.mock_ansible_module.exit_json.assert_called_once_with(
changed=False,
ansible_facts=dict(
sas_logical_interconnects=[SAS_LOGICAL_INTERCONNECT],
sas_logical_interconnect_firmware={"firmware": "data"}
)
)


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

0 comments on commit 005bd90

Please sign in to comment.