Skip to content

Commit

Permalink
apply suggestions from flake8 to helpers/migrate.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Sproul committed Oct 10, 2019
1 parent 81eb717 commit f13d9b7
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions helpers/migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import copy
import subprocess
import sys
import shutil
import re

MIGRATIONS = [
Expand All @@ -43,6 +42,7 @@
},
]


class ModuleMigration:
"""
Migrate the resources from a flat project factory to match the new
Expand Down Expand Up @@ -89,6 +89,7 @@ def targets(self):

return to_move


class TerraformModule:
"""
A Terraform module with associated resources.
Expand Down Expand Up @@ -171,7 +172,7 @@ def __init__(self, module, resource_type, name):
self.module = module
self.resource_type = resource_type

find_suffix = re.match('(^.+)\[(\d+)\]', name)
find_suffix = re.match(r'(^.+)\[(\d+)\]', name)
if find_suffix:
self.name = find_suffix.group(1)
self.index = find_suffix.group(2)
Expand All @@ -187,7 +188,7 @@ def path(self):
if parts[0] == '':
del parts[0]
path = ".".join(parts)
if self.index is not -1 and self.plural:
if self.index != -1 and self.plural:
path = "{0}[{1}]".format(path, self.index)
return path

Expand All @@ -198,6 +199,7 @@ def __repr__(self):
self.resource_type,
self.name)


def group_by_module(resources):
"""
Group a set of resources according to their containing module.
Expand Down Expand Up @@ -241,7 +243,11 @@ def state_changes_for_module(module, statefile=None):

for (old, new) in migration.moves():
wrapper = '"{0}"'
argv = ["terraform", "state", "mv", wrapper.format(old), wrapper.format(new)]
argv = ["terraform",
"state",
"mv",
wrapper.format(old),
wrapper.format(new)]
commands.append(argv)

return commands
Expand All @@ -265,8 +271,8 @@ def migrate(statefile=None, dryrun=False):

# Filter our list of Terraform modules down to anything that looks like a
# zonal GKE module. We key this off the presence off of
# `google_container_cluster.zonal_primary` since that should almost always be
# unique to a GKE module.
# `google_container_cluster.zonal_primary` since that should almost always
# be unique to a GKE module.
modules_to_migrate = [
module for module in modules
if module.has_resource("google_container_cluster", "zonal_primary")
Expand All @@ -289,6 +295,7 @@ def migrate(statefile=None, dryrun=False):
argv = [arg.strip('"') for arg in argv]
subprocess.run(argv, check=True, encoding='utf-8')


def main(argv):
parser = argparser()
args = parser.parse_args(argv[1:])
Expand All @@ -298,6 +305,7 @@ def main(argv):

migrate(dryrun=args.dryrun)


def argparser():
parser = argparse.ArgumentParser(description='Migrate Terraform state')
parser.add_argument('--dryrun', action='store_true',
Expand All @@ -307,4 +315,4 @@ def argparser():


if __name__ == "__main__":
main(sys.argv)
main(sys.argv)

0 comments on commit f13d9b7

Please sign in to comment.