2019-08-13 06:30:19 +00:00
|
|
|
import urllib.request
|
|
|
|
import json
|
2019-08-13 09:29:12 +00:00
|
|
|
import sys
|
2019-08-13 09:18:50 +00:00
|
|
|
from pyotp import TOTP
|
2019-08-20 06:20:27 +00:00
|
|
|
from tabulate import tabulate
|
2019-08-13 06:30:19 +00:00
|
|
|
|
|
|
|
UNGLEICH_PAY_SERVER_URL = "https://pay.ungleich.ch"
|
|
|
|
|
|
|
|
|
|
|
|
class ungleichPay(object):
|
|
|
|
def __init__(self, parser, parents):
|
|
|
|
self.parser = parser
|
|
|
|
|
|
|
|
self.parser['customers-list'] = self.parser['sub'].add_parser(
|
|
|
|
'customers-list',
|
|
|
|
help="List customers who have an active VM at ungleich",
|
|
|
|
parents=[parents])
|
2019-08-13 07:56:55 +00:00
|
|
|
self.parser['customers-list'].add_argument('--username', required=True)
|
|
|
|
self.parser['customers-list'].add_argument('--realm', required=True)
|
|
|
|
self.parser['customers-list'].add_argument(
|
2019-08-13 09:18:50 +00:00
|
|
|
'--seed', required=True,
|
|
|
|
help="A valid seed obtained from ungleich"
|
|
|
|
)
|
|
|
|
self.parser['customers-list'].add_argument(
|
|
|
|
'--email', required=True,
|
|
|
|
help="Email associated with the username"
|
2019-08-13 06:30:19 +00:00
|
|
|
)
|
2019-08-13 07:56:55 +00:00
|
|
|
self.parser['customers-list'].add_argument(
|
2019-08-13 07:33:59 +00:00
|
|
|
'--ungleich-pay-server', required=False,
|
|
|
|
help="A valid ungleich pay server url"
|
|
|
|
)
|
2019-08-13 06:30:19 +00:00
|
|
|
self.parser['customers-list'].set_defaults(func=self.customers_list)
|
|
|
|
|
|
|
|
def customers_list(self, args):
|
2019-08-13 07:56:55 +00:00
|
|
|
customers_list_endpoint = "/customers-list/"
|
2019-08-13 07:33:59 +00:00
|
|
|
if args.ungleich_pay_server:
|
|
|
|
request_url = args.ungleich_pay_server + customers_list_endpoint
|
|
|
|
else:
|
|
|
|
request_url = UNGLEICH_PAY_SERVER_URL + customers_list_endpoint
|
2019-08-13 09:18:50 +00:00
|
|
|
print(f"request_url={request_url}")
|
2019-08-13 06:30:19 +00:00
|
|
|
req = urllib.request.Request(url=request_url,
|
|
|
|
method='GET',
|
|
|
|
headers={
|
2019-08-13 09:18:50 +00:00
|
|
|
"email": args.email,
|
2019-08-13 06:30:19 +00:00
|
|
|
"username": args.username,
|
|
|
|
"realm": args.realm,
|
2019-08-13 09:18:50 +00:00
|
|
|
"token": TOTP(args.seed).now(),
|
2019-08-13 06:30:19 +00:00
|
|
|
"Accept": "application/json"
|
|
|
|
})
|
2019-08-13 09:29:12 +00:00
|
|
|
try:
|
|
|
|
response = urllib.request.urlopen(req)
|
|
|
|
except urllib.error.HTTPError as err:
|
|
|
|
print(str(err))
|
|
|
|
sys.exit(1)
|
|
|
|
except urllib.error.URLError as uerr:
|
|
|
|
print(str(uerr))
|
|
|
|
sys.exit(2)
|
2019-08-13 06:30:19 +00:00
|
|
|
response_json = json.loads(response.read().decode('utf-8'))
|
2019-08-20 06:20:27 +00:00
|
|
|
if (response_json['response'] == 'success'):
|
|
|
|
data = response_json["data"]
|
|
|
|
rows = [(x['email'], x['vm_count']) for x in data]
|
2019-08-20 09:42:36 +00:00
|
|
|
headers = ["customer", "vm_count"]
|
|
|
|
print("---")
|
2019-08-20 06:20:27 +00:00
|
|
|
print(tabulate(rows, headers))
|
2019-08-20 09:42:36 +00:00
|
|
|
print("---")
|
|
|
|
vm_count = 0
|
|
|
|
for x in data:
|
|
|
|
vm_count += x['vm_count']
|
|
|
|
print("Total vm_count = {}".format(vm_count))
|
2019-08-20 06:20:27 +00:00
|
|
|
else:
|
|
|
|
print("An error occurred.")
|
|
|
|
print(response_json["message"])
|