| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2020, NetApp Inc |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
"""NetApp StorageGRID - Manage Grid Groups""" |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
|
| 13 |
ANSIBLE_METADATA = { |
| 14 |
"metadata_version": "1.1", |
| 15 |
"status": ["preview"], |
| 16 |
"supported_by": "community", |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
DOCUMENTATION = """ |
| 21 |
module: na_sg_grid_group |
| 22 |
short_description: NetApp StorageGRID manage groups. |
| 23 |
extends_documentation_fragment: |
| 24 |
- netapp.storagegrid.netapp.sg |
| 25 |
version_added: '20.6.0' |
| 26 |
author: NetApp Ansible Team (@joshedmonds) <[email protected]> |
| 27 |
description: |
| 28 |
- Create, Update, Delete Administration Groups within NetApp StorageGRID. |
| 29 |
options: |
| 30 |
state: |
| 31 |
description: |
| 32 |
- Whether the specified group should exist or not. |
| 33 |
type: str |
| 34 |
choices: ['present', 'absent'] |
| 35 |
default: present |
| 36 |
display_name: |
| 37 |
description: |
| 38 |
- Name of the group. |
| 39 |
- Required for create operation |
| 40 |
type: str |
| 41 |
unique_name: |
| 42 |
description: |
| 43 |
- Unique Name for the group. Must begin with C(group/) or C(federated-group/) |
| 44 |
- Required for create, modify or delete operation. |
| 45 |
type: str |
| 46 |
required: true |
| 47 |
read_only: |
| 48 |
description: |
| 49 |
- Users can view settings and features but cannot make changes or perform operations. |
| 50 |
type: bool |
| 51 |
version_added: '21.14.0' |
| 52 |
management_policy: |
| 53 |
description: |
| 54 |
- Management access controls granted to the group within the tenancy. |
| 55 |
type: dict |
| 56 |
suboptions: |
| 57 |
alarm_acknowledgement: |
| 58 |
description: |
| 59 |
- Group members can have permission to acknowledge alarms. |
| 60 |
required: false |
| 61 |
type: bool |
| 62 |
aliases: |
| 63 |
- alarm_acknowledgment |
| 64 |
other_grid_configuration: |
| 65 |
description: |
| 66 |
- Need to investigate. |
| 67 |
required: false |
| 68 |
type: bool |
| 69 |
grid_topology_page_configuration: |
| 70 |
description: |
| 71 |
- Users in this group will have permissions to change grid topology. |
| 72 |
required: false |
| 73 |
type: bool |
| 74 |
tenant_accounts: |
| 75 |
description: |
| 76 |
- Users in this group will have permissions to manage tenant accounts. |
| 77 |
required: false |
| 78 |
type: bool |
| 79 |
change_tenant_root_password: |
| 80 |
description: |
| 81 |
- Users in this group will have permissions to change tenant password. |
| 82 |
required: false |
| 83 |
type: bool |
| 84 |
maintenance: |
| 85 |
description: |
| 86 |
- Users in this group will have permissions to run maintenance tasks on StorageGRID. |
| 87 |
required: false |
| 88 |
type: bool |
| 89 |
metrics_query: |
| 90 |
description: |
| 91 |
- Users in this group will have permissions to query metrics on StorageGRID. |
| 92 |
required: false |
| 93 |
type: bool |
| 94 |
ilm: |
| 95 |
description: |
| 96 |
- Users in this group will have permissions to manage ILM rules on StorageGRID. |
| 97 |
required: false |
| 98 |
type: bool |
| 99 |
object_metadata: |
| 100 |
description: |
| 101 |
- Users in this group will have permissions to manage object metadata. |
| 102 |
required: false |
| 103 |
type: bool |
| 104 |
root_access: |
| 105 |
description: |
| 106 |
- Users in this group will have root access. |
| 107 |
required: false |
| 108 |
type: bool |
| 109 |
manage_alerts: |
| 110 |
description: |
| 111 |
- Users in this group will have the ability to manage silences, alert notifications, and alert rules. |
| 112 |
required: false |
| 113 |
type: bool |
| 114 |
version_added: 21.16.0 |
| 115 |
storage_admin: |
| 116 |
description: |
| 117 |
- Users in this group will have the ability to view and update settings in E-Series SANtricity System Manager from StorageGRID. |
| 118 |
required: false |
| 119 |
type: bool |
| 120 |
version_added: 21.16.0 |
| 121 |
""" |
| 122 |
|
| 123 |
EXAMPLES = """ |
| 124 |
- name: create a StorageGRID group |
| 125 |
netapp.storagegrid.na_sg_grid_group: |
| 126 |
api_url: "https://<storagegrid-endpoint-url>" |
| 127 |
auth_token: "storagegrid-auth-token" |
| 128 |
validate_certs: false |
| 129 |
state: present |
| 130 |
display_name: ansiblegroup100 |
| 131 |
unique_name: group/ansiblegroup100 |
| 132 |
management_policy: |
| 133 |
tenant_accounts: true |
| 134 |
maintenance: true |
| 135 |
root_access: false |
| 136 |
""" |
| 137 |
|
| 138 |
RETURN = """ |
| 139 |
resp: |
| 140 |
description: Returns information about the StorageGRID group attributes. |
| 141 |
returned: success |
| 142 |
type: dict |
| 143 |
sample: { |
| 144 |
"displayName": "Example Group", |
| 145 |
"policies": { |
| 146 |
"management": { |
| 147 |
"alarmAcknowledgment": true, |
| 148 |
"manageAlerts": true, |
| 149 |
"otherGridConfiguration": true, |
| 150 |
"gridTopologyPageConfiguration": true, |
| 151 |
"tenantAccounts": true, |
| 152 |
"changeTenantRootPassword": true, |
| 153 |
"maintenance": true, |
| 154 |
"metricsQuery": true, |
| 155 |
"activateFeatures": false, |
| 156 |
"ilm": true, |
| 157 |
"objectMetadata": true, |
| 158 |
"storageAdmin": true, |
| 159 |
"rootAccess": true |
| 160 |
} |
| 161 |
}, |
| 162 |
"uniqueName": "group/examplegroup", |
| 163 |
"accountId": "12345678901234567890", |
| 164 |
"id": "00000000-0000-0000-0000-000000000000", |
| 165 |
"federated": false, |
| 166 |
"groupURN": "urn:sgws:identity::12345678901234567890:group/examplegroup" |
| 167 |
} |
| 168 |
""" |
| 169 |
|
| 170 |
import re |
| 171 |
|
| 172 |
import ansible_collections.netapp.storagegrid.plugins.module_utils.netapp as netapp_utils |
| 173 |
from ansible.module_utils.basic import AnsibleModule |
| 174 |
from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp_module import NetAppModule |
| 175 |
from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp import SGRestAPI |
| 176 |
|
| 177 |
|
| 178 |
class SgGridGroup(object): |
| 179 |
""" |
| 180 |
Create, modify and delete StorageGRID Grid-administration Group |
| 181 |
""" |
| 182 |
|
| 183 |
def __init__(self): |
| 184 |
""" |
| 185 |
Parse arguments, setup state variables, |
| 186 |
check parameters and ensure request module is installed |
| 187 |
""" |
| 188 |
self.argument_spec = netapp_utils.na_storagegrid_host_argument_spec() |
| 189 |
self.argument_spec.update( |
| 190 |
dict( |
| 191 |
state=dict(required=False, type="str", choices=["present", "absent"], default="present"), |
| 192 |
display_name=dict(required=False, type="str"), |
| 193 |
unique_name=dict(required=True, type="str"), |
| 194 |
read_only=dict(required=False, type="bool"), |
| 195 |
management_policy=dict( |
| 196 |
required=False, |
| 197 |
type="dict", |
| 198 |
options=dict( |
| 199 |
alarm_acknowledgement=dict(required=False, type="bool", aliases=["alarm_acknowledgment"]), |
| 200 |
other_grid_configuration=dict(required=False, type="bool"), |
| 201 |
grid_topology_page_configuration=dict(required=False, type="bool"), |
| 202 |
tenant_accounts=dict(required=False, type="bool"), |
| 203 |
change_tenant_root_password=dict(required=False, type="bool"), |
| 204 |
maintenance=dict(required=False, type="bool"), |
| 205 |
metrics_query=dict(required=False, type="bool"), |
| 206 |
ilm=dict(required=False, type="bool"), |
| 207 |
object_metadata=dict(required=False, type="bool"), |
| 208 |
root_access=dict(required=False, type="bool"), |
| 209 |
manage_alerts=dict(required=False, type="bool"), |
| 210 |
storage_admin=dict(required=False, type="bool"), |
| 211 |
), |
| 212 |
), |
| 213 |
) |
| 214 |
) |
| 215 |
parameter_map = { |
| 216 |
"alarm_acknowledgement": "alarmAcknowledgment", |
| 217 |
"alarm_acknowledgment": "alarmAcknowledgment", |
| 218 |
"other_grid_configuration": "otherGridConfiguration", |
| 219 |
"grid_topology_page_configuration": "gridTopologyPageConfiguration", |
| 220 |
"tenant_accounts": "tenantAccounts", |
| 221 |
"change_tenant_root_password": "changeTenantRootPassword", |
| 222 |
"maintenance": "maintenance", |
| 223 |
"metrics_query": "metricsQuery", |
| 224 |
"ilm": "ilm", |
| 225 |
"object_metadata": "objectMetadata", |
| 226 |
"root_access": "rootAccess", |
| 227 |
"manage_alerts": "manageAlerts", |
| 228 |
"storage_admin": "storageAdmin", |
| 229 |
} |
| 230 |
self.module = AnsibleModule( |
| 231 |
argument_spec=self.argument_spec, |
| 232 |
supports_check_mode=True, |
| 233 |
) |
| 234 |
|
| 235 |
self.na_helper = NetAppModule() |
| 236 |
|
| 237 |
# set up state variables |
| 238 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 239 |
# Calling generic SG rest_api class |
| 240 |
self.rest_api = SGRestAPI(self.module) |
| 241 |
# Get API version |
| 242 |
self.rest_api.get_sg_product_version() |
| 243 |
self.api_version = self.rest_api.get_api_version() |
| 244 |
|
| 245 |
# Checking for the parameters passed and create new parameters list |
| 246 |
self.data = {} |
| 247 |
self.data["displayName"] = self.parameters.get("display_name") |
| 248 |
self.data["uniqueName"] = self.parameters["unique_name"] |
| 249 |
if self.parameters.get("read_only"): |
| 250 |
self.data["managementReadOnly"] = self.parameters["read_only"] |
| 251 |
# Only add the parameter if value is True, as JSON response does not include non-true objects |
| 252 |
self.data["policies"] = {} |
| 253 |
|
| 254 |
if self.parameters.get("management_policy"): |
| 255 |
self.data["policies"] = { |
| 256 |
"management": dict( |
| 257 |
(parameter_map[k], v) for (k, v) in self.parameters["management_policy"].items() if v |
| 258 |
) |
| 259 |
} |
| 260 |
if not self.data["policies"].get("management"): |
| 261 |
self.data["policies"]["management"] = None |
| 262 |
|
| 263 |
self.re_local_group = re.compile("^group/") |
| 264 |
self.re_fed_group = re.compile("^federated-group/") |
| 265 |
|
| 266 |
if ( |
| 267 |
self.re_local_group.match(self.parameters["unique_name"]) is None |
| 268 |
and self.re_fed_group.match(self.parameters["unique_name"]) is None |
| 269 |
): |
| 270 |
self.module.fail_json(msg="unique_name must begin with 'group/' or 'federated-group/'") |
| 271 |
|
| 272 |
def get_grid_group(self, unique_name): |
| 273 |
# Use the unique name to check if the group exists |
| 274 |
api = "api/%s/grid/groups/%s" % (self.api_version, unique_name) |
| 275 |
response, error = self.rest_api.get(api) |
| 276 |
|
| 277 |
if error: |
| 278 |
if response["code"] != 404: |
| 279 |
self.module.fail_json(msg=error) |
| 280 |
else: |
| 281 |
return response["data"] |
| 282 |
return None |
| 283 |
|
| 284 |
def create_grid_group(self): |
| 285 |
api = "api/%s/grid/groups" % self.api_version |
| 286 |
|
| 287 |
response, error = self.rest_api.post(api, self.data) |
| 288 |
|
| 289 |
if error: |
| 290 |
self.module.fail_json(msg=error) |
| 291 |
|
| 292 |
return response["data"] |
| 293 |
|
| 294 |
def delete_grid_group(self, group_id): |
| 295 |
api = "api/%s/grid/groups/%s" % (self.api_version, group_id) |
| 296 |
|
| 297 |
self.data = None |
| 298 |
response, error = self.rest_api.delete(api, self.data) |
| 299 |
if error: |
| 300 |
self.module.fail_json(msg=error) |
| 301 |
|
| 302 |
def update_grid_group(self, group_id): |
| 303 |
api = "api/%s/grid/groups/%s" % (self.api_version, group_id) |
| 304 |
|
| 305 |
response, error = self.rest_api.put(api, self.data) |
| 306 |
if error: |
| 307 |
self.module.fail_json(msg=error) |
| 308 |
|
| 309 |
return response["data"] |
| 310 |
|
| 311 |
def apply(self): |
| 312 |
""" |
| 313 |
Perform pre-checks, call functions and exit |
| 314 |
""" |
| 315 |
grid_group = self.get_grid_group(self.parameters["unique_name"]) |
| 316 |
|
| 317 |
cd_action = self.na_helper.get_cd_action(grid_group, self.parameters) |
| 318 |
|
| 319 |
if cd_action is None and self.parameters["state"] == "present": |
| 320 |
# let's see if we need to update parameters |
| 321 |
|
| 322 |
if self.parameters.get("management_policy"): |
| 323 |
if grid_group.get("policies") is None or grid_group.get("policies", {}).get("management") != self.data["policies"]["management"]: |
| 324 |
self.na_helper.changed = True |
| 325 |
if self.parameters.get("read_only") is not None and self.parameters.get("read_only") != grid_group["managementReadOnly"]: |
| 326 |
self.na_helper.changed = True |
| 327 |
|
| 328 |
result_message = "" |
| 329 |
resp_data = grid_group |
| 330 |
if self.na_helper.changed: |
| 331 |
if self.module.check_mode: |
| 332 |
pass |
| 333 |
else: |
| 334 |
if cd_action == "delete": |
| 335 |
self.delete_grid_group(grid_group["id"]) |
| 336 |
result_message = "Grid Group deleted" |
| 337 |
|
| 338 |
elif cd_action == "create": |
| 339 |
resp_data = self.create_grid_group() |
| 340 |
result_message = "Grid Group created" |
| 341 |
|
| 342 |
else: |
| 343 |
# for a federated group, the displayName parameter needs to be specified |
| 344 |
# and must match the existing displayName |
| 345 |
if self.re_fed_group.match(self.parameters["unique_name"]): |
| 346 |
self.data["displayName"] = grid_group["displayName"] |
| 347 |
|
| 348 |
resp_data = self.update_grid_group(grid_group["id"]) |
| 349 |
result_message = "Grid Group updated" |
| 350 |
|
| 351 |
self.module.exit_json(changed=self.na_helper.changed, msg=result_message, resp=resp_data) |
| 352 |
|
| 353 |
|
| 354 |
def main(): |
| 355 |
""" |
| 356 |
Main function |
| 357 |
""" |
| 358 |
na_sg_grid_group = SgGridGroup() |
| 359 |
na_sg_grid_group.apply() |
| 360 |
|
| 361 |
|
| 362 |
if __name__ == "__main__": |
| 363 |
main() |