Skip to content

Commit

Permalink
Support multiple different APIs from the same service in a call chain
Browse files Browse the repository at this point in the history
  • Loading branch information
kchiranjewee63 committed Aug 2, 2024
1 parent dd54843 commit d3e1ee6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
11 changes: 5 additions & 6 deletions service/configsync/configsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,9 @@ func (c *Client) handleRequest(message []byte) {
func (c *Client) handleRuleUpsertRequest(request Request) {
switch request.Type {
case MessageTypeTraTVerificationRuleUpsertRequest:
var traTVerificationRule v1alpha1.TraTVerificationRule
var serviceTraTVerificationRule v1alpha1.ServiceTraTVerificationRules

if err := json.Unmarshal(request.Payload, &traTVerificationRule); err != nil {
if err := json.Unmarshal(request.Payload, &serviceTraTVerificationRule); err != nil {
c.logger.Error("Failed to unmarshal trat verification rule", zap.Error(err))
c.sendErrorResponse(
request.ID,
Expand All @@ -399,10 +399,9 @@ func (c *Client) handleRuleUpsertRequest(request Request) {
}

c.logger.Info("Received trat verification rule upsert request",
zap.String("endpoint", traTVerificationRule.Endpoint),
zap.String("method", string(traTVerificationRule.Method)))
zap.String("trat-name", serviceTraTVerificationRule.TraTName))

err := c.verificationRulesManager.UpsertTraTRule(traTVerificationRule)
err := c.verificationRulesManager.UpsertTraTRule(&serviceTraTVerificationRule)
if err != nil {
c.logger.Error("Failed to upsert trat verification rule", zap.Error(err))
c.sendErrorResponse(
Expand Down Expand Up @@ -437,7 +436,7 @@ func (c *Client) handleRuleUpsertRequest(request Request) {

c.logger.Info("Received tratteria config verification rule upsert request")

c.verificationRulesManager.UpdateTratteriaConfigRule(verificationTratteriaConfigRule)
c.verificationRulesManager.UpdateTratteriaConfigRule(&verificationTratteriaConfigRule)

err := c.sendResponse(request.ID, MessageTypeTraTVerificationRuleUpsertResponse, http.StatusOK, nil)
if err != nil {
Expand Down
39 changes: 24 additions & 15 deletions service/verificationrules/v1alpha1/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
)

type VerificationRulesManager interface {
UpsertTraTRule(TraTVerificationRule) error
UpdateTratteriaConfigRule(TratteriaConfigVerificationRule)
UpsertTraTRule(*ServiceTraTVerificationRules) error
UpdateTratteriaConfigRule(*TratteriaConfigVerificationRule)
UpdateCompleteRules(*VerificationRules)
GetRulesJSON() (json.RawMessage, error)
GetVerificationRulesHash() (string, error)
Expand All @@ -43,6 +43,11 @@ type TraTVerificationRule struct {
AzdMapping AzdMapping `json:"azdmapping,omitempty"`
}

type ServiceTraTVerificationRules struct {
TraTName string
TraTVerificationRules []*TraTVerificationRule
}

type AzdMapping map[string]AzdField
type AzdField struct {
Required bool `json:"required"`
Expand All @@ -52,14 +57,14 @@ type AzdField struct {
type IndexedTraTsVerificationRules map[common.HttpMethod]map[string][]*TraTVerificationRule

type VerificationRules struct {
TratteriaConfigVerificationRule *TratteriaConfigVerificationRule `json:"tratteriaConfigVerificationRule"`
TraTsVerificationRules map[string]*TraTVerificationRule `json:"traTsVerificationRules"`
TratteriaConfigVerificationRule *TratteriaConfigVerificationRule `json:"tratteriaConfigVerificationRule"`
TraTsVerificationRules map[string]*ServiceTraTVerificationRules `json:"traTsVerificationRules"`
}

func NewVerificationRules() *VerificationRules {
return &VerificationRules{
TratteriaConfigVerificationRule: &TratteriaConfigVerificationRule{},
TraTsVerificationRules: make(map[string]*TraTVerificationRule),
TraTsVerificationRules: make(map[string]*ServiceTraTVerificationRules),
}
}

Expand All @@ -82,15 +87,17 @@ func NewVerificationRulesImp() *VerificationRulesImp {
}
}

func (vri *VerificationRulesImp) UpsertTraTRule(verificationtraTRule TraTVerificationRule) error {
func (vri *VerificationRulesImp) UpsertTraTRule(serviceTraTVerificationRules *ServiceTraTVerificationRules) error {
vri.mu.Lock()
defer vri.mu.Unlock()

if _, exist := vri.indexedTraTsVerificationRules[verificationtraTRule.Method]; !exist {
return fmt.Errorf("invalid HTTP method: %s", string(verificationtraTRule.Method))
for _, traTVerificationRule := range serviceTraTVerificationRules.TraTVerificationRules {
if _, exist := vri.indexedTraTsVerificationRules[traTVerificationRule.Method]; !exist {
return fmt.Errorf("invalid HTTP method %s for %s trat", string(traTVerificationRule.Method), traTVerificationRule.TraTName)
}
}

vri.verificationRules.TraTsVerificationRules[verificationtraTRule.TraTName] = &verificationtraTRule
vri.verificationRules.TraTsVerificationRules[serviceTraTVerificationRules.TraTName] = serviceTraTVerificationRules

vri.indexTraTsVerificationRules()

Expand Down Expand Up @@ -120,20 +127,22 @@ func (vri *VerificationRulesImp) indexTraTsVerificationRules() {
return
}

for _, traTVerificationRules := range vri.verificationRules.TraTsVerificationRules {
indexedTraTsVerificationRules[traTVerificationRules.Method][traTVerificationRules.Endpoint] = append(
indexedTraTsVerificationRules[traTVerificationRules.Method][traTVerificationRules.Endpoint],
traTVerificationRules)
for _, serviceTraTVerificationRules := range vri.verificationRules.TraTsVerificationRules {
for _, traTVerificationRule := range serviceTraTVerificationRules.TraTVerificationRules {
indexedTraTsVerificationRules[traTVerificationRule.Method][traTVerificationRule.Endpoint] = append(
indexedTraTsVerificationRules[traTVerificationRule.Method][traTVerificationRule.Endpoint],
traTVerificationRule)
}
}

vri.indexedTraTsVerificationRules = indexedTraTsVerificationRules
}

func (vri *VerificationRulesImp) UpdateTratteriaConfigRule(tratteriaConfigRule TratteriaConfigVerificationRule) {
func (vri *VerificationRulesImp) UpdateTratteriaConfigRule(tratteriaConfigRule *TratteriaConfigVerificationRule) {
vri.mu.Lock()
defer vri.mu.Unlock()

vri.verificationRules.TratteriaConfigVerificationRule = &tratteriaConfigRule
vri.verificationRules.TratteriaConfigVerificationRule = tratteriaConfigRule
}

func (vri *VerificationRulesImp) GetRulesJSON() (json.RawMessage, error) {
Expand Down

0 comments on commit d3e1ee6

Please sign in to comment.