Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2020, 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_apiclient
20 version_added: '1.5.0'
21 short_description: Manage FlashArray API Clients
22 description:
23 - Enable or disable FlashArray API Clients
24 author:
25 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
26 options:
27 name:
28 description:
29 - Name of the API Client
30 type: str
31 required: true
32 state:
33 description:
34 - Define whether the API client should exist or not.
35 default: present
36 choices: [ absent, present ]
37 type: str
38 role:
39 description:
40 - The maximum role allowed for ID Tokens issued by this API client
41 type: str
42 choices: [readonly, ops_admin, storage_admin, array_admin]
43 issuer:
44 description:
45 - The name of the identity provider that will be issuing ID Tokens for this API client
46 - If not specified, defaults to the API client name, I(name).
47 type: str
48 public_key:
49 description:
50 - The API clients PEM formatted (Base64 encoded) RSA public key.
51 - Include the I(—–BEGIN PUBLIC KEY—–) and I(—–END PUBLIC KEY—–) lines
52 type: str
53 token_ttl:
54 description:
55 - Time To Live length in seconds for the exchanged access token
56 - Range is 1 second to 1 day (86400 seconds)
57 type: int
58 default: 86400
59 enabled:
60 description:
61 - State of the API Client Key
62 type: bool
63 default: true
64 extends_documentation_fragment:
65 - purestorage.flasharray.purestorage.fa
66 """
67
68 EXAMPLES = r"""
69 - name: Create API token ansible-token
70 purestorage.flasharray.purefa_apiclient:
71 name: ansible-token
72 issuer: "Pure Storage"
73 token_ttl: 3000
74 role: array_admin
75 public_key: "{{lookup('file', 'public_pem_file') }}"
76 fa_url: 10.10.10.2
77 api_token: e31060a7-21fc-e277-6240-25983c6c4592
78
79 - name: Disable API CLient
80 purestorage.flasharray.purefa_apiclient:
81 name: ansible-token
82 enabled: false
83 fa_url: 10.10.10.2
84 api_token: e31060a7-21fc-e277-6240-25983c6c4592
85
86 - name: Enable API CLient
87 purestorage.flasharray.purefa_apiclient:
88 name: ansible-token
89 enabled: true
90 fa_url: 10.10.10.2
91 api_token: e31060a7-21fc-e277-6240-25983c6c4592
92
93 - name: Delete API Client
94 purestorage.flasharray.purefa_apiclient:
95 state: absent
96 name: ansible-token
97 fa_url: 10.10.10.2
98 api_token: e31060a7-21fc-e277-6240-25983c6c4592
99 """
100
101 RETURN = r"""
102 """
103
104 HAS_PURESTORAGE = True
105 try:
106 from pypureclient import flasharray
107 except ImportError:
108 HAS_PURESTORAGE = False
109
110 from ansible.module_utils.basic import AnsibleModule
111 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
112 get_array,
113 purefa_argument_spec,
114 )
115 from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
116 LooseVersion,
117 )
118
119 MIN_REQUIRED_API_VERSION = "2.1"
120
121
122 def delete_client(module, array):
123 changed = True
124 if not module.check_mode:
125 res = array.delete_api_clients(names=[module.params["name"]])
126 if res.status_code != 200:
127 module.fail_json(
128 msg="Failed to delete API Client {0}. Error: {1}".format(
129 module.params["name"], res.errors[0].message
130 )
131 )
132 module.exit_json(changed=changed)
133
134
135 def update_client(module, array, client):
136 """Update API Client"""
137 changed = False
138 if client.enabled != module.params["enabled"]:
139 changed = True
140 if not module.check_mode:
141 res = array.patch_api_clients(
142 names=[module.params["name"]],
143 api_clients=flasharray.ApiClientPatch(enabled=module.params["enabled"]),
144 )
145 if res.status_code != 200:
146 module.fail_json(
147 msg="Failed to update API Client {0}. Error: {1}".format(
148 module.params["name"], res.errors[0].message
149 )
150 )
151 module.exit_json(changed=changed)
152
153
154 def create_client(module, array):
155 """Create API Client"""
156 changed = True
157 if not 1 <= module.params["token_ttl"] <= 86400:
158 module.fail_json(msg="token_ttl parameter is out of range (1 to 86400)")
159 else:
160 token_ttl = module.params["token_ttl"] * 1000
161 if not module.params["issuer"]:
162 module.params["issuer"] = module.params["name"]
163 client = flasharray.ApiClientPost(
164 max_role=module.params["role"],
165 issuer=module.params["issuer"],
166 access_token_ttl_in_ms=token_ttl,
167 public_key=module.params["public_key"],
168 )
169 if not module.check_mode:
170 res = array.post_api_clients(names=[module.params["name"]], api_clients=client)
171 if res.status_code != 200:
172 module.fail_json(
173 msg="Failed to create API CLient {0}. Error message: {1}".format(
174 module.params["name"], res.errors[0].message
175 )
176 )
177 if module.params["enabled"]:
178 res = array.patch_api_clients(
179 names=[module.params["name"]],
180 api_clients=flasharray.ApiClientPatch(enabled=module.params["enabled"]),
181 )
182 if res.status_code != 200:
183 array.delete_api_clients(names=[module.params["name"]])
184 module.fail_json(
185 msg="Failed to create API Client {0}. Error: {1}".format(
186 module.params["name"], res.errors[0].message
187 )
188 )
189 module.exit_json(changed=changed)
190
191
192 def main():
193 argument_spec = purefa_argument_spec()
194 argument_spec.update(
195 dict(
196 state=dict(type="str", default="present", choices=["absent", "present"]),
197 enabled=dict(type="bool", default=True),
198 name=dict(type="str", required=True),
199 role=dict(
200 type="str",
201 choices=["readonly", "ops_admin", "storage_admin", "array_admin"],
202 ),
203 public_key=dict(type="str", no_log=True),
204 token_ttl=dict(type="int", default=86400, no_log=False),
205 issuer=dict(type="str"),
206 )
207 )
208
209 module = AnsibleModule(argument_spec, supports_check_mode=True)
210
211 if not HAS_PURESTORAGE:
212 module.fail_json(msg="py-pure-client sdk is required for this module")
213
214 array = get_array(module)
215 api_version = array.get_rest_version()
216
217 if LooseVersion(MIN_REQUIRED_API_VERSION) > LooseVersion(api_version):
218 module.fail_json(
219 msg="FlashArray REST version not supported. "
220 "Minimum version required: {0}".format(MIN_REQUIRED_API_VERSION)
221 )
222 state = module.params["state"]
223
224 try:
225 client = list(array.get_api_clients(names=[module.params["name"]]).items)[0]
226 exists = True
227 except Exception:
228 exists = False
229
230 if not exists and state == "present":
231 create_client(module, array)
232 elif exists and state == "present":
233 update_client(module, array, client)
234 elif exists and state == "absent":
235 delete_client(module, array)
236
237 module.exit_json(changed=False)
238
239
240 if __name__ == "__main__":
241 main()