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
6from __future__ import absolute_import, division, print_function
7__metaclass__ = type
8
9ANSIBLE_METADATA = {'metadata_version': '1.1',
10                    'status': ['preview'],
11                    'supported_by': 'certified'}
12
13DOCUMENTATION = r'''
14---
15module: aci_tenant_action_rule_profile
16short_description: Manage action rule profiles (rtctrl:AttrP)
17description:
18- Manage action rule profiles on Cisco ACI fabrics.
19version_added: '2.4'
20options:
21  action_rule:
22    description:
23    - The name of the action rule profile.
24    type: str
25    aliases: [ action_rule_name, name ]
26  description:
27    description:
28    - The description for the action rule profile.
29    type: str
30    aliases: [ descr ]
31  tenant:
32    description:
33    - The name of the tenant.
34    type: str
35    aliases: [ tenant_name ]
36  state:
37    description:
38    - Use C(present) or C(absent) for adding or removing.
39    - Use C(query) for listing an object or multiple objects.
40    type: str
41    choices: [ absent, present, query ]
42    default: present
43extends_documentation_fragment: aci
44notes:
45- The C(tenant) used must exist before using this module in your playbook.
46  The M(aci_tenant) module can be used for this.
47seealso:
48- module: aci_tenant
49- name: APIC Management Information Model reference
50  description: More information about the internal APIC class B(rtctrl:AttrP).
51  link: https://developer.cisco.com/docs/apic-mim-ref/
52author:
53- Dag Wieers (@dagwieers)
54'''
55
56# FIXME: Add more, better examples
57EXAMPLES = r'''
58- aci_tenant_action_rule_profile:
59    host: apic
60    username: admin
61    password: SomeSecretPassword
62    action_rule: '{{ action_rule }}'
63    description: '{{ descr }}'
64    tenant: '{{ tenant }}'
65  delegate_to: localhost
66'''
67
68RETURN = r'''
69current:
70  description: The existing configuration from the APIC after the module has finished
71  returned: success
72  type: list
73  sample:
74    [
75        {
76            "fvTenant": {
77                "attributes": {
78                    "descr": "Production environment",
79                    "dn": "uni/tn-production",
80                    "name": "production",
81                    "nameAlias": "",
82                    "ownerKey": "",
83                    "ownerTag": ""
84                }
85            }
86        }
87    ]
88error:
89  description: The error information as returned from the APIC
90  returned: failure
91  type: dict
92  sample:
93    {
94        "code": "122",
95        "text": "unknown managed object class foo"
96    }
97raw:
98  description: The raw output returned by the APIC REST API (xml or json)
99  returned: parse error
100  type: str
101  sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
102sent:
103  description: The actual/minimal configuration pushed to the APIC
104  returned: info
105  type: list
106  sample:
107    {
108        "fvTenant": {
109            "attributes": {
110                "descr": "Production environment"
111            }
112        }
113    }
114previous:
115  description: The original configuration from the APIC before the module has started
116  returned: info
117  type: list
118  sample:
119    [
120        {
121            "fvTenant": {
122                "attributes": {
123                    "descr": "Production",
124                    "dn": "uni/tn-production",
125                    "name": "production",
126                    "nameAlias": "",
127                    "ownerKey": "",
128                    "ownerTag": ""
129                }
130            }
131        }
132    ]
133proposed:
134  description: The assembled configuration from the user-provided parameters
135  returned: info
136  type: dict
137  sample:
138    {
139        "fvTenant": {
140            "attributes": {
141                "descr": "Production environment",
142                "name": "production"
143            }
144        }
145    }
146filter_string:
147  description: The filter string used for the request
148  returned: failure or debug
149  type: str
150  sample: ?rsp-prop-include=config-only
151method:
152  description: The HTTP method used for the request to the APIC
153  returned: failure or debug
154  type: str
155  sample: POST
156response:
157  description: The HTTP response from the APIC
158  returned: failure or debug
159  type: str
160  sample: OK (30 bytes)
161status:
162  description: The HTTP status from the APIC
163  returned: failure or debug
164  type: int
165  sample: 200
166url:
167  description: The HTTP url used for the request to the APIC
168  returned: failure or debug
169  type: str
170  sample: https://10.11.12.13/api/mo/uni/tn-production.json
171'''
172
173from ansible.module_utils.basic import AnsibleModule
174from ansible.module_utils.network.aci.aci import ACIModule, aci_argument_spec
175
176
177def main():
178    argument_spec = aci_argument_spec()
179    argument_spec.update(
180        action_rule=dict(type='str', aliases=['action_rule_name', 'name']),  # Not required for querying all objects
181        tenant=dict(type='str', aliases=['tenant_name']),  # Not required for querying all objects
182        description=dict(type='str', aliases=['descr']),
183        state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
184    )
185
186    module = AnsibleModule(
187        argument_spec=argument_spec,
188        supports_check_mode=True,
189        required_if=[
190            ['state', 'absent', ['action_rule', 'tenant']],
191            ['state', 'present', ['action_rule', 'tenant']],
192        ],
193    )
194
195    action_rule = module.params['action_rule']
196    description = module.params['description']
197    state = module.params['state']
198    tenant = module.params['tenant']
199
200    aci = ACIModule(module)
201    aci.construct_url(
202        root_class=dict(
203            aci_class='fvTenant',
204            aci_rn='tn-{0}'.format(tenant),
205            module_object=tenant,
206            target_filter={'name': tenant},
207        ),
208        subclass_1=dict(
209            aci_class='rtctrlAttrP',
210            aci_rn='attr-{0}'.format(action_rule),
211            module_object=action_rule,
212            target_filter={'name': action_rule},
213        ),
214    )
215
216    aci.get_existing()
217
218    if state == 'present':
219        aci.payload(
220            aci_class='rtctrlAttrP',
221            class_config=dict(
222                name=action_rule,
223                descr=description,
224            ),
225        )
226
227        aci.get_diff(aci_class='rtctrlAttrP')
228
229        aci.post_config()
230
231    elif state == 'absent':
232        aci.delete_config()
233
234    aci.exit_json()
235
236
237if __name__ == "__main__":
238    main()
239