1#!/usr/bin/python
2
3# Copyright: (c) 2019, Tim Knipper <tim.knipper@gmail.com>
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 = {
10    'metadata_version': '1.1',
11    'status': ['preview'],
12    'supported_by': 'community'
13}
14
15DOCUMENTATION = r'''
16---
17module: aci_interface_policy_cdp
18short_description: Manage CDP interface policies (cdp:IfPol)
19description:
20- Manage CDP interface policies on Cisco ACI fabrics.
21version_added: '2.9'
22options:
23  cdp_policy:
24    description:
25    - The CDP interface policy name.
26    type: str
27    required: yes
28    aliases: [ cdp_interface, name ]
29  description:
30    description:
31    - The description for the CDP interface policy name.
32    type: str
33    aliases: [ descr ]
34  admin_state:
35    description:
36    - Enable or Disable CDP state.
37    - The APIC defaults to C(yes) when unset during creation.
38    type: bool
39  state:
40    description:
41    - Use C(present) or C(absent) for adding or removing.
42    - Use C(query) for listing an object or multiple objects.
43    type: str
44    choices: [ absent, present, query ]
45    default: present
46extends_documentation_fragment: aci
47seealso:
48- name: APIC Management Information Model reference
49  description: More information about the internal APIC class B(cdp:IfPol).
50  link: https://developer.cisco.com/docs/apic-mim-ref/
51author:
52- Tim Knipper (@tknipper11)
53'''
54
55EXAMPLES = r'''
56- name: Create CDP Test Policy
57  aci_interface_policy_cdp:
58    name: Ansible_CDP_Test_Policy
59    host: apic.example.com
60    username: admin
61    password: adminpass
62    state: present
63
64- name: Remove CDP Test Policy
65  aci_interface_policy_cdp:
66    name: Ansible_CDP_Test_Policy
67    host: apic.example.com
68    username: admin
69    password: adminpass
70    output_level: debug
71    state: absent
72
73- name: Query CDP Policy
74  aci_interface_policy_cdp:
75    host: apic.example.com
76    username: admin
77    password: adminpass
78    state: query
79'''
80
81RETURN = r'''
82current:
83  description: The existing configuration from the APIC after the module has finished
84  returned: success
85  type: list
86  sample:
87    [
88        {
89            "cdpIfPol": {
90                "attributes": {
91                    "adminSt": "disabled",
92                    "annotation": "",
93                    "descr": "Ansible Created CDP Test Policy",
94                    "dn": "uni/infra/cdpIfP-Ansible_CDP_Test_Policy",
95                    "name": "Ansible_CDP_Test_Policy",
96                    "nameAlias": "",
97                    "ownerKey": "",
98                    "ownerTag": ""
99                }
100            }
101        }
102    ]
103error:
104  description: The error information as returned from the APIC
105  returned: failure
106  type: dict
107  sample:
108    {
109        "code": "122",
110        "text": "unknown managed object class foo"
111    }
112raw:
113  description: The raw output returned by the APIC REST API (xml or json)
114  returned: parse error
115  type: str
116  sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
117sent:
118  description: The actual/minimal configuration pushed to the APIC
119  returned: info
120  type: list
121  sample:
122    {
123        "fvTenant": {
124            "attributes": {
125                "descr": "Production environment"
126            }
127        }
128    }
129previous:
130  description: The original configuration from the APIC before the module has started
131  returned: info
132  type: list
133  sample:
134    [
135        {
136            "fvTenant": {
137                "attributes": {
138                    "descr": "Production",
139                    "dn": "uni/tn-production",
140                    "name": "production",
141                    "nameAlias": "",
142                    "ownerKey": "",
143                    "ownerTag": ""
144                }
145            }
146        }
147    ]
148proposed:
149  description: The assembled configuration from the user-provided parameters
150  returned: info
151  type: dict
152  sample:
153    {
154        "fvTenant": {
155            "attributes": {
156                "descr": "Production environment",
157                "name": "production"
158            }
159        }
160    }
161filter_string:
162  description: The filter string used for the request
163  returned: failure or debug
164  type: str
165  sample: ?rsp-prop-include=config-only
166method:
167  description: The HTTP method used for the request to the APIC
168  returned: failure or debug
169  type: str
170  sample: POST
171response:
172  description: The HTTP response from the APIC
173  returned: failure or debug
174  type: str
175  sample: OK (30 bytes)
176status:
177  description: The HTTP status from the APIC
178  returned: failure or debug
179  type: int
180  sample: 200
181url:
182  description: The HTTP url used for the request to the APIC
183  returned: failure or debug
184  type: str
185  sample: https://10.11.12.13/api/mo/uni/tn-production.json
186'''
187from ansible.module_utils.network.aci.aci import ACIModule, aci_argument_spec
188
189from ansible.module_utils.basic import AnsibleModule
190
191
192def main():
193    argument_spec = aci_argument_spec()
194    argument_spec.update(
195        cdp_policy=dict(type='str', required=False, aliases=['cdp_interface', 'name']),  # Not required for querying all objects
196        description=dict(type='str', aliases=['descr']),
197        admin_state=dict(type='bool'),
198        state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
199    )
200
201    module = AnsibleModule(
202        argument_spec=argument_spec,
203        supports_check_mode=True,
204        required_if=[
205            ['state', 'absent', ['cdp_policy']],
206            ['state', 'present', ['cdp_policy']],
207        ],
208    )
209
210    aci = ACIModule(module)
211
212    cdp_policy = module.params['cdp_policy']
213    description = module.params['description']
214    admin_state = aci.boolean(module.params['admin_state'], 'enabled', 'disabled')
215    state = module.params['state']
216
217    aci.construct_url(
218        root_class=dict(
219            aci_class='cdpIfPol',
220            aci_rn='infra/cdpIfP-{0}'.format(cdp_policy),
221            module_object=cdp_policy,
222            target_filter={'name': cdp_policy},
223        ),
224    )
225
226    aci.get_existing()
227
228    if state == 'present':
229        aci.payload(
230            aci_class='cdpIfPol',
231            class_config=dict(
232                name=cdp_policy,
233                descr=description,
234                adminSt=admin_state,
235            ),
236        )
237
238        aci.get_diff(aci_class='cdpIfPol')
239
240        aci.post_config()
241
242    elif state == 'absent':
243        aci.delete_config()
244
245    aci.exit_json()
246
247
248if __name__ == '__main__':
249    main()
250