Consolidate Solum and Builder API

- Modify the CLI to make languagepack commands
talk to the solum-api

Related to the following API side change
https://review.openstack.org/#/c/157639/

Change-Id: I7df04bd3b89007e14e0a830a8c79f97d78106d6f
Closes-bug: 1423778
This commit is contained in:
Murali Allada
2015-02-18 21:09:51 -06:00
parent 6c286ed966
commit 9355f089f1
4 changed files with 86 additions and 87 deletions

View File

@@ -15,7 +15,6 @@
import os
from solumclient.builder import client as builder_client
from solumclient import client as solum_client
from solumclient.common import exc
@@ -48,8 +47,6 @@ class CommandsBase(object):
if 'os_auth_token' in client_args:
del client_args['os_auth_token']
self.bldclient = builder_client.get_client(parsed.solum_api_version,
**client_args)
self.client = solum_client.get_client(parsed.solum_api_version,
**client_args)

View File

@@ -51,6 +51,7 @@ from solumclient.common import yamlutils
from solumclient.openstack.common.apiclient import exceptions
from solumclient.openstack.common import cliutils
from solumclient.v1 import assembly as cli_assem
from solumclient.v1 import languagepack as cli_lp
from solumclient.v1 import pipeline as cli_pipe
from solumclient.v1 import plan as cli_plan
@@ -397,71 +398,24 @@ class LanguagePackCommands(cli_utils.CommandsBase):
Available commands:
solum languagepack create <NAME> <GIT_REPO> <METADATA>
Create a new language pack from a git repo.
solum languagepack list
Print and index of all available language packs.
solum languagepack show <LP>
Print the details of a language pack.
solum languagepack create <LPFILE>
Create a new language pack from a file.
solum languagepack build <NAME> <GIT_REPO> <METADATA>
Create a new language pack from a git repo.
solum languagepack delete <LP>
Destroy a language pack.
solum languagepack logs <UUID>
Show logs for a language pack.
"""
def create(self):
"""Create a language pack."""
self.parser.add_argument('lp_file',
help="Language pack file.")
self.parser._names['lp_file'] = 'languagepack file'
args, _ = self.parser.parse_known_args()
with open(args.lp_file) as lang_pack_file:
try:
data = json.load(lang_pack_file)
except ValueError as exc:
print("Error in language pack file: %s", str(exc))
sys.exit(1)
languagepack = self.client.languagepacks.create(**data)
fields = ['uuid', 'name', 'description', 'compiler_versions',
'os_platform']
data = dict([(f, getattr(languagepack, f, ''))
for f in fields])
cliutils.print_dict(data, wrap=72)
def delete(self):
"""Delete a language pack."""
self.parser.add_argument('lp_id',
help="Language pack id")
self.parser._names['lp_id'] = 'languagepack'
args, _ = self.parser.parse_known_args()
self.bldclient.images.delete(lp_id=args.lp_id)
def list(self):
"""List all language packs."""
fields = ['uuid', 'name', 'description', 'state', 'source_uri']
response = self.bldclient.images.list()
cliutils.print_list(response, fields)
def show(self):
"""Get a language pack."""
self.parser.add_argument('lp_id',
help="Language pack id")
self.parser._names['lp_id'] = 'languagepack'
args, _ = self.parser.parse_known_args()
response = self.bldclient.images.find(name_or_id=args.lp_id)
fields = ['uuid', 'name', 'description', 'state', 'source_uri']
data = dict([(f, getattr(response, f, ''))
for f in fields])
cliutils.print_dict(data, wrap=72)
def build(self):
"""Build a custom language pack."""
self.parser.add_argument('name',
help="Language pack name.")
self.parser.add_argument('git_url',
@@ -481,9 +435,9 @@ Available commands:
message = ("Malformed metadata file: %s" % str(excp))
raise exc.CommandError(message=message)
try:
response = self.bldclient.images.create(name=args.name,
source_uri=args.git_url,
lp_metadata=lp_metadata)
response = self.client.languagepacks.create(
name=args.name, source_uri=args.git_url,
lp_metadata=lp_metadata)
except exceptions.Conflict as conflict:
message = ("%s" % conflict.message)
raise exc.CommandError(message=message)
@@ -493,6 +447,60 @@ Available commands:
for f in fields])
cliutils.print_dict(data, wrap=72)
def delete(self):
"""Delete a language pack."""
self.parser.add_argument('lp_id',
help="Language pack id")
self.parser._names['lp_id'] = 'languagepack'
args, _ = self.parser.parse_known_args()
self.client.languagepacks.delete(lp_id=args.lp_id)
def list(self):
"""List all language packs."""
fields = ['uuid', 'name', 'description', 'state', 'source_uri']
response = self.client.languagepacks.list()
cliutils.print_list(response, fields)
def show(self):
"""Get a language pack."""
self.parser.add_argument('lp_id',
help="Language pack id")
self.parser._names['lp_id'] = 'languagepack'
args, _ = self.parser.parse_known_args()
response = self.client.languagepacks.find(name_or_id=args.lp_id)
fields = ['uuid', 'name', 'description', 'state', 'source_uri']
data = dict([(f, getattr(response, f, ''))
for f in fields])
cliutils.print_dict(data, wrap=72)
def logs(self):
"""Get Logs."""
self.parser.add_argument('lp_id',
help="languagepack uuid or name")
args, _ = self.parser.parse_known_args()
response = cli_lp.LanguagePackManager(self.client).logs(
lp_id=str(args.lp_id))
fields = ["resource_uuid"]
for log in response:
strategy_info = json.loads(log.strategy_info)
if log.strategy == 'local':
if 'local_storage' not in fields:
fields.append('local_storage')
log.local_storage = log.location
elif log.strategy == 'swift':
if 'swift_container' not in fields:
fields.append('swift_container')
if 'swift_path' not in fields:
fields.append('swift_path')
log.swift_container = strategy_info['container']
log.swift_path = log.location
else:
if 'location' not in fields:
fields.append('location')
cliutils.print_list(response, fields)
class AppCommands(cli_utils.CommandsBase):
"""Commands for working with applications.
@@ -629,7 +637,7 @@ Available commands:
if args.langpack is not None:
plan_definition['artifacts'][0]['language_pack'] = args.langpack
elif plan_definition['artifacts'][0].get('language_pack') is None:
langpacks = self.bldclient.images.list()
langpacks = self.client.languagepacks.list()
lpnames = [lp.name for lp in langpacks]
fields = ['uuid', 'name', 'description', 'state', 'source_uri']
cliutils.print_list(langpacks, fields)

