62 lines
1.7 KiB
Python
62 lines
1.7 KiB
Python
import json
|
|
import yaml
|
|
|
|
import click
|
|
import requests
|
|
|
|
from .cli_main import main, auth_header, base_url
|
|
|
|
|
|
@main.group("pillar")
|
|
def pillar():
|
|
pass
|
|
|
|
|
|
@pillar.command("get")
|
|
@click.argument("fqdn")
|
|
def pillar_get(fqdn):
|
|
"""Get pillar data for a given FQDN."""
|
|
try:
|
|
response = requests.get(
|
|
f"{base_url()}/pillar/{fqdn}",
|
|
headers=auth_header(),
|
|
)
|
|
response.raise_for_status()
|
|
pillar_data = response.json()
|
|
click.echo(yaml.dump({fqdn: pillar_data}))
|
|
except requests.exceptions.RequestException as e:
|
|
click.echo(f"Error: {e}")
|
|
|
|
@pillar.command("set")
|
|
@click.argument("name")
|
|
@click.option("--host")
|
|
@click.option("--hostgroup")
|
|
@click.option("--parameter-type")
|
|
@click.option("--value")
|
|
def pillar_set(name: str, host: str | None, hostgroup: str | None, parameter_type: str | None, value: str | None):
|
|
try:
|
|
if parameter_type == 'string':
|
|
pass # there is nothing to do here
|
|
elif parameter_type == 'integer':
|
|
_ = int(value)
|
|
elif parameter_type == 'float':
|
|
_ = float(value)
|
|
elif parameter_type == 'bool':
|
|
_ = bool(value)
|
|
elif parameter_type == 'list':
|
|
value = json.loads(value)
|
|
elif parameter_type == 'dict':
|
|
value = json.loads(value)
|
|
else:
|
|
raise ValueError("Invalid parameter type")
|
|
except ValueError as e:
|
|
print(f"Failed to validate value: {e}")
|
|
else:
|
|
data = {
|
|
'host': host,
|
|
'hostgroup': hostgroup,
|
|
'type': parameter_type,
|
|
'value': json.dumps(value)
|
|
}
|
|
|
|
requests.post(f"{base_url()}/pillar/{name}", headers=auth_header(), json=data) |