| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2018, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
|
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
ANSIBLE_METADATA = {'metadata_version': '1.1', |
| 11 |
'status': ['deprecated'], |
| 12 |
'supported_by': 'community'} |
| 13 |
|
| 14 |
DOCUMENTATION = """ |
| 15 |
--- |
| 16 |
module: netapp_e_global |
| 17 |
short_description: NetApp E-Series manage global settings configuration |
| 18 |
description: |
| 19 |
- Allow the user to configure several of the global settings associated with an E-Series storage-system |
| 20 |
version_added: '2.7.0' |
| 21 |
author: Michael Price (@lmprice) |
| 22 |
extends_documentation_fragment: |
| 23 |
- netapp_eseries.santricity.santricity.netapp.eseries |
| 24 |
options: |
| 25 |
name: |
| 26 |
description: |
| 27 |
- Set the name of the E-Series storage-system |
| 28 |
- This label/name doesn't have to be unique. |
| 29 |
- May be up to 30 characters in length. |
| 30 |
type: str |
| 31 |
aliases: |
| 32 |
- label |
| 33 |
log_path: |
| 34 |
description: |
| 35 |
- A local path to a file to be used for debug logging |
| 36 |
required: no |
| 37 |
type: str |
| 38 |
notes: |
| 39 |
- Check mode is supported. |
| 40 |
- This module requires Web Services API v1.3 or newer. |
| 41 |
""" |
| 42 |
|
| 43 |
EXAMPLES = """ |
| 44 |
- name: Set the storage-system name |
| 45 |
netapp_e_global: |
| 46 |
name: myArrayName |
| 47 |
api_url: "10.1.1.1:8443" |
| 48 |
api_username: "admin" |
| 49 |
api_password: "myPass" |
| 50 |
""" |
| 51 |
|
| 52 |
RETURN = """ |
| 53 |
msg: |
| 54 |
description: Success message |
| 55 |
returned: on success |
| 56 |
type: str |
| 57 |
sample: The settings have been updated. |
| 58 |
name: |
| 59 |
description: |
| 60 |
- The current name/label of the storage-system. |
| 61 |
returned: on success |
| 62 |
sample: myArrayName |
| 63 |
type: str |
| 64 |
""" |
| 65 |
import json |
| 66 |
import logging |
| 67 |
|
| 68 |
from ansible.module_utils.basic import AnsibleModule |
| 69 |
from ansible_collections.netapp_eseries.santricity.plugins.module_utils.netapp import request, eseries_host_argument_spec |
| 70 |
from ansible.module_utils._text import to_native |
| 71 |
|
| 72 |
HEADERS = { |
| 73 |
"Content-Type": "application/json", |
| 74 |
"Accept": "application/json", |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
class GlobalSettings(object): |
| 79 |
def __init__(self): |
| 80 |
argument_spec = eseries_host_argument_spec() |
| 81 |
argument_spec.update(dict( |
| 82 |
name=dict(type='str', required=False, aliases=['label']), |
| 83 |
log_path=dict(type='str', required=False), |
| 84 |
)) |
| 85 |
|
| 86 |
self.module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, ) |
| 87 |
args = self.module.params |
| 88 |
self.name = args['name'] |
| 89 |
|
| 90 |
self.ssid = args['ssid'] |
| 91 |
self.url = args['api_url'] |
| 92 |
self.creds = dict(url_password=args['api_password'], |
| 93 |
validate_certs=args['validate_certs'], |
| 94 |
url_username=args['api_username'], ) |
| 95 |
|
| 96 |
self.check_mode = self.module.check_mode |
| 97 |
|
| 98 |
log_path = args['log_path'] |
| 99 |
|
| 100 |
# logging setup |
| 101 |
self._logger = logging.getLogger(self.__class__.__name__) |
| 102 |
|
| 103 |
if log_path: |
| 104 |
logging.basicConfig( |
| 105 |
level=logging.DEBUG, filename=log_path, filemode='w', |
| 106 |
format='%(relativeCreated)dms %(levelname)s %(module)s.%(funcName)s:%(lineno)d\n %(message)s') |
| 107 |
|
| 108 |
if not self.url.endswith('/'): |
| 109 |
self.url += '/' |
| 110 |
|
| 111 |
if self.name and len(self.name) > 30: |
| 112 |
self.module.fail_json(msg="The provided name is invalid, it must be < 30 characters in length.") |
| 113 |
|
| 114 |
def get_name(self): |
| 115 |
try: |
| 116 |
(rc, result) = request(self.url + 'storage-systems/%s' % self.ssid, headers=HEADERS, **self.creds) |
| 117 |
if result['status'] in ['offline', 'neverContacted']: |
| 118 |
self.module.fail_json(msg="This storage-system is offline! Array Id [%s]." % (self.ssid)) |
| 119 |
return result['name'] |
| 120 |
except Exception as err: |
| 121 |
self.module.fail_json(msg="Connection failure! Array Id [%s]. Error [%s]." % (self.ssid, to_native(err))) |
| 122 |
|
| 123 |
def update_name(self): |
| 124 |
name = self.get_name() |
| 125 |
update = False |
| 126 |
if self.name != name: |
| 127 |
update = True |
| 128 |
|
| 129 |
body = dict(name=self.name) |
| 130 |
|
| 131 |
if update and not self.check_mode: |
| 132 |
try: |
| 133 |
(rc, result) = request(self.url + 'storage-systems/%s/configuration' % self.ssid, method='POST', |
| 134 |
data=json.dumps(body), headers=HEADERS, **self.creds) |
| 135 |
self._logger.info("Set name to %s.", result['name']) |
| 136 |
# This is going to catch cases like a connection failure |
| 137 |
except Exception as err: |
| 138 |
self.module.fail_json( |
| 139 |
msg="We failed to set the storage-system name! Array Id [%s]. Error [%s]." |
| 140 |
% (self.ssid, to_native(err))) |
| 141 |
return update |
| 142 |
|
| 143 |
def update(self): |
| 144 |
update = self.update_name() |
| 145 |
name = self.get_name() |
| 146 |
|
| 147 |
self.module.exit_json(msg="The requested settings have been updated.", changed=update, name=name) |
| 148 |
|
| 149 |
def __call__(self, *args, **kwargs): |
| 150 |
self.update() |
| 151 |
|
| 152 |
|
| 153 |
def main(): |
| 154 |
settings = GlobalSettings() |
| 155 |
settings() |
| 156 |
|
| 157 |
|
| 158 |
if __name__ == '__main__': |
| 159 |
main() |