Add logging configuration for cli. Begin action approach

Actions are not yet working as desired. This commit also
contains some small amount of api refactor for setup
of port.
This commit is contained in:
Bryan Strassner 2017-07-14 12:34:06 -05:00 committed by Scott Hussey
parent 8737b8ef4c
commit a73d5d917d
7 changed files with 109 additions and 32 deletions

View File

@ -0,0 +1,31 @@
# Copyright 2017 AT&T Intellectual Property. All other rights reserved.
#
# Licensed 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.
""" Base classes for cli actions intended to invoke the api
"""
import logging
from oslo_config import cfg
class CliAction(object):
""" Action base for CliActions
"""
def __init__(self, api_client, debug):
self.logger = logging.getLogger(cfg.CONF.logging.control_logger_name)
self.api_client = api_client
self.debug = debug
if self.debug:
self.logger.info("Action initialized with client %s" % (self.api_client), extra=extra)
def invoke(self):
raise NotImplementedError("Invoke method has not been implemented")

View File

@ -11,38 +11,59 @@
# 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.
""" The entrypoint for the cli commands
"""
import os
import logging
import click
from drydock_provisioner.drydock_client.session import DrydockSession
from drydock_provisioner.drydock_client.client import DrydockClient
from .design import commands as design
@click.group()
@click.option('--debug/--no-debug', default=False)
@click.option('--token', default=lambda: os.environ.get('DD_TOKEN', None))
@click.option('--url' , default=lambda: os.environ.get('DD_URL', None))
@click.option('--debug/--no-debug',
help='Enable or disable debugging',
default=False)
@click.option('--token',
'-t',
help='The auth token to be used',
default=lambda: os.environ.get('DD_TOKEN', ''))
@click.option('--url',
'-u',
help='The url of the running drydock instance',
default=lambda: os.environ.get('DD_URL', ''))
@click.pass_context
def drydock(ctx, debug, token, url):
""" Base cli command. Peforms validations and sets default values.
""" Drydock CLI to invoke the running instance of the drydock API
"""
if not ctx.obj:
ctx.obj = {}
ctx.obj['DEBUG'] = debug
option_validation_error = False
if not token:
click.echo("Error: Token must be specified either by "
"--token or DD_TOKEN from the environment")
option_validation_error = True
ctx.fail("Error: Token must be specified either by "
"--token or DD_TOKEN from the environment")
if not url:
click.echo("Error: URL must be specified either by "
"--url or DD_URL from the environment")
option_validation_error = True
ctx.fail("Error: URL must be specified either by "
"--url or DD_URL from the environment")
if option_validation_error:
ctx.exit()
# setup logging for the CLI
# Setup root logger
logger = logging.getLogger('drydock_cli')
logger.setLevel(logging.DEBUG if debug else logging.INFO)
logging_handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(levelname)s - '
'%(filename)s:%(funcName)s - %(message)s')
logging_handler.setFormatter(formatter)
logger.addHandler(logging_handler)
logger.debug('logging for cli initialized')
# setup the drydock client using the passed parameters.
ctx.obj['CLIENT'] = DrydockClient(DrydockSession(host=url,
token=token))
drydock.add_command(design.design)

View File

@ -11,21 +11,42 @@
# 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.
""" cli.design.commands
Contains commands related to designs
"""
import logging
import click
@click.group()
def design():
click.echo('design invoked')
@click.pass_context
def design(ctx):
""" Drydock design commands
"""
pass
@design.command()
def create():
@design.command(name='create')
@click.pass_context
def design_create(ctx):
""" Create a design
"""
click.echo('create invoked')
@design.command()
def list():
click.echo('list invoked')
@design.command(name='list')
@click.pass_context
def design_list(ctx):
""" List designs
"""
click.echo(ctx.obj['CLIENT'].get_design_ids())
@design.command()
def show():
click.echo('design invoked.')
@click.option('--design-id',
'-id',
help='The deisgn id to show')
@design.command(name='show')
@click.pass_context
def design_show(ctx, design_id):
""" show designs
"""
if not design_id:
ctx.fail('The design id must be specified by --design-id')
click.echo('show invoked for {}'.format(design_id))

View File

@ -68,7 +68,7 @@ class DrydockClient(object):
"""
endpoint = '/designs'
resp = self.__send_get(endpiont)
resp = self.__send_get(endpoint)
if resp.status_code != 200:
raise errors.ClientError("Received a %d from GET URL: %s" % (resp.status_code, endpoint),

View File

@ -18,17 +18,20 @@ class DrydockSession(object):
A session to the Drydock API maintaining credentials and API options
:param string host: The Drydock server hostname or IP
:param int port: The service port, defaults to 9000
:param int port: (optional) The service port appended if specified
:param string token: Auth token
:param string marker: (optional) external context marker
"""
def __init__(self, host=None, port=9000, token=None, marker=None):
def __init__(self, host, *, port=None, token=None, marker=None):
self.__session = requests.Session()
self.__session.headers.update({'X-Auth-Token': token, 'X-Context-Marker': marker})
self.__session.headers.update({'X-Auth-Token': token, 'X-Context-Marker': marker})
self.host = host
self.port = port
self.base_url = "http://%s:%d/api/" % (host, port)
if port:
self.base_url = "http://%s:%d/api/" % (host, port)
else:
self.base_url = "http://%s/api/" % (host)
self.token = token
self.marker = marker

View File

@ -42,7 +42,8 @@ setup(name='drydock_provisioner',
'drydock_provisioner.drivers.node.maasdriver.models',
'drydock_provisioner.control',
'drydock_provisioner.cli',
'drydock_provisioner.cli.design'],
'drydock_provisioner.cli.design',
'drydock_provisioner.drydock_client'],
install_requires=[
'PyYAML',
'pyghmi>=1.0.18',