Skip to content

Commit

Permalink
Refactoring Interconnect Link Topology module
Browse files Browse the repository at this point in the history
  • Loading branch information
marikrg committed Apr 7, 2017
1 parent d2f301e commit dbbcfe5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 90 deletions.
82 changes: 24 additions & 58 deletions library/oneview_interconnect_link_topology_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,31 @@
# See the License for the specific language governing permissions and
# limitations under the License.
###

from ansible.module_utils.basic import *

try:
from hpOneView.oneview_client import OneViewClient
from hpOneView.exceptions import HPOneViewException

HAS_HPE_ONEVIEW = True
except ImportError:
HAS_HPE_ONEVIEW = False
ANSIBLE_METADATA = {'metadata_version': '1.0',
'status': ['stableinterface'],
'supported_by': 'curated'}

DOCUMENTATION = '''
---
module: oneview_interconnect_link_topology_facts
short_description: Retrieve facts about the OneView Interconnect Link Topologies.
description:
- Retrieve facts about the Interconnect Link Topologies from OneView.
version_added: "2.3"
requirements:
- "python >= 2.7.9"
- "hpOneView >= 3.0.0"
author: "Mariana Kreisig (@marikrg)"
options:
config:
description:
- Path to a .json configuration file containing the OneView client configuration.
The configuration file is optional. If the file path is not provided, the configuration will be loaded from
environment variables.
required: false
params:
description:
- List of params to delimit, filter and sort the list of resources.
- "params allowed:
'start': The first item to return, using 0-based indexing.
'count': The number of resources to return.
'filter': A general filter/query string to narrow the list of items returned.
'sort': The sort order of the returned data set."
required: false
name:
description:
- Name of the Interconnect Link Topology.
required: false
notes:
- "A sample configuration file for the config parameter can be found at:
https://github.com/HewlettPackard/oneview-ansible/blob/master/examples/oneview_config-rename.json"
- "Check how to use environment variables for configuration at:
https://github.com/HewlettPackard/oneview-ansible#environment-variables"
- This resource is only available on HPE Synergy.
extends_documentation_fragment:
- oneview
- oneview.factsparams
'''

EXAMPLES = '''
Expand Down Expand Up @@ -96,40 +74,28 @@
returned: Always, but can be null.
type: complex
'''
HPE_ONEVIEW_SDK_REQUIRED = 'HPE OneView Python SDK is required for this module.'

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

class InterconnectLinkTopologyFactsModule(object):
argument_spec = dict(
config=dict(required=False, type='str'),
name=dict(required=False, type='str'),
params=dict(required=False, type='dict'),
)

class InterconnectLinkTopologyFactsModule(OneViewModuleBase):
def __init__(self):
self.module = AnsibleModule(argument_spec=self.argument_spec, supports_check_mode=False)
if not HAS_HPE_ONEVIEW:
self.module.fail_json(msg=HPE_ONEVIEW_SDK_REQUIRED)

if not self.module.params['config']:
self.oneview_client = OneViewClient.from_environment_variables()
argument_spec = dict(
name=dict(required=False, type='str'),
params=dict(required=False, type='dict'),
)
super(InterconnectLinkTopologyFactsModule, self).__init__(additional_arg_spec=argument_spec)

def execute_module(self):
name = self.module.params.get('name')
if name:
interconnect_link_topologies = self.oneview_client.interconnect_link_topologies.get_by('name', name)
else:
self.oneview_client = OneViewClient.from_json_file(self.module.params['config'])

def run(self):
try:
if self.module.params.get('name'):
name = self.module.params.get('name')
interconnect_link_topologies = self.oneview_client.interconnect_link_topologies.get_by('name', name)
else:
params = self.module.params.get('params') or {}
interconnect_link_topologies = self.oneview_client.interconnect_link_topologies.get_all(**params)

self.module.exit_json(changed=False,
ansible_facts=dict(interconnect_link_topologies=interconnect_link_topologies))

except HPOneViewException as exception:
self.module.fail_json(msg='; '.join(str(e) for e in exception.args))
interconnect_link_topologies = self.oneview_client.interconnect_link_topologies.get_all(**self.facts_params)

return dict(changed=False,
ansible_facts=dict(interconnect_link_topologies=interconnect_link_topologies))


def main():
Expand Down
6 changes: 4 additions & 2 deletions oneview-ansible.md
Original file line number Diff line number Diff line change
Expand Up @@ -3192,7 +3192,7 @@ Retrieve facts about the OneView Interconnect Link Topologies.
| ------------- |-------------| ---------|----------- |--------- |
| config | No | | | Path to a .json configuration file containing the OneView client configuration. The configuration file is optional. If the file path is not provided, the configuration will be loaded from environment variables. |
| name | No | | | Name of the Interconnect Link Topology. |
| params | No | | | List of params to delimit, filter and sort the list of resources. params allowed: 'start': The first item to return, using 0-based indexing. 'count': The number of resources to return. 'filter': A general filter/query string to narrow the list of items returned. 'sort': The sort order of the returned data set. |
| params | No | | | List of params to delimit, filter and sort the list of resources. params allowed: `start`: The first item to return, using 0-based indexing. `count`: The number of resources to return. `filter`: A general filter/query string to narrow the list of items returned. `sort`: The sort order of the returned data set. |



