From 686447316fc67f7b1777d94c5e02293786cec98d Mon Sep 17 00:00:00 2001 From: "naman.gupta" Date: Thu, 24 Feb 2022 14:41:55 +0530 Subject: [PATCH] Get Policy type Schema from policy id Get Policy type schema from policy id Signed-off-by: naman.gupta Change-Id: I649dd94059263244645e7a8236ad058c3f0ed26f --- a1-go/pkg/restful/restful.go | 7 ++++- a1-go/pkg/resthooks/resthooks.go | 53 +++++++++++++++++++++++++++++++++++ a1-go/pkg/resthooks/resthooks_test.go | 4 +++ a1-go/pkg/resthooks/types.go | 1 + 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/a1-go/pkg/restful/restful.go b/a1-go/pkg/restful/restful.go index 843a64c..26a229f 100644 --- a/a1-go/pkg/restful/restful.go +++ b/a1-go/pkg/restful/restful.go @@ -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 } diff --git a/a1-go/pkg/resthooks/resthooks.go b/a1-go/pkg/resthooks/resthooks.go index 0a832ff..54ca39d 100644 --- a/a1-go/pkg/resthooks/resthooks.go +++ b/a1-go/pkg/resthooks/resthooks.go @@ -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) { diff --git a/a1-go/pkg/resthooks/resthooks_test.go b/a1-go/pkg/resthooks/resthooks_test.go index e8ea9fe..d804a27 100644 --- a/a1-go/pkg/resthooks/resthooks_test.go +++ b/a1-go/pkg/resthooks/resthooks_test.go @@ -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 +} diff --git a/a1-go/pkg/resthooks/types.go b/a1-go/pkg/resthooks/types.go index f18ca57..e84e048 100644 --- a/a1-go/pkg/resthooks/types.go +++ b/a1-go/pkg/resthooks/types.go @@ -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) } -- 2.16.6