mirror of https://github.com/ansible/ansible.git
New module na_ontap_object_store (#59048)
* New module object store * fixes * fix testspull/58766/head
parent
38848a4b0c
commit
be24db8886
@ -0,0 +1,237 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
# (c) 2019, NetApp, Inc
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
|
||||
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
||||
'status': ['preview'],
|
||||
'supported_by': 'certified'}
|
||||
|
||||
|
||||
DOCUMENTATION = '''
|
||||
|
||||
module: na_ontap_object_store
|
||||
short_description: NetApp ONTAP manage object store config.
|
||||
extends_documentation_fragment:
|
||||
- netapp.na_ontap
|
||||
version_added: '2.9'
|
||||
author: NetApp Ansible Team (@carchi8py) <ng-ansibleteam@netapp.com>
|
||||
|
||||
description:
|
||||
- Create or delete object store config on ONTAP.
|
||||
|
||||
options:
|
||||
|
||||
state:
|
||||
description:
|
||||
- Whether the specified object store config should exist or not.
|
||||
choices: ['present', 'absent']
|
||||
default: 'present'
|
||||
type: str
|
||||
|
||||
name:
|
||||
required: true
|
||||
description:
|
||||
- The name of the object store config to manage.
|
||||
type: str
|
||||
|
||||
provider_type:
|
||||
required: false
|
||||
description:
|
||||
- The name of the object store config provider.
|
||||
type: str
|
||||
|
||||
server:
|
||||
required: false
|
||||
description:
|
||||
- Fully qualified domain name of the object store config.
|
||||
type: str
|
||||
|
||||
container:
|
||||
required: false
|
||||
description:
|
||||
- Data bucket/container name used in S3 requests.
|
||||
type: str
|
||||
|
||||
access_key:
|
||||
required: false
|
||||
description:
|
||||
- Access key ID for AWS_S3 and SGWS provider types.
|
||||
type: str
|
||||
|
||||
secret_password:
|
||||
required: false
|
||||
description:
|
||||
- Secret access key for AWS_S3 and SGWS provider types.
|
||||
type: str
|
||||
'''
|
||||
|
||||
EXAMPLES = """
|
||||
- name: object store Create
|
||||
na_ontap_object_store:
|
||||
state: present
|
||||
name: ansible
|
||||
provider_type: SGWS
|
||||
server: abc
|
||||
container: abc
|
||||
access_key: s3.amazonaws.com
|
||||
secret_password: abc
|
||||
hostname: "{{ hostname }}"
|
||||
username: "{{ username }}"
|
||||
password: "{{ password }}"
|
||||
|
||||
- name: object store Create
|
||||
na_ontap_object_store:
|
||||
state: absent
|
||||
name: ansible
|
||||
hostname: "{{ hostname }}"
|
||||
username: "{{ username }}"
|
||||
password: "{{ password }}"
|
||||
"""
|
||||
|
||||
RETURN = """
|
||||
|
||||
"""
|
||||
import traceback
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils._text import to_native
|
||||
import ansible.module_utils.netapp as netapp_utils
|
||||
from ansible.module_utils.netapp_module import NetAppModule
|
||||
|
||||
HAS_NETAPP_LIB = netapp_utils.has_netapp_lib()
|
||||
|
||||
|
||||
class NetAppOntapObjectStoreConfig(object):
|
||||
''' object initialize and class methods '''
|
||||
|
||||
def __init__(self):
|
||||
self.argument_spec = netapp_utils.na_ontap_host_argument_spec()
|
||||
self.argument_spec.update(dict(
|
||||
name=dict(required=True, type='str'),
|
||||
state=dict(required=False, choices=['present', 'absent'], default='present'),
|
||||
provider_type=dict(required=False, type='str'),
|
||||
server=dict(required=False, type='str'),
|
||||
container=dict(required=False, type='str'),
|
||||
access_key=dict(required=False, type='str'),
|
||||
secret_password=dict(required=False, type='str', no_log=True)
|
||||
))
|
||||
|
||||
self.module = AnsibleModule(
|
||||
argument_spec=self.argument_spec,
|
||||
supports_check_mode=True
|
||||
)
|
||||
|
||||
self.na_helper = NetAppModule()
|
||||
self.parameters = self.na_helper.set_parameters(self.module.params)
|
||||
if HAS_NETAPP_LIB is False:
|
||||
self.module.fail_json(msg="the python NetApp-Lib module is required")
|
||||
else:
|
||||
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module)
|
||||
|
||||
def get_aggr_object_store(self):
|
||||
"""
|
||||
Fetch details if object store config exists.
|
||||
:return:
|
||||
Dictionary of current details if object store config found
|
||||
None if object store config is not found
|
||||
"""
|
||||
aggr_object_store_get_iter = netapp_utils.zapi.NaElement.create_node_with_children(
|
||||
'aggr-object-store-config-get', **{'object-store-name': self.parameters['name']})
|
||||
result = None
|
||||
try:
|
||||
result = self.server.invoke_successfully(aggr_object_store_get_iter, enable_tunneling=False)
|
||||
except netapp_utils.zapi.NaApiError as error:
|
||||
# Error 15661 denotes an object store not being found.
|
||||
if to_native(error.code) == "15661":
|
||||
pass
|
||||
else:
|
||||
self.module.fail_json(msg=to_native(error), exception=traceback.format_exc())
|
||||
return result
|
||||
|
||||
def create_aggr_object_store(self):
|
||||
"""
|
||||
Create aggregate object store config
|
||||
:return: None
|
||||
"""
|
||||
required_keys = set(['provider_type', 'server', 'container', 'access_key'])
|
||||
if not required_keys.issubset(set(self.parameters.keys())):
|
||||
self.module.fail_json(msg='Error provisioning object store %s: one of the following parameters are missing '
|
||||
'%s' % (self.parameters['name'], ', '.join(required_keys)))
|
||||
options = {'object-store-name': self.parameters['name'],
|
||||
'provider-type': self.parameters['provider_type'],
|
||||
'server': self.parameters['server'],
|
||||
's3-name': self.parameters['container'],
|
||||
'access-key': self.parameters['access_key']}
|
||||
if self.parameters.get('secret_password'):
|
||||
options['secret-password'] = self.parameters['secret_password']
|
||||
object_store_create = netapp_utils.zapi.NaElement.create_node_with_children('aggr-object-store-config-create', **options)
|
||||
|
||||
try:
|
||||
self.server.invoke_successfully(object_store_create, enable_tunneling=False)
|
||||
except netapp_utils.zapi.NaApiError as error:
|
||||
self.module.fail_json(msg="Error provisioning object store config %s: %s"
|
||||
% (self.parameters['name'], to_native(error)),
|
||||
exception=traceback.format_exc())
|
||||
|
||||
def delete_aggr_object_store(self):
|
||||
"""
|
||||
Delete aggregate object store config
|
||||
:return: None
|
||||
"""
|
||||
object_store_destroy = netapp_utils.zapi.NaElement.create_node_with_children(
|
||||
'aggr-object-store-config-delete', **{'object-store-name': self.parameters['name']})
|
||||
|
||||
try:
|
||||
self.server.invoke_successfully(object_store_destroy,
|
||||
enable_tunneling=False)
|
||||
except netapp_utils.zapi.NaApiError as error:
|
||||
self.module.fail_json(msg="Error removing object store config %s: %s" %
|
||||
(self.parameters['name'], to_native(error)), exception=traceback.format_exc())
|
||||
|
||||
def asup_log_for_cserver(self, event_name):
|
||||
"""
|
||||
Fetch admin vserver for the given cluster
|
||||
Create and Autosupport log event with the given module name
|
||||
:param event_name: Name of the event log
|
||||
:return: None
|
||||
"""
|
||||
results = netapp_utils.get_cserver(self.server)
|
||||
cserver = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=results)
|
||||
netapp_utils.ems_log_event(event_name, cserver)
|
||||
|
||||
def apply(self):
|
||||
"""
|
||||
Apply action to the object store config
|
||||
:return: None
|
||||
"""
|
||||
self.asup_log_for_cserver("na_ontap_object_store_config")
|
||||
current = self.get_aggr_object_store()
|
||||
cd_action = self.na_helper.get_cd_action(current, self.parameters)
|
||||
|
||||
if self.na_helper.changed:
|
||||
if self.module.check_mode:
|
||||
pass
|
||||
else:
|
||||
if cd_action == 'create':
|
||||
self.create_aggr_object_store()
|
||||
elif cd_action == 'delete':
|
||||
self.delete_aggr_object_store()
|
||||
self.module.exit_json(changed=self.na_helper.changed)
|
||||
|
||||
|
||||
def main():
|
||||
"""
|
||||
Create Object Store Config class instance and invoke apply
|
||||
:return: None
|
||||
"""
|
||||
obj_store = NetAppOntapObjectStoreConfig()
|
||||
obj_store.apply()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
@ -0,0 +1,200 @@
|
||||
# (c) 2019, NetApp, Inc
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
""" unit tests for Ansible module: na_ontap_object_store """
|
||||
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
import json
|
||||
import pytest
|
||||
|
||||
from units.compat import unittest
|
||||
from units.compat.mock import patch, Mock
|
||||
from ansible.module_utils import basic
|
||||
from ansible.module_utils._text import to_bytes
|
||||
import ansible.module_utils.netapp as netapp_utils
|
||||
|
||||
from ansible.modules.storage.netapp.na_ontap_object_store \
|
||||
import NetAppOntapObjectStoreConfig as my_module # module under test
|
||||
|
||||
if not netapp_utils.has_netapp_lib():
|
||||
pytestmark = pytest.mark.skip('skipping as missing required netapp_lib')
|
||||
|
||||
|
||||
def set_module_args(args):
|
||||
"""prepare arguments so that they will be picked up during module creation"""
|
||||
args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
|
||||
basic._ANSIBLE_ARGS = to_bytes(args) # pylint: disable=protected-access
|
||||
|
||||
|
||||
class AnsibleExitJson(Exception):
|
||||
"""Exception class to be raised by module.exit_json and caught by the test case"""
|
||||
pass
|
||||
|
||||
|
||||
class AnsibleFailJson(Exception):
|
||||
"""Exception class to be raised by module.fail_json and caught by the test case"""
|
||||
pass
|
||||
|
||||
|
||||
def exit_json(*args, **kwargs): # pylint: disable=unused-argument
|
||||
"""function to patch over exit_json; package return data into an exception"""
|
||||
if 'changed' not in kwargs:
|
||||
kwargs['changed'] = False
|
||||
raise AnsibleExitJson(kwargs)
|
||||
|
||||
|
||||
def fail_json(*args, **kwargs): # pylint: disable=unused-argument
|
||||
"""function to patch over fail_json; package return data into an exception"""
|
||||
kwargs['failed'] = True
|
||||
raise AnsibleFailJson(kwargs)
|
||||
|
||||
|
||||
class MockONTAPConnection(object):
|
||||
''' mock server connection to ONTAP host '''
|
||||
|
||||
def __init__(self, kind=None):
|
||||
''' save arguments '''
|
||||
self.type = kind
|
||||
self.xml_in = None
|
||||
self.xml_out = None
|
||||
|
||||
def invoke_successfully(self, xml, enable_tunneling): # pylint: disable=unused-argument
|
||||
''' mock invoke_successfully returning xml data '''
|
||||
self.xml_in = xml
|
||||
if self.type == 'object_store':
|
||||
xml = self.build_object_store_info()
|
||||
elif self.type == 'object_store_fail':
|
||||
raise netapp_utils.zapi.NaApiError(code='TEST', message="This exception is from the unit test")
|
||||
self.xml_out = xml
|
||||
return xml
|
||||
|
||||
@staticmethod
|
||||
def build_object_store_info():
|
||||
''' build xml data for object store '''
|
||||
xml = netapp_utils.zapi.NaElement('xml')
|
||||
data = {'attributes':
|
||||
{'aggr-object-store-config-info':
|
||||
{'object-store-name': 'ansible'}
|
||||
}
|
||||
}
|
||||
xml.translate_struct(data)
|
||||
print(xml.to_string())
|
||||
return xml
|
||||
|
||||
|
||||
class TestMyModule(unittest.TestCase):
|
||||
''' a group of related Unit Tests '''
|
||||
|
||||
def setUp(self):
|
||||
self.mock_module_helper = patch.multiple(basic.AnsibleModule,
|
||||
exit_json=exit_json,
|
||||
fail_json=fail_json)
|
||||
self.mock_module_helper.start()
|
||||
self.addCleanup(self.mock_module_helper.stop)
|
||||
self.server = MockONTAPConnection()
|
||||
# whether to use a mock or a simulator
|
||||
self.onbox = False
|
||||
|
||||
def set_default_args(self):
|
||||
if self.onbox:
|
||||
hostname = '10.10.10.10'
|
||||
username = 'admin'
|
||||
password = 'password'
|
||||
name = 'ansible'
|
||||
else:
|
||||
hostname = 'hostname'
|
||||
username = 'username'
|
||||
password = 'password'
|
||||
name = 'ansible'
|
||||
return dict({
|
||||
'hostname': hostname,
|
||||
'username': username,
|
||||
'password': password,
|
||||
'name': name
|
||||
})
|
||||
|
||||
def call_command(self, module_args):
|
||||
''' utility function to call apply '''
|
||||
module_args.update(self.set_default_args())
|
||||
set_module_args(module_args)
|
||||
my_obj = my_module()
|
||||
my_obj.asup_log_for_cserver = Mock(return_value=None)
|
||||
if not self.onbox:
|
||||
# mock the connection
|
||||
my_obj.server = MockONTAPConnection('object_store')
|
||||
with pytest.raises(AnsibleExitJson) as exc:
|
||||
my_obj.apply()
|
||||
return exc.value.args[0]['changed']
|
||||
|
||||
def test_module_fail_when_required_args_missing(self):
|
||||
''' required arguments are reported as errors '''
|
||||
with pytest.raises(AnsibleFailJson) as exc:
|
||||
set_module_args({})
|
||||
my_module()
|
||||
print('Info: %s' % exc.value.args[0]['msg'])
|
||||
|
||||
def test_ensure_object_store_get_called(self):
|
||||
''' fetching details of object store '''
|
||||
set_module_args(self.set_default_args())
|
||||
my_obj = my_module()
|
||||
my_obj.server = self.server
|
||||
assert my_obj.get_aggr_object_store() is not None
|
||||
|
||||
def test_ensure_get_called_existing(self):
|
||||
''' test for existing object store'''
|
||||
set_module_args(self.set_default_args())
|
||||
my_obj = my_module()
|
||||
my_obj.server = MockONTAPConnection(kind='object_store')
|
||||
assert my_obj.get_aggr_object_store()
|
||||
|
||||
def test_object_store_create(self):
|
||||
''' test for creating object store'''
|
||||
module_args = {
|
||||
'provider_type': 'abc',
|
||||
'server': 'abc',
|
||||
'container': 'abc',
|
||||
'access_key': 'abc',
|
||||
'secret_password': 'abc'
|
||||
}
|
||||
module_args.update(self.set_default_args())
|
||||
set_module_args(module_args)
|
||||
my_obj = my_module()
|
||||
my_obj.asup_log_for_cserver = Mock(return_value=None)
|
||||
if not self.onbox:
|
||||
# mock the connection
|
||||
my_obj.server = MockONTAPConnection(kind='object_store')
|
||||
with pytest.raises(AnsibleExitJson) as exc:
|
||||
my_obj.apply()
|
||||
assert not exc.value.args[0]['changed']
|
||||
|
||||
def test_object_store_delete(self):
|
||||
''' test for deleting object store'''
|
||||
module_args = {
|
||||
'state': 'absent',
|
||||
}
|
||||
changed = self.call_command(module_args)
|
||||
assert changed
|
||||
|
||||
def test_if_all_methods_catch_exception(self):
|
||||
module_args = {
|
||||
'provider_type': 'abc',
|
||||
'server': 'abc',
|
||||
'container': 'abc',
|
||||
'access_key': 'abc',
|
||||
'secret_password': 'abc'
|
||||
}
|
||||
module_args.update(self.set_default_args())
|
||||
set_module_args(module_args)
|
||||
my_obj = my_module()
|
||||
if not self.onbox:
|
||||
my_obj.server = MockONTAPConnection('object_store_fail')
|
||||
with pytest.raises(AnsibleFailJson) as exc:
|
||||
my_obj.get_aggr_object_store()
|
||||
assert '' in exc.value.args[0]['msg']
|
||||
with pytest.raises(AnsibleFailJson) as exc:
|
||||
my_obj.create_aggr_object_store()
|
||||
assert 'Error provisioning object store config ' in exc.value.args[0]['msg']
|
||||
with pytest.raises(AnsibleFailJson) as exc:
|
||||
my_obj.delete_aggr_object_store()
|
||||
assert 'Error removing object store config ' in exc.value.args[0]['msg']
|
Loading…
Reference in New Issue