1818import copy
1919import subprocess
2020import sys
21- import shutil
2221import re
2322
2423MIGRATIONS = [
4342 },
4443]
4544
45+
4646class ModuleMigration :
4747 """
4848 Migrate the resources from a flat project factory to match the new
@@ -89,6 +89,7 @@ def targets(self):
8989
9090 return to_move
9191
92+
9293class TerraformModule :
9394 """
9495 A Terraform module with associated resources.
@@ -171,7 +172,7 @@ def __init__(self, module, resource_type, name):
171172 self .module = module
172173 self .resource_type = resource_type
173174
174- find_suffix = re .match ('(^.+)\[(\d+)\]' , name )
175+ find_suffix = re .match (r '(^.+)\[(\d+)\]' , name )
175176 if find_suffix :
176177 self .name = find_suffix .group (1 )
177178 self .index = find_suffix .group (2 )
@@ -187,7 +188,7 @@ def path(self):
187188 if parts [0 ] == '' :
188189 del parts [0 ]
189190 path = "." .join (parts )
190- if self .index is not - 1 and self .plural :
191+ if self .index != - 1 and self .plural :
191192 path = "{0}[{1}]" .format (path , self .index )
192193 return path
193194
@@ -198,6 +199,7 @@ def __repr__(self):
198199 self .resource_type ,
199200 self .name )
200201
202+
201203def group_by_module (resources ):
202204 """
203205 Group a set of resources according to their containing module.
@@ -241,7 +243,11 @@ def state_changes_for_module(module, statefile=None):
241243
242244 for (old , new ) in migration .moves ():
243245 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 )]
245251 commands .append (argv )
246252
247253 return commands
@@ -265,8 +271,8 @@ def migrate(statefile=None, dryrun=False):
265271
266272 # Filter our list of Terraform modules down to anything that looks like a
267273 # 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.
270276 modules_to_migrate = [
271277 module for module in modules
272278 if module .has_resource ("google_container_cluster" , "zonal_primary" )
@@ -289,6 +295,7 @@ def migrate(statefile=None, dryrun=False):
289295 argv = [arg .strip ('"' ) for arg in argv ]
290296 subprocess .run (argv , check = True , encoding = 'utf-8' )
291297
298+
292299def main (argv ):
293300 parser = argparser ()
294301 args = parser .parse_args (argv [1 :])
@@ -298,6 +305,7 @@ def main(argv):
298305
299306 migrate (dryrun = args .dryrun )
300307
308+
301309def argparser ():
302310 parser = argparse .ArgumentParser (description = 'Migrate Terraform state' )
303311 parser .add_argument ('--dryrun' , action = 'store_true' ,
@@ -307,4 +315,4 @@ def argparser():
307315
308316
309317if __name__ == "__main__" :
310- main (sys .argv )
318+ main (sys .argv )
0 commit comments