From 1c851d8bcbf526be6d7295b9e01a597be65cc692 Mon Sep 17 00:00:00 2001 From: Kiran Kamineni Date: Wed, 18 Sep 2019 12:09:06 -0700 Subject: Add a status getter api endpoint 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 --- src/k8splugin/api/api.go | 1 + src/k8splugin/api/instancehandler.go | 20 ++++++++++++ src/k8splugin/internal/app/instance.go | 56 +++++++++++++++++++++++++++++++--- 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/src/k8splugin/api/api.go b/src/k8splugin/api/api.go index 726bd116..c836fc65 100644 --- a/src/k8splugin/api/api.go +++ b/src/k8splugin/api/api.go @@ -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") diff --git a/src/k8splugin/api/instancehandler.go b/src/k8splugin/api/instancehandler.go index acbeb53f..ab98e4be 100644 --- a/src/k8splugin/api/instancehandler.go +++ b/src/k8splugin/api/instancehandler.go @@ -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) { diff --git a/src/k8splugin/internal/app/instance.go b/src/k8splugin/internal/app/instance.go index 47cea972..fef9962f 100644 --- a/src/k8splugin/internal/app/instance.go +++ b/src/k8splugin/internal/app/instance.go @@ -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) { -- cgit 1.2.3-korg