Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove asyncio and upgrade dependencies #256

Merged
merged 8 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ matrix:
- <<: *code
name: "Python 3.7"
python: 3.7
- <<: *code
name: "Python 3.8"
python: 3.8

- <<: *docs
name: "Python 3.7"
Expand Down
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Changes for croud
Unreleased
==========

- Added support for Python 3.8

0.20.0 - 2019/11/28
===================

Expand Down
154 changes: 154 additions & 0 deletions croud/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
# license agreements. See the NOTICE file distributed with this work for
# additional information regarding copyright ownership. Crate licenses
# this file to you under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. You may
# obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# However, if you have executed another commercial license agreement
# with Crate these terms will supersede the license and you may use the
# software solely pursuant to the terms of the relevant commercial agreement.

import enum
from argparse import Namespace
from typing import Dict, Optional, Tuple

import requests
from yarl import URL

from croud.config import Configuration
from croud.printer import print_error

ResponsePair = Tuple[Optional[Dict], Optional[Dict]]

CLOUD_LOCAL_URL = "http://localhost:8000"
CLOUD_DEV_DOMAIN = "cratedb-dev.cloud"
CLOUD_PROD_DOMAIN = "cratedb.cloud"


class RequestMethod(enum.Enum):
DELETE = "delete"
GET = "get"
PATCH = "patch"
POST = "post"
PUT = "put"


class Client:
def __init__(
self, *, env: str, region: str, sudo: bool = False, _verify_ssl: bool = True
):
"""
:param bool _verify_ssl: A private variable that must only be used during tests!
"""

self.env = env or Configuration.get_env()
self.region = region or Configuration.get_setting("region")
self.sudo = sudo

self.base_url = URL(construct_api_base_url(self.env, self.region))

self._token = Configuration.get_token(self.env)
self.session = requests.Session()
if _verify_ssl is False:
self.session.verify = False
self.session.cookies["session"] = self._token
if self.sudo:
self.session.headers["X-Auth-Sudo"] = "1"

@staticmethod
def from_args(args: Namespace) -> "Client":
return Client(env=args.env, region=args.region, sudo=args.sudo)

def request(
self,
method: RequestMethod,
endpoint: str,
*,
params: dict = None,
body: dict = None,
):
kwargs: dict = {"allow_redirects": False}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
kwargs: dict = {"allow_redirects": False}
kwargs = {"allow_redirects": False}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, otherwise mypy derives an implicit Dict[str, bool] which clashes with the next lines of like kwargs["params"] = params as params is Dict[Any, Any]. Using dict makes it a Dict[Any, Any].

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, ok. Sometimes mypy is more complicated than necessary :D

if params is not None:
kwargs["params"] = params
if body is not None:
kwargs["json"] = body

try:
response = self.session.request(
method.value, str(self.base_url.with_path(endpoint)), **kwargs
)
except requests.RequestException as e:
message = (
f"Failed to perform command on {e.request.url}. "
f"Original error was: '{e}' "
f"Does the environment exist in the region you specified?"
)
return None, {"message": message, "success": False}

if response.is_redirect: # login redirect
print_error("Unauthorized. Use `croud login` to login to CrateDB Cloud.")
exit(1)

# Refresh a previously provided token because it has timed out
response_token = response.cookies.get("session")
if response_token and response_token != self._token:
self._token = response_token
Configuration.set_token(response_token, self.env)

return self.decode_response(response)

def delete(
self, endpoint: str, *, params: dict = None, body: dict = None
) -> ResponsePair:
return self.request(RequestMethod.DELETE, endpoint, params=params, body=body)

def get(self, endpoint: str, *, params: dict = None) -> ResponsePair:
return self.request(RequestMethod.GET, endpoint, params=params)

def patch(
self, endpoint: str, *, params: dict = None, body: dict = None
) -> ResponsePair:
return self.request(RequestMethod.PATCH, endpoint, params=params, body=body)

