Debian Codegraph @

ansible_collections/netapp/storagegrid/plugins/modules/na_sg_grid_ssh_security.py

raw
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 - Manage SSH Security configuration"""
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_ssh_security
22 short_description: Configure ssh security on StorageGRID.
23 extends_documentation_fragment:
24 - netapp.storagegrid.netapp.sg
25 version_added: '21.16.0'
26 author: NetApp Ansible Team (@vinaykus) <[email protected]>
27 description:
28 - Configure ssh security on NetApp StorageGRID.
29 options:
30 state:
31 description:
32 - The ssh security should be present.
33 choices: ['present']
34 default: 'present'
35 type: str
36 allow_external_access:
37 description:
38 - Whether external SSH access to the grid is allowed.
39 type: bool
40 required: true
41 """
42
43 EXAMPLES = """
44 - name: configure ssh security setting
45 netapp.storagegrid.na_sg_grid_ssh_security:
46 api_url: "https://<storagegrid-endpoint-url>"
47 auth_token: "storagegrid-auth-token"
48 validate_certs: false
49 state: present
50 allow_external_access: true
51 """
52
53 RETURN = """
54 resp:
55 description: Returns information about the StorageGRID ssh security.
56 returned: If state is 'present'.
57 type: dict
58 sample: {
59 "allowExternalAccess": true
60 }
61 """
62
63 import ansible_collections.netapp.storagegrid.plugins.module_utils.netapp as netapp_utils
64 from ansible.module_utils.basic import AnsibleModule
65 from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp_module import NetAppModule
66 from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp import SGRestAPI
67
68
69 class SgSSHsecurity:
70 """
71 Configure ssh security for StorageGRID
72 """
73
74 def __init__(self):
75 """
76 Parse arguments, setup state variables,
77 check parameters and ensure request module is installed
78 """
79 self.argument_spec = netapp_utils.na_storagegrid_host_argument_spec()
80 self.argument_spec.update(
81 dict(
82 state=dict(type="str", choices=["present"], default="present"),
83 allow_external_access=dict(required=True, type="bool"),
84 )
85 )
86 self.module = AnsibleModule(
87 argument_spec=self.argument_spec,
88 supports_check_mode=True
89 )
90 self.na_helper = NetAppModule()
91
92 # set up state variables
93 self.parameters = self.na_helper.set_parameters(self.module.params)
94 # Calling generic SG rest_api class
95 self.rest_api = SGRestAPI(self.module)
96 # Get API version
97 self.rest_api.get_sg_product_version()
98 self.api_version = self.rest_api.get_api_version()
99
100 # Create new parameters list
101 self.data = {}
102
103 self.data["allowExternalAccess"] = self.parameters["allow_external_access"]
104
105 def get_ssh_security_setting(self):
106 """ Get ssh security setting """
107 api = "api/v4/grid/ssh-security-setting"
108 response, error = self.rest_api.get(api)
109
110 if error:
111 self.module.fail_json(msg=error)
112 else:
113 return response["data"]
114
115 def update_ssh_security_setting(self):
116 """ Update ssh security setting """
117 api = "api/v4/grid/ssh-security-setting"
118 response, error = self.rest_api.put(api, self.data)
119
120 if error:
121 self.module.fail_json(msg=error)
122 else:
123 return response["data"]
124
125 def apply(self):
126 ''' Apply ssh security changes '''
127
128 current_ssh_security = self.get_ssh_security_setting()
129
130 if self.parameters["state"] == "present":
131 # let's see if we need to update parameters
132 modify = self.na_helper.get_modified_attributes(current_ssh_security, self.data)
133
134 result_message = ""
135 resp_data = current_ssh_security
136 if self.na_helper.changed:
137 if self.module.check_mode:
138 pass
139 elif modify:
140 resp_data = self.update_ssh_security_setting()
141 result_message = "SSH security setting updated successfully."
142
143 self.module.exit_json(changed=self.na_helper.changed, msg=result_message, resp=resp_data)
144
145
146 def main():
147 """
148 Main function
149 """
150 na_sg_grid_ssh_security = SgSSHsecurity()
151 na_sg_grid_ssh_security.apply()
152
153
154 if __name__ == "__main__":
155 main()