Debian Codegraph @

ansible_collections/dellemc/enterprise_sonic/plugins/modules/sonic_api.py

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # (c) 2015 Peter Sprygada, <[email protected]>
4 # Copyright (c) 2025 Dell Inc.
5 #
6 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
7
8 """
9 The module file for sonic_vlans
10 """
11
12 from __future__ import absolute_import, division, print_function
13 __metaclass__ = type
14
15 DOCUMENTATION = """
16 ---
17 module: sonic_api
18 version_added: 1.0.0
19 notes:
20 - Tested against Enterprise SONiC Distribution by Dell Technologies.
21 - Supports C(check_mode).
22 author: Abirami N (@abirami-n)
23 short_description: Manages REST operations on devices running Enterprise SONiC
24 description:
25 - Manages REST operations on devices running Enterprise SONiC Distribution
26 by Dell Technologies. This module provides an implementation for working
27 with SONiC REST operations in a deterministic way.
28 options:
29 url:
30 description:
31 - The HTTP path of the request after 'restconf/'.
32 type: path
33 required: true
34 body:
35 description:
36 - The body of the HTTP request/response to the web service which contains the payload.
37 type: raw
38 method:
39 description:
40 - The HTTP method of the request or response. Must be a valid method
41 accepted by the service that handles the request.
42 type: str
43 required: true
44 choices: ['GET', 'PUT', 'POST', 'PATCH', 'DELETE']
45 status_code:
46 description:
47 - A list of valid, numeric, HTTP status codes that signifies the success of a request.
48 type: list
49 elements: int
50 required: true
51 """
52
53 EXAMPLES = """
54 - name: Checks that you can connect (GET) to a page and it returns a status 200
55 dellemc.enterprise_sonic.sonic_api:
56 url: data/openconfig-interfaces:interfaces/interface=Ethernet60
57 method: "GET"
58 status_code: 200
59
60 - name: Appends data to an existing interface using PATCH and verifies if it returns status 204
61 dellemc.enterprise_sonic.sonic_api:
62 url: data/openconfig-interfaces:interfaces/interface=Ethernet60/config/description
63 method: "PATCH"
64 body: {"openconfig-interfaces:description": "Eth-60"}
65 status_code: 204
66
67 - name: Deletes an associated IP address using DELETE and verifies if it returns status 204
68 dellemc.enterprise_sonic.sonic_api:
69 url: >
70 data/openconfig-interfaces:interfaces/interface=Ethernet64/subinterfaces/subinterface=0/
71 openconfig-if-ip:ipv4/addresses/address=1.1.1.1/config/prefix-length
72 method: "DELETE"
73 status_code: 204
74
75 - name: Adds a VLAN network instance using PUT and verifies if it returns status 204
76 dellemc.enterprise_sonic.sonic_api:
77 url: data/openconfig-network-instance:network-instances/network-instance=Vlan100/
78 method: "PUT"
79 body: {"openconfig-network-instance:network-instance": [{"name": "Vlan100", "config": {"name": "Vlan100"}}]}
80 status_code: 204
81
82 - name: Adds a prefix-set to a routing policy using POST and verifies if it returns 201
83 dellemc.enterprise_sonic.sonic_api:
84 url: data/openconfig-routing-policy:routing-policy/defined-sets/prefix-sets/prefix-set=p1
85 method: "POST"
86 body: {"openconfig-routing-policy:config": {"name": "p1", "mode": "IPV4" }}
87 status_code: 201
88 """
89
90 RETURN = """
91 response:
92 description: The response at the network device end for the REST call which contains the status code.
93 returned: always
94 type: list
95 sample: {"response": [ 204,{""}]}
96 msg:
97 description: The HTTP error message from the request.
98 returned: HTTP Error
99 type: str
100 """
101
102 from ansible.module_utils.connection import ConnectionError
103
104 from ansible.module_utils._text import to_text
105 from ansible.module_utils.basic import AnsibleModule
106 from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.sonic import edit_config, to_request
107
108
109 def initiate_request(module):
110 """
111 Get all the data available in the chassis.
112 """
113 url = module.params['url']
114 body = module.params['body']
115 method = module.params['method']
116 if method == "GET" or method == "DELETE":
117 request = to_request(module, [{"path": url, "method": method}])
118 elif method == "PATCH" or method == "PUT" or method == "POST":
119 request = to_request(module, [{"path": url, "method": method, "data": body}])
120
121 try:
122 response = edit_config(module, request, suppr_ntf_excp=False)
123 except ConnectionError as exc:
124 module.fail_json(msg=to_text(exc))
125 return response
126
127
128 def main():
129 """
130 Main entry point for module execution
131
132 :returns: the result form module invocation
133 """
134 argument_spec = dict(
135 url=dict(type='path', required=True),
136 body=dict(type='raw', required=False),
137 method=dict(type='str', choices=['GET', 'PUT', 'PATCH', 'DELETE', 'POST'], required=True),
138 status_code=dict(type='list', elements='int', required=True),
139 )
140
141 module = AnsibleModule(argument_spec=argument_spec,
142 supports_check_mode=True)
143
144 result = dict(
145 changed=False,
146 )
147 response = initiate_request(module)
148 response_code = response[0][0]
149 status_code = module.params['status_code']
150 if response_code == int(status_code[0]) and response_code in (201, 204):
151 result.update({'changed': True})
152
153 result.update({
154 'response': response,
155 })
156 module.exit_json(**result)
157
158
159 if __name__ == '__main__':
160 main()