Skip to content

Commit

Permalink
Fix bugs in main.py as it fails on VMs with multiple disks and region…
Browse files Browse the repository at this point in the history
  • Loading branch information
shashyajoshi authored May 10, 2021
1 parent 3cc4ce9 commit eb3f4be
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions tools/gce-google-keys-to-cmek/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ def main():
def migrate_instance_to_cmek(project, zone, instance, key_ring, key_name,
key_version, key_global, destructive):
start = time.time()
region = zone.rpartition("-")[2]
region = zone.rpartition("-")[0]
key_region = "global" if key_global else region
key_name = 'projects/{0}/locations/{1}/keyRings/{2}/cryptoKeys/{3}/cryptoKeyVersions/{4}'.format(
project, key_region, key_ring, key_name, key_version)

compute = googleapiclient.discovery.build('compute', 'v1')

Expand All @@ -112,10 +115,6 @@ def migrate_instance_to_cmek(project, zone, instance, key_ring, key_name,
deviceName = source_disk['deviceName'][0:46]
existing_disk_name = re.search(DISK_REGEXP, disk_url).group(3)

key_region = "global" if key_global else region
key_name = 'projects/{0}/locations/{1}/keyRings/{2}/cryptoKeys/{3}/cryptoKeyVersions/{4}'.format(
project, key_region, key_ring, key_name, key_version)

if 'diskEncryptionKey' in source_disk:
if source_disk['diskEncryptionKey']['kmsKeyName'] == key_name:
logging.info('Skipping %s, already encrypyed with %s', existing_disk_name,
Expand Down Expand Up @@ -302,7 +301,7 @@ def build():

def _wait_for_operation(operation, build_request):
"""Helper for waiting for operation to complete."""
logging.debug('Waiting for %s', operation, end='')
logging.debug('Waiting for %s', operation)
while True:
sys.stdout.flush()
result = build_request().execute()
Expand All @@ -318,3 +317,4 @@ def _wait_for_operation(operation, build_request):

if __name__ == '__main__':
main()

0 comments on commit eb3f4be

Please sign in to comment.