Add a status getter api endpoint 51/95951/3
authorKiran Kamineni <kiran.k.kamineni@intel.com>
Wed, 18 Sep 2019 19:09:06 +0000 (12:09 -0700)
committerKiran Kamineni <kiran.k.kamineni@intel.com>
Tue, 15 Oct 2019 17:02:40 +0000 (10:02 -0700)
Add a status endpoint to get status of
instances. Status information will be added to
the database asynchronously.

Issue-ID: MULTICLOUD-675
Change-Id: Ia7d79a6f18f01bf24f2690caf74a48c2a082bd73
Signed-off-by: Kiran Kamineni <kiran.k.kamineni@intel.com>
src/k8splugin/api/api.go
src/k8splugin/api/instancehandler.go
src/k8splugin/internal/app/instance.go

index 726bd11..c836fc6 100644 (file)
@@ -46,6 +46,7 @@ func NewRouter(defClient rb.DefinitionManager,
                        "profile-name", "{profile-name}").Methods("GET")
 
        instRouter.HandleFunc("/instance/{instID}", instHandler.getHandler).Methods("GET")
+       instRouter.HandleFunc("/instance/{instID}/status", instHandler.statusHandler).Methods("GET")
        instRouter.HandleFunc("/instance/{instID}", instHandler.deleteHandler).Methods("DELETE")
        // (TODO): Fix update method
        // instRouter.HandleFunc("/{vnfInstanceId}", UpdateHandler).Methods("PUT")
index acbeb53..ab98e4b 100644 (file)
@@ -106,6 +106,26 @@ func (i instanceHandler) getHandler(w http.ResponseWriter, r *http.Request) {
        }
 }
 
+// statusHandler retrieves status about an instance via the ID
+func (i instanceHandler) statusHandler(w http.ResponseWriter, r *http.Request) {
+       vars := mux.Vars(r)
+       id := vars["instID"]
+
+       resp, err := i.client.Status(id)
+       if err != nil {
+               http.Error(w, err.Error(), http.StatusInternalServerError)
+               return
+       }
+
+       w.Header().Set("Content-Type", "application/json")
+       w.WriteHeader(http.StatusOK)
+       err = json.NewEncoder(w).Encode(resp)
+       if err != nil {
+               http.Error(w, err.Error(), http.StatusInternalServerError)
+               return
+       }
+}
+
 // listHandler retrieves information about an instance via the ID
 func (i instanceHandler) listHandler(w http.ResponseWriter, r *http.Request) {
 
index 47cea97..fef9962 100644 (file)
@@ -26,6 +26,7 @@ import (
        "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb"
 
        pkgerrors "github.com/pkg/errors"
+       corev1 "k8s.io/api/core/v1"
 )
 
 // InstanceRequest contains the parameters needed for instantiation
@@ -55,10 +56,29 @@ type InstanceMiniResponse struct {
        Namespace string          `json:"namespace"`
 }
 
+// PodStatus defines the observed state of ResourceBundleState
+type PodStatus struct {
+       Name        string           `json:"name"`
+       Namespace   string           `json:"namespace"`
+       Ready       bool             `json:"ready"`
+       Status      corev1.PodStatus `json:"status,omitempty"`
+       IPAddresses []string         `json:"ipaddresses"`
+}
+
+// InstanceStatus is what is returned when status is queried for an instance
+type InstanceStatus struct {
+       Request         InstanceRequest  `json:"request"`
+       Ready           bool             `json:"ready"`
+       ResourceCount   int32            `json:"resourceCount"`
+       PodStatuses     []PodStatus      `json:"podStatuses"`
+       ServiceStatuses []corev1.Service `json:"serviceStatuses"`
+}
+
 // InstanceManager is an interface exposes the instantiation functionality
 type InstanceManager interface {
        Create(i InstanceRequest) (InstanceResponse, error)
        Get(id string) (InstanceResponse, error)
+       Status(id string) (InstanceStatus, error)
        List(rbname, rbversion, profilename string) ([]InstanceMiniResponse, error)
        Find(rbName string, ver string, profile string, labelKeys map[string]string) ([]InstanceMiniResponse, error)
        Delete(id string) error
@@ -83,16 +103,18 @@ func (dk InstanceKey) String() string {
 // InstanceClient implements the InstanceManager interface
 // It will also be used to maintain some localized state
 type InstanceClient struct {
-       storeName string
-       tagInst   string
+       storeName     string
+       tagInst       string
+       tagInstStatus string
 }
 
 // NewInstanceClient returns an instance of the InstanceClient
 // which implements the InstanceManager
 func NewInstanceClient() *InstanceClient {
        return &InstanceClient{
-               storeName: "rbdef",
-               tagInst:   "instance",
+               storeName:     "rbdef",
+               tagInst:       "instance",
+               tagInstStatus: "instanceStatus",
        }
 }
 
@@ -175,6 +197,32 @@ func (v *InstanceClient) Get(id string) (InstanceResponse, error) {
        return InstanceResponse{}, pkgerrors.New("Error getting Instance")
 }
 
+// Status returns the status for the instance
+func (v *InstanceClient) Status(id string) (InstanceStatus, error) {
+
+       //Read the status from the DB
+       key := InstanceKey{
+               ID: id,
+       }
+
+       value, err := db.DBconn.Read(v.storeName, key, v.tagInstStatus)
+       if err != nil {
+               return InstanceStatus{}, pkgerrors.Wrap(err, "Get Instance")
+       }
+
+       //value is a byte array
+       if value != nil {
+               resp := InstanceStatus{}
+               err = db.DBconn.Unmarshal(value, &resp)
+               if err != nil {
+                       return InstanceStatus{}, pkgerrors.Wrap(err, "Unmarshaling Instance Value")
+               }
+               return resp, nil
+       }
+
+       return InstanceStatus{}, pkgerrors.New("Status is not available")
+}
+
 // List returns the instance for corresponding ID
 // Empty string returns all
 func (v *InstanceClient) List(rbname, rbversion, profilename string) ([]InstanceMiniResponse, error) {