diff --git a/cloudkittyclient/v1/rating/pyscripts/__init__.py b/cloudkittyclient/v1/rating/pyscripts/__init__.py new file mode 100644 index 0000000..21f3efa --- /dev/null +++ b/cloudkittyclient/v1/rating/pyscripts/__init__.py @@ -0,0 +1,30 @@ +# Copyright 2015 Objectif Libre +# All 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. + +from cloudkittyclient.common import base + + +class Script(base.Resource): + key = 'script' + + def __repr__(self): + return "" % self._info + + +class ScriptManager(base.CrudManager): + resource_class = Script + base_url = '/v1/rating/module_config/pyscripts' + key = 'script' + collection_key = 'scripts' diff --git a/cloudkittyclient/v1/rating/pyscripts/client.py b/cloudkittyclient/v1/rating/pyscripts/client.py new file mode 100644 index 0000000..b6a61b7 --- /dev/null +++ b/cloudkittyclient/v1/rating/pyscripts/client.py @@ -0,0 +1,28 @@ +# Copyright 2015 Objectif Libre +# All 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. + +from cloudkittyclient.v1.rating import pyscripts + + +class Client(object): + """Client for the PyScripts v1 API. + + :param http_client: A http client. + """ + + def __init__(self, http_client): + """Initialize a new client for the PyScripts v1 API.""" + self.http_client = http_client + self.scripts = pyscripts.ScriptManager(self.http_client) diff --git a/cloudkittyclient/v1/rating/pyscripts/extension.py b/cloudkittyclient/v1/rating/pyscripts/extension.py new file mode 100644 index 0000000..3025c1c --- /dev/null +++ b/cloudkittyclient/v1/rating/pyscripts/extension.py @@ -0,0 +1,31 @@ +# Copyright 2015 Objectif Libre +# All 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. + +from cloudkittyclient.v1.rating.pyscripts import client +from cloudkittyclient.v1.rating.pyscripts import shell + + +class Extension(object): + """PyScripts extension. + + """ + + @staticmethod + def get_client(http_client): + return client.Client(http_client) + + @staticmethod + def get_shell(): + return shell diff --git a/cloudkittyclient/v1/rating/pyscripts/shell.py b/cloudkittyclient/v1/rating/pyscripts/shell.py new file mode 100644 index 0000000..c322f88 --- /dev/null +++ b/cloudkittyclient/v1/rating/pyscripts/shell.py @@ -0,0 +1,116 @@ +# Copyright 2015 Objectif Libre +# All 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. +import functools + +from oslo_utils import strutils +import six + +from cloudkittyclient.common import utils +from cloudkittyclient import exc + +_bool_strict = functools.partial(strutils.bool_from_string, strict=True) + + +@utils.arg('-n', '--name', + help='Script name', + required=True) +@utils.arg('-f', '--file', + help='Script file', + required=False) +def do_pyscripts_script_create(cc, args={}): + """Create a script.""" + script_args = {'name': args.name} + if args.file: + with open(args.file) as fp: + script_args['data'] = fp.read() + out = cc.pyscripts.scripts.create(**script_args) + utils.print_dict(out.to_dict()) + + +@utils.arg('-d', '--show-data', + help='Show data in the listing', + required=False, + default=False) +def do_pyscripts_script_list(cc, args={}): + """List scripts.""" + request_args = {} + if not args.show_data: + request_args['no_data'] = True + scripts = cc.pyscripts.scripts.list(**request_args) + field_labels = ['Name', 'Script id', 'Data', 'Checksum'] + fields = ['name', 'script_id', 'data', 'checksum'] + utils.print_list(scripts, + fields, + field_labels, + sortby=0) + + +@utils.arg('-s', '--script-id', + help='Script uuid', + required=True) +def do_pyscripts_script_get(cc, args={}): + """Get script.""" + try: + script = cc.pyscripts.scripts.get(script_id=args.script_id) + except exc.HTTPNotFound: + raise exc.CommandError('Script not found: %s' % args.script_id) + utils.print_dict(script.to_dict()) + + +@utils.arg('-s', '--script-id', + help='Script uuid', + required=True) +def do_pyscripts_script_get_data(cc, args={}): + """Get script data.""" + try: + script = cc.pyscripts.scripts.get(script_id=args.script_id) + except exc.HTTPNotFound: + raise exc.CommandError('Script not found: %s' % args.script_id) + six.print_(script.data) + + +@utils.arg('-s', '--script-id', + help='Script uuid', + required=True) +def do_pyscripts_script_delete(cc, args={}): + """Delete a script.""" + try: + cc.pyscripts.scripts.delete(script_id=args.script_id) + except exc.HTTPNotFound: + raise exc.CommandError('Script not found: %s' % args.counter_name) + + +@utils.arg('-s', '--script-id', + help='Script uuid', + required=True) +@utils.arg('-f', '--file', + help='Script file', + required=True) +def do_pyscripts_script_update(cc, args={}): + """Update a mapping.""" + excluded_fields = [ + 'checksum', + ] + with open(args.file) as fp: + content = fp.read() + try: + script = cc.pyscripts.scripts.get(script_id=args.script_id) + except exc.HTTPNotFound: + raise exc.CommandError('Script not found: %s' % args.script_id) + script_dict = script.to_dict() + for field in excluded_fields: + del script_dict[field] + script_dict['data'] = content + cc.pyscripts.scripts.update(**script_dict) diff --git a/setup.cfg b/setup.cfg index 6d8b9c2..441ed42 100644 --- a/setup.cfg +++ b/setup.cfg @@ -30,6 +30,7 @@ console_scripts = cloudkitty.client.modules = hashmap = cloudkittyclient.v1.rating.hashmap.extension:Extension + pyscripts = cloudkittyclient.v1.rating.pyscripts.extension:Extension [build_sphinx] source-dir = doc/source