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

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