Browse Source

map UUIDs of new objects to old

Maintain a CSV file mapping the UUIDs of old resources to the new
values.

Signed-off-by: Doug Hellmann <doug@doughellmann.com>
Doug Hellmann 2 years ago
parent
commit
c4bbbc3ab9
2 changed files with 58 additions and 2 deletions
  1. 4
    1
      downpour/export.py
  2. 54
    1
      downpour/resolver.py

+ 4
- 1
downpour/export.py View File

@@ -32,9 +32,12 @@ def export_data(cloud, config, args):
32 32
         cloud,
33 33
         use_progress_bar=(args.progress and (args.verbose_level >= 1)),
34 34
     )
35
-    res = resolver.Resolver(cloud, downloader)
35
+    res = resolver.Resolver(cloud, downloader, output_path)
36 36
     tasks = []
37 37
 
38
+    # Any tasks needed to initialize the output.
39
+    tasks.extend(res.init_tasks())
40
+
38 41
     # Export independent resources. The resolver handles dependencies
39 42
     # automatically.
40 43
     to_export = resources.load(args.resource_file)

+ 54
- 1
downpour/resolver.py View File

@@ -12,13 +12,55 @@
12 12
 # License for the specific language governing permissions and limitations
13 13
 # under the License.
14 14
 
15
+import itertools
16
+import os.path
17
+
15 18
 
16 19
 class Resolver:
17 20
 
18
-    def __init__(self, cloud, downloader):
21
+    def __init__(self, cloud, downloader, output_path):
19 22
         self.cloud = cloud
20 23
         self._memo = set()
21 24
         self._downloader = downloader
25
+        self._output_path = output_path
26
+        self._uuid_map_file = 'uuids.csv'
27
+        self._var_counter = itertools.count(1)
28
+
29
+    def _mk_var_name(self, slug):
30
+        name = '{}{:03d}'.format(slug, next(self._var_counter))
31
+        self._last_var = name
32
+        return name
33
+
34
+    def _map_uuids(self, resource_type, name, old_id, var_expr):
35
+        pat = '"{}","{}","{}"'.format(
36
+            resource_type, name, old_id,
37
+        )
38
+        line = '{},"{}"'.format(
39
+            pat, '{{' + self._last_var + '.' + var_expr + '}}',
40
+        )
41
+        return {
42
+            'name': 'Map UUID for {}'.format(name),
43
+            'lineinfile': {
44
+                'dest': self._uuid_map_file,
45
+                'state': 'present',
46
+                'regexp': pat,
47
+                'insertafter': 'EOF',
48
+                'line': line,
49
+            },
50
+        }
51
+
52
+    def init_tasks(self):
53
+        yield {
54
+            'name': 'Initializing UUID mapping file',
55
+            'lineinfile': {
56
+                'create': 'yes',
57
+                'dest': self._uuid_map_file,
58
+                'state': 'present',
59
+                'regexp': '"Resource Type","Resource Name","Old","New"',
60
+                'insertbefore': 'BOF',
61
+                'line': '"Resource Type","Resource Name","Old","New"',
62
+            },
63
+        }
22 64
 
23 65
     def security_group(self, group):
24 66
         if ('security_group', group.id) in self._memo:
@@ -32,7 +74,12 @@ class Resolver:
32 74
                 'name': group.name,
33 75
                 'description': group.description,
34 76
             },
77
+            'register': self._mk_var_name('sg'),
35 78
         }
79
+        yield self._map_uuids(
80
+            'security group', group.name, group.id,
81
+            'secgroup.id',
82
+        )
36 83
         for rule in group.security_group_rules:
37 84
             rule_data = {
38 85
                 'direction': rule.direction,
@@ -70,7 +117,9 @@ class Resolver:
70 117
                 'size': volume.size,
71 118
                 'state': 'present',
72 119
             },
120
+            'register': self._mk_var_name('vol'),
73 121
         }
122
+        yield self._map_uuids('volume', volume.name, volume.id, 'volume.id')
74 123
 
75 124
     def server(self, server, save_state):
76 125
         for sg in server.security_groups:
@@ -102,7 +151,9 @@ class Resolver:
102 151
         yield {
103 152
             'name': 'Creating server {}'.format(server.name),
104 153
             'os_server': server_data,
154
+            'register': self._mk_var_name('server'),
105 155
         }
156
+        yield self._map_uuids('server', server.name, server.id, 'server.id')
106 157
 
107 158
     def image(self, image):
108 159
         filename = self._downloader.add_image(image)
@@ -119,4 +170,6 @@ class Resolver:
119 170
         yield {
120 171
             'name': 'Creating image {}'.format(image.name),
121 172
             'os_image': image_data,
173
+            'register': self._mk_var_name('img'),
122 174
         }
175
+        yield self._map_uuids('image', image.name, image.id, 'image.id')

Loading…
Cancel
Save