Browse Source

refactory query to use resource editor class

Change-Id: I1cb37cedde5a79c3683cbe5566d2080f73f49836
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
Doug Hellmann 2 years ago
parent
commit
c4e3919e9b
1 changed files with 26 additions and 11 deletions
  1. 26
    11
      downpour/query.py

+ 26
- 11
downpour/query.py View File

@@ -19,6 +19,27 @@ from downpour import resources
19 19
 LOG = logging.getLogger(__name__)
20 20
 
21 21
 
22
+class ResourceFileEditor:
23
+
24
+    def __init__(self, filename, save_state):
25
+        self._filename = filename
26
+        self._save_state = save_state
27
+        self._resources = resources.load(filename, missing_ok=True)
28
+        self._servers = set(s.name for s in self._resources.servers)
29
+
30
+    def add_server(self, info):
31
+        if info.name in self._servers:
32
+            return
33
+        LOG.info('found server %s to export', info.name)
34
+        self._resources.servers.append({
35
+            'name': info.name,
36
+            'save_state': self._save_state,
37
+        })
38
+
39
+    def save(self):
40
+        resources.save(self._filename, self._resources)
41
+
42
+
22 43
 def register_command(subparsers):
23 44
     do_query = subparsers.add_parser(
24 45
         'query',
@@ -43,17 +64,11 @@ def register_command(subparsers):
43 64
 
44 65
 
45 66
 def query_data(cloud, config, args):
46
-    to_export = resources.load(args.resource_file, missing_ok=True)
47
-    servers = set(s.name for s in to_export.servers)
67
+    editor = ResourceFileEditor(args.resource_file, save_state=args.save_state)
48 68
 
49 69
     for pattern in args.server:
50
-        LOG.info('searching for server %r', pattern)
70
+        LOG.info('searching for servers matching pattern %r', pattern)
51 71
         for server_info in cloud.search_servers(name_or_id=pattern):
52
-            if server_info.name not in servers:
53
-                LOG.info('found server %s to export', server_info.name)
54
-                to_export.servers.append({
55
-                    'name': server_info.name,
56
-                    'save_state': args.save_state,
57
-                })
58
-
59
-    resources.save(args.resource_file, to_export)
72
+            editor.add_server(server_info)
73
+
74
+    editor.save()

Loading…
Cancel
Save