Pior Bastida avatar Pior Bastida committed ef4e1f9

add template description when displaying stack (and refactor helper)

Comments (0)

Files changed (3)

awstools/display.py

 import boto
 
 
-def format_stack_summary(stack, oneline=False):
-    if oneline:
-        return "{s.stack_name:<30} {s.stack_status:<30} {s.creation_time}".format(s=stack)
+def format_stack_summary(stack):
+    tmpl  = "Name: {s.stack_name}\n"
+    tmpl += "Id: {s.stack_id}\n"
+    tmpl += "Status: {s.stack_status}\n"
+    tmpl += "Creation : {s.creation_time}\n"
+
+    if hasattr(stack, 'description'):
+        tmpl += "Template: {s.description}"
+    elif hasattr(stack, 'template_description'):
+        tmpl += "Template: {s.template_description}"
     else:
-        return "\n".join(["Name: {}".format(stack.stack_name),
-                          "Id: {}".format(stack.stack_id),
-                          "Status: {}".format(stack.stack_status),
-                          "Creation Time: {}".format(stack.creation_time)])
+        raise ValueError("Invalid Stack object")
+    return tmpl.format(s=stack)
+
+
+def format_stack_summary_short(stack):
+    tmpl = "{s.stack_name:<26} {s.stack_status:<18} {s.creation_time}"
+
+    if hasattr(stack, 'description'):
+        tmpl += " - {s.description}"
+    elif hasattr(stack, 'template_description'):
+        tmpl += " - {s.template_description}"
+    else:
+        raise ValueError("Invalid Stack object")
+    return tmpl.format(s=stack)
 
 
 def format_stack_events(stack, limit=None):

awstools/tests/unittests/test_display.py

 import datetime
 
 import mock
+import boto.cloudformation.stack
 
 from awstools import display
 
 class TestDisplay(unittest.TestCase):
 
     def test_format_stack_summary(self):
-        stack = mock.MagicMock(
-            stack_name="stack_name",
-            stack_id="stack_id",
-            stack_status="stack_status",
-            creation_time="creation_time")
+        stack = boto.cloudformation.stack.StackSummary()
+        stack.stack_name = "stack_name"
+        stack.stack_status = "stack_status"
+        stack.creation_time = "creation_time"
+        stack.template_description = "template_description"
 
-        fmt = display.format_stack_summary(
-            stack
-            )
+        stacksummary = boto.cloudformation.stack.Stack()
+        stacksummary.stack_name = "stack_name"
+        stacksummary.stack_status = "stack_status"
+        stacksummary.creation_time = "creation_time"
+        stacksummary.description = "template_description"
 
-        self.assertIn('stack_name', fmt)
-        self.assertIn('stack_id', fmt)
-        self.assertIn('stack_status', fmt)
-        self.assertIn('creation_time', fmt)
-
-    def test_format_stack_summary_oneline(self):
-        stack = mock.MagicMock(
-            stack_name="stack_name",
-            stack_status="stack_status",
-            creation_time="creation_time")
-
-        fmt = display.format_stack_summary(
-            stack,
-            oneline=True,
-            )
-
+        fmt = display.format_stack_summary(stack)
         self.assertIn('stack_name', fmt)
         self.assertIn('stack_status', fmt)
         self.assertIn('creation_time', fmt)
+        self.assertIn('template_description', fmt)
+
+        fmt = display.format_stack_summary_short(stack)
+        self.assertIn('stack_name', fmt)
+        self.assertIn('stack_status', fmt)
+        self.assertIn('creation_time', fmt)
+        self.assertIn('template_description', fmt)
+
+        fmt = display.format_stack_summary(stacksummary)
+        self.assertIn('stack_name', fmt)
+        self.assertIn('stack_status', fmt)
+        self.assertIn('creation_time', fmt)
+        self.assertIn('template_description', fmt)
+
+        fmt = display.format_stack_summary_short(stacksummary)
+        self.assertIn('stack_name', fmt)
+        self.assertIn('stack_status', fmt)
+        self.assertIn('creation_time', fmt)
+        self.assertIn('template_description', fmt)
+
 
     @mock.patch('boto.connect_cloudformation')
     def test_format_stack_events(self, mock_cfn):
 import awstools
 from awstools import find_stacks, find_one_stack
 from awstools.display import (format_stack_summary,
+                              format_stack_summary_short,
                               format_stack_events)
 from awstools.application import Applications
 from awstools import cfntemplate
 def ls(args):
     stacks = find_stacks(args.stack_name, findall=args.all)
     for stack in stacks:
-        yield format_stack_summary(stack, oneline=True)
+        yield format_stack_summary_short(stack)
 
 
 @arg('stack_name', help=HELP_SN)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.