diff options
author | Ritu Sood <ritu.sood@intel.com> | 2021-02-25 14:17:19 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2021-02-25 14:17:19 +0000 |
commit | 965da8328566d58b2b38373f76aa1ab79c0b314e (patch) | |
tree | 21d98c7a7d73b9f8db7fb0b78f779601f71b4f17 /src/k8splugin/internal/app/instance.go | |
parent | 2f09583725c8481ea3d7505ed2394c180af5ef03 (diff) | |
parent | 74dfd71d3628c52e63f66c079244638c675b2b9c (diff) |
Merge "Provide Query API for CNF Instances"
Diffstat (limited to 'src/k8splugin/internal/app/instance.go')
-rw-r--r-- | src/k8splugin/internal/app/instance.go | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/k8splugin/internal/app/instance.go b/src/k8splugin/internal/app/instance.go index 69ade3a8..b11283e0 100644 --- a/src/k8splugin/internal/app/instance.go +++ b/src/k8splugin/internal/app/instance.go @@ -74,6 +74,7 @@ type InstanceManager interface { Create(i InstanceRequest) (InstanceResponse, error) Get(id string) (InstanceResponse, error) Status(id string) (InstanceStatus, error) + Query(id, apiVersion, kind, name, labels 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 @@ -208,6 +209,68 @@ func (v *InstanceClient) Get(id string) (InstanceResponse, error) { return InstanceResponse{}, pkgerrors.New("Error getting Instance") } +// Query returns state of instance's filtered resources +func (v *InstanceClient) Query(id, apiVersion, kind, name, labels string) (InstanceStatus, error) { + + //Read the status from the DB + key := InstanceKey{ + ID: id, + } + value, err := db.DBconn.Read(v.storeName, key, v.tagInst) + if err != nil { + return InstanceStatus{}, pkgerrors.Wrap(err, "Get Instance") + } + if value == nil { //value is a byte array + return InstanceStatus{}, pkgerrors.New("Status is not available") + } + resResp := InstanceResponse{} + err = db.DBconn.Unmarshal(value, &resResp) + if err != nil { + return InstanceStatus{}, pkgerrors.Wrap(err, "Unmarshaling Instance Value") + } + + k8sClient := KubernetesClient{} + err = k8sClient.init(resResp.Request.CloudRegion, id) + if err != nil { + return InstanceStatus{}, pkgerrors.Wrap(err, "Getting CloudRegion Information") + } + + var resourcesStatus []ResourceStatus + if labels != "" { + resList, err := k8sClient.queryResources(apiVersion, kind, labels, resResp.Namespace) + if err != nil { + return InstanceStatus{}, pkgerrors.Wrap(err, "Querying Resources") + } + // If user specifies both label and name, we want to pick up only single resource from these matching label + if name != "" { + //Assigning 0-length, because we may actually not find matching name + resourcesStatus = make([]ResourceStatus, 0) + for _, res := range resList { + if res.Name == name { + resourcesStatus = append(resourcesStatus, res) + break + } + } + } else { + resourcesStatus = resList + } + } else if name != "" { + resIdentifier := helm.KubernetesResource{} + res, err := k8sClient.getResourceStatus(resIdentifier, resResp.Namespace) + if err != nil { + return InstanceStatus{}, pkgerrors.Wrap(err, "Querying Resource") + } + resourcesStatus = []ResourceStatus{res} + } + + resp := InstanceStatus{ + Request: resResp.Request, + ResourceCount: int32(len(resourcesStatus)), + ResourcesStatus: resourcesStatus, + } + return resp, nil +} + // Status returns the status for the instance func (v *InstanceClient) Status(id string) (InstanceStatus, error) { |