Expand Down Expand Up @@ -3236,11 +3236,13 @@ Retrieve facts about the OneView Interconnect Link Topologies.

#### Notes

- This resource is only available on HPE Synergy.

- A sample configuration file for the config parameter can be found at: https://github.com/HewlettPackard/oneview-ansible/blob/master/examples/oneview_config-rename.json

- Check how to use environment variables for configuration at: https://github.com/HewlettPackard/oneview-ansible#environment-variables

- This resource is only available on HPE Synergy.
- Additional Playbooks for the HPE OneView Ansible modules can be found at: https://github.com/HewlettPackard/oneview-ansible/tree/master/examples


---
Expand Down
55 changes: 25 additions & 30 deletions test/test_oneview_interconnect_link_topology_facts.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
###
# Copyright (2016) Hewlett Packard Enterprise Development LP
#
Expand All @@ -13,63 +15,56 @@
# See the License for the specific language governing permissions and
# limitations under the License.
###

import unittest

from oneview_interconnect_link_topology_facts import InterconnectLinkTopologyFactsModule
from test.utils import FactsParamsTestCase
from test.utils import ModuleContructorTestCase
from test.utils import ErrorHandlingTestCase
from oneview_module_loader import InterconnectLinkTopologyFactsModule
from hpe_test_utils import FactsParamsTestCase

ERROR_MSG = 'Fake message error'

PARAMS_GET_ALL = dict(
config='config.json',
name=None
)
class InterconnectLinkTopologyFactsSpec(unittest.TestCase,
FactsParamsTestCase):

PARAMS_GET_BY_NAME = dict(
config='config.json',
name="Interconnect Link Topology Name 2"
)
INTERCONNECT_LINK_TOPOLOGIES = [{"name": "Interconnect Link Topology 1"},
{"name": "Interconnect Link Topology 2"},
{"name": "Interconnect Link Topology 3"}]

INTERCONNECT_LINK_TOPOLOGIES = [{"name": "Interconnect Link Topology 1"},
{"name": "Interconnect Link Topology 2"},
{"name": "Interconnect Link Topology 3"}]
ERROR_MSG = 'Fake message error'

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

PARAMS_GET_BY_NAME = dict(
config='config.json',
name="Interconnect Link Topology Name 2"
)

class InterconnectLinkTopologyFactsSpec(unittest.TestCase,
ModuleContructorTestCase,
FactsParamsTestCase,
ErrorHandlingTestCase):
def setUp(self):
self.configure_mocks(self, InterconnectLinkTopologyFactsModule)
self.interconnect_link_topologies = self.mock_ov_client.interconnect_link_topologies
FactsParamsTestCase.configure_client_mock(self, self.interconnect_link_topologies)
ErrorHandlingTestCase.configure(self, method_to_fire=self.interconnect_link_topologies.get_by)

def test_should_get_all_interconnect_link_topologies(self):
self.interconnect_link_topologies.get_all.return_value = INTERCONNECT_LINK_TOPOLOGIES

self.mock_ansible_module.params = PARAMS_GET_ALL
self.interconnect_link_topologies.get_all.return_value = self.INTERCONNECT_LINK_TOPOLOGIES
self.mock_ansible_module.params = self.PARAMS_GET_ALL

InterconnectLinkTopologyFactsModule().run()

self.mock_ansible_module.exit_json.assert_called_once_with(
changed=False,
ansible_facts=dict(interconnect_link_topologies=(INTERCONNECT_LINK_TOPOLOGIES))
ansible_facts=dict(interconnect_link_topologies=(self.INTERCONNECT_LINK_TOPOLOGIES))
)

def test_should_get_all_interconnect_link_topology_by_name(self):
self.interconnect_link_topologies.get_by.return_value = [INTERCONNECT_LINK_TOPOLOGIES[1]]

self.mock_ansible_module.params = PARAMS_GET_BY_NAME
self.interconnect_link_topologies.get_by.return_value = [self.INTERCONNECT_LINK_TOPOLOGIES[1]]
self.mock_ansible_module.params = self.PARAMS_GET_BY_NAME

InterconnectLinkTopologyFactsModule().run()

self.mock_ansible_module.exit_json.assert_called_once_with(
changed=False,
ansible_facts=dict(interconnect_link_topologies=([INTERCONNECT_LINK_TOPOLOGIES[1]]))
ansible_facts=dict(interconnect_link_topologies=([self.INTERCONNECT_LINK_TOPOLOGIES[1]]))
)


Expand Down

0 comments on commit dbbcfe5

Please sign in to comment.