summaryrefslogtreecommitdiff
path: root/plugins/httpapi
diff options
context:
space:
mode:
authoromnom62 <omnom62@outlook.com>2026-03-02 21:20:30 +1000
committeromnom62 <omnom62@outlook.com>2026-03-02 21:20:30 +1000
commit7952a8645124ef160c2759a2dae9e9cb32eac1a9 (patch)
treea01436ff4f4af14d7386715e9e7c613f05d5ac2c /plugins/httpapi
downloadrest.vyos-7952a8645124ef160c2759a2dae9e9cb32eac1a9.tar.gz
rest.vyos-7952a8645124ef160c2759a2dae9e9cb32eac1a9.zip
first commit
Diffstat (limited to 'plugins/httpapi')
-rw-r--r--plugins/httpapi/vyos.py65
1 files changed, 65 insertions, 0 deletions
diff --git a/plugins/httpapi/vyos.py b/plugins/httpapi/vyos.py
new file mode 100644
index 0000000..049ef97
--- /dev/null
+++ b/plugins/httpapi/vyos.py
@@ -0,0 +1,65 @@
+from ansible.plugins.httpapi import HttpApiBase
+from urllib.parse import urlencode
+import json
+
+
+DOCUMENTATION = r'''
+---
+httpapi: vyos
+short_description: VyOS REST API
+description:
+ - HTTPAPI plugin for interacting with VyOS REST API.
+options:
+ api_key:
+ description: VyOS API key
+ required: true
+ type: str
+ vars:
+ - name: ansible_httpapi_api_key
+ env:
+ - name: ANSIBLE_HTTPAPI_API_KEY
+'''
+
+
+class HttpApi(HttpApiBase):
+
+ def set_options(self, task_keys=None, var_options=None, direct=None):
+ super().set_options(task_keys=task_keys, var_options=var_options, direct=direct)
+
+ def send_request(self, path, data=None, method="POST", headers=None):
+
+ api_key = self.get_option("api_key")
+
+ form_data = {}
+
+ if data is not None:
+ if not isinstance(data, str):
+ data = json.dumps(data)
+ form_data["data"] = data
+
+ form_data["key"] = api_key
+
+ body = urlencode(form_data).encode("utf-8")
+
+ if headers is None:
+ headers = {}
+
+ headers["Content-Type"] = "application/x-www-form-urlencoded"
+
+ status, response = self.connection.send(
+ path,
+ body,
+ method=method,
+ headers=headers
+ )
+
+ if hasattr(response, "read"):
+ response = response.read()
+
+ if isinstance(response, bytes):
+ response = response.decode("utf-8")
+
+ if isinstance(response, str):
+ response = json.loads(response)
+
+ return response \ No newline at end of file