Save state of cloud before running tempest

This patch provides an option to the user to save the state of
cloud before running tempest.

Change-Id: I53d62cc7863bce3adf5201c6db162a4b5989e3a3
Closes-bug: 1671426
diff --git a/releasenotes/notes/add-save-state-option-5ea67858cbaca969.yaml b/releasenotes/notes/add-save-state-option-5ea67858cbaca969.yaml
new file mode 100644
index 0000000..8fdf4f0
--- /dev/null
+++ b/releasenotes/notes/add-save-state-option-5ea67858cbaca969.yaml
@@ -0,0 +1,4 @@
+---
+features:
+  - |
+    Add --save-state option to allow saving state of cloud before tempest run.
diff --git a/tempest/cmd/run.py b/tempest/cmd/run.py
index b36bf5c..350dd0b 100644
--- a/tempest/cmd/run.py
+++ b/tempest/cmd/run.py
@@ -97,15 +97,19 @@
 from cliff import command
 from os_testr import regex_builder
 from os_testr import subunit_trace
+from oslo_serialization import jsonutils as json
 import six
 from testrepository.commands import run_argv
 
+from tempest.cmd import cleanup_service
 from tempest.cmd import init
 from tempest.cmd import workspace
+from tempest.common import credentials_factory as credentials
 from tempest import config
 
 
 CONF = config.CONF
+SAVED_STATE_JSON = "saved_state.json"
 
 
 class TempestRun(command.Command):
@@ -174,6 +178,11 @@
         else:
             print("No .testr.conf file was found for local execution")
             sys.exit(2)
+        if parsed_args.state:
+            self._init_state()
+        else:
+            pass
+
         if parsed_args.combine:
             temp_stream = tempfile.NamedTemporaryFile()
             return_code = run_argv(['tempest', 'last', '--subunit'], sys.stdin,
@@ -203,6 +212,25 @@
     def get_description(self):
         return 'Run tempest'
 
+    def _init_state(self):
+        print("Initializing saved state.")
+        data = {}
+        self.global_services = cleanup_service.get_global_cleanup_services()
+        self.admin_mgr = credentials.AdminManager()
+        admin_mgr = self.admin_mgr
+        kwargs = {'data': data,
+                  'is_dry_run': False,
+                  'saved_state_json': data,
+                  'is_preserve': False,
+                  'is_save_state': True}
+        for service in self.global_services:
+            svc = service(admin_mgr, **kwargs)
+            svc.run()
+
+        with open(SAVED_STATE_JSON, 'w+') as f:
+            f.write(json.dumps(data,
+                    sort_keys=True, indent=2, separators=(',', ': ')))
+
     def get_parser(self, prog_name):
         parser = super(TempestRun, self).get_parser(prog_name)
         parser = self._add_args(parser)
@@ -253,6 +281,10 @@
         parallel.add_argument('--serial', '-t', dest='parallel',
                               action='store_false',
                               help='Run tests serially')
+        parser.add_argument('--save-state', dest='state',
+                            action='store_true',
+                            help="To save the state of the cloud before "
+                                 "running tempest.")
         # output args
         parser.add_argument("--subunit", action='store_true',
                             help='Enable subunit v2 output')