18
18
import copy
19
19
import subprocess
20
20
import sys
21
- import shutil
22
21
import re
23
22
24
23
MIGRATIONS = [
43
42
},
44
43
]
45
44
45
+
46
46
class ModuleMigration :
47
47
"""
48
48
Migrate the resources from a flat project factory to match the new
@@ -89,6 +89,7 @@ def targets(self):
89
89
90
90
return to_move
91
91
92
+
92
93
class TerraformModule :
93
94
"""
94
95
A Terraform module with associated resources.
@@ -171,7 +172,7 @@ def __init__(self, module, resource_type, name):
171
172
self .module = module
172
173
self .resource_type = resource_type
173
174
174
- find_suffix = re .match ('(^.+)\[(\d+)\]' , name )
175
+ find_suffix = re .match (r '(^.+)\[(\d+)\]' , name )
175
176
if find_suffix :
176
177
self .name = find_suffix .group (1 )
177
178
self .index = find_suffix .group (2 )
@@ -187,7 +188,7 @@ def path(self):
187
188
if parts [0 ] == '' :
188
189
del parts [0 ]
189
190
path = "." .join (parts )
190
- if self .index is not - 1 and self .plural :
191
+ if self .index != - 1 and self .plural :
191
192
path = "{0}[{1}]" .format (path , self .index )
192
193
return path
193
194
@@ -198,6 +199,7 @@ def __repr__(self):
198
199
self .resource_type ,
199
200
self .name )
200
201
202
+
201
203
def group_by_module (resources ):
202
204
"""
203
205
Group a set of resources according to their containing module.
@@ -241,7 +243,11 @@ def state_changes_for_module(module, statefile=None):
241
243
242
244
for (old , new ) in migration .moves ():
243
245
wrapper = '"{0}"'
244
- argv = ["terraform" , "state" , "mv" , wrapper .format (old ), wrapper .format (new )]
246
+ argv = ["terraform" ,
247
+ "state" ,
248
+ "mv" ,
249
+ wrapper .format (old ),
250
+ wrapper .format (new )]
245
251
commands .append (argv )
246
252
247
253
return commands
@@ -265,8 +271,8 @@ def migrate(statefile=None, dryrun=False):
265
271
266
272
# Filter our list of Terraform modules down to anything that looks like a
267
273
# zonal GKE module. We key this off the presence off of
268
- # `google_container_cluster.zonal_primary` since that should almost always be
269
- # unique to a GKE module.
274
+ # `google_container_cluster.zonal_primary` since that should almost always
275
+ # be unique to a GKE module.
270
276
modules_to_migrate = [
271
277
module for module in modules
272
278
if module .has_resource ("google_container_cluster" , "zonal_primary" )
@@ -289,6 +295,7 @@ def migrate(statefile=None, dryrun=False):
289
295
argv = [arg .strip ('"' ) for arg in argv ]
290
296
subprocess .run (argv , check = True , encoding = 'utf-8' )
291
297
298
+
292
299
def main (argv ):
293
300
parser = argparser ()
294
301
args = parser .parse_args (argv [1 :])
@@ -298,6 +305,7 @@ def main(argv):
298
305
299
306
migrate (dryrun = args .dryrun )
300
307
308
+
301
309
def argparser ():
302
310
parser = argparse .ArgumentParser (description = 'Migrate Terraform state' )
303
311
parser .add_argument ('--dryrun' , action = 'store_true' ,
@@ -307,4 +315,4 @@ def argparser():
307
315
308
316
309
317
if __name__ == "__main__" :
310
- main (sys .argv )
318
+ main (sys .argv )
0 commit comments