Debian Codegraph @

ansible_collections/cisco/mso/plugins/modules/mso_schema_site_anp.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2019, Dag Wieers (@dagwieers) <[email protected]>
5 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
12
13 DOCUMENTATION = r"""
14 ---
15 module: mso_schema_site_anp
16 short_description: Manage site-local Application Network Profiles (ANPs) in schema template
17 description:
18 - Manage site-local ANPs in schema template on Cisco ACI Multi-Site.
19 author:
20 - Dag Wieers (@dagwieers)
21 options:
22 schema:
23 description:
24 - The name of the schema.
25 type: str
26 required: true
27 site:
28 description:
29 - The name of the site.
30 type: str
31 required: true
32 template:
33 description:
34 - The name of the template.
35 type: str
36 required: true
37 anp:
38 description:
39 - The name of the ANP to manage.
40 type: str
41 aliases: [ name ]
42 state:
43 description:
44 - Use C(present) or C(absent) for adding or removing.
45 - Use C(query) for listing an object or multiple objects.
46 type: str
47 choices: [ absent, present, query ]
48 default: present
49 seealso:
50 - module: cisco.mso.mso_schema_site
51 - module: cisco.mso.mso_schema_site_anp_epg
52 - module: cisco.mso.mso_schema_template_anp
53 extends_documentation_fragment: cisco.mso.modules
54 """
55
56 EXAMPLES = r"""
57 - name: Add a new site ANP
58 cisco.mso.mso_schema_site_anp:
59 host: mso_host
60 username: admin
61 password: SomeSecretPassword
62 schema: Schema1
63 site: Site1
64 template: Template1
65 anp: ANP1
66 state: present
67
68 - name: Remove a site ANP
69 cisco.mso.mso_schema_site_anp:
70 host: mso_host
71 username: admin
72 password: SomeSecretPassword
73 schema: Schema1
74 site: Site1
75 template: Template1
76 anp: ANP1
77 state: absent
78
79 - name: Query a specific site ANP
80 cisco.mso.mso_schema_site_anp:
81 host: mso_host
82 username: admin
83 password: SomeSecretPassword
84 schema: Schema1
85 site: Site1
86 template: Template1
87 anp: ANP1
88 state: query
89 register: query_result
90
91 - name: Query all site ANPs
92 cisco.mso.mso_schema_site_anp:
93 host: mso_host
94 username: admin
95 password: SomeSecretPassword
96 schema: Schema1
97 site: Site1
98 template: Template1
99 state: query
100 register: query_result
101 """
102
103 RETURN = r"""
104 """
105
106 from ansible.module_utils.basic import AnsibleModule
107 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
108
109
110 def main():
111 argument_spec = mso_argument_spec()
112 argument_spec.update(
113 schema=dict(type="str", required=True),
114 site=dict(type="str", required=True),
115 template=dict(type="str", required=True),
116 anp=dict(type="str", aliases=["name"]), # This parameter is not required for querying all objects
117 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
118 )
119
120 module = AnsibleModule(
121 argument_spec=argument_spec,
122 supports_check_mode=True,
123 required_if=[
124 ["state", "absent", ["anp"]],
125 ["state", "present", ["anp"]],
126 ],
127 )
128
129 schema = module.params.get("schema")
130 site = module.params.get("site")
131 template = module.params.get("template").replace(" ", "")
132 anp = module.params.get("anp")
133 state = module.params.get("state")
134
135 mso = MSOModule(module)
136
137 # Get schema objects
138 schema_id, schema_path, schema_obj = mso.query_schema(schema)
139
140 # Get site
141 site_id = mso.lookup_site(site)
142
143 # Get site_idx
144 if not schema_obj.get("sites"):
145 mso.fail_json(msg="No site associated with template '{0}'. Associate the site with the template using mso_schema_site.".format(template))
146 sites = [(s.get("siteId"), s.get("templateName")) for s in schema_obj.get("sites")]
147 sites_list = [s.get("siteId") + "/" + s.get("templateName") for s in schema_obj.get("sites")]
148 if (site_id, template) not in sites:
149 mso.fail_json(
150 msg="Provided site/siteId/template '{0}/{1}/{2}' does not exist. "
151 "Existing siteIds/templates: {3}".format(site, site_id, template, ", ".join(sites_list))
152 )
153
154 # Schema-access uses indexes
155 site_idx = sites.index((site_id, template))
156 # Path-based access uses site_id-template
157 site_template = "{0}-{1}".format(site_id, template)
158
159 # Get ANP
160 anp_ref = mso.anp_ref(schema_id=schema_id, template=template, anp=anp)
161 anps = [a.get("anpRef") for a in schema_obj.get("sites")[site_idx]["anps"]]
162
163 if anp is not None and anp_ref in anps:
164 anp_idx = anps.index(anp_ref)
165 anp_path = "/sites/{0}/anps/{1}".format(site_template, anp)
166 mso.existing = schema_obj.get("sites")[site_idx]["anps"][anp_idx]
167
168 if state == "query":
169 if anp is None:
170 mso.existing = schema_obj.get("sites")[site_idx]["anps"]
171 elif not mso.existing:
172 mso.fail_json(msg="ANP '{anp}' not found".format(anp=anp))
173 mso.exit_json()
174
175 anps_path = "/sites/{0}/anps".format(site_template)
176 ops = []
177
178 # Workaround due to inconsistency in attributes REQUEST/RESPONSE API
179 # FIX for MSO Error 400: Bad Request: (0)(0)(0)(0)/deploymentImmediacy error.path.missing
180 mso.replace_keys_in_dict("deployImmediacy", "deploymentImmediacy")
181 if mso.existing.get("anpRef"):
182 anp_ref = mso.dict_from_ref(mso.existing.get("anpRef"))
183 mso.existing["anpRef"] = anp_ref
184
185 mso.previous = mso.existing
186 if state == "absent":
187 if mso.existing:
188 mso.sent = mso.existing = {}
189 ops.append(dict(op="remove", path=anp_path))
190
191 elif state == "present":
192 payload = dict(
193 anpRef=dict(
194 schemaId=schema_id,
195 templateName=template,
196 anpName=anp,
197 ),
198 )
199
200 if "epgs" in mso.existing:
201 for epg in mso.existing.get("epgs"):
202 epg = mso.recursive_dict_from_ref(epg)
203
204 mso.sanitize(payload, collate=True)
205
206 if mso.existing:
207 ops.append(dict(op="replace", path=anp_path, value=mso.sent))
208 else:
209 ops.append(dict(op="add", path=anps_path + "/-", value=mso.sent))
210
211 mso.existing = mso.proposed
212
213 if not module.check_mode and mso.existing != mso.previous:
214 mso.request(schema_path, method="PATCH", data=ops)
215
216 mso.exit_json()
217
218
219 if __name__ == "__main__":
220 main()