Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, Dag Wieers (@dagwieers) <[email protected]>
5 # Copyright: (c) 2023, Akini Ross (@akinross) <[email protected]>
6
7 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
8
9 from __future__ import absolute_import, division, print_function
10
11 __metaclass__ = type
12
13 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
14
15 DOCUMENTATION = r"""
16 ---
17 module: mso_schema
18 short_description: Manage schemas
19 description:
20 - Manage schemas on Cisco ACI Multi-Site.
21 author:
22 - Dag Wieers (@dagwieers)
23 - Akini Ross (@akinross)
24 options:
25 schema:
26 description:
27 - The name of the schema.
28 type: str
29 aliases: [ name ]
30 id:
31 description:
32 - The id of the schema.
33 - This parameter is required when the C(schema) needs to be updated.
34 type: str
35 description:
36 description:
37 - The description of the schema.
38 type: str
39 state:
40 description:
41 - Use C(absent) for removing.
42 - Use C(query) for listing an object or multiple objects.
43 - Use C(present) for creating or updating. Only supported on versions of MSO that are 4.1 or greater.
44 type: str
45 choices: [ absent, query, present ]
46 default: query
47 notes:
48 - Due to restrictions of the MSO REST API this module can only create empty schemas (i.e. schemas without templates) on versions of MSO that are 4.1 or greater.
49 Use the M(cisco.mso.mso_schema_template) to automatically create schemas with templates.
50 seealso:
51 - module: cisco.mso.mso_schema_site
52 - module: cisco.mso.mso_schema_template
53 extends_documentation_fragment: cisco.mso.modules
54 """
55
56 EXAMPLES = r"""
57 - name: Create schema
58 cisco.mso.mso_schema:
59 host: mso_host
60 username: admin
61 password: SomeSecretPassword
62 schema: Schema 1
63 state: present
64 delegate_to: localhost
65
66 - name: Remove schemas
67 cisco.mso.mso_schema:
68 host: mso_host
69 username: admin
70 password: SomeSecretPassword
71 schema: Schema 1
72 state: absent
73
74 - name: Query a schema
75 cisco.mso.mso_schema:
76 host: mso_host
77 username: admin
78 password: SomeSecretPassword
79 schema: Schema 1
80 state: query
81 register: query_result
82
83 - name: Query all schemas
84 cisco.mso.mso_schema:
85 host: mso_host
86 username: admin
87 password: SomeSecretPassword
88 state: query
89 register: query_result
90 """
91
92 RETURN = r"""
93 """
94
95 from ansible.module_utils.basic import AnsibleModule
96 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
97
98
99 def main():
100 argument_spec = mso_argument_spec()
101 argument_spec.update(
102 schema=dict(type="str", aliases=["name"]),
103 id=dict(type="str"),
104 description=dict(type="str"),
105 state=dict(type="str", default="query", choices=["absent", "query", "present"]),
106 )
107
108 module = AnsibleModule(
109 argument_spec=argument_spec,
110 supports_check_mode=True,
111 required_if=[
112 ["state", "absent", ["schema"]],
113 ["state", "present", ["schema"]],
114 ],
115 )
116
117 schema = module.params.get("schema")
118 schema_id = module.params.get("id")
119 description = module.params.get("description")
120 state = module.params.get("state")
121
122 mso = MSOModule(module)
123 path = "schemas"
124
125 # Query for existing object(s)
126 if schema:
127 if schema_id:
128 mso.existing = mso.get_obj(path, id=schema_id)
129 else:
130 mso.existing = mso.get_obj(path, displayName=schema)
131
132 if mso.existing:
133 if not schema_id:
134 schema_id = mso.existing.get("id")
135 path = "schemas/{id}".format(id=schema_id)
136 else:
137 mso.existing = mso.query_objs(path)
138
139 mso.previous = mso.existing
140 if state == "present":
141 mso.sanitize(dict(displayName=schema, id=schema_id, description=description), collate=True)
142 if mso.existing:
143 ops = []
144 if mso.existing.get("displayName") != schema:
145 ops.append(dict(op="replace", path="/displayName", value=schema))
146 if mso.existing.get("description") != description and description is not None:
147 ops.append(dict(op="replace", path="/description", value=description))
148
149 if not module.check_mode:
150 mso.request(path, method="PATCH", data=ops)
151 else:
152 if not module.check_mode:
153 mso.request(path, method="POST", data=dict(displayName=schema, description=description))
154 mso.existing = mso.proposed
155
156 elif state == "absent":
157 mso.previous = mso.existing
158 if mso.existing:
159 if module.check_mode:
160 mso.existing = {}
161 else:
162 mso.existing = mso.request(path, method="DELETE")
163
164 mso.exit_json()
165
166
167 if __name__ == "__main__":
168 main()