def post(
self, endpoint: str, *, params: dict = None, body: dict = None
) -> ResponsePair:
return self.request(RequestMethod.POST, endpoint, params=params, body=body)

def put(
self, endpoint: str, *, params: dict = None, body: dict = None
) -> ResponsePair:
return self.request(RequestMethod.PUT, endpoint, params=params, body=body)

def decode_response(self, resp: requests.Response) -> ResponsePair:
if resp.status_code == 204:
# response is empty
return None, None

try:
# API always returns JSON, unless there's an unhandled server error
body = resp.json()
except ValueError:
body = {"message": "Invalid response type.", "success": False}

if resp.status_code >= 400:
return None, body
else:
return body, None


def construct_api_base_url(env: str, region: str = "bregenz.a1") -> str:
if env == "local":
return CLOUD_LOCAL_URL

host = CLOUD_DEV_DOMAIN if env == "dev" else CLOUD_PROD_DOMAIN
return f"https://{region}.{host}"
19 changes: 6 additions & 13 deletions croud/clusters/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@

from argparse import Namespace

from croud.api import Client
from croud.config import get_output_format
from croud.printer import print_response
from croud.rest import Client
from croud.session import RequestMethod
from croud.util import require_confirmation


Expand All @@ -36,7 +35,7 @@ def clusters_list(args: Namespace) -> None:
params["project_id"] = args.project_id

