diff --git a/README.md b/README.md index 38b595d..e6df687 100755 --- a/README.md +++ b/README.md @@ -1 +1,80 @@ -# ucloud-cli \ No newline at end of file +# ucloud-cli + + +### Listing available ucloud products + +``` +(ucloud-cli) [test@ungleich ucloud-cli]$ UCLOUD_PAY_SERVER=http://127.0.0.1:5000 python ucloud.py product list +{ + "/v1/products/": { + "description": "YEARLY IPV6 subscription", + "name": "IPV6 VPN", + "price": 12000, + "recurring_duration": 31536000, + "recurring_duration_units": 0, + "type": "recurring" + } +} +``` + +### Adding new products + +##### Recurring product (subscription) +``` +(ucloud-cli) [test@ungleich ucloud-cli]$ UCLOUD_PAY_SERVER=http://127.0.0.1:5000 python ucloud.py product add \ +--name xxxxxxxxxxxx \ +--realm xxxxxxxxxxxx \ +--seed xxxxxxxxxxxx \ +--product-name "IPV6 VPN" \ +--product-description="YEARLY IPV6 subscription" \ +--product-type=recurring \ +--product-recurring-period=year \ +--product-price=12000 \ +--product-minimum-subscription-period=year + +``` + +##### One-time payment product +``` +(ucloud-cli) [test@ungleich ucloud-cli]$ UCLOUD_PAY_SERVER=https://ucloud-pay-dev.ungleich.ch python ucloud.py product add \ +--name xxxxxxxxxxxx \ +--realm xxxxxxxxxxxx \ +--seed xxxxxxxxxxxx \ +--product-name "One time membership" \ +--product-description="One time membership fees" \ +--product-type=one-time \ +--product-price=3500 +``` + +### Register a payment method (cc for now) + +``` +(ucloud-cli) [test@ungleich ucloud-cli]$ UCLOUD_PAY_SERVER=http://127.0.0.1:5000 python ucloud.py user register-payment \ + --name xxxxxxxxxxxx \ + --realm xxxxxxxx \ + --seed xxxxxxxxxx \ + --cc --number 4242424242424242 \ + --cvc 225 \ + --expiry-month 08 \ + --expiry-year 2022 \ + --card-holder-name "The tester" +``` + +### Order a product + +``` +(ucloud-cli) [test@ungleich ucloud-cli]$ UCLOUD_PAY_SERVER=http://127.0.0.1:5000 python ucloud.py product order \ +--name xxxxxxxxxxxxxxxxx \ +--realm xxxxxxxxxxxxxxxx \ +--seed xxxxxxxxxxxxxxxx \ +--product-id aabc34208e514803a0ee62d697b37e8c +``` + +### Listing different orders + +``` +(ucloud-cli) [test@ungleich ucloud-cli]$ UCLOUD_PAY_SERVER=127.0.0.1:5000 python ucloud.py order list \ +--name xxxxxxxxxxxxxxx \ +--realm xxxxxxxxxxxxxxx \ +--seed xxxxxxxxxxxxxxxxx +``` \ No newline at end of file diff --git a/commands/order.py b/commands/order.py new file mode 100644 index 0000000..ca52006 --- /dev/null +++ b/commands/order.py @@ -0,0 +1,23 @@ +import click +import requests + +from decouple import config +from .helper import OTPCredentials, load_dump_pretty + + +@click.group() +def order(): + pass + + +@order.command("list") +@click.option("--name", envvar="OTP_NAME", required=True) +@click.option("--realm", envvar="OTP_REALM", required=True) +@click.option("--seed", envvar="OTP_SEED", required=True) +def list(name, realm, seed): + data = { + **OTPCredentials(name, realm, seed).get_json() + } + r = requests.get("{}/order/list".format(config('UCLOUD_PAY_SERVER')), + json=data) + print(load_dump_pretty(r.content)) diff --git a/commands/product.py b/commands/product.py new file mode 100755 index 0000000..33861d2 --- /dev/null +++ b/commands/product.py @@ -0,0 +1,65 @@ +import click +import requests +from decouple import config + +from .helper import OTPCredentials, load_dump_pretty + + +@click.group() +def product(): + pass + + +@product.command("list") +def list(): + r = requests.get("{}/product/list".format(config('UCLOUD_PAY_SERVER'))) + print(load_dump_pretty(r.content)) + + +@product.command("add") +@click.option("--name", envvar="OTP_NAME", required=True) +@click.option("--realm", envvar="OTP_REALM", required=True) +@click.option("--seed", envvar="OTP_SEED", required=True) +@click.option("--product-name", required=True) +@click.option("--product-description", required=True) +@click.option("--product-type", required=True, + help="Either one-time or recurring", + type=click.Choice(['one-time', 'recurring'])) +@click.option("--product-price", required=True, help="Price in CHF cents", + type=int) +@click.option("--product-recurring-period", required=False, + help="Only used for products with recurring costs") +@click.option("--product-minimum-subscription-period", required=False, + help="Minimum period for which a user needs to " + "subscribe the product for") +def add(name, realm, seed, product_name, product_description, product_type, + product_price, product_recurring_period, + product_minimum_subscription_period): + data = { + **OTPCredentials(name, realm, seed).get_json(), + "product_name": product_name, + "product_description": product_description, + "product_type": product_type, + "product_price": product_price, + "product_recurring_period": product_recurring_period, + "product_minimum_subscription_period": + product_minimum_subscription_period + } + r = requests.post("{}/product/add".format(config('UCLOUD_PAY_SERVER')), + json=data) + print(load_dump_pretty(r.content)) + + +@product.command("order") +@click.option("--name", envvar="OTP_NAME", required=True) +@click.option("--realm", envvar="OTP_REALM", required=True) +@click.option("--seed", envvar="OTP_SEED", required=True) +@click.option("--product-id", help="The uuid of the product", required=True) +def order(name, realm, seed, product_id): + data = { + **OTPCredentials(name, realm, seed).get_json(), + "product_id": product_id + } + r = requests.post("{}/product/order".format(config('UCLOUD_PAY_SERVER')), + json=data) + print(load_dump_pretty(r.content)) diff --git a/commands/user.py b/commands/user.py index cd23d31..77dc487 100755 --- a/commands/user.py +++ b/commands/user.py @@ -1,8 +1,7 @@ import click -import json import requests - from decouple import config + from .helper import OTPCredentials, load_dump_pretty @@ -17,7 +16,8 @@ def user(): @click.option("--seed", envvar="OTP_SEED", required=True) def list_files(name, realm, seed): data = OTPCredentials(name, realm, seed).get_json() - r = requests.get(f"{config('UCLOUD_API_SERVER')}/user/files", json=data) + r = requests.get("{}/user/files".format(config('UCLOUD_API_SERVER')), + json=data) print(load_dump_pretty(r.content)) @@ -27,5 +27,33 @@ def list_files(name, realm, seed): @click.option("--seed", envvar="OTP_SEED", required=True) def list_vms(name, realm, seed): data = OTPCredentials(name, realm, seed).get_json() - r = requests.get(f"{config('UCLOUD_API_SERVER')}/user/vms", json=data) + r = requests.get("{}/user/vms".format(config('UCLOUD_API_SERVER')), + json=data) + print(load_dump_pretty(r.content)) + + +@user.command("register-payment") +@click.option("--name", envvar="OTP_NAME", required=True) +@click.option("--realm", envvar="OTP_REALM", required=True) +@click.option("--seed", envvar="OTP_SEED", required=True) +@click.option("--cc", required=True, is_flag=True) +@click.option("--number", required=True) +@click.option("--cvc", required=True) +@click.option("--expiry-month", required=True, help="MM", type=str) +@click.option("--expiry-year", required=True, help="YY", type=str) +@click.option("--card-holder-name", required=True, + help="The name as printed on the card", type=str) +def register_payment(name, realm, seed, number, cvc, expiry_year, expiry_month, + card_holder_name, cc): + data = { + **OTPCredentials(name, realm, seed).get_json(), + "card_number": number, + "cvc": cvc, + "expiry_year": expiry_year, + "expiry_month": expiry_month, + "card_holder_name": card_holder_name + } + r = requests.post( + "{}/user/register_payment".format(config('UCLOUD_PAY_SERVER')), + json=data) print(load_dump_pretty(r.content)) diff --git a/ucloud.py b/ucloud.py index ceb26fc..964ade8 100755 --- a/ucloud.py +++ b/ucloud.py @@ -4,6 +4,8 @@ from commands.vm import vm from commands.user import user from commands.host import host from commands.image import image +from commands.product import product +from commands.order import order @click.group() @@ -15,6 +17,8 @@ entry_point.add_command(vm) entry_point.add_command(user) entry_point.add_command(image) entry_point.add_command(host) +entry_point.add_command(product) +entry_point.add_command(order) if __name__ == "__main__": entry_point()