Debian Codegraph @

ansible_collections/community/general/plugins/modules/emc_vnx_sg_member.py

raw
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2018, Luca 'remix_tj' Lorenzetto <[email protected]>
4 #
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7 #
8
9 from __future__ import annotations
10
11 DOCUMENTATION = r"""
12 module: emc_vnx_sg_member
13
14 short_description: Manage storage group member on EMC VNX
15
16
17 description:
18 - This module manages the members of an existing storage group.
19 extends_documentation_fragment:
20 - community.general._emc.emc_vnx
21 - community.general._attributes
22
23 attributes:
24 check_mode:
25 support: full
26 diff_mode:
27 support: none
28
29 options:
30 name:
31 description:
32 - Name of the Storage group to manage.
33 required: true
34 type: str
35 lunid:
36 description:
37 - LUN ID to be added.
38 required: true
39 type: int
40 state:
41 description:
42 - Indicates the desired lunid state.
43 - V(present) ensures specified O(lunid) is present in the Storage Group.
44 - V(absent) ensures specified O(lunid) is absent from Storage Group.
45 default: present
46 choices: ["present", "absent"]
47 type: str
48
49
50 author:
51 - Luca 'remix_tj' Lorenzetto (@remixtj)
52 """
53
54 EXAMPLES = r"""
55 - name: Add lun to storage group
56 community.general.emc_vnx_sg_member:
57 name: sg01
58 sp_address: sp1a.fqdn
59 sp_user: sysadmin
60 sp_password: sysadmin
61 lunid: 100
62 state: present
63
64 - name: Remove lun from storage group
65 community.general.emc_vnx_sg_member:
66 name: sg01
67 sp_address: sp1a.fqdn
68 sp_user: sysadmin
69 sp_password: sysadmin
70 lunid: 100
71 state: absent
72 """
73
74 RETURN = r"""
75 hluid:
76 description: LUNID visible to hosts attached to the storage group.
77 type: int
78 returned: success
79 """
80
81 import traceback
82
83 from ansible.module_utils.basic import AnsibleModule, missing_required_lib
84
85 from ansible_collections.community.general.plugins.module_utils._emc_vnx import emc_vnx_argument_spec
86
87 LIB_IMP_ERR = None
88 try:
89 from storops import VNXSystem
90 from storops.exception import (
91 VNXAluAlreadyAttachedError,
92 VNXAttachAluError,
93 VNXCredentialError,
94 VNXDetachAluNotFoundError,
95 VNXStorageGroupError,
96 )
97
98 HAS_LIB = True
99 except Exception:
100 LIB_IMP_ERR = traceback.format_exc()
101 HAS_LIB = False
102
103
104 def run_module():
105 module_args = dict(
106 name=dict(type="str", required=True),
107 lunid=dict(type="int", required=True),
108 state=dict(default="present", choices=["present", "absent"]),
109 )
110
111 module_args.update(emc_vnx_argument_spec)
112
113 result = dict(changed=False, hluid=None)
114
115 module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)
116
117 if not HAS_LIB:
118 module.fail_json(msg=missing_required_lib("storops >= 0.5.10"), exception=LIB_IMP_ERR)
119
120 sp_user = module.params["sp_user"]
121 sp_address = module.params["sp_address"]
122 sp_password = module.params["sp_password"]
123 alu = module.params["lunid"]
124
125 # if the user is working with this module in only check mode we do not
126 # want to make any changes to the environment, just return the current
127 # state with no modifications
128 if module.check_mode:
129 return result
130
131 try:
132 vnx = VNXSystem(sp_address, sp_user, sp_password)
133 sg = vnx.get_sg(module.params["name"])
134 if sg.existed:
135 if module.params["state"] == "present":
136 if not sg.has_alu(alu):
137 try:
138 result["hluid"] = sg.attach_alu(alu)
139 result["changed"] = True
140 except VNXAluAlreadyAttachedError:
141 result["hluid"] = sg.get_hlu(alu)
142 except (VNXAttachAluError, VNXStorageGroupError) as e:
143 module.fail_json(msg=f"Error attaching {alu}: {e} ", **result)
144 else:
145 result["hluid"] = sg.get_hlu(alu)
146 if module.params["state"] == "absent" and sg.has_alu(alu):
147 try:
148 sg.detach_alu(alu)
149 result["changed"] = True
150 except VNXDetachAluNotFoundError:
151 # being not attached when using absent is OK
152 pass
153 except VNXStorageGroupError as e:
154 module.fail_json(msg=f"Error detaching alu {alu}: {e} ", **result)
155 else:
156 module.fail_json(msg=f"No such storage group named {module.params['name']}", **result)
157 except VNXCredentialError as e:
158 module.fail_json(msg=f"{e}", **result)
159
160 module.exit_json(**result)
161
162
163 def main():
164 run_module()
165
166
167 if __name__ == "__main__":
168 main()