Commit 62d6e983 authored by Stavros Sachtouris's avatar Stavros Sachtouris

Use "get_endpoint_url" in client instantiations

Refs: #2
parent 5690a5fc
......@@ -36,7 +36,7 @@ from kamaki.cli.utils import (
print_list, print_dict, print_json, print_items, ask_user,
filter_dicts_by_dict, DontRaiseUnicodeError, pref_enc)
from kamaki.cli.argument import FlagArgument, ValueArgument
from kamaki.cli.errors import CLIInvalidArgument
from kamaki.cli.errors import CLIInvalidArgument, CLIBaseUrlError
from sys import stdin, stdout, stderr
import codecs
......@@ -114,6 +114,20 @@ class _command_init(object):
self.auth_base = auth_base or getattr(self, 'auth_base', None) = cloud or getattr(self, 'cloud', None)
def get_client(self, cls, service): = getattr(self, 'cloud', 'default')
URL, TOKEN = self._custom_url(service), self._custom_token(service)
if not all([URL, TOKEN]):
astakos = getattr(self, 'auth_base', None)
if astakos:
URL = URL or astakos.get_endpoint_url(
self._custom_type(service) or cls.service_type,
TOKEN = TOKEN or astakos.token
raise CLIBaseUrlError(service=service)
return cls(URL, TOKEN)
def write(self, s):
......@@ -36,7 +36,6 @@ from base64 import b64encode
from os.path import exists, expanduser
from io import StringIO
from pydoc import pager
from json import dumps
from kamaki.cli import command
from kamaki.cli.command_tree import CommandTree
......@@ -116,26 +115,8 @@ class _server_wait(_service_wait):
class _init_cyclades(_command_init):
def _run(self, service='compute'):
if getattr(self, 'cloud', None):
base_url = self._custom_url(service) or self._custom_url(
if base_url:
token = self._custom_token(service) or self._custom_token(
'cyclades') or self.config.get_cloud('token')
self.client = CycladesClient(base_url=base_url, token=token)
else: = 'default'
if getattr(self, 'auth_base', False):
cyclades_endpoints = self.auth_base.get_service_endpoints(
self._custom_type('cyclades') or 'compute',
self._custom_version('cyclades') or '')
base_url = cyclades_endpoints['publicURL']
token = self.auth_base.token
self.client = CycladesClient(base_url=base_url, token=token)
raise CLIBaseUrlError(service='cyclades')
def _run(self):
self.client = self.get_client(CycladesClient, 'cyclades')
def _restruct_server_info(self, vm):
......@@ -80,24 +80,7 @@ class _init_image(_command_init):
def _run(self):
if getattr(self, 'cloud', None):
img_url = self._custom_url('image') or self._custom_url('plankton')
if img_url:
token = self._custom_token('image') or self._custom_token(
'plankton') or self.config.get_cloud(, 'token')
self.client = ImageClient(base_url=img_url, token=token)
if getattr(self, 'auth_base', False):
plankton_endpoints = self.auth_base.get_service_endpoints(
self._custom_type('image') or self._custom_type(
'plankton') or 'image',
self._custom_version('image') or self._custom_version(
'plankton') or '')
base_url = plankton_endpoints['publicURL']
token = self.auth_base.token
raise CLIBaseUrlError(service='plankton')
self.client = ImageClient(base_url=base_url, token=token)
self.client = self.get_client(ImageClient, 'plankton')
def main(self):
......@@ -477,16 +460,9 @@ class image_register(_init_image, _optional_json):
required = ('name', 'pithos_location')
def _get_pithos_client(self, locator):
ptoken = self.client.token
if getattr(self, 'auth_base', False):
pithos_endpoints = self.auth_base.get_service_endpoints(
purl = pithos_endpoints['publicURL']
purl = self.config.get_cloud('pithos', 'url')
if not purl:
raise CLIBaseUrlError(service='pithos')
return PithosClient(purl, ptoken, locator.uuid, locator.container)
pithos = self.get_client(PithosClient, 'pithos')
pithos.account, pithos.container = locator.uuid, locator.container
return pithos
def _load_params_from_file(self, location):
params, properties = dict(), dict()
......@@ -38,7 +38,8 @@ from kamaki.cli import command
from kamaki.cli.command_tree import CommandTree
from kamaki.cli.errors import (
CLIBaseUrlError, CLIInvalidArgument, raiseCLIError)
from kamaki.clients.cyclades import CycladesNetworkClient, ClientError
from kamaki.clients.cyclades import (
CycladesNetworkClient, ClientError, CycladesClient)
from kamaki.cli.argument import (
FlagArgument, ValueArgument, RepeatableArgument, IntArgument,
......@@ -74,27 +75,8 @@ class _port_wait(_service_wait):
class _init_network(_command_init):
def _run(self, service='network'):
if getattr(self, 'cloud', None):
base_url = self._custom_url(service) or self._custom_url(
if base_url:
token = self._custom_token(service) or self._custom_token(
'network') or self.config.get_cloud('token')
self.client = CycladesNetworkClient(
base_url=base_url, token=token)
else: = 'default'
if getattr(self, 'auth_base', False):
network_endpoints = self.auth_base.get_service_endpoints(
self._custom_type('network') or 'network',
self._custom_version('network') or '')
base_url = network_endpoints['publicURL']
token = self.auth_base.token
self.client = CycladesNetworkClient(base_url=base_url, token=token)
raise CLIBaseUrlError(service='network')
def _run(self):
self.client = self.get_client(CycladesNetworkClient, 'network')
def _filter_by_user_id(self, nets):
return [net for net in nets if net['user_id'] == self['user_id']] if (
......@@ -376,21 +358,6 @@ class subnet_create(_init_network, _optional_json):
self._run(network_id=self['network_id'], cidr=self['cidr'])
# @command(subnet_cmds)
# class subnet_delete(_init_network, _optional_output_cmd):
# """Delete a subnet"""
# @errors.generic.all
# @errors.cyclades.connection
# def _run(self, subnet_id):
# r = self.client.delete_subnet(subnet_id)
# self._optional_output(r)
# def main(self, subnet_id):
# super(self.__class__, self)._run()
# self._run(subnet_id=subnet_id)
class subnet_modify(_init_network, _optional_json):
"""Modify the attributes of a subnet"""
......@@ -802,11 +769,7 @@ class network_disconnect(_init_network, _port_wait, _optional_json):
"""Disconnect a network from a device"""
def _cyclades_client(self):
auth = getattr(self, 'auth_base')
endpoints = auth.get_service_endpoints('compute')
URL = endpoints['publicURL']
from kamaki.clients.cyclades import CycladesClient
return CycladesClient(URL, self.client.token)
return self.get_client(CycladesClient, 'cyclades')
arguments = dict(
wait=FlagArgument('Wait network to disconnect', ('-w', '--wait')),
......@@ -88,28 +88,12 @@ class _pithos_init(_command_init):
def _run(self):
cloud = getattr(self, 'cloud', None)
if cloud:
self.base_url = self._custom_url('pithos')
else: = 'default'
self.token = self._custom_token('pithos')
self.container = self._custom_container() or 'pithos'
astakos = getattr(self, 'auth_base', None)
if astakos:
self.token = self.token or astakos.token
if not self.base_url:
pithos_endpoints = astakos.get_service_endpoints(
self._custom_type('pithos') or 'object-store',
self._custom_version('pithos') or '')
self.base_url = pithos_endpoints['publicURL']
raise CLIBaseUrlError(service='astakos')
self.client = self.get_client(PithosClient, 'pithos')
self.base_url, self.token = self.client.base_url, self.client.token
self.client = PithosClient(
self.base_url, self.token, self.account, self.container)
self.client.account = self.account
self.container = self._custom_container() or 'pithos'
self.client.container = self.container
def main(self):
......@@ -72,16 +72,22 @@ class CLIUnimplemented(CLIError):
class CLIBaseUrlError(CLIError):
def __init__(self, message='', details=[], importance=2, service=None):
def __init__(
message='', details=[], importance=2, service=None):
service = '%s' % (service or '')
message = message or 'No URL for %s' % service.lower()
details = details or [
'Two ways to resolve this:',
'(Use the correct cloud name, instead of "default")',
'A. (recommended) Let kamaki discover endpoint URLs for all',
'services by setting a single Authentication URL and token:',
' /config set cloud.default.url <AUTH_URL>',
' /config set cloud.default.token <t0k3n>']
'To resolve this:',
'Set the authentication URL and TOKEN:',
' kamaki config set cloud.<CLOUD NAME>.url <AUTH_URL>',
' kamaki config set cloud.<CLOUD NAME>.token <t0k3n>',
'set a service-specific URL and/or TOKEN',
' kamaki config set '
'cloud.<CLOUD NAME>.%s_url <URL>' % (service or '<SERVICE>'),
' kamaki config set '
'cloud.<CLOUD NAME>.%s_token <TOKEN>' % (service or '<SERVICE>')]
super(CLIBaseUrlError, self).__init__(message, details, importance)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment