From 5157f9bb4c1cde93f78b9fae0de568ac0f1fb8c5 Mon Sep 17 00:00:00 2001
From: lijianguo <lijianguo1991@outlook.com>
Date: Tue, 19 Dec 2023 18:42:09 +0800
Subject: [PATCH] chore(resources): retrieve resource instance response add
 message

---
 rootfs/api/models/resource.py       | 21 ++++++++++++++++++---
 rootfs/api/serializers.py           |  2 +-
 rootfs/api/views.py                 |  4 +++-
 rootfs/scheduler/resources/svcat.py |  4 ++--
 4 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/rootfs/api/models/resource.py b/rootfs/api/models/resource.py
index a50bafdd8..d1b1b2d37 100644
--- a/rootfs/api/models/resource.py
+++ b/rootfs/api/models/resource.py
@@ -102,9 +102,10 @@ def delete(self, *args, **kwargs):
 
     def detach(self, *args, **kwargs):
         try:
-            # We raise an exception when a resource doesn't exist
-            self._scheduler.svcat.get_instance(self.app.id, self.name)
-            self._scheduler.svcat.delete_instance(self.app.id, self.name)
+            resp = self._scheduler.svcat.get_instance(
+                self.app.id, self.name, ignore_exception=True)
+            if resp.status_code != 404:
+                self._scheduler.svcat.delete_instance(self.app.id, self.name)
         except KubeException as e:
             raise ServiceUnavailable("Could not delete resource {} for application {}".format(self.name, self.app_id)) from e  # noqa
 
@@ -178,6 +179,20 @@ def attach_update(self, *args, **kwargs):
                   '{} for {}'.format(self.name, self.app_id)
             raise ServiceUnavailable(msg) from e
 
+    @property
+    def message(self):
+        try:
+            resp = self._scheduler.svcat.get_instance(
+                self.app.id, self.name)
+            if resp.status_code != 200:
+                message = ""
+            message = resp.json().get("status", {}).get("conditions", [{}])[-1].\
+                get("message", "")
+            return message
+        except KubeException as e:
+            logger.info("retrieve instance info error: {}".format(e))
+            return ""
+
     def retrieve(self, *args, **kwargs):
         update_flag = False
         if self.status != "Ready":
diff --git a/rootfs/api/serializers.py b/rootfs/api/serializers.py
index 254ec4dde..9b4f7d202 100644
--- a/rootfs/api/serializers.py
+++ b/rootfs/api/serializers.py
@@ -616,7 +616,7 @@ class Meta:
     def update(self, instance, validated_data):
         if instance.plan.split(':')[0] != validated_data.get('plan', '').split(':')[0]:  # noqa
             raise DryccException("the resource instance cann't changed")
-        if instance.state == "Provisioning":
+        if instance.status == "Provisioning":
             raise DryccException("this resource instance is in progress")
         instance.plan = validated_data.get('plan')
         instance.options.update(validated_data.get('options', {}))
diff --git a/rootfs/api/views.py b/rootfs/api/views.py
index dc5e3f2a5..a190f2998 100644
--- a/rootfs/api/views.py
+++ b/rootfs/api/views.py
@@ -799,7 +799,9 @@ def get_object(self):
     def retrieve(self, request, *args, **kwargs):
         resource = self.get_object()
         resource.retrieve(request)
-        return super(AppSingleResourceViewSet, self).retrieve(request, *args, **kwargs)  # noqa
+        response =  super(AppSingleResourceViewSet, self).retrieve(request, *args, **kwargs)  # noqa
+        response.data["message"] = resource.message
+        return response
 
     def destroy(self, request, *args, **kwargs):
         resource = self.get_object()
diff --git a/rootfs/scheduler/resources/svcat.py b/rootfs/scheduler/resources/svcat.py
index 874eb707b..9ca9a1f44 100644
--- a/rootfs/scheduler/resources/svcat.py
+++ b/rootfs/scheduler/resources/svcat.py
@@ -59,7 +59,7 @@ def get_serviceplans(self, namespace=None):
             raise KubeHTTPException(response, message)
         return response
 
-    def get_instance(self, namespace, name=None):
+    def get_instance(self, namespace, name=None, ignore_exception=False,):
         """
         Fetch a single serviceinstance or a list of serviceinstances
         """
@@ -71,7 +71,7 @@ def get_instance(self, namespace, name=None):
             url = self.api('/namespaces/{}/serviceinstances', namespace)
             message = 'get serviceinstances'
         response = self.http_get(url)
-        if self.unhealthy(response.status_code):
+        if not ignore_exception and self.unhealthy(response.status_code):
             raise KubeHTTPException(response, message)
         return response
 
