Blame SOURCES/bz1935798-gcp-pd-move-fix-partially-matched-disk_name.patch

f315e6
From 2927279ba1677e9dda202121176a8245a7ef76ca Mon Sep 17 00:00:00 2001
f315e6
From: tositaka77 <45960626+tositaka77@users.noreply.github.com>
f315e6
Date: Wed, 14 Oct 2020 22:22:56 +0900
f315e6
Subject: [PATCH] fixes and improvements
f315e6
f315e6
- Fixed "regional" PD functionality in attach_disk()
f315e6
- Improve to exact match disk_name with disks.source in detach_disk()
f315e6
---
f315e6
 heartbeat/gcp-pd-move.in | 8 +++++++-
f315e6
 1 file changed, 7 insertions(+), 1 deletion(-)
f315e6
f315e6
diff --git a/heartbeat/gcp-pd-move.in b/heartbeat/gcp-pd-move.in
f315e6
index f82bd25e5..e99cc71f8 100644
f315e6
--- a/heartbeat/gcp-pd-move.in
f315e6
+++ b/heartbeat/gcp-pd-move.in
f315e6
@@ -49,6 +49,7 @@ else:
f315e6
 CONN = None
f315e6
 PROJECT = None
f315e6
 ZONE = None
f315e6
+REGION = None
f315e6
 LIST_DISK_ATTACHED_INSTANCES = None
f315e6
 INSTANCE_NAME = None
f315e6
 
f315e6
@@ -148,6 +149,7 @@ def populate_vars():
f315e6
   global INSTANCE_NAME
f315e6
   global PROJECT
f315e6
   global ZONE
f315e6
+  global REGION
f315e6
   global LIST_DISK_ATTACHED_INSTANCES
f315e6
 
f315e6
   # Populate global vars
f315e6
@@ -175,6 +177,7 @@ def populate_vars():
f315e6
   PROJECT = get_metadata('project/project-id')
f315e6
   if PARAMETERS['disk_scope'] in ['detect', 'regional']:
f315e6
     ZONE = get_metadata('instance/zone').split('/')[-1]
f315e6
+    REGION = ZONE[:-2] 
f315e6
   else:
f315e6
     ZONE = PARAMETERS['disk_scope']
f315e6
   LIST_DISK_ATTACHED_INSTANCES = get_disk_attached_instances(
f315e6
@@ -255,7 +258,7 @@ def detach_disk(instance, disk_name):
f315e6
 
f315e6
   device_name = None
f315e6
   for disk in response['disks']:
f315e6
-    if disk_name in disk['source']:
f315e6
+    if disk_name == re.sub('.*disks/',"",disk['source']):
f315e6
       device_name = disk['deviceName']
f315e6
       break
f315e6
 
f315e6
@@ -273,6 +276,9 @@ def detach_disk(instance, disk_name):
f315e6
 
f315e6
 def attach_disk(instance, disk_name):
f315e6
   location = 'zones/%s' % ZONE
f315e6
+  if PARAMETERS['disk_scope'] == 'regional':
f315e6
+    location = 'regions/%s' % REGION
f315e6
+
f315e6
   prefix = 'https://www.googleapis.com/compute/v1'
f315e6
   body = {
f315e6
     'source': '%(prefix)s/projects/%(project)s/%(location)s/disks/%(disk)s' % {