Merge branch '6430/customers_list' into 'master'

6430/customers list

See merge request ungleich-public/ungleich-cli!5
This commit is contained in:
pcoder116 2019-08-21 11:35:17 +02:00
commit 3a78cd4638
5 changed files with 97 additions and 9 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
.idea/
__pycache__/

View File

@ -6,12 +6,10 @@ name = "pypi"
[packages]
requests = "*"
pyotp = "*"
apixu = {git = "https://github.com/apixu/apixu-python.git",ref = "master"}
tabulate = "*"
[dev-packages]
[requires]
python_version = "3.7"
[packages.apixu]
git = "https://github.com/apixu/apixu-python.git"
ref = "master"

25
Pipfile.lock generated
View File

@ -1,7 +1,7 @@
{
"_meta": {
"hash": {
"sha256": "ca9c2522bf07f03d1588afe76e9f6fc73bc1efec20d4f155d82b709efaf14a56"
"sha256": "b6d5bcc7b985514e125b8217b8d9f9467b2fc357f4760cd5e83bf0c115b37ea2"
},
"pipfile-spec": 6,
"requires": {
@ -18,14 +18,14 @@
"default": {
"apixu": {
"git": "https://github.com/apixu/apixu-python.git",
"ref": "master"
"ref": "370216999346d5caf7f8dc6724b5766dcc6da25d"
},
"certifi": {
"hashes": [
"sha256:59b7658e26ca9c7339e00f8f4636cdfe59d34fa37b9b04f6f9e9926b3cece1a5",
"sha256:b26104d6835d1f5e49452a26eb2ff87fe7090b89dfcaee5ea2212697e1e1d7ae"
"sha256:046832c04d4e752f37383b628bc601a7ea7211496b4638f6514d0e5b9acc4939",
"sha256:945e3ba63a0b9f577b1395204e13c3a231f9bc0223888be653286534e5873695"
],
"version": "==2019.3.9"
"version": "==2019.6.16"
},
"chardet": {
"hashes": [
@ -41,6 +41,14 @@
],
"version": "==2.8"
},
"pyotp": {
"hashes": [
"sha256:c88f37fd47541a580b744b42136f387cdad481b560ef410c0d85c957eb2a2bc0",
"sha256:fc537e8acd985c5cbf51e11b7d53c42276fee017a73aec7c07380695671ca1a1"
],
"index": "pypi",
"version": "==2.3.0"
},
"requests": {
"hashes": [
"sha256:11e007a8a2aa0323f5a921e9e6a2d7e4e67d9877e85773fba9ba6419025cbeb4",
@ -49,6 +57,13 @@
"index": "pypi",
"version": "==2.22.0"
},
"tabulate": {
"hashes": [
"sha256:8af07a39377cee1103a5c8b3330a421c2d99b9141e9cc5ddd2e3263fea416943"
],
"index": "pypi",
"version": "==0.8.3"
},
"urllib3": {
"hashes": [
"sha256:b246607a25ac80bedac05c6f282e3cdaf3afb65420fd024ac94435cabe6e18d1",

View File

@ -8,6 +8,7 @@ from ungleich_account import Account_Create
from ungleich_weather import ungleichWeather
from ungleich_ssh_key import SSHKey
from ungleich_config import Ungleich_Config
from ungleich_pay import ungleichPay
VERSION = "0.0.4"
@ -22,6 +23,7 @@ class ungleichCLI(object):
config = Ungleich_Config(self.parser, self.parser_parents)
SSHKey(self.parser, self.parser_parents)
ungleichWeather(self.parser, self.parser_parents)
ungleichPay(self.parser, self.parser_parents)
def _init_parser(self):
self.parser = {}

72
ungleich_pay.py Normal file
View File

@ -0,0 +1,72 @@
import urllib.request
import json
import sys
from pyotp import TOTP
from tabulate import tabulate
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])
self.parser['customers-list'].add_argument('--username', required=True)
self.parser['customers-list'].add_argument('--realm', required=True)
self.parser['customers-list'].add_argument(
'--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"
)
self.parser['customers-list'].add_argument(
'--ungleich-pay-server', required=False,
help="A valid ungleich pay server url"
)
self.parser['customers-list'].set_defaults(func=self.customers_list)
def customers_list(self, args):
customers_list_endpoint = "/customers-list/"
if args.ungleich_pay_server:
request_url = args.ungleich_pay_server + customers_list_endpoint
else:
request_url = UNGLEICH_PAY_SERVER_URL + customers_list_endpoint
print(f"request_url={request_url}")
req = urllib.request.Request(url=request_url,
method='GET',
headers={
"email": args.email,
"username": args.username,
"realm": args.realm,
"token": TOTP(args.seed).now(),
"Accept": "application/json"
})
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)
response_json = json.loads(response.read().decode('utf-8'))
if (response_json['response'] == 'success'):
data = response_json["data"]
rows = [(x['email'], x['vm_count']) for x in data]
headers = ["customer", "vm_count"]
print("---")
print(tabulate(rows, headers))
print("---")
vm_count = 0
for x in data:
vm_count += x['vm_count']
print("Total vm_count = {}".format(vm_count))
else:
print("An error occurred.")
print(response_json["message"])