| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
from __future__ import absolute_import, division, print_function |
| 10 |
|
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
DOCUMENTATION = ''' |
| 14 |
--- |
| 15 |
module: ibm_svc_manage_ownershipgroup |
| 16 |
short_description: This module manages ownership group on IBM Storage Virtualize family systems |
| 17 |
version_added: "1.7.0" |
| 18 |
description: |
| 19 |
- Ansible interface to manage 'mkownershipgroup' and 'rmownershipgroup' commands. |
| 20 |
options: |
| 21 |
name: |
| 22 |
description: |
| 23 |
- Specifies the name or label for the new ownership group object. |
| 24 |
required: true |
| 25 |
type: str |
| 26 |
state: |
| 27 |
description: |
| 28 |
- Creates (C(present)) or removes (C(absent)) an ownership group. |
| 29 |
choices: [ absent, present ] |
| 30 |
required: true |
| 31 |
type: str |
| 32 |
clustername: |
| 33 |
description: |
| 34 |
- The hostname or management IP of the Storage Virtualize system. |
| 35 |
required: true |
| 36 |
type: str |
| 37 |
domain: |
| 38 |
description: |
| 39 |
- Domain for the Storage Virtualize system. |
| 40 |
- Valid when hostname is used for the parameter I(clustername). |
| 41 |
type: str |
| 42 |
username: |
| 43 |
description: |
| 44 |
- REST API username for the Storage Virtualize system. |
| 45 |
- The parameters I(username) and I(password) are required if not using I(token) to authenticate a user. |
| 46 |
type: str |
| 47 |
password: |
| 48 |
description: |
| 49 |
- REST API password for the Storage Virtualize system. |
| 50 |
- The parameters I(username) and I(password) are required if not using I(token) to authenticate a user. |
| 51 |
type: str |
| 52 |
token: |
| 53 |
description: |
| 54 |
- The authentication token to verify a user on the Storage Virtualize system. |
| 55 |
- To generate a token, use the M(ibm.storage_virtualize.ibm_svc_auth) module. |
| 56 |
type: str |
| 57 |
keepobjects: |
| 58 |
description: |
| 59 |
- If specified, the objects that currently belong to the ownership group will be kept but will be moved to noownershipgroup. |
| 60 |
- Applies when I(state=disabled). |
| 61 |
type: bool |
| 62 |
log_path: |
| 63 |
description: |
| 64 |
- Path of debug log file. |
| 65 |
type: str |
| 66 |
validate_certs: |
| 67 |
description: |
| 68 |
- Validates certification. |
| 69 |
default: false |
| 70 |
type: bool |
| 71 |
author: |
| 72 |
- Sanjaikumaar M (@sanjaikumaar) |
| 73 |
notes: |
| 74 |
- This module supports C(check_mode). |
| 75 |
''' |
| 76 |
|
| 77 |
EXAMPLES = ''' |
| 78 |
- name: Create ownership group |
| 79 |
ibm.storage_virtualize.ibm_svc_manage_ownershipgroup: |
| 80 |
clustername: "{{ clustername }}" |
| 81 |
domain: "{{ domain }}" |
| 82 |
username: "{{ username }}" |
| 83 |
password: "{{ password }}" |
| 84 |
log_path: /tmp/playbook.debug |
| 85 |
name: newOwner |
| 86 |
state: present |
| 87 |
- name: Delete ownership group |
| 88 |
ibm.storage_virtualize.ibm_svc_manage_ownershipgroup: |
| 89 |
clustername: "{{ clustername }}" |
| 90 |
domain: "{{ domain }}" |
| 91 |
username: "{{ username }}" |
| 92 |
password: "{{ password }}" |
| 93 |
log_path: /tmp/playbook.debug |
| 94 |
name: newOwner |
| 95 |
state: absent |
| 96 |
keepobjects: true |
| 97 |
''' |
| 98 |
|
| 99 |
RETURN = '''#''' |
| 100 |
|
| 101 |
from traceback import format_exc |
| 102 |
from ansible.module_utils.basic import AnsibleModule |
| 103 |
from ansible.module_utils._text import to_native |
| 104 |
from ansible_collections.ibm.storage_virtualize.plugins.module_utils.ibm_svc_utils import ( |
| 105 |
IBMSVCRestApi, |
| 106 |
svc_argument_spec, |
| 107 |
get_logger |
| 108 |
) |
| 109 |
|
| 110 |
|
| 111 |
class IBMSVCOwnershipgroup: |
| 112 |
|
| 113 |
def __init__(self): |
| 114 |
|
| 115 |
argument_spec = svc_argument_spec() |
| 116 |
argument_spec.update( |
| 117 |
dict( |
| 118 |
name=dict(type='str', required=True), |
| 119 |
state=dict( |
| 120 |
type='str', |
| 121 |
required=True, |
| 122 |
choices=['present', 'absent'] |
| 123 |
), |
| 124 |
keepobjects=dict(type='bool') |
| 125 |
) |
| 126 |
) |
| 127 |
|
| 128 |
|
| 129 |
self.module = AnsibleModule( |
| 130 |
argument_spec=argument_spec, |
| 131 |
supports_check_mode=True |
| 132 |
) |
| 133 |
|
| 134 |
|
| 135 |
self.name = self.module.params['name'] |
| 136 |
self.state = self.module.params['state'] |
| 137 |
|
| 138 |
|
| 139 |
self.keepobjects = self.module.params.get('keepobjects') |
| 140 |
|
| 141 |
if not self.name: |
| 142 |
self.module.fail_json(msg='Missing mandatory parameter: name') |
| 143 |
|
| 144 |
|
| 145 |
log_path = self.module.params['log_path'] |
| 146 |
logger = get_logger(self.__class__.__name__, log_path) |
| 147 |
self.log = logger.info |
| 148 |
self.changed = False |
| 149 |
self.msg = None |
| 150 |
|
| 151 |
self.restapi = IBMSVCRestApi( |
| 152 |
module=self.module, |
| 153 |
clustername=self.module.params['clustername'], |
| 154 |
domain=self.module.params['domain'], |
| 155 |
username=self.module.params['username'], |
| 156 |
password=self.module.params['password'], |
| 157 |
validate_certs=self.module.params['validate_certs'], |
| 158 |
log_path=log_path, |
| 159 |
token=self.module.params['token'] |
| 160 |
) |
| 161 |
|
| 162 |
def check_existing_owgroups(self): |
| 163 |
merged_result = {} |
| 164 |
|
| 165 |
data = self.restapi.svc_obj_info(cmd='lsownershipgroup', cmdopts=None, |
| 166 |
cmdargs=[self.name]) |
| 167 |
|
| 168 |
if isinstance(data, list): |
| 169 |
for d in data: |
| 170 |
merged_result.update(d) |
| 171 |
else: |
| 172 |
merged_result = data |
| 173 |
|
| 174 |
return merged_result |
| 175 |
|
| 176 |
def create_ownershipgroup(self): |
| 177 |
if self.module.check_mode: |
| 178 |
self.changed = True |
| 179 |
return |
| 180 |
|
| 181 |
if self.keepobjects: |
| 182 |
self.module.fail_json( |
| 183 |
msg='Keepobjects should only be passed while deleting ownershipgroup' |
| 184 |
) |
| 185 |
|
| 186 |
cmd = 'mkownershipgroup' |
| 187 |
cmdopts = None |
| 188 |
cmdargs = ['-name', self.name] |
| 189 |
|
| 190 |
result = self.restapi.svc_run_command(cmd, cmdopts, cmdargs) |
| 191 |
self.changed = True |
| 192 |
self.log('Create ownership group result: %s', result) |
| 193 |
|
| 194 |
def delete_ownershipgroup(self): |
| 195 |
if self.module.check_mode: |
| 196 |
self.changed = True |
| 197 |
return |
| 198 |
|
| 199 |
cmd = 'rmownershipgroup' |
| 200 |
cmdopts = None |
| 201 |
cmdargs = [self.name] |
| 202 |
|
| 203 |
if self.keepobjects: |
| 204 |
cmdargs.insert(0, '-keepobjects') |
| 205 |
|
| 206 |
result = self.restapi.svc_run_command(cmd, cmdopts, cmdargs) |
| 207 |
self.changed = True |
| 208 |
self.log('Delete ownership group result: %s', result) |
| 209 |
|
| 210 |
def apply(self): |
| 211 |
if self.check_existing_owgroups(): |
| 212 |
if self.state == 'present': |
| 213 |
self.msg = 'Ownership group (%s) already exist.' % (self.name) |
| 214 |
else: |
| 215 |
self.delete_ownershipgroup() |
| 216 |
self.msg = 'Ownership group (%s) deleted.' % (self.name) |
| 217 |
else: |
| 218 |
if self.state == 'absent': |
| 219 |
self.msg = 'Ownership group (%s) does not exist.' % (self.name) |
| 220 |
else: |
| 221 |
self.create_ownershipgroup() |
| 222 |
self.msg = 'Ownership group (%s) created.' % \ |
| 223 |
(self.name) |
| 224 |
|
| 225 |
if self.module.check_mode: |
| 226 |
self.msg = 'skipping changes due to check mode.' |
| 227 |
|
| 228 |
self.module.exit_json( |
| 229 |
changed=self.changed, |
| 230 |
msg=self.msg |
| 231 |
) |
| 232 |
|
| 233 |
|
| 234 |
def main(): |
| 235 |
v = IBMSVCOwnershipgroup() |
| 236 |
try: |
| 237 |
v.apply() |
| 238 |
except Exception as e: |
| 239 |
v.log('Exception in apply(): \n%s', format_exc()) |
| 240 |
v.module.fail_json(msg='Module failed. Error [{0}].'.format(to_native(e))) |
| 241 |
|
| 242 |
|
| 243 |
if __name__ == '__main__': |
| 244 |
main() |