1#!/usr/bin/python
2# Copyright: Ansible Project
3# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
5from __future__ import absolute_import, division, print_function
6__metaclass__ = type
7
8
9ANSIBLE_METADATA = {'metadata_version': '1.1',
10                    'status': ['preview'],
11                    'supported_by': 'community'}
12
13
14DOCUMENTATION = '''
15---
16module: rax_mon_notification_plan
17short_description: Create or delete a Rackspace Cloud Monitoring notification
18                   plan.
19description:
20- Create or delete a Rackspace Cloud Monitoring notification plan by
21  associating existing rax_mon_notifications with severity levels. Rackspace
22  monitoring module flow | rax_mon_entity -> rax_mon_check ->
23  rax_mon_notification -> *rax_mon_notification_plan* -> rax_mon_alarm
24version_added: "2.0"
25options:
26  state:
27    description:
28    - Ensure that the notification plan with this C(label) exists or does not
29      exist.
30    choices: ['present', 'absent']
31  label:
32    description:
33    - Defines a friendly name for this notification plan. String between 1 and
34      255 characters long.
35    required: true
36  critical_state:
37    description:
38    - Notification list to use when the alarm state is CRITICAL. Must be an
39      array of valid rax_mon_notification ids.
40  warning_state:
41    description:
42    - Notification list to use when the alarm state is WARNING. Must be an array
43      of valid rax_mon_notification ids.
44  ok_state:
45    description:
46    - Notification list to use when the alarm state is OK. Must be an array of
47      valid rax_mon_notification ids.
48author: Ash Wilson (@smashwilson)
49extends_documentation_fragment: rackspace.openstack
50'''
51
52EXAMPLES = '''
53- name: Example notification plan
54  gather_facts: False
55  hosts: local
56  connection: local
57  tasks:
58  - name: Establish who gets called when.
59    rax_mon_notification_plan:
60      credentials: ~/.rax_pub
61      state: present
62      label: defcon1
63      critical_state:
64      - "{{ everyone['notification']['id'] }}"
65      warning_state:
66      - "{{ opsfloor['notification']['id'] }}"
67    register: defcon1
68'''
69
70try:
71    import pyrax
72    HAS_PYRAX = True
73except ImportError:
74    HAS_PYRAX = False
75
76from ansible.module_utils.basic import AnsibleModule
77from ansible.module_utils.rax import rax_argument_spec, rax_required_together, setup_rax_module
78
79
80def notification_plan(module, state, label, critical_state, warning_state, ok_state):
81
82    if len(label) < 1 or len(label) > 255:
83        module.fail_json(msg='label must be between 1 and 255 characters long')
84
85    changed = False
86    notification_plan = None
87
88    cm = pyrax.cloud_monitoring
89    if not cm:
90        module.fail_json(msg='Failed to instantiate client. This typically '
91                             'indicates an invalid region or an incorrectly '
92                             'capitalized region name.')
93
94    existing = []
95    for n in cm.list_notification_plans():
96        if n.label == label:
97            existing.append(n)
98
99    if existing:
100        notification_plan = existing[0]
101
102    if state == 'present':
103        should_create = False
104        should_delete = False
105
106        if len(existing) > 1:
107            module.fail_json(msg='%s notification plans are labelled %s.' %
108                                 (len(existing), label))
109
110        if notification_plan:
111            should_delete = (critical_state and critical_state != notification_plan.critical_state) or \
112                (warning_state and warning_state != notification_plan.warning_state) or \
113                (ok_state and ok_state != notification_plan.ok_state)
114
115            if should_delete:
116                notification_plan.delete()
117                should_create = True
118        else:
119            should_create = True
120
121        if should_create:
122            notification_plan = cm.create_notification_plan(label=label,
123                                                            critical_state=critical_state,
124                                                            warning_state=warning_state,
125                                                            ok_state=ok_state)
126            changed = True
127    else:
128        for np in existing:
129            np.delete()
130            changed = True
131
132    if notification_plan:
133        notification_plan_dict = {
134            "id": notification_plan.id,
135            "critical_state": notification_plan.critical_state,
136            "warning_state": notification_plan.warning_state,
137            "ok_state": notification_plan.ok_state,
138            "metadata": notification_plan.metadata
139        }
140        module.exit_json(changed=changed, notification_plan=notification_plan_dict)
141    else:
142        module.exit_json(changed=changed)
143
144
145def main():
146    argument_spec = rax_argument_spec()
147    argument_spec.update(
148        dict(
149            state=dict(default='present', choices=['present', 'absent']),
150            label=dict(required=True),
151            critical_state=dict(type='list'),
152            warning_state=dict(type='list'),
153            ok_state=dict(type='list')
154        )
155    )
156
157    module = AnsibleModule(
158        argument_spec=argument_spec,
159        required_together=rax_required_together()
160    )
161
162    if not HAS_PYRAX:
163        module.fail_json(msg='pyrax is required for this module')
164
165    state = module.params.get('state')
166
167    label = module.params.get('label')
168    critical_state = module.params.get('critical_state')
169    warning_state = module.params.get('warning_state')
170    ok_state = module.params.get('ok_state')
171
172    setup_rax_module(module, pyrax)
173
174    notification_plan(module, state, label, critical_state, warning_state, ok_state)
175
176
177if __name__ == '__main__':
178    main()
179