Debian Codegraph @

ansible_collections/purestorage/flasharray/plugins/modules/purefa_arrayname.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2018, Simon Dodsley ([email protected])
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {
12 "metadata_version": "1.1",
13 "status": ["preview"],
14 "supported_by": "community",
15 }
16
17 DOCUMENTATION = r"""
18 ---
19 module: purefa_arrayname
20 version_added: '1.0.0'
21 short_description: Configure Pure Storage FlashArray array name
22 description:
23 - Configure name of array for Pure Storage FlashArrays.
24 - Ideal for Day 0 initial configuration.
25 author:
26 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
27 options:
28 state:
29 description:
30 - Set the array name
31 type: str
32 default: present
33 choices: [ present ]
34 name:
35 description:
36 - Name of the array. Must conform to correct naming schema.
37 type: str
38 required: true
39 context:
40 description:
41 - Name of fleet member on which to perform the operation.
42 - This requires the array receiving the request is a member of a fleet
43 and the context name to be a member of the same fleet.
44 type: str
45 default: ""
46 version_added: '1.39.0'
47 extends_documentation_fragment:
48 - purestorage.flasharray.purestorage.fa
49 """
50
51 EXAMPLES = r"""
52 - name: Set new array name
53 purestorage.flasharray.purefa_arrayname:
54 name: new-array-name
55 state: present
56 fa_url: 10.10.10.2
57 api_token: e31060a7-21fc-e277-6240-25983c6c4592
58 """
59
60 RETURN = r"""
61 """
62
63 HAS_PURESTORAGE = True
64 try:
65 from pypureclient.flasharray import Arrays
66 except ImportError:
67 HAS_PURESTORAGE = False
68
69
70 import re
71 from ansible.module_utils.basic import AnsibleModule
72 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
73 get_array,
74 purefa_argument_spec,
75 )
76 from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
77 LooseVersion,
78 )
79
80 CONTEXT_VERSION = "2.38"
81
82
83 def update_name(module, array):
84 """Change array name"""
85 changed = True
86 api_version = array.get_rest_version()
87 if not module.check_mode:
88 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
89 res = array.patch_arrays(
90 array=Arrays(name=module.params["name"]),
91 context_names=[module.params["context"]],
92 )
93 else:
94 res = array.patch_arrays(array=Arrays(name=module.params["name"]))
95 if res.status_code != 200:
96 module.fail_json(
97 msg="Failed to change array name to {0}. Error: {1}".format(
98 module.params["name"], res.errors[0].message
99 )
100 )
101
102 module.exit_json(changed=changed)
103
104
105 def main():
106 argument_spec = purefa_argument_spec()
107 argument_spec.update(
108 dict(
109 name=dict(type="str", required=True),
110 state=dict(type="str", default="present", choices=["present"]),
111 context=dict(type="str", default=""),
112 )
113 )
114
115 module = AnsibleModule(argument_spec, supports_check_mode=True)
116
117 if not HAS_PURESTORAGE:
118 module.fail_json(msg="py-pure-client sdk is required for this module")
119
120 array = get_array(module)
121 api_version = array.get_rest_version()
122 pattern = re.compile("^[a-zA-Z0-9]([a-zA-Z0-9-]{0,54}[a-zA-Z0-9])?$")
123 if not pattern.match(module.params["name"]):
124 module.fail_json(
125 msg="Array name {0} does not conform to array name rules".format(
126 module.params["name"]
127 )
128 )
129 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
130 current_name = list(
131 array.get_arrays(context_names=[module.params["context"]]).items
132 )[0].name
133 else:
134 current_name = list(array.get_arrays().items)[0].name
135 if module.params["name"] != current_name:
136 update_name(module, array)
137
138 module.exit_json(changed=False)
139
140
141 if __name__ == "__main__":
142 main()