View File

@@ -13,7 +13,6 @@
# under the License.
import collections
import json
import re
import sys
import uuid
@@ -24,7 +23,6 @@ import six
from stevedore import extension
from testtools import matchers
from solumclient.builder.v1 import image
from solumclient.common import yamlutils
from solumclient.openstack.common.apiclient import auth
from solumclient.openstack.common import cliutils
@@ -337,57 +335,37 @@ class TestSolum(base.TestCase):
expected_show_pub_keys_args)
# LanguagePack Tests #
@mock.patch.object(image.ImageManager, "list")
@mock.patch.object(languagepack.LanguagePackManager, "list")
def test_languagepack_list(self, mock_lp_list):
self.make_env()
self.shell("languagepack list")
mock_lp_list.assert_called_once()
@mock.patch.object(cliutils, "print_dict")
@mock.patch.object(languagepack.LanguagePackManager, "create")
def test_languagepack_create(self, mock_lp_create, mock_print_dict):
def test_languagepack_create(self, mock_lp_create):
FakeResource = collections.namedtuple("FakeResource",
"uuid name description "
"compiler_versions os_platform")
mock_lp_create.return_value = FakeResource(
'foo', 'foo', 'foo', 'foo', 'foo')
expected_printed_dict_args = mock_lp_create.return_value._asdict()
lp_data = json.loads(languagepack_file_data)
mopen = mock.mock_open(read_data=languagepack_file_data)
with mock.patch('%s.open' % solum.__name__, mopen, create=True):
self.make_env()
self.shell("languagepack create /dev/null")
mock_lp_create.assert_called_once_with(**lp_data)
mock_print_dict.assert_called_once_with(
expected_printed_dict_args,
wrap=72)
@mock.patch.object(image.ImageManager, "create")
def test_languagepack_build(self, mock_image_build):
FakeResource = collections.namedtuple("FakeResource",
"uuid name description "
"compiler_versions os_platform")
mock_image_build.return_value = FakeResource(
'foo', 'foo', 'foo', 'foo', 'foo')
lp_metadata = '{"OS": "Ubuntu"}'
mopen = mock.mock_open(read_data=lp_metadata)
with mock.patch('%s.open' % solum.__name__, mopen, create=True):
self.make_env()
self.shell("languagepack build lp_name github.com/test "
self.shell("languagepack create lp_name github.com/test "
"--lp_metadata=/dev/null")
mock_image_build.assert_called_once_with(
mock_lp_create.assert_called_once_with(
name='lp_name',
source_uri='github.com/test',
lp_metadata=lp_metadata)
@mock.patch.object(image.ImageManager, "delete")
@mock.patch.object(languagepack.LanguagePackManager, "delete")
def test_languagepack_delete(self, mock_lp_delete):
self.make_env()
self.shell("languagepack delete fake-lp-id")
mock_lp_delete.assert_called_once_with(lp_id='fake-lp-id')
@mock.patch.object(image.ImageManager, "find")
@mock.patch.object(languagepack.LanguagePackManager, "find")
def test_languagepack_get(self, mock_lp_get):
self.make_env()
self.shell("languagepack show fake-lp-id1")

View File

@@ -21,6 +21,11 @@ class LanguagePack(apiclient_base.Resource):
return "<LanguagePack %s>" % self._info
class UserLog(apiclient_base.Resource):
def __repr__(self):
return "<Log %s>" % self._info
class LanguagePackManager(solum_base.CrudManager):
resource_class = LanguagePack
collection_key = 'language_packs'
@@ -40,3 +45,14 @@ class LanguagePackManager(solum_base.CrudManager):
def delete(self, **kwargs):
return super(LanguagePackManager,
self).delete(base_url="/v1", **kwargs)
def find(self, **kwargs):
name_or_uuid = kwargs['name_or_id']
return super(LanguagePackManager, self).get(base_url="/v1",
lp_id=name_or_uuid)
def logs(self, **kwargs):
self.resource_class = UserLog
url = self.build_url(base_url="/v1", **kwargs)
url += '/logs/'
return self._list(url)