Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1 from Rainforest/fc_network
Browse files Browse the repository at this point in the history
Added create method for "present" state on FC Network Module
  • Loading branch information
Bruno Souza committed May 31, 2016
2 parents 06c7f8b + bfe5a3f commit 52b658f
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
inventory
**/*.retry
*.pyc
32 changes: 32 additions & 0 deletions examples/fc_network.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
- hosts: all
vars:
- oneview_host: 172.16.102.149
- username: administrator
- password: serveradmin
tasks:
- name: Create FC Network
fc_network:
oneview_host: "{{ oneview_host }}"
username: "{{ username }}"
password: "{{ password }}"
state: present
template:
name: 'New FC Network 2'
connectionTemplateUri: null
autoLoginRedistribution: True
fabricType: 'FabricAttach'
delegate_to: localhost

- name: Should not create FC Network again
fc_network:
oneview_host: "{{ oneview_host }}"
username: "{{ username }}"
password: "{{ password }}"
state: present
template:
name: 'New FC Network 2'
connectionTemplateUri: null
autoLoginRedistribution: True
fabricType: 'FabricAttach'
delegate_to: localhost
Empty file added library/__init__.py
Empty file.
108 changes: 108 additions & 0 deletions library/fc_network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/usr/bin/python

###
# (C) Copyright (2016) Hewlett Packard Enterprise Development LP
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
###

from ansible.module_utils.basic import *
from hpOneView.oneview_client import OneViewClient

FC_NETWORK_CREATED = 'FC Network created sucessfully.'
FC_NETWORK_ALREADY_EXIST = 'FC Network already exists.'


class FcNetworkModule(object):

argument_spec = dict(
oneview_host=dict(required=True, type='str'),
username=dict(required=True, type='str'),
password=dict(required=True, type='str'),
state=dict(
required=True,
choices=['present', 'absent']
),
template=dict(required=True, type='dict')
)


def __init__(self):
self.module = AnsibleModule(argument_spec=self.argument_spec,
supports_check_mode=False)
self.oneview_client = OneViewClient(self.__get_config())


def __get_config(self):
return dict(
ip=self.module.params['oneview_host'],
credentials=dict(
userName=self.module.params['username'],
password=self.module.params['password']
)
)


def run(self):
state = self.module.params['state']
template = self.module.params['template']

if state != 'present':
self.module.exit_json(changed=False)
else:
try:
changed, message, facts = self.__present(template)
self.module.exit_json(changed=changed,
msg=message,
ansible_facts=facts)
except Exception as exception:
self.module.fail_json(msg=exception.message)


def __present(self, template):
result = self.__get_by_name(template)

if not result:
msg, fc_network = self.__create(template)
changed = True
else:
msg = FC_NETWORK_ALREADY_EXIST
fc_network = result[0]
changed = False

facts = dict(fc_network=fc_network)

return changed, msg, facts


def __create(self, template):
new_fc_network = self.oneview_client.fc_networks.create(template)
return FC_NETWORK_CREATED, new_fc_network


def __get_by_name(self, template):
return self.oneview_client.fc_networks.get_by('name', template['name'])


def main():
FcNetworkModule().run()


if __name__ == '__main__':
main()
Empty file added test/__init__.py
Empty file.
Empty file added test/fc_network/__init__.py
Empty file.
96 changes: 96 additions & 0 deletions test/fc_network/test_fc_network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import unittest
import mock

from hpOneView import oneview_client
from fc_network import FcNetworkModule
from fc_network import FC_NETWORK_CREATED, FC_NETWORK_ALREADY_EXIST


FAKE_MSG_ERROR = 'Fake message error'

DEFAULT_FC_NETWORK_TEMPLATE = dict(
name='New FC Network 2',
connectionTemplateUri=None,
autoLoginRedistribution=True,
fabricType='FabricAttach'
)

PARAMS_FOR_PRESENT = dict(
oneview_host="oneview_host",
username="username",
password="password",
state='present',
template=dict(name=DEFAULT_FC_NETWORK_TEMPLATE['name'])
)

def create_ansible_mock(params):
mock_params = mock.Mock()
mock_params.__getitem__ = mock.Mock(side_effect=lambda name: params[name])

mock_ansible = mock.Mock()
mock_ansible.params = mock_params
return mock_ansible


class FcNetworkPresentStateSpec(unittest.TestCase):

@mock.patch('fc_network.OneViewClient')
@mock.patch('fc_network.AnsibleModule')
def test_should_create_new_fc_network(self, mock_ansible_module, mock_ov_client):
mock_ov_instance = mock.Mock()
mock_ov_instance.fc_networks.get_by.return_value = []
mock_ov_instance.fc_networks.create.return_value = DEFAULT_FC_NETWORK_TEMPLATE

mock_ov_client.return_value = mock_ov_instance
mock_ansible_instance = create_ansible_mock(PARAMS_FOR_PRESENT)
mock_ansible_module.return_value = mock_ansible_instance

FcNetworkModule().run()

mock_ansible_instance.exit_json.assert_called_once_with(
changed=True,
msg=FC_NETWORK_CREATED,
ansible_facts=dict(fc_network=DEFAULT_FC_NETWORK_TEMPLATE)
)

@mock.patch('fc_network.OneViewClient')
@mock.patch('fc_network.AnsibleModule')
def test_should_not_update_when_fc_network_already_exist(self, mock_ansible_module, mock_ov_client):
mock_ov_instance = mock.Mock()
mock_ov_instance.fc_networks.get_by.return_value = [DEFAULT_FC_NETWORK_TEMPLATE]

mock_ov_client.return_value = mock_ov_instance
mock_ansible_instance = create_ansible_mock(PARAMS_FOR_PRESENT)
mock_ansible_module.return_value = mock_ansible_instance

FcNetworkModule().run()

mock_ansible_instance.exit_json.assert_called_once_with(
changed=False,
msg=FC_NETWORK_ALREADY_EXIST,
ansible_facts=dict(fc_network=DEFAULT_FC_NETWORK_TEMPLATE)
)


class FcNetworkErrorHandlingSpec(unittest.TestCase):

@mock.patch('fc_network.OneViewClient')
@mock.patch('fc_network.AnsibleModule')
def test_should_not_update_when_create_raises_exception(self, mock_ansible_module, mock_ov_client):
mock_ov_instance = mock.Mock()
mock_ov_instance.fc_networks.get_by.return_value = []
mock_ov_instance.fc_networks.create.side_effect = Exception(FAKE_MSG_ERROR)

mock_ov_client.return_value = mock_ov_instance
mock_ansible_instance = create_ansible_mock(PARAMS_FOR_PRESENT)
mock_ansible_module.return_value = mock_ansible_instance

self.assertRaises(Exception, FcNetworkModule().run())

mock_ansible_instance.fail_json.assert_called_once_with(
msg=FAKE_MSG_ERROR
)


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

0 comments on commit 52b658f

Please sign in to comment.