client = Client.from_args(args)
data, errors = client.send(RequestMethod.GET, "/api/v2/clusters/", params=params)
data, errors = client.get("/api/v2/clusters/", params=params)
print_response(
data=data,
errors=errors,
Expand Down Expand Up @@ -72,7 +71,7 @@ def clusters_deploy(args: Namespace) -> None:
if args.unit:
body["product_unit"] = args.unit
client = Client.from_args(args)
data, errors = client.send(RequestMethod.POST, "/api/v2/clusters/", body=body)
data, errors = client.post("/api/v2/clusters/", body=body)
print_response(
data=data,
errors=errors,
Expand All @@ -91,9 +90,7 @@ def clusters_scale(args: Namespace) -> None:

body = {"product_unit": args.unit}
client = Client.from_args(args)
data, errors = client.send(
RequestMethod.PUT, f"/api/v2/clusters/{args.cluster_id}/scale/", body=body
)
data, errors = client.put(f"/api/v2/clusters/{args.cluster_id}/scale/", body=body)
print_response(
data=data,
errors=errors,
Expand All @@ -112,9 +109,7 @@ def clusters_upgrade(args: Namespace) -> None:

body = {"crate_version": args.version}
client = Client.from_args(args)
data, errors = client.send(
RequestMethod.PUT, f"/api/v2/clusters/{args.cluster_id}/upgrade/", body=body
)
data, errors = client.put(f"/api/v2/clusters/{args.cluster_id}/upgrade/", body=body)
print_response(
data=data,
errors=errors,
Expand All @@ -132,9 +127,7 @@ def clusters_upgrade(args: Namespace) -> None:
)
def clusters_delete(args: Namespace) -> None:
client = Client.from_args(args)
data, errors = client.send(
RequestMethod.DELETE, f"/api/v2/clusters/{args.cluster_id}/"
)
data, errors = client.delete(f"/api/v2/clusters/{args.cluster_id}/")
print_response(
data=data,
errors=errors,
Expand Down
15 changes: 5 additions & 10 deletions croud/consumers/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@

from argparse import Namespace

from croud.api import Client
from croud.config import get_output_format
from croud.printer import print_response
from croud.rest import Client
from croud.session import RequestMethod
from croud.util import require_confirmation


Expand All @@ -44,7 +43,7 @@ def consumers_deploy(args: Namespace) -> None:
"table_schema": args.consumer_schema,
}
client = Client.from_args(args)
data, errors = client.send(RequestMethod.POST, "/api/v2/consumers/", body=body)
data, errors = client.post("/api/v2/consumers/", body=body)
print_response(
data=data,
errors=errors,
Expand Down Expand Up @@ -76,7 +75,7 @@ def consumers_list(args: Namespace) -> None:
params["project_id"] = args.project_id

client = Client.from_args(args)
data, errors = client.send(RequestMethod.GET, "/api/v2/consumers/", params=params)
data, errors = client.get("/api/v2/consumers/", params=params)
print_response(
data=data,
errors=errors,
Expand Down Expand Up @@ -114,9 +113,7 @@ def consumers_edit(args: Namespace) -> None:
if config:
body["config"] = config
client = Client.from_args(args)
data, errors = client.send(
RequestMethod.PATCH, f"/api/v2/consumers/{args.consumer_id}/", body=body
)
data, errors = client.patch(f"/api/v2/consumers/{args.consumer_id}/", body=body)
print_response(
data=data, errors=errors, keys=["id"], output_fmt=get_output_format(args)
)
Expand All @@ -128,9 +125,7 @@ def consumers_edit(args: Namespace) -> None:
)
def consumers_delete(args: Namespace) -> None:
client = Client.from_args(args)
data, errors = client.send(
RequestMethod.DELETE, f"/api/v2/consumers/{args.consumer_id}/"
)
data, errors = client.delete(f"/api/v2/consumers/{args.consumer_id}/")
print_response(
data=data,
errors=errors,
Expand Down
53 changes: 20 additions & 33 deletions croud/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@
# with Crate these terms will supersede the license and you may use the
# software solely pursuant to the terms of the relevant commercial agreement.

import asyncio
from argparse import Namespace
from functools import partial
from typing import Optional

from croud.api import Client, construct_api_base_url
from croud.config import Configuration
from croud.printer import print_error, print_info
from croud.rest import Client, RequestMethod
from croud.printer import print_error, print_info, print_warning
from croud.server import Server
from croud.session import cloud_url
from croud.util import can_launch_browser, open_page_in_browser

LOGIN_PATH = "/oauth2/login?cli=true"
Expand All @@ -36,7 +34,7 @@ def get_org_id() -> Optional[str]:
client = Client(
env=Configuration.get_env(), region=Configuration.get_setting("region")
)
data, error = client.send(RequestMethod.GET, "/api/v2/users/me/")
data, error = client.get("/api/v2/users/me/")
if data and not error:
return data.get("organization_id")
return None
Expand All @@ -47,41 +45,30 @@ def login(args: Namespace) -> None:
Performs an OAuth2 Login to CrateDB Cloud
"""

if can_launch_browser():
env = args.env or Configuration.get_env()

loop = asyncio.get_event_loop()
server = Server(loop)
server.create_web_app(partial(Configuration.set_token, env=env))
loop.run_until_complete(server.start())

open_page_in_browser(_login_url(env))
print_info("A browser tab has been launched for you to login.")

try:
loop.run_forever()
except KeyboardInterrupt:
loop.run_until_complete(server.stop())
exit(1)
finally:
loop.run_until_complete(server.stop())

Configuration.set_context(env.lower())
if not can_launch_browser():
print_error("Login only works with a valid browser installed.")
exit(1)

env = args.env or Configuration.get_env()
Configuration.set_context(env.lower())
server = Server(partial(Configuration.set_token, env=env)).start_in_background()
open_page_in_browser(_login_url(env))
print_info("A browser tab has been launched for you to login.")
try:
# Wait for the user to login. They'll be redirected to the `SetTokenHandler`
# which will set the token in the configuration.
server.wait_for_shutdown()
except (KeyboardInterrupt, SystemExit):
print_warning("Login cancelled.")
else:
organization_id = get_org_id()
if organization_id:
Configuration.set_organization_id(organization_id, env)
else:
Configuration.set_organization_id("", env)

loop.close()

else:
print_error("Login only works with a valid browser installed.")
exit(1)

print_info("Login successful.")
print_info("Login successful.")


def _login_url(env: str) -> str:
return cloud_url(env.lower()) + LOGIN_PATH
return construct_api_base_url(env.lower()) + LOGIN_PATH
Loading