Get Policy type Schema from policy id 34/7834/2
authornaman.gupta <naman.gupta@samsung.com>
Thu, 24 Feb 2022 09:11:55 +0000 (14:41 +0530)
committernaman.gupta <naman.gupta@samsung.com>
Thu, 10 Mar 2022 09:38:29 +0000 (15:08 +0530)
Get Policy type schema from policy id

Signed-off-by: naman.gupta <naman.gupta@samsung.com>
Change-Id: I649dd94059263244645e7a8236ad058c3f0ed26f

a1-go/pkg/restful/restful.go
a1-go/pkg/resthooks/resthooks.go
a1-go/pkg/resthooks/resthooks_test.go
a1-go/pkg/resthooks/types.go

index 843a64c..26a229f 100644 (file)
@@ -55,7 +55,7 @@ func (r *Restful) setupHandler() *operations.A1API {
        })
 
        api.A1MediatorA1ControllerCreatePolicyTypeHandler = a1_mediator.A1ControllerCreatePolicyTypeHandlerFunc(func(params a1_mediator.A1ControllerCreatePolicyTypeParams) middleware.Responder {
-               a1.Logger.Debug("handler for get policy type from policytypeID")
+               a1.Logger.Debug("handler for Create policy type ")
                if err = r.rh.CreatePolicyType(models.PolicyTypeID(params.PolicyTypeID), *params.Body); err == nil {
                        //Increase prometheus counter
                        return a1_mediator.NewA1ControllerCreatePolicyTypeCreated()
@@ -67,6 +67,11 @@ func (r *Restful) setupHandler() *operations.A1API {
 
        })
 
+       api.A1MediatorA1ControllerGetPolicyTypeHandler = a1_mediator.A1ControllerGetPolicyTypeHandlerFunc(func(params a1_mediator.A1ControllerGetPolicyTypeParams) middleware.Responder {
+               a1.Logger.Error("handler for get policy type from policytypeID")
+               return a1_mediator.NewA1ControllerGetPolicyTypeOK().WithPayload(r.rh.GetPolicyType(models.PolicyTypeID(params.PolicyTypeID)))
+       })
+
        return api
 
 }
index 0a832ff..54ca39d 100644 (file)
@@ -21,7 +21,9 @@
 package resthooks
 
 import (
+       "encoding/json"
        "errors"
+       "fmt"
        "strconv"
        "strings"
 
@@ -78,6 +80,57 @@ func (rh *Resthook) GetAllPolicyType() []models.PolicyTypeID {
        return policyTypeIDs
 }
 
+func (rh *Resthook) GetPolicyType(policyTypeId models.PolicyTypeID) *models.PolicyTypeSchema {
+       a1.Logger.Debug("GetPolicyType1")
+
+       var policytypeschema *models.PolicyTypeSchema
+       var keys [1]string
+
+       key := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10)
+       keys[0] = key
+
+       a1.Logger.Debug("key : %+v", key)
+
+       valmap, err := rh.db.Get(a1MediatorNs, keys[:])
+
+       a1.Logger.Debug("policytype map : ", valmap)
+
+       if len(valmap) == 0 {
+               a1.Logger.Error("policy type Not Present for policyid : %v", policyTypeId)
+               return policytypeschema
+       }
+
+       if err != nil {
+               a1.Logger.Error("error in retrieving policy type. err: %v", err)
+               return policytypeschema
+       }
+
+       if valmap[key] == nil {
+               a1.Logger.Error("policy type Not Present for policyid : %v", policyTypeId)
+               return policytypeschema
+       }
+
+       a1.Logger.Debug("keysmap : %+v", valmap[key])
+
+       var item models.PolicyTypeSchema
+       valStr := fmt.Sprint(valmap[key])
+
+       a1.Logger.Debug("Policy type for %+v :  %+v", key, valStr)
+       valkey := "`" + valStr + "`"
+       valToUnmarshall, err := strconv.Unquote(valkey)
+       if err != nil {
+               panic(err)
+       }
+
+       a1.Logger.Debug("Policy type for %+v :  %+v", key, string(b))
+       errunm := json.Unmarshal([]byte(valToUnmarshall), &item)
+
+       a1.Logger.Debug(" Unmarshalled json : %+v", (errunm))
+       a1.Logger.Debug("Policy type Name :  %v", (item.Name))
+
+       return &item
+}
+
 func (rh *Resthook) CreatePolicyType(policyTypeId models.PolicyTypeID, httprequest models.PolicyTypeSchema) error {
        a1.Logger.Debug("CreatePolicyType function")
        if policyTypeId != models.PolicyTypeID(*httprequest.PolicyTypeID) {
index e8ea9fe..d804a27 100644 (file)
@@ -96,3 +96,7 @@ func (s *SdlMock) SetIfNotExists(ns string, key string, data interface{}) (bool,
        args := s.MethodCalled("SetIfNotExists", ns, key, data)
        return args.Bool(0), nil
 }
+
+func (s *SdlMock) Get(ns string, keys []string) (map[string]interface{}, error) {
+       return make(map[string]interface{}), nil
+}
index f18ca57..e84e048 100644 (file)
@@ -27,4 +27,5 @@ type Resthook struct {
 type iSdl interface {
        GetAll(string) ([]string, error)
        SetIfNotExists(ns string, key string, data interface{}) (bool, error)
+       Get(string, []string) (map[string]interface{}, error)
 }