Skip to content

Commit

Permalink
cli: create client from configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jirikuncar committed Sep 11, 2017
1 parent ac3760d commit 1435229
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 194 deletions.
83 changes: 0 additions & 83 deletions renga/api/_datastructures.py

This file was deleted.

33 changes: 8 additions & 25 deletions renga/cli/_token.py → renga/cli/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,19 @@
# 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.
"""Perform token operations."""
"""Client utilities."""

from contextlib import contextmanager
from renga import RengaClient

import click
import requests
from ._options import default_endpoint_from_config


def exchange_token(refresh_token, token_endpoint, client_id):
"""Exchange token for access token."""
response = requests.post(
token_endpoint,
data={
'grant_type': 'refresh_token',
'client_id': client_id,
'refresh_token': refresh_token,
})
return response.json()


@contextmanager
def with_access_token(config, endpoint):
def from_config(config, endpoint=None):
"""Yield access token for endpoint in the config."""
token = config['endpoints'][endpoint]['token']['refresh_token']
endpoint = endpoint or default_endpoint_from_config(config)
token = config['endpoints'][endpoint]['token']
url = config['endpoints'][endpoint]['url']
client_id = config['endpoints'][endpoint]['client_id']
data = exchange_token(token, url, client_id)

if 'error' in data:
raise click.ClickException(
'{error_description} ({error})'.format(**data))

yield data['access_token']
return RengaClient(
endpoint, client_id=client_id, token=token, auto_refresh_url=url)
11 changes: 8 additions & 3 deletions renga/cli/_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
import click


def default_endpoint_from_config(config):
"""Return a default endpoint."""
default_endpoint = config.get('core', {}).get('default')
return config.get('project', {}).get('core', {}).get(
'default', default_endpoint)


def default_endpoint(ctx, param, value):
"""Return default endpoint if specified."""
if ctx.resilient_parsing:
Expand All @@ -28,9 +35,7 @@ def default_endpoint(ctx, param, value):
config = ctx.obj['config']

if value is None:
default_endpoint = config.get('core', {}).get('default')
endpoint = config.get('project', {}).get('core', {}).get(
'default', default_endpoint)
endpoint = default_endpoint_from_config(config)

if endpoint is None:
raise click.UsageError('No default endpoint found.')
Expand Down
19 changes: 8 additions & 11 deletions renga/cli/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@

import click

from renga.client import RengaClient

from ._config import with_config
from ._client import from_config
from ._options import option_endpoint
from ._token import with_access_token


@click.command()
Expand All @@ -52,14 +50,13 @@ def add(config, pathspec, endpoint):
bucket_id = config['project']['endpoints'][endpoint]['default_bucket']
resource.setdefault('endpoints', {})

with with_access_token(config, endpoint) as access_token:
client = RengaClient(endpoint=endpoint, access_token=access_token)
bucket = client.buckets.get(bucket_id)
file_ = bucket.create_file(file_name=pathspec)
client = from_config(config, endpoint=endpoint)
bucket = client.buckets.get(bucket_id)
file_ = bucket.create_file(file_name=pathspec)

resource['endpoints'][endpoint] = {
'vertex_id': file_.id,
'access_token': file_.access_token,
}
resource['endpoints'][endpoint] = {
'vertex_id': file_.id,
'access_token': file_.access_token,
}

config['project']['resources'][pathspec] = resource
25 changes: 10 additions & 15 deletions renga/cli/deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@
# limitations under the License.
"""Interact with the deployment service."""

import json

import click

from renga.cli._options import option_endpoint
from renga.client import RengaClient

from ._client import from_config
from ._config import with_config
from ._token import with_access_token


@click.group(invoke_without_command=True)
Expand All @@ -35,10 +32,9 @@
def contexts(ctx, config, endpoint):
"""Manage execution contexts."""
if ctx.invoked_subcommand is None:
with with_access_token(config, endpoint) as access_token:
client = RengaClient(endpoint, access_token=access_token)
for context in client.contexts:
click.echo(context)
client = from_config(config, endpoint=endpoint)
for context in client.contexts:
click.echo(context)


@click.group()
Expand All @@ -52,10 +48,9 @@ def executions():
@with_config
def show(config, context_id, endpoint):
"""Show the executions of a context."""
with with_access_token(config, endpoint) as access_token:
deployer_client = RengaClient(endpoint, access_token).deployer
for execution in deployer_client.list_executions(context_id):
click.echo(json.dumps(execution))
client = from_config(config, endpoint=endpoint)
for execution in client.contexts[context_id].executions:
click.echo(execution)


