Skip to content

Commit

Permalink
Fix reporters
Browse files Browse the repository at this point in the history
  • Loading branch information
ezh committed Feb 18, 2020
1 parent d05f90f commit 87b7bfc
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 24 deletions.
8 changes: 2 additions & 6 deletions cloudselect/report/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
"""Module that represents list of selected instances as JSON output."""
import json

from cloudselect import Container

from . import ReportService


Expand All @@ -18,9 +16,7 @@ class Json(ReportService): # pylint: disable=too-few-public-methods

def run(self, selected):
"""Represent instances as JSON output."""
# get first instance
# assume that all instances match the same group/pattern
instance = next(iter(selected), None)
options = Container.options("option", instance.metadata)
options = self.get_option(selected)
report = {"instances": [i.to_dict() for i in selected], "option": options}
print(json.dumps(report, sort_keys=True))
return report
8 changes: 2 additions & 6 deletions cloudselect/report/json_pp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
"""Module that represents list of selected instances as formatted JSON output."""
import json

from cloudselect import Container

from . import ReportService


Expand All @@ -18,9 +16,7 @@ class JsonPP(ReportService): # pylint: disable=too-few-public-methods

def run(self, selected):
"""Represent instances as formatted JSON output."""
# get first instance
# assume that all instances match the same group/pattern
instance = next(iter(selected), None)
options = Container.options("option", instance.metadata)
options = self.get_option(selected)
report = {"instances": [i.to_dict() for i in selected], "option": options}
print(json.dumps(report, indent=2, sort_keys=True))
return report
14 changes: 11 additions & 3 deletions cloudselect/report/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,20 @@ class ReportService: # pylint: disable=too-few-public-methods
def run(selected):
"""Prepare report based on list of selected instances."""
pp = pprint.PrettyPrinter()
options = ReportService.get_option(selected)
report = {"instances": [i.to_dict() for i in selected], "option": options}
pp.pprint(report)
return report

@staticmethod
def get_option(selected):
"""Get option block for report."""
# get first instance
# assume that all instances match the same group/pattern
instance = next(iter(selected), None)
options = Container.options("option", instance.metadata)
report = {"instances": [i.to_dict() for i in selected], "option": options}
pp.pprint(report)
if instance:
return Container.options("option", instance.metadata)
return Container.options("option")


class ReportServiceProvider(providers.Singleton):
Expand Down
8 changes: 2 additions & 6 deletions cloudselect/report/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
"""Module that represents list of selected instances as JSON output."""
from yaml import safe_dump

from cloudselect import Container

from . import ReportService


Expand All @@ -18,9 +16,7 @@ class Yaml(ReportService): # pylint: disable=too-few-public-methods

def run(self, selected):
"""Represent instances as YAML output."""
# get first instance
# assume that all instances match the same group/pattern
instance = next(iter(selected), None)
options = Container.options("option", instance.metadata)
options = self.get_option(selected)
report = {"instances": [i.to_dict() for i in selected], "option": options}
print(safe_dump(report, indent=2))
return report
2 changes: 1 addition & 1 deletion test/report/test_stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_stub_report(cfgdir):
args = cloud.parse_args([])
cloud.fabric(profile, args)
assert Container.report().__class__.__name__ == "Stub"
assert Container.report().run([]) == []
assert Container.report().run([]) == {"instances": [], "option": {}}
assert Container.report() == Container.report()


Expand Down
4 changes: 2 additions & 2 deletions test/test_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,5 @@ def test_select_single(cfgdir):
selector = cloud.fabric(configuration, args)
assert isinstance(Container.discovery(), Local)
report = selector.select()
assert len(report) == 1
assert report[0].host == "my.cloud.instance"
assert len(report["instances"]) == 1
assert report["instances"][0]["host"] == "my.cloud.instance"

0 comments on commit 87b7bfc

Please sign in to comment.