| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2025, 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 - Generate auth token to Login into grid/tenant module""" |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
|
| 13 |
ANSIBLE_METADATA = { |
| 14 |
"metadata_version": "1.0", |
| 15 |
"status": ["preview"], |
| 16 |
"supported_by": "community", |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
DOCUMENTATION = """ |
| 21 |
module: na_sg_grid_login |
| 22 |
short_description: Login to StorageGRID grid/tenant. |
| 23 |
version_added: '21.16.0' |
| 24 |
author: NetApp Ansible Team (@vinaykus) <[email protected]> |
| 25 |
description: |
| 26 |
- This module generates authentication token to get login into grid/tenant module. |
| 27 |
options: |
| 28 |
validate_certs: |
| 29 |
required: false |
| 30 |
default: true |
| 31 |
description: |
| 32 |
- Should https certificates be validated? |
| 33 |
type: bool |
| 34 |
hostname: |
| 35 |
description: |
| 36 |
- The ip or fqdn of the admin node for the grid. |
| 37 |
type: str |
| 38 |
required: true |
| 39 |
username: |
| 40 |
description: |
| 41 |
- The username of the user authenticating. |
| 42 |
type: str |
| 43 |
required: true |
| 44 |
password: |
| 45 |
description: |
| 46 |
- The password of the user authenticating. |
| 47 |
type: str |
| 48 |
required: true |
| 49 |
tenant_id: |
| 50 |
description: |
| 51 |
- The uuid of the tenant to log into tenant manager. |
| 52 |
type: str |
| 53 |
""" |
| 54 |
|
| 55 |
EXAMPLES = """ |
| 56 |
- name: generate auth token for grid module on StorageGRID |
| 57 |
netapp.storagegrid.na_sg_grid_login: |
| 58 |
hostname: 1.2.3.4 |
| 59 |
username: root |
| 60 |
password: admin123 |
| 61 |
validate_certs: false |
| 62 |
register: auth |
| 63 |
|
| 64 |
- name: generate auth token for org module on StorageGRID |
| 65 |
netapp.storagegrid.na_sg_grid_login: |
| 66 |
hostname: 1.2.3.4 |
| 67 |
username: tenant_username |
| 68 |
password: tenant_password |
| 69 |
tenant_id: 24240721088016532652 |
| 70 |
validate_certs: false |
| 71 |
register: auth |
| 72 |
""" |
| 73 |
|
| 74 |
RETURN = """ |
| 75 |
resp: |
| 76 |
description: Returns information about the authentication token. |
| 77 |
returned: success |
| 78 |
type: str |
| 79 |
sample: "24263715-99c1-4d32-8c7f-e21cd0f6a731" |
| 80 |
""" |
| 81 |
|
| 82 |
from ansible.module_utils.basic import AnsibleModule |
| 83 |
from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp_module import NetAppModule |
| 84 |
from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp import SGAuthGenRestAPI |
| 85 |
|
| 86 |
|
| 87 |
class SgLogin(object): |
| 88 |
""" |
| 89 |
Class to generate auth token for StorageGRID grid/tenant |
| 90 |
""" |
| 91 |
|
| 92 |
def __init__(self): |
| 93 |
""" |
| 94 |
Parse arguments, setup state variables, |
| 95 |
check parameters and ensure request module is installed |
| 96 |
""" |
| 97 |
self.argument_spec = ( |
| 98 |
dict( |
| 99 |
hostname=dict(type="str", required=True), |
| 100 |
username=dict(type="str", required=True), |
| 101 |
password=dict(type="str", required=True, no_log=True), |
| 102 |
tenant_id=dict(type="str", required=False), |
| 103 |
validate_certs=dict(type="bool", default=True, required=False) |
| 104 |
) |
| 105 |
) |
| 106 |
self.module = AnsibleModule( |
| 107 |
argument_spec=self.argument_spec, |
| 108 |
supports_check_mode=True |
| 109 |
) |
| 110 |
self.na_helper = NetAppModule() |
| 111 |
|
| 112 |
# set up state variables |
| 113 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 114 |
# Calling generic SG auth rest_api class |
| 115 |
self.rest_api = SGAuthGenRestAPI(self.module) |
| 116 |
|
| 117 |
def generate_auth_token(self): |
| 118 |
"""Generate auth token using username and password""" |
| 119 |
api = "api/v3/authorize" |
| 120 |
body = { |
| 121 |
"username": self.parameters["username"], |
| 122 |
"password": self.parameters["password"], |
| 123 |
"cookie": False, |
| 124 |
"csrfToken": False, |
| 125 |
} |
| 126 |
if self.parameters.get("tenant_id"): |
| 127 |
body["accountId"] = self.parameters["tenant_id"] |
| 128 |
|
| 129 |
response, error = self.rest_api.post(api, body) |
| 130 |
if error: |
| 131 |
self.module.fail_json(msg=error) |
| 132 |
return response["data"] |
| 133 |
|
| 134 |
def apply(self): |
| 135 |
''' Apply login changes ''' |
| 136 |
|
| 137 |
resp_data = self.generate_auth_token() |
| 138 |
result_message = "authentication token generated successfully." |
| 139 |
self.na_helper.changed = True |
| 140 |
|
| 141 |
self.module.exit_json(changed=self.na_helper.changed, msg=result_message, na_sa_token=resp_data) |
| 142 |
|
| 143 |
|
| 144 |
def main(): |
| 145 |
""" |
| 146 |
Main function |
| 147 |
""" |
| 148 |
na_sg_grid_login = SgLogin() |
| 149 |
na_sg_grid_login.apply() |
| 150 |
|
| 151 |
|
| 152 |
if __name__ == "__main__": |
| 153 |
main() |