From d10c6313000133c48bca4b289b6cd258ceacca98 Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen@paragon-es.de>
Date: Sun, 26 Jul 2015 18:14:17 +0200
Subject: [PATCH] Optionally read API token from variables, new option to
 output settings in YAML file

---
 README.md                       |  1 +
 action_plugins/serverdensity.py | 32 +++++++++++++++++++++-----------
 2 files changed, 22 insertions(+), 11 deletions(-)

diff --git a/README.md b/README.md
index bd0b041..7785583 100644
--- a/README.md
+++ b/README.md
@@ -23,6 +23,7 @@ There are plugin parameters to define how the plugin will behave:
 * **cache** (optional, defaults to None): Fully qualified filename for a cache of der Server Density data
 * **cleanup** (optional, defaults to False): Decides if undefined alerts in your Ansible inventory available at Server Density should be deleted
 * **readonly** (optional, defaults to False): If set to True it will only read the current settings from SD and stores them in a temporary file and does nothing else. This is useful when you want to find out some variable names for alerts or similar things
+* **output** (optional, defaults to False): If provided, all current settings that are currently set upstream at Server Density will be written in YAML to this file
 
 ##Installation##
 
diff --git a/action_plugins/serverdensity.py b/action_plugins/serverdensity.py
index 622d615..9961292 100644
--- a/action_plugins/serverdensity.py
+++ b/action_plugins/serverdensity.py
@@ -38,34 +38,44 @@ class ActionModule(object):
         if complex_args:
             args.update(complex_args)
         args.update(parse_kv(module_args))
-        if 'api_token' not in args:
-            raise ae("'api_token' is a required argument.")
-
-        self.api_token = args.get('api_token')
+        if 'api_token' in args:
+            self.api_token = args.get('api_token')
+        else:
+            allgroup = self.runner.inventory.get_group('all')
+            allvariables = allgroup.get_variables()
+            if 'sd_api_token' in allvariables:
+                self.api_token = allvariables.get('sd_api_token')
+            else:
+                raise ae("'api_token' is a required argument.")
 
         self.force_update = args.get('force', False)
         self.cache_file_name = args.get('cache', False)
         cleanup = args.get('cleanup', False)
         just_download = args.get('readonly', False)
+        output = args.get('output', False)
 
         if just_download:
             self.force_update = False
             self.cache_file_name = tempfile.mktemp(prefix='sd_', suffix='.json')
             cleanup = False
 
+        if output and self.cache_file_name != 'False':
+            self.cache_file_name = tempfile.mktemp(prefix='sd_', suffix='.json')
+
         result = {}
 
         self.list_all()
 
+        if output:
+            if os.path.exists(self.cache_file_name):
+                vv('Writing upstream settings to %s' % os.path.abspath(output))
+                with open(self.cache_file_name, 'r') as content_file:
+                    content = json.load(content_file)
+                    with open(output, 'w') as output_file:
+                        yaml.safe_dump(content, output_file, encoding='utf-8', allow_unicode=True)
+
         if just_download:
             vv('Downloaded settings to %s' % self.cache_file_name)
-            output = args.get('output', False)
-            if output:
-                if os.path.exists(self.cache_file_name):
-                    with open(self.cache_file_name, 'r') as content_file:
-                        content = json.load(content_file)
-                        with open(output, 'w') as output_file:
-                            yaml.safe_dump(content, output_file, encoding='utf-8', allow_unicode=True)
             return ReturnData(conn=conn, comm_ok=True, result=result)
 
         services = {}
-- 
GitLab