Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2024, Samita Bhattacharjee (@samiib) <[email protected]>
5
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_template_anp_epg_annotation
17 short_description: Manage EPG Annotations on Cisco Nexus Dashboard Orchestrator (NDO).
18 description:
19 - Manage Endpoint Group (EPG) Annotations on Cisco Nexus Dashboard Orchestrator (NDO).
20 - This module is only supported on ND v3.0 (NDO v4.2) and later.
21 author:
22 - Samita Bhattacharjee (@samiib)
23 options:
24 schema:
25 description:
26 - The name of the schema.
27 type: str
28 required: true
29 template:
30 description:
31 - The name of the template.
32 type: str
33 required: true
34 anp:
35 description:
36 - The name of the Application Network Profile (ANP).
37 type: str
38 required: true
39 epg:
40 description:
41 - The name of the EPG to manage.
42 type: str
43 required: true
44 annotation_key:
45 description:
46 - The key of the Annotation object.
47 type: str
48 aliases: [ key ]
49 annotation_value:
50 description:
51 - The value of the Annotation object.
52 type: str
53 aliases: [ value ]
54 state:
55 description:
56 - Use C(present) or C(absent) for adding or removing.
57 - Use C(query) for listing an object or multiple objects.
58 type: str
59 choices: [ absent, present, query ]
60 default: present
61 extends_documentation_fragment: cisco.mso.modules
62 notes:
63 - The O(schema), O(template), O(anp) and O(epg) must exist before using this module in your playbook.
64 Use M(cisco.mso.mso_schema_template) to create the schema and template.
65 Use M(cisco.mso.mso_schema_template_anp) to create the ANP.
66 Use M(cisco.mso.mso_schema_template_anp_epg) to create the EPG.
67 seealso:
68 - module: cisco.mso.mso_schema_template
69 - module: cisco.mso.mso_schema_template_anp
70 - module: cisco.mso.mso_schema_template_anp_epg
71 """
72
73 EXAMPLES = r"""
74 - name: Add an annotation with key and value
75 cisco.mso.mso_schema_template_anp_epg_annotation:
76 host: mso_host
77 username: admin
78 password: SomeSecretPassword
79 schema: Schema 1
80 template: Template 1
81 anp: ANP 1
82 epg: EPG 1
83 annotation_key: annotation_key_1
84 annotation_value: annotation_value_1
85 state: present
86
87 - name: Update an annotation value with key
88 cisco.mso.mso_schema_template_anp_epg_annotation:
89 host: mso_host
90 username: admin
91 password: SomeSecretPassword
92 schema: Schema 1
93 template: Template 1
94 anp: ANP 1
95 epg: EPG 1
96 annotation_key: annotation_key_1
97 annotation_value: annotation_value_1_updated
98 state: present
99
100 - name: Query a specific annotation with key
101 cisco.mso.mso_schema_template_anp_epg_annotation:
102 host: mso_host
103 username: admin
104 password: SomeSecretPassword
105 schema: Schema 1
106 template: Template 1
107 anp: ANP 1
108 epg: EPG 1
109 annotation_key: annotation_key_1
110 state: query
111 register: query_one
112
113 - name: Query all annotations
114 cisco.mso.mso_schema_template_anp_epg_annotation:
115 host: mso_host
116 username: admin
117 password: SomeSecretPassword
118 schema: Schema 1
119 template: Template 1
120 anp: ANP 1
121 epg: EPG 1
122 state: query
123 register: query_all
124
125 - name: Delete an annotation
126 cisco.mso.mso_schema_template_anp_epg_annotation:
127 host: mso_host
128 username: admin
129 password: SomeSecretPassword
130 schema: Schema 1
131 template: Template 1
132 anp: ANP 1
133 epg: EPG 1
134 annotation_key: annotation_key_1
135 state: absent
136 """
137
138 RETURN = r"""
139 """
140
141 import copy
142 from ansible.module_utils.basic import AnsibleModule
143 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
144 from ansible_collections.cisco.mso.plugins.module_utils.schema import MSOSchema
145
146
147 def main():
148 argument_spec = mso_argument_spec()
149 argument_spec.update(
150 schema=dict(type="str", required=True),
151 template=dict(type="str", required=True),
152 anp=dict(type="str", required=True),
153 epg=dict(type="str", required=True),
154 annotation_key=dict(type="str", aliases=["key"], no_log=False),
155 annotation_value=dict(type="str", aliases=["value"]),
156 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
157 )
158
159 module = AnsibleModule(
160 argument_spec=argument_spec,
161 supports_check_mode=True,
162 required_if=[
163 ["state", "absent", ["annotation_key"]],
164 ["state", "present", ["annotation_key", "annotation_value"]],
165 ],
166 )
167
168 schema = module.params.get("schema")
169 template = module.params.get("template")
170 anp = module.params.get("anp")
171 epg = module.params.get("epg")
172 annotation_key = module.params.get("annotation_key")
173 annotation_value = module.params.get("annotation_value")
174 state = module.params.get("state")
175
176 mso = MSOModule(module)
177
178 mso_schema = MSOSchema(mso, schema, template)
179 mso_schema.set_template(template)
180 mso_schema.set_template_anp(anp)
181 mso_schema.set_template_anp_epg(epg)
182
183 if annotation_key:
184 mso_schema.set_template_anp_epg_annotation(annotation_key, False)
185 annotation = mso_schema.schema_objects.get("template_anp_epg_annotation")
186 if annotation is not None:
187 mso.existing = mso.previous = copy.deepcopy(annotation.details) # Query a specific Annotation
188 else:
189 epg_object = mso_schema.schema_objects["template_anp_epg"]
190 mso.existing = epg_object.details.get("tagAnnotations", []) # Query all
191
192 path = "/templates/{0}/anps/{1}/epgs/{2}/tagAnnotations".format(template, anp, epg)
193
194 ops = []
195 if state == "present":
196 mso_values = {"tagKey": annotation_key, "tagValue": annotation_value}
197 mso.sanitize(mso_values)
198 if mso.existing:
199 if annotation_value is not None and mso.existing.get("tagValue") != annotation_value:
200 ops.append({"op": "replace", "path": "{0}/{1}".format(path, annotation.index), "value": mso_values})
201
202 else:
203 ops.append({"op": "add", "path": "{0}/-".format(path), "value": mso_values})
204 elif state == "absent":
205 if mso.existing:
206 ops.append({"op": "remove", "path": "{0}/{1}".format(path, annotation.index)})
207
208 if not module.check_mode and ops:
209 mso.request(mso_schema.path, method="PATCH", data=ops)
210 mso.existing = mso.proposed
211 elif module.check_mode and state != "query": # When the state is present/absent with check mode
212 mso.existing = mso.proposed if state == "present" else {}
213
214 mso.exit_json()
215
216
217 if __name__ == "__main__":
218 main()