Debian Codegraph @

ansible_collections/netapp/ontap/plugins/modules/na_ontap_fdsp.py

raw
1 #!/usr/bin/python
2
3 # (c) 2021-2025, NetApp, Inc
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7 __metaclass__ = type
8
9 DOCUMENTATION = """
10 module: na_ontap_fdsp
11 short_description: NetApp ONTAP create or delete a file directory security policy
12 extends_documentation_fragment:
13 - netapp.ontap.netapp.na_ontap_rest
14 version_added: 21.8.0
15 author: NetApp Ansible Team (@carchi8py) <[email protected]>
16
17 description:
18 - Create or delete a file directory security policy.
19 options:
20 state:
21 description:
22 - Whether the specified policy should exist or not.
23 choices: ['present', 'absent']
24 default: present
25 type: str
26
27 name:
28 description:
29 - Specifies the name of the policy.
30 required: true
31 type: str
32
33 vserver:
34 description:
35 - Specifies the vserver for the security policy.
36 required: true
37 type: str
38 """
39
40 EXAMPLES = """
41 - name: Create File Directory Security Policy
42 netapp.ontap.na_ontap_fdsp:
43 state: present
44 name: "ansible_security_policyl"
45 vserver: "svm1"
46 username: "{{ netapp_username }}"
47 password: "{{ netapp_password }}"
48 hostname: "{{ netapp_hostname }}"
49
50 - name: Delete File Directory Security Policy
51 netapp.ontap.na_ontap_fdsp:
52 state: absent
53 vserver: "svm1"
54 name: "ansible_security_policyl"
55 username: "{{ netapp_username }}"
56 password: "{{ netapp_password }}"
57 hostname: "{{ netapp_hostname }}"
58 """
59
60 RETURN = """
61
62 """
63
64 from ansible.module_utils.basic import AnsibleModule
65 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
66 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
67 from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI
68 import ansible_collections.netapp.ontap.plugins.module_utils.rest_response_helpers as rrh
69
70
71 class NetAppOntapFDSP():
72 """
73 Creates or Destroys a File Directory Security Policy
74 """
75 def __init__(self):
76 """
77 Initialize the ONTAP File Directory Security Policy class
78 """
79 self.argument_spec = netapp_utils.na_ontap_rest_only_spec()
80 self.argument_spec.update(dict(
81 state=dict(required=False, choices=['present', 'absent'], default='present'),
82 vserver=dict(required=True, type='str'),
83 name=dict(required=True, type='str')
84 ))
85
86 self.module = AnsibleModule(
87 argument_spec=self.argument_spec,
88 supports_check_mode=True
89 )
90
91 # set up variables
92 self.na_helper = NetAppModule()
93 self.parameters = self.na_helper.set_parameters(self.module.params)
94
95 self.rest_api = OntapRestAPI(self.module)
96 self.use_rest = self.rest_api.is_rest()
97
98 if not self.use_rest:
99 self.module.fail_json(msg=self.rest_api.requires_ontap_version('na_ontap_fdsp', '9.6'))
100
101 def get_fdsp(self):
102 """
103 Get File Directory Security Policy
104 """
105 api = "private/cli/vserver/security/file-directory/policy"
106 query = {
107 'policy-name': self.parameters['name'],
108 'vserver': self.parameters['vserver']
109 }
110
111 message, error = self.rest_api.get(api, query)
112 records, error = rrh.check_for_0_or_more_records(api, message, error)
113
114 if error:
115 self.module.fail_json(msg=error)
116
117 return records if records else None
118
119 def add_fdsp(self):
120 """
121 Adds a new File Directory Security Policy
122 """
123 api = "private/cli/vserver/security/file-directory/policy"
124 body = {
125 'policy-name': self.parameters['name'],
126 'vserver': self.parameters['vserver']
127 }
128
129 dummy, error = self.rest_api.post(api, body)
130 if error:
131 self.module.fail_json(msg=error)
132
133 def remove_fdsp(self):
134 """
135 Deletes a File Directory Security Policy
136 """
137 api = "private/cli/vserver/security/file-directory/policy"
138 body = {
139 'policy-name': self.parameters['name'],
140 'vserver': self.parameters['vserver']
141 }
142
143 dummy, error = self.rest_api.delete(api, body)
144 if error:
145 self.module.fail_json(msg=error)
146
147 def apply(self):
148 current = self.get_fdsp()
149 cd_action = self.na_helper.get_cd_action(current, self.parameters)
150
151 if self.na_helper.changed:
152 if not self.module.check_mode:
153 if cd_action == 'create':
154 self.add_fdsp()
155 elif cd_action == 'delete':
156 self.remove_fdsp()
157
158 result = netapp_utils.generate_result(self.na_helper.changed, cd_action)
159 self.module.exit_json(**result)
160
161
162 def main():
163 """
164 Creates or removes File Directory Security Policy
165 """
166 obj = NetAppOntapFDSP()
167 obj.apply()
168
169
170 if __name__ == '__main__':
171 main()