Sfoglia il codice sorgente

Merge pull request #334 from CloudVE/fix-gcp-volume-attach-detach

Wait for GCP volume attach/detach operations; fix attachments check
Nuwan Goonasekera 14 ore fa
parent
commit
aea097b4a6
2 ha cambiato i file con 34 aggiunte e 27 eliminazioni
  1. 27 21
      cloudbridge/providers/gcp/resources.py
  2. 7 6
      requirements.txt

+ 27 - 21
cloudbridge/providers/gcp/resources.py

@@ -1700,12 +1700,11 @@ class GCPVolume(BaseVolume):
         # disk. In cloudbridge, volume usage pattern is that a disk is attached
         # to a single instance in a read-write mode. Therefore, we only check
         # the first user of a disk.
-        if 'users' in self._volume and len(self._volume) > 0:
-            if len(self._volume) > 1:
+        users = self._volume.get('users', [])
+        if users:
+            if len(users) > 1:
                 log.warning("This volume is attached to multiple instances")
-            return BaseAttachmentInfo(self,
-                                      self._volume.get('users')[0],
-                                      None)
+            return BaseAttachmentInfo(self, users[0], None)
         else:
             return None
 
@@ -1726,14 +1725,18 @@ class GCPVolume(BaseVolume):
         }
         if not isinstance(instance, GCPInstance):
             instance = self._provider.get_resource('instances', instance)
-        (self._provider
-             .gcp_compute
-             .instances()
-             .attachDisk(project=self._provider.project_name,
-                         zone=instance.zone_name,
-                         instance=instance.name,
-                         body=attach_disk_body)
-             .execute())
+        response = (self._provider
+                    .gcp_compute
+                    .instances()
+                    .attachDisk(project=self._provider.project_name,
+                                zone=instance.zone_name,
+                                instance=instance.name,
+                                body=attach_disk_body)
+                    .execute())
+        # attachDisk is asynchronous; wait for it to finish so the disk is
+        # actually attached (and the instance's disk list is consistent)
+        # before returning.
+        self._provider.wait_for_operation(response, zone=instance.zone_name)
 
     def detach(self, force=False):
         """
@@ -1754,14 +1757,17 @@ class GCPVolume(BaseVolume):
                 device_name = disk['deviceName']
         if not device_name:
             return
-        (self._provider
-             .gcp_compute
-             .instances()
-             .detachDisk(project=self._provider.project_name,
-                         zone=self.zone_name,
-                         instance=instance_data.get('name'),
-                         deviceName=device_name)
-             .execute())
+        response = (self._provider
+                    .gcp_compute
+                    .instances()
+                    .detachDisk(project=self._provider.project_name,
+                                zone=self.zone_name,
+                                instance=instance_data.get('name'),
+                                deviceName=device_name)
+                    .execute())
+        # detachDisk is asynchronous; wait for it to finish so the disk is
+        # actually released before returning.
+        self._provider.wait_for_operation(response, zone=self.zone_name)
 
     def create_snapshot(self, label, description=None):
         """

+ 7 - 6
requirements.txt

@@ -1,8 +1,9 @@
 -e ".[dev]"
 
-# Temporary: pin moto to the fix for the describe_instances availability-zone
-# filter bug (https://github.com/getmoto/moto/pull/10066). Released moto
-# (<= 5.2.2) returns an empty list when CloudBridge filters instances by AZ,
-# which breaks test_crud_instance against the mock provider. Drop this line and
-# bump the moto floor in pyproject.toml's [dev] extra once the fix is released.
-moto[ec2,s3] @ git+https://github.com/CloudVE/moto@fix-describe-instances-az-filter
+# Temporary: install moto from upstream master, which includes the fix for the
+# describe_instances availability-zone filter bug
+# (https://github.com/getmoto/moto/pull/10066). Released moto (<= 5.2.2) returns
+# an empty list when CloudBridge filters instances by AZ, which breaks
+# test_crud_instance against the mock provider. Drop this line and bump the moto
+# floor in pyproject.toml's [dev] extra to >=5.2.3 once that release is out.
+moto[ec2,s3] @ git+https://github.com/getmoto/moto.git@master