Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, Dag Wieers (@dagwieers) <[email protected]>
5 # Copyright: (c) 2020, Shreyas Srish (@shrsr) <[email protected]>
6 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
7
8 from __future__ import absolute_import, division, print_function
9
10 __metaclass__ = type
11
12 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
13
14 DOCUMENTATION = r"""
15 ---
16 module: mso_schema_site
17 short_description: Manage sites in schemas
18 description:
19 - Manage sites on Cisco ACI Multi-Site.
20 author:
21 - Dag Wieers (@dagwieers)
22 - Shreyas Srish (@shrsr)
23 options:
24 schema:
25 description:
26 - The name of the schema.
27 type: str
28 required: true
29 site:
30 description:
31 - The name of the site to manage.
32 type: str
33 aliases: [ name ]
34 template:
35 description:
36 - The name of the template.
37 type: str
38 state:
39 description:
40 - Use C(present) or C(absent) for adding or removing.
41 - Use C(query) for listing an object or multiple objects.
42 type: str
43 choices: [ absent, present, query ]
44 default: present
45 seealso:
46 - module: cisco.mso.mso_schema_template
47 - module: cisco.mso.mso_site
48 extends_documentation_fragment: cisco.mso.modules
49 """
50
51 EXAMPLES = r"""
52 - name: Add a new site to a schema
53 cisco.mso.mso_schema_site:
54 host: mso_host
55 username: admin
56 password: SomeSecretPassword
57 schema: Schema 1
58 site: Site1
59 template: Template 1
60 state: present
61
62 - name: Remove a site from a schema
63 cisco.mso.mso_schema_site:
64 host: mso_host
65 username: admin
66 password: SomeSecretPassword
67 schema: Schema 1
68 site: Site1
69 template: Template 1
70 state: absent
71
72 - name: Query a schema site
73 cisco.mso.mso_schema_site:
74 host: mso_host
75 username: admin
76 password: SomeSecretPassword
77 schema: Schema 1
78 site: Site1
79 template: Template 1
80 state: query
81 register: query_result
82
83 - name: Query all schema sites
84 cisco.mso.mso_schema_site:
85 host: mso_host
86 username: admin
87 password: SomeSecretPassword
88 schema: Schema 1
89 state: query
90 register: query_result
91 """
92
93 RETURN = r"""
94 """
95
96 from ansible.module_utils.basic import AnsibleModule
97 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
98
99
100 def main():
101 argument_spec = mso_argument_spec()
102 argument_spec.update(
103 schema=dict(type="str", required=True),
104 site=dict(type="str", aliases=["name"]),
105 template=dict(type="str"),
106 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
107 )
108
109 module = AnsibleModule(
110 argument_spec=argument_spec,
111 supports_check_mode=True,
112 required_if=[
113 ["state", "absent", ["site", "template"]],
114 ["state", "present", ["site", "template"]],
115 ],
116 )
117
118 schema = module.params.get("schema")
119 site = module.params.get("site")
120 template = module.params.get("template")
121 if template is not None:
122 template = template.replace(" ", "")
123 state = module.params.get("state")
124
125 mso = MSOModule(module)
126
127 # Get schema
128 schema_id, schema_path, schema_obj = mso.query_schema(schema)
129
130 # Get site
131 site_id = mso.lookup_site(site)
132
133 mso.existing = {}
134 site_path = None
135 if "sites" in schema_obj:
136 sites = [(s.get("siteId"), s.get("templateName")) for s in schema_obj.get("sites")]
137 if template:
138 if (site_id, template) in sites:
139 site_idx = sites.index((site_id, template))
140 site_path = "/sites/{0}".format(site_idx)
141 mso.existing = schema_obj.get("sites")[site_idx]
142 else:
143 mso.existing = schema_obj.get("sites")
144
145 if state == "query":
146 if not mso.existing:
147 if template:
148 mso.fail_json(msg="Template '{0}' not found".format(template))
149 else:
150 mso.existing = []
151 mso.exit_json()
152
153 sites_path = "/sites"
154 ops = []
155
156 mso.previous = mso.existing
157 if state == "absent":
158 if mso.existing and site_path:
159 # Remove existing site
160 mso.sent = mso.existing = {}
161 ops.append(dict(op="remove", path=site_path))
162
163 elif state == "present":
164 if not mso.existing:
165 # Add new site
166 payload = dict(
167 siteId=site_id,
168 templateName=template,
169 anps=[],
170 bds=[],
171 contracts=[],
172 externalEpgs=[],
173 intersiteL3outs=[],
174 serviceGraphs=[],
175 vrfs=[],
176 )
177
178 mso.sanitize(payload, collate=True)
179
180 ops.append(dict(op="add", path=sites_path + "/-", value=mso.sent))
181
182 mso.existing = mso.proposed
183
184 if not module.check_mode:
185 mso.request(schema_path, method="PATCH", data=ops)
186
187 mso.exit_json()
188
189
190 if __name__ == "__main__":
191 main()