'client-test' toevoegen
This commit is contained in:
70
client-test
Normal file
70
client-test
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
import click
|
||||||
|
from fast_api_client import Client
|
||||||
|
import json
|
||||||
|
|
||||||
|
from fast_api_client.models import Environment,HTTPValidationError,ValidationError
|
||||||
|
from fast_api_client.api.default import root_get,get_inventory_api_v1_environments_inventory_post,create_environments_api_v1_environments_post,delete_environments_api_v1_environments_delete,list_environments_api_v1_environments_list_post,get_ports_api_v1_environments_ports_post
|
||||||
|
from fast_api_client.types import Response
|
||||||
|
|
||||||
|
client = Client(base_url="http://bla:8000", timeout=120.0)
|
||||||
|
|
||||||
|
@click.command(no_args_is_help=True)
|
||||||
|
@click.option("--create",'-c', type=(str),is_flag=False, help="Create environment (non root user)")
|
||||||
|
@click.option("--delete", '-d', type=(str),is_flag=False, help="Delete environment")
|
||||||
|
@click.option("--getlist", "-l", help="List environments")
|
||||||
|
@click.option("--inventory", "-i", type=(str), help="Get ansible inventory of environment")
|
||||||
|
@click.option("--ports", "-p", type=(str), help="Get ports of environment")
|
||||||
|
@click.option("--uid", '-u', type=(str),is_flag=False, help="Uniq identifier")
|
||||||
|
@click.version_option(version="1.0", prog_name="Dragontool client", message="%(prog)s version %(version)s")
|
||||||
|
|
||||||
|
def dragontool_client(create, delete, getlist, inventory, uid, ports):
|
||||||
|
"""Dragontool client for managing environments"""
|
||||||
|
if create:
|
||||||
|
response: Response[Environment] = create_environments_api_v1_environments_post.sync_detailed(client=client,json_body=Environment(username=create,uid=uid))
|
||||||
|
#print(f"{response.status_code}")
|
||||||
|
if response.status_code == 200:
|
||||||
|
#click.echo(f"Environment {create} created!")
|
||||||
|
data = json.loads(response.content.decode('utf-8'))
|
||||||
|
print(data['uid'])
|
||||||
|
#print(f"{response.content.decode('utf-8')}")
|
||||||
|
elif response.status_code == 500:
|
||||||
|
click.echo(f"Environment {create} not created, could be it already exists!")
|
||||||
|
elif delete:
|
||||||
|
if uid is None:
|
||||||
|
click.echo(f"Missing uid, please add -u <uid>")
|
||||||
|
else:
|
||||||
|
response: Response[Environment] = delete_environments_api_v1_environments_delete.sync_detailed(client=client,json_body=Environment(username=delete,uid=uid))
|
||||||
|
if response.status_code == 200:
|
||||||
|
click.echo(f"Environment {delete} deleted!")
|
||||||
|
elif getlist:
|
||||||
|
response: Response[Environment] = list_environments_api_v1_environments_list_post.sync_detailed(client=client,json_body=Environment(username=getlist))
|
||||||
|
response_json = json.loads(response.content.decode('utf-8'))
|
||||||
|
click.echo(f"Environments:")
|
||||||
|
for json_data in response_json:
|
||||||
|
if "uid" in json_data.keys():
|
||||||
|
click.echo(f"\t- {json_data['uid']}")
|
||||||
|
#click.echo(f"{response.content.decode('utf-8')}")
|
||||||
|
elif inventory:
|
||||||
|
if uid is None:
|
||||||
|
click.echo(f"Missing uid, please add -u <uid>")
|
||||||
|
else:
|
||||||
|
response: Response[Environment] = get_inventory_api_v1_environments_inventory_post.sync_detailed(client=client,json_body=Environment(username=inventory,uid=uid))
|
||||||
|
click.echo(f"{response.content.decode('utf-8')}")
|
||||||
|
elif ports:
|
||||||
|
if uid is None:
|
||||||
|
click.echo(f"Missing uid, please add -u <uid>")
|
||||||
|
else:
|
||||||
|
response: Response[Environment] = get_ports_api_v1_environments_ports_post.sync_detailed(client=client,json_body=Environment(username=ports,uid=uid))
|
||||||
|
response_json = json.loads(response.content.decode('utf-8'))
|
||||||
|
for json_data in response_json:
|
||||||
|
if "container_pub_ssh_port" in json_data.keys():
|
||||||
|
click.echo(f"SSH:\t{json_data['container_alias']} on {json_data['host_ip']}:{json_data['container_pub_ssh_port']}")
|
||||||
|
elif "container_pub_ldap_port" in json_data.keys():
|
||||||
|
click.echo(f"LDAP:\t{json_data['container_alias']} on {json_data['host_ip']}:{json_data['container_pub_ldap_port']}\n")
|
||||||
|
#print(f"{data[0]}")
|
||||||
|
#print(f"{response.content.decode('utf-8')}")
|
||||||
|
#click.echo(f"{data}")
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
dragontool_client()
|
||||||
Reference in New Issue
Block a user