@executions.command()
Expand All @@ -65,6 +60,6 @@ def show(config, context_id, endpoint):
@with_config
def ports(config, context_id, execution_id, endpoint):
"""Show the port and host mapping of an execution."""
with with_access_token(config, endpoint) as access_token:
deployer_client = RengaClient(endpoint, access_token).deployer
click.echo(deployer_client.get_ports(context_id, execution_id))
client = from_config(config, endpoint=endpoint)
execution = client.contexts[context_id].executions[execution_id]
click.echo(execution.ports)
15 changes: 7 additions & 8 deletions renga/cli/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

from renga import RengaClient

from ._client import from_config
from ._config import create_project_config_path, get_project_config_path, \
read_config, with_config, write_config
from ._options import option_endpoint
from ._token import with_access_token


def validate_name(ctx, param, value):
Expand Down Expand Up @@ -70,12 +70,11 @@ def init(config, directory, autosync, name, force, endpoint):
datetime.datetime.utcnow().isoformat())

if autosync:
with with_access_token(config, endpoint) as access_token:
client = RengaClient(endpoint, access_token=access_token)
project = client.projects.create(name=name)
project_config.setdefault('endpoints', {})
project_config['endpoints'].setdefault(endpoint, {})
project_config['endpoints'][endpoint][
'vertex_id'] = project.id
client = from_config(config, endpoint=endpoint)
project = client.projects.create(name=name)
project_config.setdefault('endpoints', {})
project_config['endpoints'].setdefault(endpoint, {})
project_config['endpoints'][endpoint][
'vertex_id'] = project.id

write_config(project_config, path=project_config_path)
30 changes: 13 additions & 17 deletions renga/cli/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@
import click
import requests

from renga.client import RengaClient

from ._client import from_config
from ._config import config_path, with_config
from ._options import option_endpoint
from ._token import exchange_token, with_access_token


@click.group(name='io', invoke_without_command=True)
Expand All @@ -41,10 +39,9 @@ def storage(ctx, config):
@with_config
def backends(config, endpoint):
"""List all available storage backends."""
with with_access_token(config, endpoint) as access_token:
client = RengaClient(endpoint=endpoint, access_token=access_token)
for backend in client.storage.backends:
click.echo(backend)
client = from_config(config, endpoint=endpoint)
for backend in client.buckets.backends:
click.echo(backend)


@storage.group()
Expand All @@ -59,19 +56,18 @@ def bucket():
@with_config
def create(config, name, backend, endpoint):
"""Create new bucket."""
with with_access_token(config, endpoint) as access_token:
client = RengaClient(endpoint=endpoint, access_token=access_token)
bucket = client.buckets.create(name=name, backend=backend)
client = from_config(config, endpoint=endpoint)
bucket = client.buckets.create(name=name, backend=backend)

config['project']['endpoints'].setdefault(endpoint, {})
config['project']['endpoints'][endpoint].setdefault('buckets', {})
config['project']['endpoints'][endpoint]['buckets'][bucket.id] = name
config['project']['endpoints'].setdefault(endpoint, {})
config['project']['endpoints'][endpoint].setdefault('buckets', {})
config['project']['endpoints'][endpoint]['buckets'][bucket.id] = name

# Set default bucket
config['project']['endpoints'][endpoint].setdefault(
'default_bucket', bucket.id)
# Set default bucket
config['project']['endpoints'][endpoint].setdefault(
'default_bucket', bucket.id)

click.echo(bucket.id)
click.echo(bucket.id)


@bucket.command()
Expand Down
6 changes: 3 additions & 3 deletions renga/cli/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
from renga.api.authorization import LegacyApplicationClient

from ._config import config_path, with_config
from ._client import from_config
from ._options import default_endpoint
from ._token import exchange_token, with_access_token


@click.command()
Expand Down Expand Up @@ -84,5 +84,5 @@ def access(ctx, config, endpoint):
if endpoint not in config.get('endpoints', {}):
raise click.UsageError('Unknown endpoint: {0}'.format(endpoint))

with with_access_token(config, endpoint) as access_token:
click.echo(access_token)
client = from_config(config, endpoint=endpoint)
click.echo(client.refresh_token()['access_token'])
Loading

0 comments on commit 1435229

Please sign in to comment.