Debian Codegraph @

ansible_collections/cisco/mso/plugins/modules/mso_schema_site_vrf.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_vrf
16 short_description: Manage site-local VRFs in schema template
17 description:
18 - Manage site-local VRFs 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 vrf:
38 description:
39 - The name of the VRF 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_template_vrf
52 extends_documentation_fragment: cisco.mso.modules
53 """
54
55 EXAMPLES = r"""
56 - name: Add a new site VRF
57 cisco.mso.mso_schema_site_vrf:
58 host: mso_host
59 username: admin
60 password: SomeSecretPassword
61 schema: Schema1
62 site: Site1
63 template: Template1
64 vrf: VRF1
65 state: present
66
67 - name: Remove a site VRF
68 cisco.mso.mso_schema_site_vrf:
69 host: mso_host
70 username: admin
71 password: SomeSecretPassword
72 schema: Schema1
73 site: Site1
74 template: Template1
75 vrf: VRF1
76 state: absent
77
78 - name: Query a specific site VRF
79 cisco.mso.mso_schema_site_vrf:
80 host: mso_host
81 username: admin
82 password: SomeSecretPassword
83 schema: Schema1
84 site: Site1
85 template: Template1
86 vrf: VRF1
87 state: query
88 register: query_result
89
90 - name: Query all site VRFs
91 cisco.mso.mso_schema_site_vrf:
92 host: mso_host
93 username: admin
94 password: SomeSecretPassword
95 schema: Schema1
96 site: Site1
97 template: Template1
98 state: query
99 register: query_result
100 """
101
102 RETURN = r"""
103 """
104
105 from ansible.module_utils.basic import AnsibleModule
106 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
107
108
109 def main():
110 argument_spec = mso_argument_spec()
111 argument_spec.update(
112 schema=dict(type="str", required=True),
113 site=dict(type="str", required=True),
114 template=dict(type="str", required=True),
115 vrf=dict(type="str", aliases=["name"]), # This parameter is not required for querying all objects
116 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
117 )
118
119 module = AnsibleModule(
120 argument_spec=argument_spec,
121 supports_check_mode=True,
122 required_if=[
123 ["state", "absent", ["vrf"]],
124 ["state", "present", ["vrf"]],
125 ],
126 )
127
128 schema = module.params.get("schema")
129 site = module.params.get("site")
130 template = module.params.get("template").replace(" ", "")
131 vrf = module.params.get("vrf")
132 state = module.params.get("state")
133
134 mso = MSOModule(module)
135
136 # Get schema objects
137 schema_id, schema_path, schema_obj = mso.query_schema(schema)
138
139 # Get site
140 site_id = mso.lookup_site(site)
141
142 # Get site_idx
143 if not schema_obj.get("sites"):
144 mso.fail_json(msg="No site associated with template '{0}'. Associate the site with the template using mso_schema_site.".format(template))
145 sites = [(s.get("siteId"), s.get("templateName")) for s in schema_obj.get("sites")]
146 if (site_id, template) not in sites:
147 mso.fail_json(msg="Provided site/template '{0}-{1}' does not exist. Existing sites/templates: {2}".format(site, template, ", ".join(sites)))
148
149 # Schema-access uses indexes
150 site_idx = sites.index((site_id, template))
151 # Path-based access uses site_id-template
152 site_template = "{0}-{1}".format(site_id, template)
153
154 # Get VRF
155 vrf_ref = mso.vrf_ref(schema_id=schema_id, template=template, vrf=vrf)
156 vrfs = [v.get("vrfRef") for v in schema_obj.get("sites")[site_idx]["vrfs"]]
157 if vrf is not None and vrf_ref in vrfs:
158 vrf_idx = vrfs.index(vrf_ref)
159 vrf_path = "/sites/{0}/vrfs/{1}".format(site_template, vrf)
160 mso.existing = schema_obj.get("sites")[site_idx]["vrfs"][vrf_idx]
161
162 if state == "query":
163 if vrf is None:
164 mso.existing = schema_obj.get("sites")[site_idx]["vrfs"]
165 elif not mso.existing:
166 mso.fail_json(msg="VRF '{vrf}' not found".format(vrf=vrf))
167 mso.exit_json()
168
169 vrfs_path = "/sites/{0}/vrfs".format(site_template)
170 ops = []
171
172 mso.previous = mso.existing
173 if state == "absent":
174 if mso.existing:
175 mso.sent = mso.existing = {}
176 ops.append(dict(op="remove", path=vrf_path))
177
178 elif state == "present":
179 payload = dict(
180 vrfRef=dict(
181 schemaId=schema_id,
182 templateName=template,
183 vrfName=vrf,
184 ),
185 )
186
187 mso.sanitize(payload, collate=True)
188
189 if mso.existing:
190 ops.append(dict(op="replace", path=vrf_path, value=mso.sent))
191 else:
192 ops.append(dict(op="add", path=vrfs_path + "/-", value=mso.sent))
193
194 mso.existing = mso.proposed
195
196 if not module.check_mode:
197 mso.request(schema_path, method="PATCH", data=ops)
198
199 mso.exit_json()
200
201
202 if __name__ == "__main__":
203 main()