49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
import sys
|
|
import json
|
|
import urllib.request
|
|
|
|
def get_status(ip, script_id):
|
|
url = f"http://{ip}/rpc"
|
|
payload = {
|
|
"id": 1,
|
|
"method": "Script.GetStatus",
|
|
"params": {"id": int(script_id)}
|
|
}
|
|
req = urllib.request.Request(
|
|
url,
|
|
data=json.dumps(payload).encode('utf-8'),
|
|
headers={'Content-Type': 'application/json'}
|
|
)
|
|
try:
|
|
with urllib.request.urlopen(req) as resp:
|
|
res = json.loads(resp.read().decode('utf-8'))
|
|
if "result" in res:
|
|
status = res["result"]
|
|
# Convert all values to strings as HashiCorp External Provider only accepts string properties
|
|
return {k: str(v) for k, v in status.items() if v is not None}
|
|
elif "error" in res:
|
|
return {"error": str(res["error"])}
|
|
except Exception as e:
|
|
return {"error": str(e)}
|
|
return {"error": "unknown"}
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) < 3:
|
|
# Check stdin if arguments are missing (sometimes Terraform passes parameters to stdin)
|
|
try:
|
|
input_data = json.loads(sys.stdin.read())
|
|
ip = input_data.get("ip")
|
|
script_id = input_data.get("script_id")
|
|
except:
|
|
ip, script_id = None, None
|
|
|
|
if not ip or not script_id:
|
|
print(json.dumps({"error": "Usage: get_status.py <ip> <script_id> or pass JSON via stdin"}))
|
|
sys.exit(1)
|
|
else:
|
|
ip = sys.argv[1]
|
|
script_id = sys.argv[2]
|
|
|
|
status = get_status(ip, script_id)
|
|
print(json.dumps(status))
|