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

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