Commit 0642e884 authored by Jérome Perrin's avatar Jérome Perrin

cli/info: better output of connection parameters

before:

    $ slapos service info slapos-sr-testing
    Software Release URL: /srv/slapgrid/slappart15/srv/project/slapos/software/slapos-sr-testing/software.cfg
    Instance state: busy
    Instance parameters:
    {}
    Connection parameters:
    ("<?xml version='1.0' encoding='utf-8'?>\n"
     '<instance>\n'
     '  <parameter '
     'id="environment-script">/srv/slapgrid/slappart15/srv/runner/instance/slappart7/etc/slapos-local-development-environment.sh</parameter>\n'

after:

    $ slapos service info slapos-sr-testing
    Software Release URL: /srv/slapgrid/slappart15/srv/project/slapos/software/slapos-sr-testing/software.cfg
    Instance state: busy
    Instance parameters:
    {}
    Connection parameters:
    {'environment-script': '/srv/slapgrid/slappart15/srv/runner/instance/slappart7/etc/slapos-local-development-environment.sh'}
parent 974c16d3
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
# #
############################################################################## ##############################################################################
import json
import pprint import pprint
import sys import sys
...@@ -35,6 +36,14 @@ from slapos.cli.config import ClientConfigCommand ...@@ -35,6 +36,14 @@ from slapos.cli.config import ClientConfigCommand
from slapos.client import init, ClientConfig from slapos.client import init, ClientConfig
from slapos.slap import ResourceNotReady, NotFoundError from slapos.slap import ResourceNotReady, NotFoundError
from slapos.util import (
SoftwareReleaseSchema,
SoftwareReleaseSerialisation,
StrPrettyPrinter,
UndefinedSerializationError,
xml2dict,
)
class InfoCommand(ClientConfigCommand): class InfoCommand(ClientConfigCommand):
"""get status, software_release and parameters of an instance""" """get status, software_release and parameters of an instance"""
...@@ -70,10 +79,21 @@ def do_info(logger, conf, local): ...@@ -70,10 +79,21 @@ def do_info(logger, conf, local):
logger.warning('Instance %s does not exist.', conf.reference) logger.warning('Instance %s does not exist.', conf.reference)
return(2) return(2)
software_schema = SoftwareReleaseSchema(
instance._software_release_url,
getattr(instance, '_software_type', None))
connection_parameter_dict = xml2dict(instance._connection_dict)
try:
software_serialisation = software_schema.getSerialisation()
except UndefinedSerializationError:
software_serialisation = SoftwareReleaseSerialisation.JsonInXml
if software_serialisation == SoftwareReleaseSerialisation.JsonInXml:
if '_' in connection_parameter_dict:
connection_parameter_dict = json.loads(connection_parameter_dict['_'])
logger.info('Software Release URL: %s', instance._software_release_url) logger.info('Software Release URL: %s', instance._software_release_url)
logger.info('Instance state: %s', instance._requested_state) logger.info('Instance state: %s', instance._requested_state)
logger.info('Instance parameters:') logger.info('Instance parameters:')
logger.info(pprint.pformat(instance._parameter_dict)) logger.info(StrPrettyPrinter().pformat(instance._parameter_dict))
logger.info('Connection parameters:') logger.info('Connection parameters:')
logger.info(pprint.pformat(instance._connection_dict)) logger.info(StrPrettyPrinter().pformat(connection_parameter_dict))
...@@ -41,7 +41,7 @@ from slapos.client import (ClientConfig, _getSoftwareReleaseFromSoftwareString, ...@@ -41,7 +41,7 @@ from slapos.client import (ClientConfig, _getSoftwareReleaseFromSoftwareString,
init) init)
from slapos.slap import ResourceNotReady from slapos.slap import ResourceNotReady
from slapos.util import (SoftwareReleaseSchema, SoftwareReleaseSerialisation, from slapos.util import (SoftwareReleaseSchema, SoftwareReleaseSerialisation,
UndefinedSerializationError) UndefinedSerializationError, StrPrettyPrinter)
try: try:
from typing import IO, Dict from typing import IO, Dict
...@@ -138,16 +138,6 @@ class RequestCommand(ClientConfigCommand): ...@@ -138,16 +138,6 @@ class RequestCommand(ClientConfigCommand):
do_request(self.app.log, conf, local) do_request(self.app.log, conf, local)
# BBB on python3 we can use pprint.pformat
class StrPrettyPrinter(pprint.PrettyPrinter):
"""A PrettyPrinter which produces consistent output on python 2 and 3
"""
def format(self, object, context, maxlevels, level):
if six.PY2 and isinstance(object, six.text_type):
object = object.encode('utf-8')
return pprint.PrettyPrinter.format(self, object, context, maxlevels, level)
def do_request(logger, conf, local): def do_request(logger, conf, local):
logger.info('Requesting %s as instance of %s...', logger.info('Requesting %s as instance of %s...',
conf.reference, conf.software_url) conf.reference, conf.software_url)
......
...@@ -615,25 +615,41 @@ class TestCliList(CliMixin): ...@@ -615,25 +615,41 @@ class TestCliList(CliMixin):
@patch.object(slapos.slap.slap, 'registerOpenOrder', return_value=slapos.slap.OpenOrder()) @patch.object(slapos.slap.slap, 'registerOpenOrder', return_value=slapos.slap.OpenOrder())
class TestCliInfo(CliMixin): class TestCliInfo(CliMixin):
def test_info(self, _):
def test_info_xml_serialisation(self, _):
self._test_info_output(
slapos.slap.SoftwareInstance(
_software_release_url='SR1',
_requested_state='mystate',
_connection_dict=
'<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<instance>\n <parameter id="myconnectionparameter">value1</parameter>\n</instance>\n',
_parameter_dict={'myinstanceparameter': 'value2'}))
def test_info_json_in_serialisation(self, _):
self._test_info_output(
slapos.slap.SoftwareInstance(
_software_release_url='SR1',
_requested_state='mystate',
_connection_dict=
'<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<instance>\n <parameter id="_">{"myconnectionparameter": "value1"}</parameter>\n</instance>\n',
_parameter_dict={'myinstanceparameter': 'value2'}))
def _test_info_output(self, instance):
""" """
Test "slapos service info" command output. Test "slapos service info" command output.
""" """
setattr(self.conf, 'reference', 'instance1') setattr(self.conf, 'reference', 'instance1')
instance = slapos.slap.SoftwareInstance( with patch.object(slapos.slap.OpenOrder, 'getInformation',
_software_release_url='SR1', return_value=instance):
_requested_state = 'mystate',
_connection_dict = {'myconnectionparameter': 'value1'},
_parameter_dict = {'myinstanceparameter': 'value2'}
)
with patch.object(slapos.slap.OpenOrder, 'getInformation', return_value=instance):
slapos.cli.info.do_info(self.logger, self.conf, self.local) slapos.cli.info.do_info(self.logger, self.conf, self.local)
self.logger.info.assert_any_call(pprint.pformat(instance._parameter_dict)) self.logger.info.assert_any_call(pprint.pformat(instance._parameter_dict))
self.logger.info.assert_any_call('Software Release URL: %s', instance._software_release_url) self.logger.info.assert_any_call(
self.logger.info.assert_any_call('Instance state: %s', instance._requested_state) 'Software Release URL: %s', instance._software_release_url)
self.logger.info.assert_any_call(pprint.pformat(instance._parameter_dict)) self.logger.info.assert_any_call(
self.logger.info.assert_any_call(pprint.pformat(instance._connection_dict)) 'Instance state: %s', instance._requested_state)
self.logger.info.assert_any_call("{'myinstanceparameter': 'value2'}", )
self.logger.info.assert_any_call("{'myconnectionparameter': 'value1'}")
def test_unknownReference(self, _): def test_unknownReference(self, _):
""" """
......
...@@ -1305,7 +1305,7 @@ class TestCliInformation(CliMasterMixin): ...@@ -1305,7 +1305,7 @@ class TestCliInformation(CliMasterMixin):
'Instance parameters:', 'Instance parameters:',
'{}', '{}',
'Connection parameters:', 'Connection parameters:',
'None' '{}'
], ],
) )
output1 = self.cliDoSlapos(('service', 'info', 'MyInstance1'), stderr=subprocess.DEVNULL) output1 = self.cliDoSlapos(('service', 'info', 'MyInstance1'), stderr=subprocess.DEVNULL)
...@@ -1317,7 +1317,7 @@ class TestCliInformation(CliMasterMixin): ...@@ -1317,7 +1317,7 @@ class TestCliInformation(CliMasterMixin):
'Instance parameters:', 'Instance parameters:',
"{'couscous': 'hello'}", "{'couscous': 'hello'}",
'Connection parameters:', 'Connection parameters:',
'None' '{}'
], ],
) )
try: try:
......
...@@ -32,6 +32,7 @@ import errno ...@@ -32,6 +32,7 @@ import errno
import hashlib import hashlib
import json import json
import os import os
import pprint
import shutil import shutil
import socket import socket
import sqlite3 import sqlite3
...@@ -474,3 +475,13 @@ class SoftwareReleaseSchema(object): ...@@ -474,3 +475,13 @@ class SoftwareReleaseSchema(object):
instance=instance, instance=instance,
schema=self.getInstanceRequestParameterSchema(), schema=self.getInstanceRequestParameterSchema(),
) )
# BBB on python3 we can use pprint.pformat
class StrPrettyPrinter(pprint.PrettyPrinter):
"""A PrettyPrinter which produces consistent output on python 2 and 3
"""
def format(self, object, context, maxlevels, level):
if six.PY2 and isinstance(object, six.text_type):
object = object.encode('utf-8')
return pprint.PrettyPrinter.format(self, object, context, maxlevels, level)
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