Debian Codegraph @

ansible_collections/cisco/intersight/plugins/modules/intersight_smtp_policy.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
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 ANSIBLE_METADATA = {'metadata_version': '1.1',
10 'status': ['preview'],
11 'supported_by': 'community'}
12
13 DOCUMENTATION = r'''
14 ---
15 module: intersight_smtp_policy
16 short_description: SMTP Policy configuration for Cisco Intersight
17 description:
18 - Manages SMTP Policy configuration on Cisco Intersight.
19 - Configures SMTP settings for email notifications.
20 - For more information see L(Cisco Intersight,https://intersight.com/apidocs).
21 extends_documentation_fragment: intersight
22 options:
23 state:
24 description:
25 - If C(present), will verify the resource is present and will create if needed.
26 - If C(absent), will verify the resource is absent and will delete if needed.
27 type: str
28 choices: [present, absent]
29 default: present
30 organization:
31 description:
32 - The name of the Organization this resource is assigned to.
33 - Profiles, Policies, and Pools that are created within a Custom Organization are applicable only to devices in the same Organization.
34 type: str
35 default: default
36 name:
37 description:
38 - The name assigned to the SMTP Policy.
39 - The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_.
40 type: str
41 required: true
42 description:
43 description:
44 - The user-defined description for the SMTP Policy.
45 - Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_).
46 type: str
47 aliases: [descr]
48 tags:
49 description:
50 - List of tags in Key:<user-defined key> Value:<user-defined value> format.
51 type: list
52 elements: dict
53 default: []
54 enabled:
55 description:
56 - If enabled, controls the state of the SMTP client service on the managed device.
57 type: bool
58 default: true
59 smtp_server:
60 description:
61 - IP address or hostname of the SMTP server.
62 - The SMTP server is used by the managed device to send email notifications.
63 - Required when C(enabled) is True.
64 type: str
65 smtp_port:
66 description:
67 - Port number used by the SMTP server for outgoing SMTP communication.
68 - Valid range is 1-65535.
69 type: int
70 default: 25
71 min_severity:
72 description:
73 - Minimum fault severity level to receive email notifications.
74 - Email notifications are sent for all faults whose severity is equal to or greater than the chosen level.
75 type: str
76 choices: [critical, major, minor, warning, condition, condition]
77 default: critical
78 sender_email:
79 description:
80 - The email address entered here will be displayed as the from address (mail received from address) of all the SMTP mail alerts that are received.
81 - If not configured, the hostname of the server is used in the from address field.
82 type: str
83 smtp_recipients:
84 description:
85 - List of email addresses that will receive notifications for faults.
86 type: list
87 elements: str
88 author:
89 - Ron Gershburg (@rgershbu)
90 '''
91
92 EXAMPLES = r'''
93 - name: Create SMTP Policy
94 cisco.intersight.intersight_smtp_policy:
95 api_private_key: "{{ api_private_key }}"
96 api_key_id: "{{ api_key_id }}"
97 organization: "default"
98 name: "smtp-policy-test"
99 description: "SMTP Policy for Alerting"
100 enabled: true
101 smtp_server: "smtp.example.com"
102 smtp_port: 25
103 min_severity: "warning"
104 sender_email: "[email protected]"
105 smtp_recipients:
106 - "[email protected]"
107 - "[email protected]"
108 tags:
109 - Key: "Environment"
110 Value: "Production"
111 state: present
112
113 - name: Disable SMTP Policy
114 cisco.intersight.intersight_smtp_policy:
115 api_private_key: "{{ api_private_key }}"
116 api_key_id: "{{ api_key_id }}"
117 name: "smtp-policy-test"
118 enabled: false
119 state: present
120
121 - name: Delete SMTP Policy
122 cisco.intersight.intersight_smtp_policy:
123 api_private_key: "{{ api_private_key }}"
124 api_key_id: "{{ api_key_id }}"
125 name: "smtp-policy-test"
126 state: absent
127 '''
128
129 RETURN = r'''
130 api_response:
131 description: The API response output returned by the specified resource.
132 returned: always
133 type: dict
134 sample:
135 "api_response": {
136 "Name": "smtp-policy-test",
137 "ObjectType": "smtp.Policy",
138 "Description": "SMTP Policy for Alerting",
139 "Enabled": true,
140 "SmtpServer": "smtp.example.com",
141 "SmtpPort": 25,
142 "MinSeverity": "warning",
143 "SenderEmail": "[email protected]",
144 "SmtpRecipients": [
145 "[email protected]",
146 "[email protected]"
147 ],
148 "Tags": [
149 {
150 "Key": "Environment",
151 "Value": "Production"
152 }
153 ]
154 }
155 '''
156
157 from ansible.module_utils.basic import AnsibleModule
158 from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec
159
160
161 def main():
162 argument_spec = intersight_argument_spec.copy()
163 argument_spec.update(
164 state=dict(type='str', choices=['present', 'absent'], default='present'),
165 organization=dict(type='str', default='default'),
166 name=dict(type='str', required=True),
167 description=dict(type='str', aliases=['descr']),
168 tags=dict(type='list', elements='dict', default=[]),
169 enabled=dict(type='bool', default=True),
170 smtp_server=dict(type='str'),
171 smtp_port=dict(type='int', default=25),
172 min_severity=dict(type='str', choices=['critical', 'major', 'minor', 'warning', 'condition', 'condition'], default='critical'),
173 sender_email=dict(type='str'),
174 smtp_recipients=dict(type='list', elements='str')
175 )
176
177 module = AnsibleModule(
178 argument_spec,
179 supports_check_mode=True,
180 )
181
182 intersight = IntersightModule(module)
183 intersight.result['api_response'] = {}
184 intersight.result['trace_id'] = ''
185
186 # Build API Body
187 intersight.api_body = {
188 'Organization': {
189 'Name': intersight.module.params['organization'],
190 },
191 'Name': intersight.module.params['name']
192 }
193
194 if module.params['state'] == 'present':
195 intersight.set_tags_and_description()
196 intersight.api_body['Enabled'] = module.params['enabled']
197 if module.params['enabled']:
198 if not module.params.get('smtp_server'):
199 module.fail_json(msg="smtp_server is required when enabled is True")
200
201 if module.params.get('smtp_server'):
202 intersight.api_body['SmtpServer'] = module.params['smtp_server']
203 if module.params.get('smtp_port'):
204 intersight.api_body['SmtpPort'] = module.params['smtp_port']
205 if module.params.get('min_severity'):
206 intersight.api_body['MinSeverity'] = module.params['min_severity']
207 if module.params.get('sender_email'):
208 intersight.api_body['SenderEmail'] = module.params['sender_email']
209 if module.params.get('smtp_recipients'):
210 intersight.api_body['SmtpRecipients'] = module.params['smtp_recipients']
211
212 resource_path = '/smtp/Policies'
213 intersight.configure_policy_or_profile(resource_path=resource_path)
214
215 module.exit_json(**intersight.result)
216
217
218 if __name__ == '__main__':
219 main()